Subversion Repositories shark

Rev

Rev 1496 | Rev 1499 | 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>
3
 
4
#define MAXCONTEXT 100
5
#define MAXJOB 100000
6
 
7
#define INT_CTX 1
8
#define INT_PID 9999
9
#define PID_NO_DEF -1
10
 
1495 giacomo 11
#define BACKGROUND 0
12
#define PERIODICAL 1
13
#define INTERRUPT  2
14
 
15
struct ctx_exec {
1494 giacomo 16
        int ctx;
17
        unsigned long long dtsc;
18
        unsigned long long start;
19
};
20
 
1495 giacomo 21
struct ctx_to_pid {
1494 giacomo 22
        int ctx;
23
        int pid;
24
};
25
 
1495 giacomo 26
struct endcycle {
1494 giacomo 27
        int ctx;
28
        unsigned long long tsc;
29
};
30
 
31
void Error(int num) {
1495 giacomo 32
        printf("Finite-State machine error: %d\n",num);
1494 giacomo 33
        exit(2);
34
}
35
 
1495 giacomo 36
int context_total = 0,endcycle_total = 0,job_total = 0,exec_total = 0;
37
struct ctx_exec *exec_list;
38
struct ctx_to_pid *context_list;
39
struct endcycle *endcycle_list;
40
struct ctx_exec *job_list;
1494 giacomo 41
 
1497 giacomo 42
unsigned int clk_per_msec = 0;
43
 
1495 giacomo 44
unsigned long long log_start_tsc = 0;
45
unsigned long long log_end_tsc = 0;
46
 
47
int create_lists(char *filename) {
48
 
1494 giacomo 49
  FILE *input_file;
50
 
1495 giacomo 51
  int type,par1,par2,k,i,state;
52
 
1494 giacomo 53
  int current_context = 0;
1495 giacomo 54
  int current_exec = 0;
1494 giacomo 55
  int current_endcycle = 0;
56
 
1495 giacomo 57
  unsigned long long last_tsc, tsc;
1494 giacomo 58
 
1495 giacomo 59
  input_file = fopen(filename,"r");
1494 giacomo 60
 
1495 giacomo 61
  /* Memory alloc */
62
  exec_list = malloc(sizeof(struct ctx_exec) * MAXJOB);
63
  context_list = malloc(sizeof(struct ctx_to_pid) * MAXCONTEXT);
64
  endcycle_list = malloc(sizeof(struct endcycle) * MAXJOB);
1494 giacomo 65
 
1495 giacomo 66
  /* Finite-State machine
67
   *
68
   * FS-Machine states:
1494 giacomo 69
 
70
 
71
        1 - Context running
72
        2 - Interrupt running
73
 
74
        10 - End
75
 
1495 giacomo 76
   */
1494 giacomo 77
 
78
  for(i=0;i<MAXCONTEXT;i++) {
1495 giacomo 79
    context_list[i].ctx = 0;
80
    context_list[i].pid = PID_NO_DEF;
1494 giacomo 81
  }
82
 
83
  /* The start context + interrupt context */
1495 giacomo 84
  context_total = 2;
1494 giacomo 85
  current_context = 0;
86
  last_tsc = 0;
87
  context_list[0].ctx = 0;
88
  context_list[0].pid = PID_NO_DEF;
89
  context_list[1].ctx = INT_CTX;
90
  context_list[1].pid = INT_PID;
91
 
92
  state = 0;
93
 
94
  k = 0;
95
  while(!feof(input_file)) {
96
 
97
    fscanf(input_file,"%d %llu",&type,&tsc);
98
    k++;
99
 
100
    switch (type) {
101
 
102
        case 0:
103
        case 1:
104
 
105
                /* No par */
106
                break;
107
 
108
        case 2:
109
        case 3:
110
        case 4:
111
        case 6:
112
        case 7:
113
        case 8:
114
 
115
                /* 1 par */
116
                fscanf(input_file,"%d",&par1);
117
                break;
118
 
119
        case 5:
120
                /* 2 par */
121
                fscanf(input_file,"%d %d",&par1,&par2);
122
                break;
123
 
124
    }
125
 
126
    switch (type) {
127
 
128
        case 0:
129
                if (state != 0) Error(1);
130
                printf("EVT:Log starts at [%12llu]\n",tsc);
131
                last_tsc = tsc;
132
                log_start_tsc = tsc;
1495 giacomo 133
                exec_list[current_exec].start = tsc;
1494 giacomo 134
                state = 1;
135
                break;
136
 
137
        case 1:
138
                printf("EVT:Log   ends at [%12llu]\n",tsc);
1495 giacomo 139
                exec_list[current_exec].dtsc = tsc - last_tsc;
140
                exec_list[current_exec].ctx = current_context;
141
                current_exec++;
1494 giacomo 142
                last_tsc = tsc;
143
                log_end_tsc = tsc;
144
                state = 10;
145
                break;
146
 
147
        /* Int start */
148
        case 2:
149
                if (state == 0) Error(2);
1495 giacomo 150
                exec_list[current_exec].dtsc = tsc - last_tsc;
151
                exec_list[current_exec].ctx = current_context;
152
                current_exec++;
1494 giacomo 153
                last_tsc = tsc;
154
                current_context = INT_CTX;
1495 giacomo 155
                exec_list[current_exec].start = tsc;
1494 giacomo 156
                state = 2;
157
                break;
158
 
159
        /* Int end */
160
        case 3:
161
                if (state != 2) Error(3);              
1495 giacomo 162
                exec_list[current_exec].dtsc = tsc - last_tsc;
163
                exec_list[current_exec].ctx = current_context;
164
                current_exec++;
1494 giacomo 165
                last_tsc = tsc;
166
                current_context = par1;
167
 
1495 giacomo 168
                for (i=0;i<context_total;i++)
1494 giacomo 169
                        if (par1 == context_list[i].ctx) break;
1495 giacomo 170
                if (i == context_total) {
171
                        context_list[context_total].ctx = par1;
172
                        context_total++;
1494 giacomo 173
                }
174
 
1495 giacomo 175
                exec_list[current_exec].start = tsc;
1494 giacomo 176
                state = 1;
177
                break;
178
 
179
        /* Change ctx */
180
        case 4:
181
 
1495 giacomo 182
                exec_list[current_exec].dtsc = tsc - last_tsc;
183
                exec_list[current_exec].ctx = current_context;
184
                current_exec++;
1494 giacomo 185
                last_tsc = tsc;
186
                current_context = par1;
187
 
1495 giacomo 188
                for (i=0;i<context_total;i++)
1494 giacomo 189
                        if (par1 == context_list[i].ctx) break;
1495 giacomo 190
                if (i == context_total) {
191
                        context_list[context_total].ctx = par1;
192
                        context_total++;
1494 giacomo 193
                }
194
 
1495 giacomo 195
                exec_list[current_exec].start = tsc;
1494 giacomo 196
                state = 1;
197
                break;
198
 
199
        /* Task create */
200
        case 5:
201
 
1495 giacomo 202
                for (i=0;i<context_total;i++)
1494 giacomo 203
                        if (par1 == context_list[i].ctx) break;
1495 giacomo 204
                if (i == context_total) {
205
                        context_list[context_total].ctx = par1;
206
                        context_list[context_total].pid = par2;
207
                        context_total++;
1494 giacomo 208
                }
209
 
210
                break;
211
 
212
        /* Task endcycle */
213
        case 8:
214
 
1495 giacomo 215
                for (i=0;i<context_total;i++)
1494 giacomo 216
                        if (par1 == context_list[i].ctx) break;
1495 giacomo 217
                if (i == context_total) Error(4);
1494 giacomo 218
 
219
                endcycle_list[current_endcycle].ctx = par1;
220
                endcycle_list[current_endcycle].tsc = tsc;
221
                current_endcycle++;
222
 
223
                break;
224
 
225
    }
226
 
1495 giacomo 227
    if (current_exec == MAXJOB-1) {
228
        printf("Too many execs...\n");
1494 giacomo 229
        exit(3);
230
    }
231
 
232
    if (current_endcycle == MAXJOB-1) {
233
        printf("Too many endcycle...\n");
234
        exit(4);
235
    }
236
 
237
    if (state == 10) break;
238
 
239
  }
240
 
1495 giacomo 241
  endcycle_total = current_endcycle;
242
  exec_total = current_exec;
1494 giacomo 243
 
1495 giacomo 244
  return k;
1494 giacomo 245
 
1495 giacomo 246
}
1494 giacomo 247
 
1495 giacomo 248
int create_job_list() {
1494 giacomo 249
 
1495 giacomo 250
  int current_job = 0, h, i, k;
251
  int temp_ctx;
252
  unsigned long long temp_tsc, endcycle_start_tsc;
253
  unsigned long long endcycle_end_tsc;
1494 giacomo 254
 
1495 giacomo 255
  job_list = malloc(sizeof(struct ctx_exec) * MAXJOB);
1494 giacomo 256
 
1495 giacomo 257
  for (k=0;k<context_total;k++) {
1494 giacomo 258
 
1495 giacomo 259
    temp_ctx = context_list[k].ctx;
260
    endcycle_start_tsc = 0;
1494 giacomo 261
 
1495 giacomo 262
    for (h=0;h<endcycle_total;h++) {
1494 giacomo 263
 
1495 giacomo 264
      if (endcycle_list[h].ctx == temp_ctx) {
1494 giacomo 265
 
1495 giacomo 266
        if (endcycle_start_tsc == 0)
267
          endcycle_start_tsc = log_start_tsc;
1494 giacomo 268
 
269
        endcycle_end_tsc = endcycle_list[h].tsc;
270
        temp_tsc = 0;
271
 
1495 giacomo 272
        job_list[current_job].start = 0;
1494 giacomo 273
 
1495 giacomo 274
        for(i=0;i<exec_total;i++)
275
                if (exec_list[i].ctx == temp_ctx) {
276
                        if (exec_list[i].start < endcycle_end_tsc &&
277
                                exec_list[i].start >= endcycle_start_tsc) {
278
                                if (job_list[current_job].start == 0)
279
                                  job_list[current_job].start = exec_list[i].start;
280
                                temp_tsc += exec_list[i].dtsc;
1494 giacomo 281
                        }
282
                }
283
 
1495 giacomo 284
        job_list[current_job].dtsc = temp_tsc;
285
        job_list[current_job].ctx = temp_ctx;
286
        current_job++;
1494 giacomo 287
 
288
        endcycle_start_tsc = endcycle_end_tsc;
289
 
1495 giacomo 290
      }
1494 giacomo 291
 
1495 giacomo 292
    }
1494 giacomo 293
 
1495 giacomo 294
  }    
295
 
296
  job_total = current_job;
297
 
1494 giacomo 298
  return 0;
299
 
300
}
301
 
1495 giacomo 302
int elaborate_statistics(int num, int task_type) {
303
 
304
  int i,k;
305
  char pidstr[10];
306
  unsigned long long temp_tsc,max_tsc;
1496 giacomo 307
  unsigned long long last_start, delta_start;
1495 giacomo 308
 
309
  switch (context_list[num].pid) {
310
      case PID_NO_DEF:
311
        sprintf(pidstr,"NODEF");
312
        break;
313
      case INT_PID:
314
        sprintf(pidstr,"  INT");
315
        break;
316
      default:
317
        sprintf(pidstr,"%5d",context_list[num].pid);
318
        break;
319
  }
320
 
321
  if (task_type == BACKGROUND) {
322
 
323
    printf("Background Task CTX [%5d] PID [%s]\n",context_list[num].ctx,pidstr);
324
 
325
    temp_tsc = 0;
326
    max_tsc = 0;
327
    k = 0;
328
    for (i=0;i<exec_total;i++)
329
      if (exec_list[i].ctx == context_list[num].ctx) {
330
        if (exec_list[i].dtsc > max_tsc) max_tsc = exec_list[i].dtsc;
331
        temp_tsc += exec_list[i].dtsc;
332
        k++;
333
      }
1497 giacomo 334
    printf("  Total Execution dTSC [%12llu] us [%12llu]\n",temp_tsc,temp_tsc*1000/clk_per_msec);
1495 giacomo 335
    printf("  Execs Number         [%12d]\n",k);
1497 giacomo 336
    printf("  Mean Exec       dTSC [%12llu] us [%12llu]\n",temp_tsc / k, temp_tsc / k*1000/clk_per_msec);
337
    printf("  Max  Exec       dTSC [%12llu] us [%12llu]\n\n",max_tsc, max_tsc*1000/clk_per_msec);
1495 giacomo 338
 
339
  }
340
 
341
  if (task_type == INTERRUPT) {
342
 
343
    printf("Interrupts\n");
344
 
345
    temp_tsc = 0;
346
    max_tsc = 0;
347
    k = 0;
348
    for (i=0;i<exec_total;i++)
349
      if (exec_list[i].ctx == context_list[num].ctx) {
350
        if (exec_list[i].dtsc > max_tsc) max_tsc = exec_list[i].dtsc;
351
        temp_tsc += exec_list[i].dtsc;
352
        k++;
353
      }
1497 giacomo 354
    printf("  Total Execution dTSC [%12llu] us [%12llu]\n",temp_tsc,temp_tsc*1000/clk_per_msec);
1495 giacomo 355
    printf("  Interrupts Number    [%12d]\n",k);
1497 giacomo 356
    printf("  Mean Interrupt  dTSC [%12llu] us [%12llu]\n",temp_tsc / k,temp_tsc / k*1000/clk_per_msec);
357
    printf("  Max  Interrupt  dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1495 giacomo 358
 
1496 giacomo 359
    last_start = 0;
360
    temp_tsc = 0;
361
    max_tsc = 0;
362
    k = 0;
363
    for (i=0;i<exec_total;i++)
364
      if (exec_list[i].ctx == context_list[num].ctx) {
365
        if (last_start == 0) {
366
                last_start = exec_list[i].start;
367
        } else {
368
                delta_start = exec_list[i].start - last_start;
369
                if (delta_start > max_tsc) max_tsc = delta_start;
370
                temp_tsc += delta_start;
371
                k++;
372
                last_start = exec_list[i].start;
373
        }
374
      }
375
 
1497 giacomo 376
    printf("  Mean Arr. Delta dTSC [%12llu] us [%12llu]\n",temp_tsc / k,temp_tsc / k*1000/clk_per_msec);
377
    printf("  Max  Arr. Delta dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1496 giacomo 378
 
1495 giacomo 379
  }
380
 
381
  if (task_type == PERIODICAL) {
382
 
383
    printf("Periodical Task CTX [%5d] PID [%s]\n",context_list[num].ctx,pidstr);    
384
 
385
    temp_tsc = 0;
386
    max_tsc = 0;
387
    k = 0;
388
    for (i=0;i<exec_total;i++)
389
      if (exec_list[i].ctx == context_list[num].ctx) {
390
        if (exec_list[i].dtsc > max_tsc) max_tsc = exec_list[i].dtsc;
391
        temp_tsc += exec_list[i].dtsc;
392
        k++;
393
      }
1497 giacomo 394
    printf("  Total Execution dTSC [%12llu] us [%12llu]\n",temp_tsc,temp_tsc*1000/clk_per_msec);
1495 giacomo 395
    printf("  Execs Number         [%12d]\n",k);
1497 giacomo 396
    printf("  Mean Exec       dTSC [%12llu] us [%12llu]\n",temp_tsc / k,temp_tsc / k*1000/clk_per_msec);
397
    printf("  Max  Exec       dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);    
1495 giacomo 398
 
399
    temp_tsc = 0;
400
    max_tsc = 0;
401
    k = 0;
402
    for (i=0;i<job_total;i++)
403
      if (job_list[i].ctx == context_list[num].ctx) {
404
        if (job_list[i].dtsc > max_tsc) max_tsc = job_list[i].dtsc;
405
        temp_tsc += job_list[i].dtsc;
406
        k++;
407
      }
1497 giacomo 408
    printf("  Total Job Exec  dTSC [%12llu] us [%12llu]\n",temp_tsc,temp_tsc*1000/clk_per_msec);
1495 giacomo 409
    printf("  Jobs Number          [%12d]\n",k);
1497 giacomo 410
    printf("  Mean Job        dTSC [%12llu] us [%12llu]\n",temp_tsc / k,temp_tsc / k*1000/clk_per_msec);
411
    printf("  Max  Job        dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1496 giacomo 412
 
413
    last_start = 0;
414
    temp_tsc = 0;
415
    max_tsc = 0;
416
    k = 0;
417
    for (i=0;i<job_total;i++)
418
      if (job_list[i].ctx == context_list[num].ctx) {
419
        if (last_start == 0) {
420
                last_start = job_list[i].start;
421
        } else {
422
                delta_start = job_list[i].start - last_start;
423
                if (delta_start > max_tsc) max_tsc = delta_start;
424
                temp_tsc += delta_start;
425
                k++;
426
                last_start = job_list[i].start;
427
        }
428
      }
429
 
1497 giacomo 430
    printf("  Mean Arr. Delta dTSC [%12llu] us [%12llu]\n",temp_tsc / k,temp_tsc / k*1000/clk_per_msec);
431
    printf("  Max  Arr. Delta dTSC [%12llu] us [%12llu]\n\n",max_tsc,max_tsc*1000/clk_per_msec);
1495 giacomo 432
 
433
  }
434
 
435
  return 0;
436
 
437
}
438
 
439
int main(int argc, char *argv[]) {
440
 
441
  int events_total,k,i;
442
  int task_type;
443
  unsigned long long total_tsc;
444
 
1497 giacomo 445
  if (argc < 3) {
446
    printf("%s: Enter the input file name and clk_per_msec [%s filename clk_per_msec]\n",argv[0],argv[0]);
1495 giacomo 447
    exit(1);
448
  }
449
 
450
  printf("\n");
1497 giacomo 451
 
452
  clk_per_msec = atoi(argv[2]);
453
 
454
  printf("Clk/msec = %u\n\n",clk_per_msec);
455
 
1495 giacomo 456
  events_total = create_lists(argv[1]);
457
 
458
  printf("\nDelta TSC = %llu\n",log_end_tsc - log_start_tsc);
459
  printf("Events    [%8d]\n",events_total);
460
  printf("Execs     [%8d]\n",exec_total);
461
  printf("EndCycles [%8d]\n",endcycle_total);
462
 
463
  total_tsc = 0;
464
  for (i=0;i<exec_total;i++)
465
        total_tsc += exec_list[i].dtsc;
466
 
467
  /* Exec total execution check */
468
  printf("\nExec TSC sum = %llu (%2.2f)\n",total_tsc,(float)total_tsc/((float)(log_end_tsc - log_start_tsc))*100.0);
469
 
470
  printf("\nPreemption Removing.... \n");
471
 
472
  /* Remove preemption from the computation time */
473
  create_job_list();
474
 
475
  for (k=0;k<job_total;k++)
476
        printf("Job CTX [%5d] Start [%12llu] dTSC [%12llu]\n",
477
                job_list[k].ctx,job_list[k].start,job_list[k].dtsc);
478
 
479
  printf("\nCompute Task Statistics.... \n\n");
480
 
481
  for (i=0;i<context_total;i++) {
482
 
483
        task_type = BACKGROUND;
484
 
485
        if (context_list[i].ctx == INT_CTX) task_type = INTERRUPT;
486
 
487
        for (k=0;k<job_total;k++)
488
                if (job_list[k].ctx == context_list[i].ctx) {
489
                  task_type = PERIODICAL;
490
                  break;
491
                }
492
 
493
        elaborate_statistics(i,task_type);
494
 
495
  }
496
 
497
  return 0;
498
 
499
}
500