00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 # include <stdio.h>
00043 # include <stdlib.h>
00044 # include <sys/types.h>
00045 # include <fcntl.h>
00046 # include <unistd.h>
00047
00048 # include <string.h>
00049
00050 # define TRACING_ON
00051 # define PCXX_EVENT_SRC
00052
00053 # ifdef __PCXX__
00054 # include "Profile/pcxx_events_def.h"
00055 # else
00056 # include "Profile/pcxx_events.h"
00057 # endif
00058 # include "Profile/pcxx_ansi.h"
00059
00060 # ifndef TRUE
00061 # define FALSE 0
00062 # define TRUE 1
00063 # endif
00064
00065 # define F_EXISTS 0
00066
00067 # include <vector.h>
00068 # include <map.h>
00069
00070 # define FILENAME_SIZE 1024
00071 # define MAX_OPEN_FILES 256
00072 # define LINEMAX 64*1024
00073 FILE * edfFiles[MAX_OPEN_FILES];
00074
00075 int dynamictrace = FALSE;
00076
00077 extern "C" {
00078 int open_edf_file(char *prefix, int nodeid);
00079 int parse_edf_file(int node);
00080 int store_merged_edffile(char *filename);
00081 int GID(int node, long localEventId);
00082 }
00083
00084 char header[256];
00085
00086 struct ltstr
00087 {
00088 bool operator()(const char* s1, const char* s2) const
00089 {
00090 return strcmp(s1, s2) < 0;
00091 }
00092 };
00093
00094 struct EventDescr {
00095 int gid;
00096 char state[64];
00097 int tag;
00098 char param[64];
00099 public:
00100 EventDescr(int evGlobalId, char *evState, int evTag, char *evParam)
00101 {
00102 gid = evGlobalId;
00103 tag = evTag;
00104 strcpy(state, evState);
00105 strcpy(param, evParam);
00106 }
00107 EventDescr()
00108 {
00109 }
00110 EventDescr(const EventDescr & X)
00111 {
00112 gid = X.gid;
00113 tag = X.tag;
00114 strcpy(state, X.state);
00115 strcpy(param, X.param);
00116 }
00117 EventDescr& operator= (const EventDescr& X)
00118 {
00119 gid = X.gid;
00120 tag = X.tag;
00121 strcpy(state, X.state);
00122 strcpy(param, X.param);
00123 return *this;
00124 }
00125 ~EventDescr() { }
00126 };
00127
00128 map<const char*, EventDescr, ltstr> eventNameMap;
00129
00130
00131 int globalEventId = 1;
00132
00133 map<long, int> nodeEventTable[MAX_OPEN_FILES];
00134
00135
00136
00137
00138 int open_edf_file(char *prefix, int nodeid)
00139 {
00140 char filename[FILENAME_SIZE];
00141
00142 if (nodeid > MAX_OPEN_FILES)
00143 {
00144 printf("nodeid %d exceeds MAX_OPEN_FILES. Recompile with extended limit\n", nodeid);
00145 exit(1);
00146 }
00147
00148 sprintf(filename, "%s.%d.edf", prefix,nodeid);
00149 if ( (edfFiles[nodeid] = fopen (filename, "r")) == NULL )
00150 {
00151 perror (filename);
00152 exit (1);
00153 }
00154
00155 return 1;
00156 }
00157
00158 int parse_edf_file(int node)
00159 {
00160 int i,j,k;
00161 char linebuf[LINEMAX], eventname[LINEMAX], traceflag[32];
00162 char *stlEvName;
00163 int numevents;
00164 long localEventId;
00165 EventDescr inputev;
00166 map<const char*, EventDescr, ltstr>::iterator iter;
00167
00168 fgets (linebuf, LINEMAX, edfFiles[node]);
00169 sscanf (linebuf, "%d %s", &numevents, traceflag);
00170
00171 if (strcmp(traceflag, "dynamic_trace_events") == 0)
00172 {
00173 dynamictrace = TRUE;
00174 }
00175
00176 for (i=0; i<numevents; i++)
00177 {
00178 fgets (linebuf, LINEMAX, edfFiles[node]);
00179 if ( (linebuf[0] == '\n') || (linebuf[0] == '#') )
00180 {
00181 if (linebuf[0] == '#')
00182 {
00183 strcpy(header, linebuf);
00184 }
00185
00186 i--;
00187 continue;
00188 }
00189
00190 localEventId = -1;
00191 eventname[0] = '\0';
00192 inputev.param[0] = '\0';
00193 if (dynamictrace)
00194 {
00195 sscanf (linebuf, "%ld %s %d", &localEventId, inputev.state, &inputev.tag);
00196 #if DEBUG
00197 printf("Got localEventId %d state %s tag %d\n", localEventId, inputev.state, inputev.tag);
00198 #endif
00199 for(j=0; linebuf[j] !='"'; j++)
00200 ;
00201 eventname[0] = linebuf[j];
00202 j++;
00203
00204 for (k=j; linebuf[k] != '"'; k++)
00205 {
00206 eventname[k-j+1] = linebuf[k];
00207 }
00208 eventname[k-j+1] = '"';
00209 eventname[k-j+2] = '\0';
00210
00211 strcpy(inputev.param, &linebuf[k+2]);
00212
00213 #ifdef DEBUG
00214 printf(" Got eventname=%s param=%s\n", eventname, inputev.param);
00215 #endif
00216
00217
00218 stlEvName = new char[strlen(eventname)+1];
00219 strcpy(stlEvName, eventname);
00220 if((iter = eventNameMap.find((const char *)stlEvName)) != eventNameMap.end())
00221 {
00222
00223 #ifdef DEBUG
00224 printf("Found %s in map \n", eventname);
00225 #endif
00226 delete stlEvName;
00227
00228 nodeEventTable[node][localEventId] = eventNameMap[(const char *)stlEvName].gid;
00229 #ifdef DEBUG
00230 printf("node %d local %ld global %d %s \n", node, localEventId,
00231 nodeEventTable[node][localEventId], stlEvName);
00232 #endif
00233 }
00234 else
00235 {
00236 #ifdef DEBUG
00237 printf("Event %s not found in map. Assigning new event id %d\n",
00238 eventname, globalEventId);
00239 #endif
00240
00241 eventNameMap[(const char *)stlEvName] = EventDescr(globalEventId,
00242 inputev.state, inputev.tag, inputev.param);
00243
00244
00245 nodeEventTable[node][localEventId] = globalEventId;
00246
00247 #ifdef DEBUG
00248 printf("node %d local %ld global %d %s \n", node, localEventId,
00249 nodeEventTable[node][localEventId], stlEvName);
00250 #endif
00251
00252 globalEventId ++;
00253 }
00254
00255
00256 }
00257 else
00258 {
00259 sscanf (linebuf, "%ld %s %d %s %s", &localEventId,
00260 inputev.state, &inputev.tag, eventname, inputev.param);
00261 }
00262
00263 if ( (localEventId < 0) || !*eventname )
00264 {
00265 fprintf (stderr, "events.%d.edf: blurb in line %d\n", node, i+2);
00266 exit (1);
00267 }
00268
00269 }
00270 return 1;
00271 }
00272
00273 int store_merged_edffile(char *filename)
00274 {
00275 FILE *fp;
00276 char *errormsg;
00277 map<const char*, EventDescr, ltstr>::iterator it;
00278
00279 if ((fp = fopen (filename, "w+")) == NULL) {
00280 errormsg = new char[strlen(filename)+32];
00281 sprintf(errormsg,"Error: Could not create %s",filename);
00282 perror(errormsg);
00283 return 0;
00284 }
00285
00286 fprintf(fp,"%d dynamic_trace_events\n%s",eventNameMap.size(), header);
00287
00288 for (it = eventNameMap.begin(); it != eventNameMap.end(); it++)
00289 {
00290 fprintf(fp, "%d %s %d %s %s", (*it).second.gid, (*it).second.state,
00291 (*it).second.tag, (*it).first, (*it).second.param);
00292
00293 #ifdef DEBUG
00294 printf("%d %s %d %s %s\n", (*it).second.gid, (*it).second.state,
00295 (*it).second.tag, (*it).first, (*it).second.param);
00296 #endif
00297 }
00298
00299 return 1;
00300 }
00301
00302 int GID(int node, long local)
00303 {
00304 return nodeEventTable[node][local];
00305 }
00306
00307 #ifdef OLDMAIN
00308 int main(int argc, char **argv)
00309 {
00310 int numtraces, i;
00311
00312
00313 strcpy(edfFilePrefix, "events");
00314
00315 numtraces = 4;
00316
00317 for(i=0; i <numtraces; i++)
00318 {
00319 open_edf_file(edfFilePrefix, i);
00320 parse_edf_file(i);
00321 }
00322
00323 printf("NODE 3 LOCAL ID 60000 global id = %d\n", GID(3,60000));
00324 store_merged_edffile("events.edf");
00325
00326 }
00327 #endif
00328
00329
00330
00331
00332
00333
00334