Subversion Repositories shark

Rev

Rev 1508 | Rev 1511 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1494 giacomo 1
#include <stdio.h>
2
#include <stdlib.h>
1499 giacomo 3
#include <math.h>
4
#include <unistd.h>
1500 giacomo 5
#include <string.h>
1494 giacomo 6
 
7
#define MAXCONTEXT 100
8
#define MAXJOB 100000
9
 
10
#define INT_CTX 1
11
#define INT_PID 9999
12
#define PID_NO_DEF -1
13
 
1495 giacomo 14
#define BACKGROUND 0
15
#define PERIODICAL 1
16
#define INTERRUPT  2
17
 
1499 giacomo 18
#define DRAW_NUM 1000
19
 
1495 giacomo 20
struct ctx_exec {
1494 giacomo 21
        int ctx;
22
        unsigned long long dtsc;
23
        unsigned long long start;
24
};
25
 
1495 giacomo 26
struct ctx_to_pid {
1494 giacomo 27
        int ctx;
28
        int pid;
29
};
30
 
1495 giacomo 31
struct endcycle {
1494 giacomo 32
        int ctx;
33
        unsigned long long tsc;
34
};
35
 
1510 giacomo 36
void Error(int num, int line) {
37
        printf("Finite-State machine error %d at line %d\n",num,line);
1494 giacomo 38
        exit(2);
39
}
40
 
1495 giacomo 41
int context_total = 0,endcycle_total = 0,job_total = 0,exec_total = 0;
42
struct ctx_exec *exec_list;
43
struct ctx_to_pid *context_list;
44
struct endcycle *endcycle_list;
45
struct ctx_exec *job_list;
1494 giacomo 46
 
1497 giacomo 47
unsigned int clk_per_msec = 0;
48
 
1495 giacomo 49
unsigned long long log_start_tsc = 0;
50
unsigned long long log_end_tsc = 0;
1499 giacomo 51
unsigned long long total_dtsc = 0;
1495 giacomo 52
 
1501 giacomo 53
/* Data for gnuplot external call */
1500 giacomo 54
int draw_data[DRAW_NUM+1];
1499 giacomo 55
 
56
int gnuplot_clear() {
57
 
58
   int i;
59
 
60
   for (i=0;i<DRAW_NUM;i++)
61
     draw_data[i] = 0;
62
 
63
   return 0;
64
 
65
}
66
 
1500 giacomo 67
int gnuplot_draw(char *title,unsigned long long max_limit,int type) {
1499 giacomo 68
 
69
   FILE *gnuplot_data, *gnuplot_command;
1500 giacomo 70
   char temp_name[30];
71
   int i,pid,*current_mem;
1499 giacomo 72
 
1500 giacomo 73
   current_mem = malloc(sizeof(int)*(DRAW_NUM+1));
74
   memcpy(current_mem,draw_data,sizeof(int)*(DRAW_NUM+1));
1499 giacomo 75
 
1500 giacomo 76
   pid = fork();
77
   if (pid == 0) {
1499 giacomo 78
 
1500 giacomo 79
     srand(getpid());
1499 giacomo 80
 
1500 giacomo 81
     sprintf(temp_name,"/tmp/pwcet%d",rand()%10000);
82
 
83
     gnuplot_data = fopen(temp_name,"w");
1504 giacomo 84
     gnuplot_command = popen("gnuplot -persist","w");
1500 giacomo 85
 
86
     for (i=0;i<DRAW_NUM;i++)
87
       fprintf(gnuplot_data,"%f\t%f\n",(double)i * (double)max_limit / (double)DRAW_NUM,(float)(current_mem[i]));
88
 
89
     fflush(gnuplot_data);
90
     fclose(gnuplot_data);
91
 
92
     fprintf(gnuplot_command,"set xlabel \"Time [us]\"\n");
93
     if (type == 0) {
94
        fprintf(gnuplot_command,"set ylabel \"Frequency [#]\"\n");
95
 
96
        fprintf(gnuplot_command,"plot \"%s\" using 1:2 title \"%s\" with lines\n",temp_name,title);
97
        fflush(gnuplot_command);
98
     } else {
99
       fprintf(gnuplot_command,"set ylabel \"Time [us]\"\n");
100
 
101
       fprintf(gnuplot_command,"plot \"%s\" using 1:2 title \"%s\" with lines\n",temp_name,title);
102
       fflush(gnuplot_command);
103
     }
104
 
105
     pclose(gnuplot_command);
106
 
107
     exit(0);
108
 
109
   }
110
 
1499 giacomo 111
   return 0;
112
 
113
}
114
 
1501 giacomo 115
int stats_from_execs(int ctx_num, unsigned long long *tot_tsc,
116
                                unsigned long long *min_tsc,
117
                                unsigned long long *mean_tsc,
118
                                unsigned long long *max_tsc,
119
                                unsigned long long *first_tsc,
120
                                int *number) {
121
 
122
   unsigned long long temp_tsc;
123
   int k,i;
124
 
125
   temp_tsc = 0;
126
   *max_tsc = 0;
127
   *mean_tsc = 0;
128
   *min_tsc = 0xFFFFFFFF;
129
   *first_tsc = 0;
130
   k = 0;
131
   for (i=0;i<exec_total;i++)
132
     if (exec_list[i].ctx == context_list[ctx_num].ctx) {
133
       if (*first_tsc == 0) *first_tsc = exec_list[i].start - log_start_tsc;
134
       if (exec_list[i].dtsc > *max_tsc) *max_tsc = exec_list[i].dtsc;
135
       if (exec_list[i].dtsc < *min_tsc) *min_tsc = exec_list[i].dtsc;
136
       temp_tsc += exec_list[i].dtsc;
137
       k++;
138
     }
139
 
140
   *number = k;
141
   *tot_tsc = temp_tsc;
1505 giacomo 142
   if (k != 0) *mean_tsc = temp_tsc / k;
1501 giacomo 143
 
144
   return 0;
145
 
146
}
147
 
148
int stats_from_jobs(int ctx_num, unsigned long long *tot_tsc,
149
                                unsigned long long *min_tsc,
150
                                unsigned long long *mean_tsc,
151
                                unsigned long long *max_tsc,
152
                                unsigned long long *first_tsc,
153
                                int *number) {
154
 
155
   unsigned long long temp_tsc;
156
   int k,i;
157
 
158
   temp_tsc = 0;
159
   *max_tsc = 0;
160
   *mean_tsc = 0;
161
   *min_tsc = 0xFFFFFFFF;
162
   *first_tsc = 0;
163
   k = 0;
164
   for (i=0;i<job_total;i++)
165
     if (job_list[i].ctx == context_list[ctx_num].ctx) {
166
       if (*first_tsc == 0) *first_tsc = job_list[i].start - log_start_tsc;
167
       if (job_list[i].dtsc > *max_tsc) *max_tsc = job_list[i].dtsc;
168
       if (job_list[i].dtsc < *min_tsc) *min_tsc = job_list[i].dtsc;
169
       temp_tsc += job_list[i].dtsc;
170
       k++;
171
     }
172
 
173
   *number = k;
174
   *tot_tsc = temp_tsc;
1505 giacomo 175
   if (k != 0) *mean_tsc = temp_tsc / k;
1501 giacomo 176
 
177
   return 0;
178
 
179
}
180
 
181
int arr_stats_from_execs(int ctx_num, unsigned long long *min_tsc,
182
                                unsigned long long *mean_tsc,
183
                                unsigned long long *max_tsc) {
184
 
185
   unsigned long long last_start,temp_tsc,delta_start;
186
   int i,k;
187
 
188
   last_start = 0;
189
   temp_tsc = 0;
190
   *max_tsc = 0;
191
   *min_tsc = 0xFFFFFFFF;
1505 giacomo 192
   *mean_tsc = 0;
1501 giacomo 193
   k = 0;
194
   for (i=0;i<exec_total;i++)
195
     if (exec_list[i].ctx == context_list[ctx_num].ctx) {
196
        if (last_start == 0) {
197
                last_start = exec_list[i].start;
198
        } else {
199
                delta_start = exec_list[i].start - last_start;
200
                if (delta_start > *max_tsc) *max_tsc = delta_start;
201
                if (delta_start < *min_tsc) *min_tsc = delta_start;
202
                temp_tsc += delta_start;
203
                k++;
204
                last_start = exec_list[i].start;
205
        }
206
      }
207
 
1505 giacomo 208
   if (k != 0) *mean_tsc = temp_tsc / k;
1501 giacomo 209
 
210
   return 0;
211
 
212
}
213
 
214
int arr_stats_from_jobs(int ctx_num, unsigned long long *min_tsc,
215
                                unsigned long long *mean_tsc,
216
                                unsigned long long *max_tsc) {
217
 
218
   unsigned long long last_start,temp_tsc,delta_start;
219
   int i,k;
220
 
221
   last_start = 0;
222
   temp_tsc = 0;
223
   *max_tsc = 0;
224
   *min_tsc = 0xFFFFFFFF;
1505 giacomo 225
   *mean_tsc = 0;
1501 giacomo 226
   k = 0;
227
   for (i=0;i<job_total;i++)
228
     if (job_list[i].ctx == context_list[ctx_num].ctx) {
229
        if (last_start == 0) {
230
                last_start = job_list[i].start;
231
        } else {
232
                delta_start = job_list[i].start - last_start;
233
                if (delta_start > *max_tsc) *max_tsc = delta_start;
234
                if (delta_start < *min_tsc) *min_tsc = delta_start;
235
                temp_tsc += delta_start;
236
                k++;
237
                last_start = job_list[i].start;
238
        }
239
      }
240
 
1505 giacomo 241
   if (k != 0) *mean_tsc = temp_tsc / k;
1501 giacomo 242
 
243
   return 0;
244
 
245
}
246
 
247
int plot_exec_demand_function(int ctx_num, char *pidstr) {
248
 
249
   unsigned long long max_limit;
250
   char tmpstr[50];
251
   int i;
252
 
253
   gnuplot_clear();
254
 
255
   max_limit = total_dtsc*1000/clk_per_msec;
256
 
257
   for (i=0;i<exec_total;i++)
258
     if (exec_list[i].ctx == context_list[ctx_num].ctx) {
259
       int h1,h2,h3;
260
 
261
       h1 = ((exec_list[i].start-log_start_tsc)*1000/clk_per_msec) * DRAW_NUM / max_limit;
262
       h2 = ((exec_list[i].start+exec_list[i].dtsc-log_start_tsc)*1000/clk_per_msec) * DRAW_NUM / max_limit;
263
       for (h3=h1;h3<h2;h3++)
264
         if (h3 <= DRAW_NUM) draw_data[h3] += (exec_list[i].dtsc*1000/clk_per_msec)*(h3-h1)/(h2-h1);
265
       for (h3=h2;h3<=DRAW_NUM;h3++)
266
         if (h3 <= DRAW_NUM) draw_data[h3] += (exec_list[i].dtsc*1000/clk_per_msec);
267
 
268
     }
269
 
270
   sprintf(tmpstr,"Ctx [%d:%s] Demand-Function",context_list[ctx_num].ctx,pidstr);
271
   gnuplot_draw(tmpstr,max_limit,1);
272
 
273
   return 0;
274
 
275
}
276
 
277
int plot_exec_c_distrib(int ctx_num, unsigned long long max_tsc, char *pidstr) {
278
 
279
   unsigned long long max_limit;
280
   char tmpstr[50];
281
   int i,h;
282
 
1505 giacomo 283
   if (max_tsc == 0) return 0;
284
 
1501 giacomo 285
   gnuplot_clear();
286
 
287
   max_limit = max_tsc*1000/clk_per_msec;
288
 
289
   for (i=0;i<exec_total;i++)
290
      if (exec_list[i].ctx == context_list[ctx_num].ctx) {
291
        h = (exec_list[i].dtsc*1000/clk_per_msec) * DRAW_NUM / max_limit;
292
        if (h <= DRAW_NUM) draw_data[h]++;
293
      }
294
 
295
   sprintf(tmpstr,"Ctx [%d:%s] Exec C-dist",context_list[ctx_num].ctx,pidstr);
296
   gnuplot_draw(tmpstr,max_limit,0);
297
 
298
   return 0;
299
 
300
}
301
 
302
int plot_job_c_distrib(int ctx_num, unsigned long long max_tsc, char *pidstr) {
303
 
304
   unsigned long long max_limit;
305
   char tmpstr[50];
306
   int i,h;
307
 
1505 giacomo 308
   if (max_tsc == 0) return 0;
309
 
1501 giacomo 310
   gnuplot_clear();
311
 
312
   max_limit = max_tsc*1000/clk_per_msec;
313
 
314
   for (i=0;i<job_total;i++)
315
      if (job_list[i].ctx == context_list[ctx_num].ctx) {
316
        h = (job_list[i].dtsc*1000/clk_per_msec) * DRAW_NUM / max_limit;
317
        if (h <= DRAW_NUM) draw_data[h]++;
318
      }
319
 
320
   sprintf(tmpstr,"Ctx [%d:%s] Job C-dist",context_list[ctx_num].ctx,pidstr);
321
   gnuplot_draw(tmpstr,max_limit,0);
322
 
323
   return 0;
324
 
325
}
326
 
327
int plot_exec_arr_distrib(int ctx_num, unsigned long long max_tsc, char *pidstr) {
328
 
329
  unsigned long long max_limit,last_start,delta_start;
330
  char tmpstr[50];
331
  int i,h;
332
 
1505 giacomo 333
  if (max_tsc == 0) return 0;
334
 
1501 giacomo 335
  gnuplot_clear();
336
 
337
  max_limit = max_tsc*1000/clk_per_msec;
338
 
339
  last_start = 0;
340
  for (i=0;i<exec_total;i++)
341
    if (exec_list[i].ctx == context_list[ctx_num].ctx) {
342
      if (last_start == 0) {
343
              last_start = exec_list[i].start;
344
      } else {
345
              delta_start = exec_list[i].start - last_start;
346
 
347
              h = (delta_start*1000/clk_per_msec) * DRAW_NUM / max_limit;
348
              if (h <= DRAW_NUM) draw_data[h]++;
349
 
350
              last_start = exec_list[i].start;
351
      }
352
    }
353
 
354
  sprintf(tmpstr,"Ctx [%d:%s] Exec Arr.Delta",context_list[ctx_num].ctx,pidstr);
355
  gnuplot_draw(tmpstr,max_limit,0);
356
 
357
  return 0;
358
 
359
}
360
 
361
int plot_job_arr_distrib(int ctx_num, unsigned long long max_tsc, char *pidstr) {
362
 
363
  unsigned long long max_limit,last_start,delta_start;
364
  char tmpstr[50];
365
  int i,h;
366
 
1505 giacomo 367
  if (max_tsc == 0) return 0;
368
 
1501 giacomo 369
  gnuplot_clear();
370
 
371
  max_limit = max_tsc*1000/clk_per_msec;
372
 
373
  last_start = 0;
374
  for (i=0;i<job_total;i++)
375
    if (job_list[i].ctx == context_list[ctx_num].ctx) {
376
      if (last_start == 0) {
377
              last_start = job_list[i].start;
378
      } else {
379
              delta_start = job_list[i].start - last_start;
380
 
381
              h = (delta_start*1000/clk_per_msec) * DRAW_NUM / max_limit;
382
              if (h <= DRAW_NUM) draw_data[h]++;
383
 
384
              last_start = job_list[i].start;
385
      }
386
    }
387
 
388
  sprintf(tmpstr,"Ctx [%d:%s] Job Arr.Delta",context_list[ctx_num].ctx,pidstr);
389
  gnuplot_draw(tmpstr,max_limit,0);
390
 
391
  return 0;
392
 
393
}
394
 
1495 giacomo 395
int create_lists(char *filename) {
396
 
1494 giacomo 397
  FILE *input_file;
398
 
1495 giacomo 399
  int type,par1,par2,k,i,state;
400
 
1494 giacomo 401
  int current_context = 0;
1495 giacomo 402
  int current_exec = 0;
1494 giacomo 403
  int current_endcycle = 0;
404
 
1508 giacomo 405
  int kill_delta = 0;
406
 
1495 giacomo 407
  unsigned long long last_tsc, tsc;
1494 giacomo 408
 
1495 giacomo 409
  input_file = fopen(filename,"r");
1494 giacomo 410
 
1495 giacomo 411
  /* Memory alloc */
412
  exec_list = malloc(sizeof(struct ctx_exec) * MAXJOB);
413
  context_list = malloc(sizeof(struct ctx_to_pid) * MAXCONTEXT);
414
  endcycle_list = malloc(sizeof(struct endcycle) * MAXJOB);
1494 giacomo 415
 
1495 giacomo 416
  /* Finite-State machine
417
   *
418
   * FS-Machine states:
1494 giacomo 419
 
420
 
421
        1 - Context running
422
        2 - Interrupt running
423
 
424
        10 - End
425
 
1495 giacomo 426
   */
1494 giacomo 427
 
428
  for(i=0;i<MAXCONTEXT;i++) {
1495 giacomo 429
    context_list[i].ctx = 0;
430
    context_list[i].pid = PID_NO_DEF;
1494 giacomo 431
  }
432
 
433
  /* The start context + interrupt context */
1495 giacomo 434
  context_total = 2;
1494 giacomo 435
  current_context = 0;
436
  last_tsc = 0;
437
  context_list[0].ctx = 0;
438
  context_list[0].pid = PID_NO_DEF;
439
  context_list[1].ctx = INT_CTX;
440
  context_list[1].pid = INT_PID;
441
 
442
  state = 0;
443
 
444
  k = 0;
445
  while(!feof(input_file)) {
446
 
447
    fscanf(input_file,"%d %llu",&type,&tsc);
448
    k++;
449
 
450
    switch (type) {
451
 
452
        case 0:
453
        case 1:
454
 
455
                /* No par */
456
                break;
457
 
458
        case 2:
459
        case 3:
460
        case 4:
461
        case 6:
462
        case 7:
463
        case 8:
464
 
465
                /* 1 par */
466
                fscanf(input_file,"%d",&par1);
467
                break;
468
 
469
        case 5:
1508 giacomo 470
        case 9:
1494 giacomo 471
                /* 2 par */
472
                fscanf(input_file,"%d %d",&par1,&par2);
473
                break;
474
 
475
    }
476
 
477
    switch (type) {
478
 
479
        case 0:
1510 giacomo 480
                if (state != 0) Error(1,k);
1494 giacomo 481
                printf("EVT:Log starts at [%12llu]\n",tsc);
482
                last_tsc = tsc;
483
                log_start_tsc = tsc;
1495 giacomo 484
                exec_list[current_exec].start = tsc;
1494 giacomo 485
                state = 1;
486
                break;
487
 
488
        case 1:
489
                printf("EVT:Log   ends at [%12llu]\n",tsc);
1495 giacomo 490
                exec_list[current_exec].dtsc = tsc - last_tsc;
491
                exec_list[current_exec].ctx = current_context;
492
                current_exec++;
1494 giacomo 493
                last_tsc = tsc;
494
                log_end_tsc = tsc;
495
                state = 10;
496
                break;
497
 
498
        /* Int start */
499
        case 2:
1510 giacomo 500
                if (state == 0) Error(2,k);
1495 giacomo 501
                exec_list[current_exec].dtsc = tsc - last_tsc;
502
                exec_list[current_exec].ctx = current_context;
503
                current_exec++;
1494 giacomo 504
                last_tsc = tsc;
505
                current_context = INT_CTX;
1495 giacomo 506
                exec_list[current_exec].start = tsc;
1494 giacomo 507
                state = 2;
508
                break;
509
 
510
        /* Int end */
511
        case 3:
1510 giacomo 512
                if (state != 2) Error(3,k);            
1495 giacomo 513
                exec_list[current_exec].dtsc = tsc - last_tsc;
514
                exec_list[current_exec].ctx = current_context;
515
                current_exec++;
1494 giacomo 516
                last_tsc = tsc;
517
                current_context = par1;
518
 
1495 giacomo 519
                for (i=0;i<context_total;i++)
1494 giacomo 520
                        if (par1 == context_list[i].ctx) break;
1495 giacomo 521
                if (i == context_total) {
522
                        context_list[context_total].ctx = par1;
523
                        context_total++;
1494 giacomo 524
                }
525
 
1495 giacomo 526
                exec_list[current_exec].start = tsc;
1494 giacomo 527
                state = 1;
528
                break;
529
 
530
        /* Change ctx */
531
        case 4:
532
 
1495 giacomo 533
                exec_list[current_exec].dtsc = tsc - last_tsc;
534
                exec_list[current_exec].ctx = current_context;
535
                current_exec++;
1494 giacomo 536
                last_tsc = tsc;
537
                current_context = par1;
538
 
1495 giacomo 539
                for (i=0;i<context_total;i++)
1494 giacomo 540
                        if (par1 == context_list[i].ctx) break;
1495 giacomo 541
                if (i == context_total) {
542
                        context_list[context_total].ctx = par1;
543
                        context_total++;
1494 giacomo 544
                }
545
 
1495 giacomo 546
                exec_list[current_exec].start = tsc;
1494 giacomo 547
                state = 1;
548
                break;
549
 
550
        /* Task create */
551
        case 5:
552
 
1495 giacomo 553
                for (i=0;i<context_total;i++)
1508 giacomo 554
                        if (par1 == context_list[i].ctx) {
555
                                context_list[i].pid = par2;
556
                                break;
557
                        }
1495 giacomo 558
                if (i == context_total) {
559
                        context_list[context_total].ctx = par1;
560
                        context_list[context_total].pid = par2;
561
                        context_total++;
1494 giacomo 562
                }
563
 
564
                break;
1508 giacomo 565
 
566
        /* Task kill */
567
        case 7:
1494 giacomo 568
 
1508 giacomo 569
                for (i=0;i<context_total;i++)
570
                        if (par1 == context_list[i].ctx) break;
1510 giacomo 571
                if (i == context_total) Error(5,k);
1508 giacomo 572
                  else {
573
 
574
                        kill_delta += 1000;
575
 
576
                        for (k=0;k<current_endcycle;k++)
577
                                if (endcycle_list[k].ctx == par1)
578
                                        endcycle_list[k].ctx += kill_delta;
579
                        for (k=0;k<current_exec;k++)
580
                                if (exec_list[k].ctx == par1)
581
                                        exec_list[k].ctx += kill_delta;
582
                        context_list[context_total].ctx = context_list[i].ctx + kill_delta;
583
                        context_list[context_total].pid = context_list[i].pid;
584
                        context_total++;
585
 
586
                        if (current_context == par1) current_context += kill_delta;
587
 
588
                  }
589
 
590
                break;
591
 
1494 giacomo 592
        /* Task endcycle */
593
        case 8:
594
 
1495 giacomo 595
                for (i=0;i<context_total;i++)
1494 giacomo 596
                        if (par1 == context_list[i].ctx) break;
1510 giacomo 597
                if (i == context_total) Error(4,k);
1494 giacomo 598
 
599
                endcycle_list[current_endcycle].ctx = par1;
600
                endcycle_list[current_endcycle].tsc = tsc;
601
                current_endcycle++;
602
 
603
                break;
604
 
1508 giacomo 605
        /* Task id */
606
        case 9:
607
 
608
                for (i=0;i<context_total;i++)
609
                        if (par1 == context_list[i].ctx) {
610
                                context_list[i].pid = par2;
611
                                break;
612
                        }      
613
                if (i == context_total) {
614
                        context_list[context_total].ctx = par1;
615
                        context_list[context_total].pid = par2;
616
                        context_total++;
617
                }
618
 
619
                break;
620
 
1494 giacomo 621
    }
622
 
1495 giacomo 623
    if (current_exec == MAXJOB-1) {
624
        printf("Too many execs...\n");
1494 giacomo 625
        exit(3);
626
    }
627
 
628
    if (current_endcycle == MAXJOB-1) {
629
        printf("Too many endcycle...\n");
630
        exit(4);
631
    }
632
 
633
    if (state == 10) break;
634
 
635
  }
636
 
1495 giacomo 637
  endcycle_total = current_endcycle;
638
  exec_total = current_exec;
1494 giacomo 639
 
1495 giacomo 640
  return k;
1494 giacomo 641
 
1495 giacomo 642
}
1494 giacomo 643
 
1495 giacomo 644
int create_job_list() {
1494 giacomo 645
 
1495 giacomo 646
  int current_job = 0, h, i, k;
647
  int temp_ctx;
648
  unsigned long long temp_tsc, endcycle_start_tsc;
649
  unsigned long long endcycle_end_tsc;
1494 giacomo 650
 
1495 giacomo 651
  job_list = malloc(sizeof(struct ctx_exec) * MAXJOB);
1494 giacomo 652
 
1495 giacomo 653
  for (k=0;k<context_total;k++) {
1494 giacomo 654
 
1495 giacomo 655
    temp_ctx = context_list[k].ctx;
656
    endcycle_start_tsc = 0;
1494 giacomo 657
 
1495 giacomo 658
    for (h=0;h<endcycle_total;h++) {
1494 giacomo 659
 
1495 giacomo 660
      if (endcycle_list[h].ctx == temp_ctx) {
1494 giacomo 661
 
1495 giacomo 662
        if (endcycle_start_tsc == 0)
663
          endcycle_start_tsc = log_start_tsc;
1494 giacomo 664
 
665
        endcycle_end_tsc = endcycle_list[h].tsc;
666
        temp_tsc = 0;
667
 
1495 giacomo 668
        job_list[current_job].start = 0;
1494 giacomo 669
 
1495 giacomo 670
        for(i=0;i<exec_total;i++)
671
                if (exec_list[i].ctx == temp_ctx) {
672
                        if (exec_list[i].start < endcycle_end_tsc &&
673
                                exec_list[i].start >= endcycle_start_tsc) {
674
                                if (job_list[current_job].start == 0)
675
                                  job_list[current_job].start = exec_list[i].start;
676
                                temp_tsc += exec_list[i].dtsc;
1494 giacomo 677
                        }
678
                }
679
 
1495 giacomo 680
        job_list[current_job].dtsc = temp_tsc;
681
        job_list[current_job].ctx = temp_ctx;
682
        current_job++;
1494 giacomo 683
 
684
        endcycle_start_tsc = endcycle_end_tsc;
685
 
1495 giacomo 686
      }
1494 giacomo 687
 
1495 giacomo 688
    }
1494 giacomo 689
 
1495 giacomo 690
  }    
691
 
692
  job_total = current_job;
693
 
1494 giacomo 694
  return 0;
695
 
696
}
697
 
1495 giacomo 698
int elaborate_statistics(int num, int task_type) {
699
 
1501 giacomo 700
  char pidstr[10];
701
  unsigned long long tot_tsc,mean_tsc,max_tsc,min_tsc,first_tsc;
702
  int number;
1495 giacomo 703
 
704
  switch (context_list[num].pid) {
705
      case PID_NO_DEF:
706
        sprintf(pidstr,"NODEF");
707
        break;
708
      case INT_PID:
709
        sprintf(pidstr,"  INT");
710
        break;
711
      default:
712
        sprintf(pidstr,"%5d",context_list[num].pid);
713
        break;
714
  }
715
 
716
  if (task_type == BACKGROUND) {
717
 
718
    printf("Background Task CTX [%5d] PID [%s]\n",context_list[num].ctx,pidstr);
719
 
1501 giacomo 720
    stats_from_execs(num,&tot_tsc,&min_tsc,&mean_tsc,&max_tsc,&first_tsc,&number);    
721
 
722
    printf("  Total Execution dTSC [%12llu] us [%12llu]\n",tot_tsc,tot_tsc*1000/clk_per_msec);
723
    printf("  Mean  CPU Bandwidth  [%11f%c]\n",(double)(tot_tsc)/(double)(total_dtsc)*100.0,'%');
724
    printf("    after first exec   [%11f%c]\n",(double)(tot_tsc)/(double)(total_dtsc-first_tsc)*100.0,'%');
725
    printf("  Execs Number         [%12d]\n",number);
1499 giacomo 726
    printf("  Min  Exec       dTSC [%12llu] us [%12llu]\n",min_tsc, min_tsc*1000/clk_per_msec);
1501 giacomo 727
    printf("  Mean Exec       dTSC [%12llu] us [%12llu]\n",mean_tsc, mean_tsc*1000/clk_per_msec);
1497 giacomo 728
    printf("  Max  Exec       dTSC [%12llu] us [%12llu]\n\n",max_tsc, max_tsc*1000/clk_per_msec);
1495 giacomo 729
 
1501 giacomo 730
    plot_exec_demand_function(num,pidstr);
1500 giacomo 731
 
1495 giacomo 732
  }
733
 
734
  if (task_type == INTERRUPT) {
735
 
736
    printf("Interrupts\n");
737
 
1501 giacomo 738
    stats_from_execs(num,&tot_tsc,&min_tsc,&mean_tsc,&max_tsc,&first_tsc,&number);
1499 giacomo 739
 
1501 giacomo 740
    printf("  Total Execution dTSC [%12llu] us [%12llu]\n",tot_tsc,tot_tsc*1000/clk_per_msec);
741
    printf("  Mean  CPU Bandwidth  [%11f%c]\n",(double)(tot_tsc)/(double)(total_dtsc)*100.0,'%');
742
    printf("    after first int    [%11f%c]\n",(double)(tot_tsc)/(double)(total_dtsc-first_tsc)*100.0,'%');
743
    printf("  Interrupts Number    [%12d]\n",number);
1499 giacomo 744
    printf("  Min  Interrupt  dTSC [%12llu] us [%12llu]\n",min_tsc,min_tsc*1000/clk_per_msec);
1501 giacomo 745
    printf("  Mean Interrupt  dTSC [%12llu] us [%12llu]\n",mean_tsc,mean_tsc*1000/clk_per_msec);
1497 giacomo 746
    printf("  Max  Interrupt  dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1495 giacomo 747
 
1501 giacomo 748
    plot_exec_c_distrib(num,max_tsc,pidstr);
1500 giacomo 749
 
1501 giacomo 750
    arr_stats_from_execs(num,&min_tsc,&mean_tsc,&max_tsc);
1499 giacomo 751
 
1505 giacomo 752
    if (max_tsc > 0) {
1496 giacomo 753
 
1505 giacomo 754
      printf("  Min  Arr. Delta dTSC [%12llu] us [%12llu]\n",min_tsc,min_tsc*1000/clk_per_msec);
755
      printf("  Mean Arr. Delta dTSC [%12llu] us [%12llu]\n",mean_tsc,mean_tsc*1000/clk_per_msec);
756
      printf("  Max  Arr. Delta dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1499 giacomo 757
 
1505 giacomo 758
      plot_exec_arr_distrib(num,max_tsc,pidstr);
759
 
760
    }
761
 
1495 giacomo 762
  }
763
 
764
  if (task_type == PERIODICAL) {
765
 
766
    printf("Periodical Task CTX [%5d] PID [%s]\n",context_list[num].ctx,pidstr);    
767
 
1501 giacomo 768
    stats_from_execs(num,&tot_tsc,&min_tsc,&mean_tsc,&max_tsc,&first_tsc,&number);
769
 
770
    printf("  Total Execution dTSC [%12llu] us [%12llu]\n",tot_tsc,tot_tsc*1000/clk_per_msec);
771
    printf("  Mean  CPU Bandwidth  [%11f%c]\n",(double)(tot_tsc)/(double)(total_dtsc)*100.0,'%');
772
    printf("    after first exec   [%11f%c]\n",(double)(tot_tsc)/(double)(total_dtsc-first_tsc)*100.0,'%');
773
    printf("  Execs Number         [%12d]\n",number);
1499 giacomo 774
    printf("  Min  Exec       dTSC [%12llu] us [%12llu]\n",min_tsc,min_tsc*1000/clk_per_msec);
1501 giacomo 775
    printf("  Mean Exec       dTSC [%12llu] us [%12llu]\n",mean_tsc,mean_tsc*1000/clk_per_msec);
1497 giacomo 776
    printf("  Max  Exec       dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);    
1495 giacomo 777
 
1501 giacomo 778
    plot_exec_demand_function(num,pidstr);
1500 giacomo 779
 
1501 giacomo 780
    stats_from_jobs(num,&tot_tsc,&min_tsc,&mean_tsc,&max_tsc,&first_tsc,&number);  
781
 
782
    printf("  Total Job Exec  dTSC [%12llu] us [%12llu]\n",tot_tsc,tot_tsc*1000/clk_per_msec);
783
    printf("  Jobs Number          [%12d]\n",number);
1499 giacomo 784
    printf("  Min  Job        dTSC [%12llu] us [%12llu]\n",min_tsc,min_tsc*1000/clk_per_msec);
1501 giacomo 785
    printf("  Mean Job        dTSC [%12llu] us [%12llu]\n",mean_tsc,mean_tsc*1000/clk_per_msec);
1497 giacomo 786
    printf("  Max  Job        dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1496 giacomo 787
 
1501 giacomo 788
    plot_job_c_distrib(num,max_tsc,pidstr);
1499 giacomo 789
 
1501 giacomo 790
    arr_stats_from_jobs(num,&min_tsc,&mean_tsc,&max_tsc);
1499 giacomo 791
 
1505 giacomo 792
    if (max_tsc > 0) {
793
 
794
      printf("  Min  Arr. Delta dTSC [%12llu] us [%12llu]\n",min_tsc,min_tsc*1000/clk_per_msec);
795
      printf("  Mean Arr. Delta dTSC [%12llu] us [%12llu]\n",mean_tsc,mean_tsc*1000/clk_per_msec);
796
      printf("  Max  Arr. Delta dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1495 giacomo 797
 
1505 giacomo 798
      plot_job_arr_distrib(num,max_tsc,pidstr);
1499 giacomo 799
 
1505 giacomo 800
    }
801
 
1495 giacomo 802
  }
803
 
804
  return 0;
805
 
806
}
807
 
808
int main(int argc, char *argv[]) {
809
 
810
  int events_total,k,i;
811
  int task_type;
812
  unsigned long long total_tsc;
813
 
1500 giacomo 814
  srand(getpid());
815
 
1497 giacomo 816
  if (argc < 3) {
817
    printf("%s: Enter the input file name and clk_per_msec [%s filename clk_per_msec]\n",argv[0],argv[0]);
1495 giacomo 818
    exit(1);
819
  }
820
 
821
  printf("\n");
1497 giacomo 822
 
823
  clk_per_msec = atoi(argv[2]);
824
 
825
  printf("Clk/msec = %u\n\n",clk_per_msec);
826
 
1495 giacomo 827
  events_total = create_lists(argv[1]);
828
 
1499 giacomo 829
  total_dtsc = log_end_tsc - log_start_tsc;
1495 giacomo 830
 
1499 giacomo 831
  printf("\nTotal dTSC [%12llu] us [%12llu]\n", total_dtsc, total_dtsc*1000/clk_per_msec);
832
  printf("Events     [%12d]\n",events_total);
833
  printf("Execs      [%12d]\n",exec_total);
834
  printf("EndCycles  [%12d]\n",endcycle_total);
835
 
1495 giacomo 836
  total_tsc = 0;
837
  for (i=0;i<exec_total;i++)
838
        total_tsc += exec_list[i].dtsc;
839
 
840
  /* Exec total execution check */
841
  printf("\nExec TSC sum = %llu (%2.2f)\n",total_tsc,(float)total_tsc/((float)(log_end_tsc - log_start_tsc))*100.0);
842
 
843
  printf("\nPreemption Removing.... \n");
844
 
845
  /* Remove preemption from the computation time */
846
  create_job_list();
1500 giacomo 847
/*
1495 giacomo 848
  for (k=0;k<job_total;k++)
849
        printf("Job CTX [%5d] Start [%12llu] dTSC [%12llu]\n",
850
                job_list[k].ctx,job_list[k].start,job_list[k].dtsc);
1500 giacomo 851
*/
1495 giacomo 852
  printf("\nCompute Task Statistics.... \n\n");
853
 
854
  for (i=0;i<context_total;i++) {
855
 
856
        task_type = BACKGROUND;
857
 
858
        if (context_list[i].ctx == INT_CTX) task_type = INTERRUPT;
859
 
860
        for (k=0;k<job_total;k++)
861
                if (job_list[k].ctx == context_list[i].ctx) {
862
                  task_type = PERIODICAL;
863
                  break;
864
                }
865
 
866
        elaborate_statistics(i,task_type);
867
 
868
  }
1500 giacomo 869
 
1495 giacomo 870
  return 0;
871
 
872
}
873