fa86c02d37b256c1cc50aba0b45074147a43224f
[IRC.git] / Robust / src / Runtime / bamboo / multicoretaskprofile.c
1 #ifdef PROFILE
2
3 #include "multicoretaskprofile.h"
4
5 INLINE void inittaskprofiledata() {
6   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
7     // startup core to initialize corestatus[]
8     for(i = 0; i < NUMCORESACTIVE; ++i) {
9       // initialize the profile data arrays
10       profilestatus[i] = 1;
11     } // for(i = 0; i < NUMCORESACTIVE; ++i)
12   }
13
14   stall = false;
15   totalexetime = -1;
16   taskInfoIndex = 0;
17   taskInfoOverflow = false;
18 #ifdef PROFILE_INTERRUPT
19   interruptInfoIndex = 0;
20   interruptInfoOverflow = false;
21 #endif // PROFILE_INTERRUPT
22 }
23
24 inline void setTaskExitIndex(int index) {
25   taskInfoArray[taskInfoIndex]->exitIndex = index;
26 }
27
28 inline void addNewObjInfo(void * nobj) {
29   if(taskInfoArray[taskInfoIndex]->newObjs == NULL) {
30     taskInfoArray[taskInfoIndex]->newObjs = createQueue();
31   }
32   addNewItem(taskInfoArray[taskInfoIndex]->newObjs, nobj);
33 }
34
35 inline void profileTaskStart(char * taskname) {
36   if(!taskInfoOverflow) {
37     TaskInfo* taskInfo = RUNMALLOC(sizeof(struct task_info));
38     taskInfoArray[taskInfoIndex] = taskInfo;
39     taskInfo->taskName = taskname;
40     taskInfo->startTime = BAMBOO_GET_EXE_TIME();
41     taskInfo->endTime = -1;
42     taskInfo->exitIndex = -1;
43     taskInfo->newObjs = NULL;
44   }
45 }
46
47 inline void profileTaskEnd() {
48   if(!taskInfoOverflow) {
49     taskInfoArray[taskInfoIndex]->endTime = BAMBOO_GET_EXE_TIME();
50     taskInfoIndex++;
51     if(taskInfoIndex == TASKINFOLENGTH) {
52       taskInfoOverflow = true;
53     }
54   }
55 }
56
57 #ifdef PROFILE_INTERRUPT
58 INLINE void profileInterruptStart_I(void) {
59   if(!interruptInfoOverflow) {
60     InterruptInfo* intInfo = RUNMALLOC_I(sizeof(struct interrupt_info));
61     interruptInfoArray[interruptInfoIndex] = intInfo;
62     intInfo->startTime = BAMBOO_GET_EXE_TIME();
63     intInfo->endTime = -1;
64   }
65 }
66
67 INLINE void profileInterruptEnd_I(void) {
68   if(!interruptInfoOverflow) {
69     interruptInfoArray[interruptInfoIndex]->endTime=BAMBOO_GET_EXE_TIME();
70     interruptInfoIndex++;
71     if(interruptInfoIndex == INTERRUPTINFOLENGTH) {
72       interruptInfoOverflow = true;
73     }
74   }
75 }
76 #endif // PROFILE_INTERRUPT
77
78 // output the profiling data
79 void outputProfileData() {
80 #ifdef USEIO
81   int i;
82   unsigned long long totaltasktime = 0;
83   unsigned long long preprocessingtime = 0;
84   unsigned long long objqueuecheckingtime = 0;
85   unsigned long long postprocessingtime = 0;
86   unsigned long long other = 0;
87   unsigned long long averagetasktime = 0;
88   int tasknum = 0;
89
90   printf("Task Name, Start Time, End Time, Duration, Exit Index(, NewObj Name, Num)+\n");
91   // output task related info
92   for(i = 0; i < taskInfoIndex; i++) {
93     TaskInfo* tmpTInfo = taskInfoArray[i];
94     unsigned long long duration = tmpTInfo->endTime - tmpTInfo->startTime;
95     printf("%s, %lld, %lld, %lld, %lld", tmpTInfo->taskName, 
96         tmpTInfo->startTime, tmpTInfo->endTime, duration, tmpTInfo->exitIndex);
97     // summarize new obj info
98     if(tmpTInfo->newObjs != NULL) {
99       struct RuntimeHash * nobjtbl = allocateRuntimeHash(5);
100       struct RuntimeIterator * iter = NULL;
101       while(0 == isEmpty(tmpTInfo->newObjs)) {
102                 char * objtype = (char *)(getItem(tmpTInfo->newObjs));
103                 if(RuntimeHashcontainskey(nobjtbl, (int)(objtype))) {
104                   int num = 0;
105                   RuntimeHashget(nobjtbl, (int)objtype, &num);
106                   RuntimeHashremovekey(nobjtbl, (int)objtype);
107                   num++;
108                   RuntimeHashadd(nobjtbl, (int)objtype, num);
109                 } else {
110                   RuntimeHashadd(nobjtbl, (int)objtype, 1);
111                 }
112       }
113
114       // output all new obj info
115       iter = RuntimeHashcreateiterator(nobjtbl);
116       while(RunhasNext(iter)) {
117                 char * objtype = (char *)Runkey(iter);
118                 int num = Runnext(iter);
119                 printf(", %s, %d", objtype, num);
120       }
121     }
122     printf("\n");
123     if(strcmp(tmpTInfo->taskName, "tpd checking") == 0) {
124       preprocessingtime += duration;
125     } else if(strcmp(tmpTInfo->taskName, "post task execution") == 0) {
126       postprocessingtime += duration;
127     } else if(strcmp(tmpTInfo->taskName, "objqueue checking") == 0) {
128       objqueuecheckingtime += duration;
129     } else {
130       totaltasktime += duration;
131       averagetasktime += duration;
132       tasknum++;
133     }
134   }
135
136   if(taskInfoOverflow) {
137     printf("Caution: task info overflow!\n");
138   }
139
140   other = totalexetime-totaltasktime-preprocessingtime-postprocessingtime;
141   averagetasktime /= tasknum;
142
143   printf("\nTotal time: %lld\n", totalexetime);
144   printf("Total task execution time: %lld (%d%%)\n", totaltasktime,
145          (int)(((double)totaltasktime/(double)totalexetime)*100));
146   printf("Total objqueue checking time: %lld (%d%%)\n",
147          objqueuecheckingtime,
148          (int)(((double)objqueuecheckingtime/(double)totalexetime)*100));
149   printf("Total pre-processing time: %lld (%d%%)\n", preprocessingtime,
150          (int)(((double)preprocessingtime/(double)totalexetime)*100));
151   printf("Total post-processing time: %lld (%d%%)\n", postprocessingtime,
152          (int)(((double)postprocessingtime/(double)totalexetime)*100));
153   printf("Other time: %lld (%d%%)\n", other,
154          (int)(((double)other/(double)totalexetime)*100));
155
156   printf("\nAverage task execution time: %lld\n", averagetasktime);
157
158 #else
159   int i = 0;
160   int j = 0;
161
162   BAMBOO_PRINT(0xdddd);
163   // output task related info
164   for(i= 0; i < taskInfoIndex; i++) {
165     TaskInfo* tmpTInfo = taskInfoArray[i];
166     char* tmpName = tmpTInfo->taskName;
167     int nameLen = strlen(tmpName);
168     BAMBOO_PRINT(0xddda);
169     for(j = 0; j < nameLen; j++) {
170       BAMBOO_PRINT_REG(tmpName[j]);
171     }
172     BAMBOO_PRINT(0xdddb);
173     BAMBOO_PRINT_REG(tmpTInfo->startTime);
174     BAMBOO_PRINT_REG(tmpTInfo->endTime);
175     BAMBOO_PRINT_REG(tmpTInfo->exitIndex);
176     if(tmpTInfo->newObjs != NULL) {
177       struct RuntimeHash * nobjtbl = allocateRuntimeHash(5);
178       struct RuntimeIterator * iter = NULL;
179       while(0 == isEmpty(tmpTInfo->newObjs)) {
180                 char * objtype = (char *)(getItem(tmpTInfo->newObjs));
181                 if(RuntimeHashcontainskey(nobjtbl, (int)(objtype))) {
182                   int num = 0;
183                   RuntimeHashget(nobjtbl, (int)objtype, &num);
184                   RuntimeHashremovekey(nobjtbl, (int)objtype);
185                   num++;
186                   RuntimeHashadd(nobjtbl, (int)objtype, num);
187                 } else {
188                   RuntimeHashadd(nobjtbl, (int)objtype, 1);
189                 }
190       }
191
192       // ouput all new obj info
193       iter = RuntimeHashcreateiterator(nobjtbl);
194       while(RunhasNext(iter)) {
195                 char * objtype = (char *)Runkey(iter);
196                 int num = Runnext(iter);
197                 int nameLen = strlen(objtype);
198                 BAMBOO_PRINT(0xddda);
199                 for(j = 0; j < nameLen; j++) {
200                   BAMBOO_PRINT_REG(objtype[j]);
201                 }
202                 BAMBOO_PRINT(0xdddb);
203                 BAMBOO_PRINT_REG(num);
204           }
205     }
206     BAMBOO_PRINT(0xdddc);
207   }
208
209   if(taskInfoOverflow) {
210         BAMBOO_PRINT(0xefee);
211   }
212
213 #ifdef PROFILE_INTERRUPT
214   // output interrupt related info
215   for(i = 0; i < interruptInfoIndex; i++) {
216     InterruptInfo* tmpIInfo = interruptInfoArray[i];
217     BAMBOO_PRINT(0xddde);
218     BAMBOO_PRINT_REG(tmpIInfo->startTime);
219     BAMBOO_PRINT_REG(tmpIInfo->endTime);
220     BAMBOO_PRINT(0xdddf);
221   }
222
223   if(interruptInfoOverflow) {
224     BAMBOO_PRINT(0xefef);
225   }
226 #endif // PROFILE_INTERRUPT
227
228   BAMBOO_PRINT(0xeeee);
229 #endif
230 }
231
232 #endif // PROFILE