Subversion Repositories shark

Rev

Rev 1304 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1301 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *    Giacomo Guidi      <giacomo@gandalf.sssup.it>
10
 *
11
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
12
 *
13
 * http://www.sssup.it
14
 * http://retis.sssup.it
15
 * http://shark.sssup.it
16
 */
17
 
18
#include <stdio.h>
19
#include <unistd.h>
20
#include <stdlib.h>
21
#include <string.h>
22
 
23
#define READ_BUFFER 2000
24
#define DELTA_BUFFER 100
25
 
26
struct event_type {
27
  int type;
28
  unsigned long long tsc;
29
  int par[10];
30
};
31
 
32
struct ctx_time_type {
33
  int ctx;
34
  unsigned long long start_time;
35
  unsigned long long total_time;
36
};
37
 
38
struct ctx_time_type ctx_time[256];
1302 giacomo 39
struct ctx_time_type int_time;
1301 giacomo 40
unsigned long long last_time;
41
int total_ctx = 0;
42
int last_ctx = -1;
43
 
44
void analyze_event(struct event_type *e) {
45
 
46
  int i;
47
 
1302 giacomo 48
  // Start interrupt
49
  if (e->type == 0x2b || e->type == 0x03) {
50
 
51
    if (int_time.start_time == 0) int_time.start_time = e->tsc;
52
 
53
    if (last_ctx != -1) {
1301 giacomo 54
 
1302 giacomo 55
      if (e->tsc < last_time) {
56
        printf("Error events sequence\n");
57
        exit(2);
58
      }
59
 
60
      ctx_time[last_ctx].total_time += (e->tsc - last_time);
61
      last_time = e->tsc;
62
 
63
    } else {
64
 
65
      last_time = e->tsc;
66
 
67
    }
68
 
69
  }
70
 
71
  //End Interrupt
72
  if (e->type == 0x3b || e->type == 0x13) {
73
 
74
    if (int_time.start_time == 0) int_time.start_time = e->tsc;
75
 
76
    if (last_time != 0) {
77
 
78
      if (e->tsc < last_time) {
79
        printf("Error events sequence\n");
80
        exit(2);
81
      }
82
 
83
      int_time.total_time += (e->tsc - last_time);
84
      last_time = e->tsc;
85
 
86
    } else {
87
 
88
      last_time = e->tsc;
89
 
90
    }
91
 
1301 giacomo 92
    i = 0;
93
    while (ctx_time[i].ctx != e->par[0] && i < total_ctx) {
94
      i++;
95
    }
96
 
97
    if (ctx_time[i].start_time == 0) {
1302 giacomo 98
      ctx_time[i].start_time = e->tsc;
99
      ctx_time[i].total_time = 0;
1301 giacomo 100
      ctx_time[i].ctx = e->par[0];
1302 giacomo 101
      last_ctx = i;
102
      total_ctx++;
103
    } else {
104
      last_ctx = i;
105
    }
106
 
107
  }
108
 
109
  //Context Switch
110
  if (e->type == 0x15) {
111
 
112
    i = 0;
113
    while (ctx_time[i].ctx != e->par[0] && i < total_ctx) {
114
      i++;
115
    }
116
 
117
    if (ctx_time[i].start_time == 0) {
118
      ctx_time[i].ctx = e->par[0];
1301 giacomo 119
      ctx_time[i].start_time = e->tsc;
120
      ctx_time[i].total_time = 0;
121
 
122
      if (e->tsc < last_time) {
123
        printf("Error events sequence\n");
124
        exit(2);
125
      }
126
 
127
      if (last_ctx != -1) ctx_time[last_ctx].total_time += (e->tsc - last_time);  
128
 
129
      last_time = e->tsc;
130
      last_ctx = i;
131
      total_ctx++;
132
 
133
    } else {
134
 
135
      if (e->tsc < last_time) {
136
        printf("Error events sequence\n");
137
        exit(2);
138
      }
139
 
1302 giacomo 140
      if (last_ctx != -1) ctx_time[last_ctx].total_time += (e->tsc - last_time);
1301 giacomo 141
      last_time = e->tsc;
142
      last_ctx = i;
143
 
144
    }
145
 
146
  }
147
 
148
}
149
 
150
int main(int argc, char *argv[])
151
{
152
 
153
  char buffer[READ_BUFFER+DELTA_BUFFER];
154
  void *p, *last;
155
  int n,i,delta,size;
156
  struct event_type evt;
157
 
158
  unsigned long long ev = 0,start,stop;
159
  unsigned int total_simulation = 0;
160
 
161
  FILE *input_file;
162
 
163
  if (argc < 2) {
164
    printf("%s: Enter the input file name [%s filename]\n",argv[0],argv[0]);
165
    exit(1);
166
  }
167
 
168
  memset(ctx_time,0,sizeof(ctx_time));
1302 giacomo 169
  memset(&int_time,0,sizeof(int_time));
1301 giacomo 170
  total_ctx = 0;
171
 
172
  input_file = fopen(argv[1],"rb");
173
 
174
  last = buffer + READ_BUFFER;
175
 
176
  while(!feof(input_file)) {
177
 
178
    //move remaining byte
179
    delta = (unsigned int)(buffer) + READ_BUFFER - (unsigned int)(last);
180
    if (delta > 0) memcpy(buffer,last,delta);    
181
 
182
    n = fread(buffer+delta,1,READ_BUFFER+10,input_file);
183
 
184
    fseek(input_file,-(delta+10),SEEK_CUR);
185
 
186
    p = buffer;
187
 
188
    while ((unsigned int)(p) + *(unsigned char *)(p+9) <= (unsigned int)(buffer + READ_BUFFER) &&
189
           (unsigned int)(p) + *(unsigned char *)(p+9) <= (unsigned int)(buffer + n + delta)) {
190
 
191
      evt.type = (int)(*(unsigned char *)(p));
192
      evt.tsc = (unsigned long long)(*(unsigned int *)(p+1));
193
      evt.tsc <<= 32;
194
      evt.tsc += (unsigned long long)(*(unsigned int *)(p+5));
195
 
196
      size = *(unsigned char *)(p+9);
197
 
198
      if (evt.type == 0x20) {
199
         printf("Start Simulation\n");
200
         start = evt.tsc;
201
      }
202
 
203
      if (evt.type == 0x30) {
204
         printf("Stop Simulation\n");
205
         stop = evt.tsc;
206
      }
207
 
208
      size -= 10;
209
      i = 0;
210
      while (size > 0) {
211
        evt.par[i] = (int)(*(unsigned int *)(p+10+i*4));
212
        i++;
213
        size -= 4;
214
      }
215
 
216
      ev++;
217
 
218
      analyze_event(&evt);
219
 
220
      p += *(unsigned char *)(p+9);
221
 
222
      if ((unsigned int)p + 10 > (unsigned int)(buffer + n + delta)) break;
223
 
224
      last = p;
225
 
226
    }
227
 
228
    if ((unsigned int)p + 10 > (unsigned int)(buffer + n + delta)) break;
229
 
230
  }
231
 
232
  fclose(input_file);
233
 
234
  printf("Total Time %d\n",(unsigned int)((stop-start) * 1000 / 501162));
235
 
1302 giacomo 236
  printf("Total Ctx = %d\n",total_ctx);
1301 giacomo 237
 
1302 giacomo 238
  printf("Total Interrupt = %d\n",(unsigned int)(int_time.total_time * 1000 / 501162));
239
 
1301 giacomo 240
  total_simulation = 0;
241
  for (i=0;i<total_ctx;i++) {
242
    printf("Total Time Ctx %d => %d\n",ctx_time[i].ctx,(unsigned int)(ctx_time[i].total_time * 1000 / 501162));
243
    total_simulation += (unsigned int)(ctx_time[i].total_time * 1000 / 501162);
244
  }
245
 
246
  printf ("Total Simulation %d\n",total_simulation);
247
 
248
  return 0;
249
 
250
}
251