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 <string.h>
00044 #include <signal.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 #include "workaround.h"
00048 #include "dipa_errors.h"
00049 #include "resource.h"
00050 #include "timing.h"
00051 #include "tracing.h"
00052 #include "resource.h"
00053 #include "messg.h"
00054 #include "dipa_phase.h"
00055 #include "dipa_phase_ctrl.h"
00056 #include "dipa_args.h"
00057 #include "cxx_memory.h"
00058
00059 INT DIPA_Phase_Manager::last_phase_id = T_DIPA_TIMER_FIRST;
00060 Phase_List DIPA_Phase_Manager::phase_list;
00061 DIPA_Phase *DIPA_Phase_Manager::cur_phase=NULL;
00062 MEM_POOL DIPA_Phase_Manager::MEM_dipa_pool;
00063 MEM_POOL DIPA_Phase_Manager::MEM_dipa_nz_pool;
00064 const char *DIPA_Phase_Manager::tfile_name=NULL;
00065 FILE *DIPA_Phase_Manager::tfile=NULL;
00066 CMD_ARGS DIPA_Phase_Manager::dipa_args;
00067 DIPA_Olist DIPA_Phase_Manager::dipa_olist;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 PHASE_ID DIPA_Phase_Manager::Register_Phase(const char *name, INT parentId, INT prevId)
00078 {
00079 if ( last_phase_id >= T_DIPA_TIMER_LAST ) {
00080 FmtAssert((false), ("Too many timers allocated\n"));
00081 }
00082 INT curId = last_phase_id++;
00083 Alloc_Phase (curId, name, parentId);
00084 return (PHASE_ID)curId;
00085 }
00086
00087 PHASE_ID DIPA_Phase_Manager::Register_Phase(DIPA_Phase *cur, DIPA_Phase *parent, DIPA_Phase *prev)
00088 {
00089 Phase_List* ph_list=NULL;
00090 if (parent == NULL) {
00091
00092 ph_list = &phase_list;
00093 } else {
00094
00095 ph_list = parent->Get_Childs();
00096 }
00097
00098 if (prev) {
00099 Phase_Iter it;
00100 for (it = ph_list->begin(); it != ph_list->end(); it++) {
00101 if ((*it) == prev) break;
00102 }
00103
00104 FmtAssert((it != ph_list->end()), ("Fatal error: phase %s not registered at the same level\n", prev->Get_Name()));
00105
00106 it++;
00107 ph_list->insert(it, cur);
00108 } else {
00109
00110 ph_list->push_back(cur);
00111 }
00112
00113
00114
00115
00116
00117
00118 FmtAssert((last_phase_id < T_DIPA_TIMER_LAST), ("Too many timers allocated\n"));
00119
00120 INT32 curId = last_phase_id++;
00121 cur->Set_Id(curId);
00122 Alloc_Phase (curId, cur->Get_Name(), (parent)?parent->Get_Id():INVALID_PHASE_ID);
00123
00124
00125 Dipa_add_phase_opt((char *)cur->Get_Name(), cur->Get_Id());
00126
00127
00128 cur->Enable();
00129 cur->Enable_Tracing();
00130
00131 return 0;
00132 }
00133
00134 void DIPA_Phase_Manager::Add_Global_Arg(const char *arg_name, const char *arg_val)
00135 {
00136 CMD_ARG pair1;
00137
00138 pair1.first = arg_name;
00139 pair1.second = NULL;
00140 if (arg_val && arg_val[0])
00141 pair1.second = arg_val;
00142 dipa_args.push_back(pair1);
00143 }
00144
00145 void DIPA_Phase_Manager::Add_DIPA_File(char *fname)
00146 {
00147 DIPA_Obj *robj = CXX_NEW (DIPA_Obj(), Get_Nz_Mempool());
00148
00149 FmtAssert((robj != NULL), ("Failed to allocate DIPA obj!\n"));
00150 robj->Set_Rfile(fname);
00151
00152 dipa_olist.push_back(robj);
00153 }
00154
00155 PHASE_ID DIPA_Phase_Manager::Get_Phase_ID (const char *name)
00156 {
00157 INT tid;
00158 for (tid=0; tid<last_phase_id; tid++) {
00159 if (strcmp(name, Get_Timer_Name(Timer(tid))) == 0) {
00160 return (PHASE_ID)tid;
00161 }
00162 }
00163 return INVALID_PHASE_ID;
00164 }
00165
00166 void DIPA_Phase_Manager::Dump_All_Stats(FILE *file)
00167 {
00168 Phase_Iter it;
00169
00170 for (it = phase_list.begin(); it != phase_list.end(); it++) {
00171 (*it)->Dump_Stats();
00172 }
00173 }
00174
00175 void DIPA_Phase_Manager::Accum_All_Stats(void)
00176 {
00177 Phase_Iter it;
00178
00179 printf("entering Accum_All_Stats\n");
00180
00181 for (it = phase_list.begin(); it != phase_list.end(); it++) {
00182 (*it)->Accum_Stats();
00183 }
00184 }
00185
00186 void DIPA_Phase_Manager::Dump_All_Phases(void)
00187 {
00188 printf("\n---Dump all phases - total #:%d\n", phase_list.size());
00189 for (Phase_Iter it=phase_list.begin(); it!=phase_list.end(); it++ ) {
00190 (*it)->Dump_Phase(0);
00191 }
00192 printf("\tDump global arguments\n");
00193 CMD_ARGS_Iter iter;
00194 for (iter=dipa_args.begin(); iter!=dipa_args.end(); iter++) {
00195 if ((*iter).first != NULL) {
00196 printf("\t\toption name [%s]: val %s\n", (*iter).first, (*iter).second);
00197 } else {
00198 printf("\t\tfile: %s\n", (*iter).second);
00199 }
00200 }
00201 printf("---Dump end\n\n");
00202 }
00203
00204
00205 void Catch_Signal(INT sig)
00206 {
00207 signal (sig, SIG_DFL);
00208
00209 const char *ph_name = DIPA_Phase_Manager::Get_Cur_Phase_Name();
00210
00211 fprintf (stderr, "SIGNAL: %s", strsignal(sig));
00212 fprintf (stderr, " in %s phase.\n", ph_name ? ph_name : "startup");
00213 fflush (stderr);
00214
00215 if ( SIGHUP == sig || SIGTERM == sig || SIGINT == sig) {
00216 kill (getpid(), sig);
00217 exit (DIPA_INTERNAL_ERROR);
00218 }
00219
00220 fprintf(stderr, "Signal %s not handled. Terminated.\n", strsignal(sig));
00221 exit (DIPA_INTERNAL_ERROR);
00222 }
00223
00224 void setup_signal_handler(INT sig)
00225 {
00226 signal (sig, Catch_Signal);
00227 }
00228
00229 void Init_Signal_Handlers(void)
00230 {
00231 setup_signal_handler (SIGHUP);
00232 setup_signal_handler (SIGINT);
00233 setup_signal_handler (SIGQUIT);
00234 setup_signal_handler (SIGILL);
00235 setup_signal_handler (SIGTRAP);
00236 setup_signal_handler (SIGIOT);
00237 setup_signal_handler (SIGFPE);
00238 setup_signal_handler (SIGBUS);
00239 setup_signal_handler (SIGSEGV);
00240 setup_signal_handler (SIGTERM);
00241 }
00242
00243 BOOL DIPA_Phase_Manager::Init_Memory(void)
00244 {
00245 MEM_POOL_Initialize(&MEM_dipa_pool,"dipa",TRUE);
00246
00247 MEM_POOL_Push(&MEM_dipa_pool);
00248
00249 MEM_POOL_Initialize(&MEM_dipa_nz_pool,"dipa (nz)",FALSE);
00250
00251 MEM_POOL_Push(&MEM_dipa_nz_pool);
00252
00253 return true;
00254 }
00255
00256 void DIPA_Phase_Manager::Set_Trace_File(const char *fname)
00257 {
00258 tfile_name = fname;
00259 }
00260
00261 BOOL DIPA_Phase_Manager::Init_Trace_File(const char *fname)
00262 {
00263 tfile_name = fname;
00264 if (fname && fname[0]) {
00265 tfile = fopen(fname, "wb");
00266 FmtAssert((tfile!=NULL),("Can't create trace file %s\n", fname));
00267 return true;
00268 }
00269
00270 return false;
00271 }
00272
00273 BOOL DIPA_Phase_Manager::Init_IPA(void)
00274 {
00275 Init_Signal_Handlers();
00276 Init_Memory();
00277
00278
00279 Initialize_Timing (true);
00280
00281 return true;
00282 }
00283
00284 BOOL DIPA_Phase_Manager::Do_IPA(void)
00285 {
00286 Reset_Timers();
00287
00288 Phase_Iter it;
00289
00290 for (it = phase_list.begin(); it != phase_list.end(); it++) {
00291 if ((*it)->Is_Enabled() == false ) continue;
00292
00293 Set_Cur_Phase(*it);
00294 Start_Timer((*it)->Get_Id());
00295
00296
00297 (*it)->Start(&dipa_args, &dipa_olist);
00298
00299 Stop_Timer((*it)->Get_Id());
00300 }
00301
00302 return true;
00303 }
00304
00305 BOOL DIPA_Phase_Manager::End_IPA(void)
00306 {
00307
00308 Accum_All_Stats();
00309 Dump_All_Stats(Get_Trace_File_Desc());
00310
00311 return true;
00312 }
00313
00314 const char *DIPA_Phase_Manager::Get_Cur_Phase_Name(void)
00315 {
00316 const char *p = (cur_phase)?cur_phase->Get_Name():NULL;
00317 return p;
00318 }
00319
00320
00321
00322
00323
00324 DIPA_Phase *DIPA_Phase_Manager::Get_Phase (PHASE_ID ph_id)
00325 {
00326 Phase_Iter it;
00327 for (it = phase_list.begin(); it != phase_list.end(); it++) {
00328 if ((*it)->Get_Id() == ph_id)
00329 return *it;
00330 }
00331 FmtAssert((false),("Can't find phase with id %d\n", ph_id));
00332 }
00333
00334
00335 #ifdef __cplusplus
00336 extern "C" {
00337 #endif
00338
00339 char *Get_Trace_File(void) { return (char *)DIPA_Phase_Manager::Get_Trace_File(); }
00340 FILE *Get_Trace_File_Desc(void) { return DIPA_Phase_Manager::Get_Trace_File_Desc(); }
00341 BOOL Get_Trace ( INT func, INT arg ) { return false; }
00342
00343 #ifdef __cplusplus
00344 }
00345 #endif