Subversion Repositories shark

Rev

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