src/Profile/utils/tau_events.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  * This program was prepared by PSI. 
00007  * All rights in the program are reserved by PSI.
00008  * Neither PSI nor the author(s)
00009  * makes any warranty, express or implied, or assumes any liability or
00010  * responsibility for the use of this software
00011  *
00012  * Visit http://www.acl.lanl.gov/POOMS for more details
00013  *
00014  ***************************************************************************/
00015 
00016 // -*- C++ -*-
00017 /***************************************************************************
00018  *
00019  * The IPPL Framework
00020  * 
00021  * This program was prepared by PSI. 
00022  * All rights in the program are reserved by PSI.
00023  * Neither PSI nor the author(s)
00024  * makes any warranty, express or implied, or assumes any liability or
00025  * responsibility for the use of this software
00026  *
00027  * Visit http://www.acl.lanl.gov/POOMS for more details
00028  *
00029  ***************************************************************************/
00030 
00031 /*********************************************************************/
00032 /*      TAU Tracing                                                  */
00033 /*      U.Oregon, ACL, LANL (C) 1997                                 */
00034 /*                  pC++/Sage++  Copyright (C) 1994                  */
00035 /*  Indiana University  University of Oregon  University of Rennes   */
00036 /*********************************************************************/
00037 
00038 /*
00039  * tau_events.cpp
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]; /* array of descriptors */
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]; /* File header like # FunctionId Group Tag "Name Type" Par */
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; /* global event id */
00096   char state[64]; /* state as in TAU_VIZ */
00097   int  tag; /* -7 for send etc. */
00098   char param[64]; /* param as in EntryExit */
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 /* eventNameMap stores name to global event id mapping */
00130 
00131 int globalEventId = 1; /* numbers start from 1 */
00132 
00133 map<long, int> nodeEventTable[MAX_OPEN_FILES];
00134 /* nodeEventTable stores nodeid, localevent ->globaleventid mapping */
00135 /* e.g., nodeEventTable[0] gives event map for node 0, emap[60000] 
00136 gives global id say 105 of event 60000 */
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] == '#') /* store header for output file */
00182       {
00183         strcpy(header, linebuf);
00184       }
00185       /* -- skip empty and comment lines -- */
00186       i--;
00187       continue;
00188     }
00189 
00190     localEventId = -1;
00191     eventname[0]  = '\0';
00192     inputev.param[0] = '\0';
00193     if (dynamictrace) /* get eventname in quotes */
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 /* DEBUG */
00199       for(j=0; linebuf[j] !='"'; j++)
00200         ;
00201       eventname[0] = linebuf[j];
00202       j++;
00203       /* skip over till eventname begins */
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'; /* terminate eventname */
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 /* DEBUG */
00216       /* now that we have eventname, see if it exists in the map */
00217       /* Since pointers create a problem with stl we have to do this */
00218       stlEvName = new char[strlen(eventname)+1];
00219       strcpy(stlEvName, eventname);
00220       if((iter = eventNameMap.find((const char *)stlEvName)) != eventNameMap.end()) 
00221       {
00222         /* Found eventname in the map */
00223 #ifdef DEBUG
00224         printf("Found %s in map \n", eventname);
00225 #endif /* DEBUG */
00226         delete stlEvName; /* don't need this if its already there */
00227         /* add to nodeEventTable the global id of the event */
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 /* DEBUG */
00233       } 
00234       else 
00235       { /* Not found. Create a new entry in the map! */
00236 #ifdef DEBUG
00237         printf("Event %s not found in map. Assigning new event id %d\n",
00238           eventname, globalEventId);
00239 #endif /* DEBUG */
00240 
00241         eventNameMap[(const char *)stlEvName] = EventDescr(globalEventId,
00242           inputev.state, inputev.tag, inputev.param);
00243         // Adds a null record and creates the name key in the map
00244         // Note: don't delete stlEvName - STL needs it.
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 /* DEBUG */
00251 
00252         globalEventId ++;
00253       }  
00254 
00255 
00256     } /* not dynamic trace- what is to be done? */ 
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   } /* i < numevents  for loop*/
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 /* DEBUG */
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   /* get default edf File prefix and open edf files */
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 /* OLDMAIN */
00328 
00329 /***************************************************************************
00330  * $RCSfile: addheaderfooter,v $   $Author: adelmann $
00331  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:17 $
00332  * IPPL_VERSION_ID: $Id: addheaderfooter,v 1.1.1.1 2003/01/23 07:40:17 adelmann Exp $ 
00333  ***************************************************************************/
00334 

Generated on Mon Jan 16 13:23:55 2006 for IPPL by  doxygen 1.4.6