Subversion Repositories shark

Rev

Rev 1494 | Rev 1496 | 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;
305
 
306
  switch (context_list[num].pid) {
307
      case PID_NO_DEF:
308
        sprintf(pidstr,"NODEF");
309
        break;
310
      case INT_PID:
311
        sprintf(pidstr,"  INT");
312
        break;
313
      default:
314
        sprintf(pidstr,"%5d",context_list[num].pid);
315
        break;
316
  }
317
 
318
  if (task_type == BACKGROUND) {
319
 
320
    printf("Background Task CTX [%5d] PID [%s]\n",context_list[num].ctx,pidstr);
321
 
322
    temp_tsc = 0;
323
    max_tsc = 0;
324
    k = 0;
325
    for (i=0;i<exec_total;i++)
326
      if (exec_list[i].ctx == context_list[num].ctx) {
327
        if (exec_list[i].dtsc > max_tsc) max_tsc = exec_list[i].dtsc;
328
        temp_tsc += exec_list[i].dtsc;
329
        k++;
330
      }
331
    printf("  Total Execution dTSC [%12llu]\n",temp_tsc);
332
    printf("  Execs Number         [%12d]\n",k);
333
    printf("  Mean Exec       dTSC [%12llu]\n",temp_tsc / k);
334
    printf("  Max  Exec       dTSC [%12llu]\n\n",max_tsc);
335
 
336
  }
337
 
338
  if (task_type == INTERRUPT) {
339
 
340
    printf("Interrupts\n");
341
 
342
    temp_tsc = 0;
343
    max_tsc = 0;
344
    k = 0;
345
    for (i=0;i<exec_total;i++)
346
      if (exec_list[i].ctx == context_list[num].ctx) {
347
        if (exec_list[i].dtsc > max_tsc) max_tsc = exec_list[i].dtsc;
348
        temp_tsc += exec_list[i].dtsc;
349
        k++;
350
      }
351
    printf("  Total Execution dTSC [%12llu]\n",temp_tsc);
352
    printf("  Interrupts Number    [%12d]\n",k);
353
    printf("  Mean Interrupt  dTSC [%12llu]\n",temp_tsc / k);
354
    printf("  Max  Interrupt  dTSC [%12llu]\n\n",max_tsc);
355
 
356
  }
357
 
358
  if (task_type == PERIODICAL) {
359
 
360
    printf("Periodical Task CTX [%5d] PID [%s]\n",context_list[num].ctx,pidstr);    
361
 
362
    temp_tsc = 0;
363
    max_tsc = 0;
364
    k = 0;
365
    for (i=0;i<exec_total;i++)
366
      if (exec_list[i].ctx == context_list[num].ctx) {
367
        if (exec_list[i].dtsc > max_tsc) max_tsc = exec_list[i].dtsc;
368
        temp_tsc += exec_list[i].dtsc;
369
        k++;
370
      }
371
    printf("  Total Execution dTSC [%12llu]\n",temp_tsc);
372
    printf("  Execs Number         [%12d]\n",k);
373
    printf("  Mean Exec       dTSC [%12llu]\n",temp_tsc / k);
374
    printf("  Max  Exec       dTSC [%12llu]\n\n",max_tsc);    
375
 
376
    temp_tsc = 0;
377
    max_tsc = 0;
378
    k = 0;
379
    for (i=0;i<job_total;i++)
380
      if (job_list[i].ctx == context_list[num].ctx) {
381
        if (job_list[i].dtsc > max_tsc) max_tsc = job_list[i].dtsc;
382
        temp_tsc += job_list[i].dtsc;
383
        k++;
384
      }
385
    printf("  Total Job Exec  dTSC [%12llu]\n",temp_tsc);
386
    printf("  Jobs Number          [%12d]\n",k);
387
    printf("  Mean Job        dTSC [%12llu]\n",temp_tsc / k);
388
    printf("  Max  Job        dTSC [%12llu]\n\n",max_tsc);
389
 
390
  }
391
 
392
  return 0;
393
 
394
}
395
 
396
int main(int argc, char *argv[]) {
397
 
398
  int events_total,k,i;
399
  int task_type;
400
  unsigned long long total_tsc;
401
 
402
  if (argc < 2) {
403
    printf("%s: Enter the input file name [%s filename]\n",argv[0],argv[0]);
404
    exit(1);
405
  }
406
 
407
  printf("\n");
408
 
409
  events_total = create_lists(argv[1]);
410
 
411
  printf("\nDelta TSC = %llu\n",log_end_tsc - log_start_tsc);
412
  printf("Events    [%8d]\n",events_total);
413
  printf("Execs     [%8d]\n",exec_total);
414
  printf("EndCycles [%8d]\n",endcycle_total);
415
 
416
  total_tsc = 0;
417
  for (i=0;i<exec_total;i++)
418
        total_tsc += exec_list[i].dtsc;
419
 
420
  /* Exec total execution check */
421
  printf("\nExec TSC sum = %llu (%2.2f)\n",total_tsc,(float)total_tsc/((float)(log_end_tsc - log_start_tsc))*100.0);
422
 
423
  printf("\nPreemption Removing.... \n");
424
 
425
  /* Remove preemption from the computation time */
426
  create_job_list();
427
 
428
  for (k=0;k<job_total;k++)
429
        printf("Job CTX [%5d] Start [%12llu] dTSC [%12llu]\n",
430
                job_list[k].ctx,job_list[k].start,job_list[k].dtsc);
431
 
432
  printf("\nCompute Task Statistics.... \n\n");
433
 
434
  for (i=0;i<context_total;i++) {
435
 
436
        task_type = BACKGROUND;
437
 
438
        if (context_list[i].ctx == INT_CTX) task_type = INTERRUPT;
439
 
440
        for (k=0;k<job_total;k++)
441
                if (job_list[k].ctx == context_list[i].ctx) {
442
                  task_type = PERIODICAL;
443
                  break;
444
                }
445
 
446
        elaborate_statistics(i,task_type);
447
 
448
  }
449
 
450
  return 0;
451
 
452
}
453