File: parse.c

package info (click to toggle)
ga 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,472 kB
  • sloc: ansic: 192,963; fortran: 53,761; f90: 11,218; cpp: 5,784; makefile: 2,248; sh: 1,945; python: 1,734; perl: 534; csh: 134; asm: 106
file content (252 lines) | stat: -rw-r--r-- 6,239 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#if HAVE_CONFIG_H
#   include "config.h"
#endif

#if HAVE_STDIO_H
#   include <stdio.h>
#endif
#if HAVE_STDLIB_H
#   include <stdlib.h>
#endif

#define MAX_TOKEN 2048

/**
 * Read next token from file. Tokens are separated by the character
 * delimeter.
 * Tokens are returned as NUL terminated character strings which
 * almost certainly should always be freed with free() after use.
 *
 * There is an internally imposed maximum token size of MAX_TOKEN bytes.
 *
 * All errors are handled by returning a NULL pointer.
 */
char *ReadToken(FILE *file, char delimiter)
{
    char *buf = malloc((unsigned) MAX_TOKEN);
    char *temp;
    int input, used=0;

    if (!buf) {
        return (char *) NULL;
    }

    temp = buf;

    while ( (input = getc(file)) != EOF ) {

        used++;

        if (input == delimiter) {
            *temp = '\0';
            break;
        }
        else {
            *temp++ = (char) input;
            if (used == MAX_TOKEN) {
                used = 0; break;
            }
        }
    }

    /* duplicate string to minimize problems if string is not
       freed in calling program */

    if (used) {
        temp = strdup(buf);
    }
    else {
        temp = (char *) NULL;
    }

    (void) free(buf);

    return temp;
}


int FindToken(char *token, FILE *file, char delimiter)
{
    char *input;

    while (input = ReadToken(file, delimiter)) {
        if (strcmp(input, token) == 0) {
            return 1;
        }
        else {
            (void) free(input);
        }
    }

    return 0;
}


/**
 * Return the starting time and duration of the events file.
 */
void GetTimeSpan(FILE *file, DoublePrecision *start, DoublePrecision *duration)
{
    char *input;
    DoublePrecision end;

    end = *start = 0.0;

    if (FindToken("TIME", file, ':')) {
        end = *start = atof(input = ReadToken(file, ':'));
        (void) free(input);
    }

    while (FindToken("TIME", file, ':')) {
        end = atof(input = ReadToken(file, ':'));
        (void) free(input);
    }

    *duration = end - *start;

    (void) fseek(file, 0L, 0);
}
  

int main(int argc, char **argv)
{
    char filename[11];
    FILE *file, *plot;
    char *token;
    char *event;
    DoublePrecision time, start, duration=0.0, otim, span, margin, comms, useful;
    int newstate, state, i, nproc, lo, hi;

    if (argc == 1) {
        lo = 0;
        hi = 127;
    }
    else if (argc == 3) {
        lo = atoi(argv[1]);
        hi = atoi(argv[2]);
    }
    else {
        (void) fprintf(stderr, "usage: %s [lo hi]\n", argv[0]);
        (void) fprintf(stderr, "... with no arguments parse all event files\n");
        (void) fprintf(stderr, "... or with lo & hi only files in this range\n");
        (void) fprintf(stderr, "... e.g.  parse 16 31\n");
        return 1;
    }

    /* open the file that will have the plot stuff in it */
    /* change of heart here ... just write to stdout */

    /* 
       if (!(plot = fopen("plot", "w"))) {
       perror("failed to open plot file");
       return 1;
       }
       */

    plot = stdout;

    /* Determine how many processes there are and maximum time span */

    nproc = 0;
    for (i=lo; i<=hi; i++) {

        (void) sprintf(filename, "events.%03d", nproc);

        if ( !(file = fopen(filename, "r")) ) {
            break;
        }

        GetTimeSpan(file, &start, &span);
        (void) fclose(file);

        if (span > duration) {
            duration = span;
        }

        nproc++;
    }      

    margin = duration * 0.1;

    (void) fprintf(plot, "s %d %d %d %d\n",0,0,
                   (int) ((margin*2.0+duration)*100.0), 5*nproc);
    /* (void) fprintf(stderr, "nproc=%d, duration=%4.2f\n", nproc, duration); */

    /* Now go thru the files and actually parse the contents */

    for (i=lo; i<=hi; i++) {
        (void) sprintf(filename, "events.%03d", i);

        if ( !(file = fopen(filename, "r")) ) {
            break;
        }

        GetTimeSpan(file, &start, &span);

        comms = 0.0;
        state = 5*(i-lo);
        otim = 0.0 + margin;

        (void) fprintf(plot, "t %d %d %d\n", 0, state, i);

        while ( token = ReadToken(file, ':') ) {
            if (strcmp(token, "BEGIN") == 0)  {
                newstate = 5*(i-lo) + 1;
            }
            else if (strcmp(token, "END") == 0) {
                newstate = 5*(i-lo);
            }
            else {
                continue;
            }

            /* Have a BEGIN or END ... only process Snd/Rcv at moment */

            event = ReadToken(file, ':');
            if ((strcmp(event, "Snd") == 0)
                    || (strcmp(event,"Rcv") == 0)
                    || (strcmp(event,"Waitcom") == 0)) {

                if (FindToken("TIME", file, ':')) {
                    time = atof(ReadToken(file, ':')) - start + margin;

                    (void) fprintf(plot, "l %d %d %d %d\n",
                                   (int) (otim*100.0), state,
                                   (int) (time*100.0), state);
                    (void) fprintf(plot, "l %d %d %d %d\n",
                                   (int) (time*100.0), state,
                                   (int) (time*100.0), newstate);

                    /* Accumulate the time spent in communication */

                    if (newstate == (5*(i-lo)))  {
                        comms = comms + time - otim;
                    }

                    otim = time;
                    state = newstate;
                }
            }
            else if (strcmp(event, "Process") == 0) {
                if (FindToken("TIME", file, ':')) {
                    time = atof(ReadToken(file, ':')) - start + margin;

                    (void) fprintf(plot, "l %d %d %d %d\n", (int) (otim*100.0), state,
                                   (int) (time*100.0), state);
                    otim = time;
                }
            }
        }

        /* Assume that non-communication time is useful */

        useful = 100.0 * (span - comms) / duration;

        (void) fprintf(plot, "t %d %d %4.1f%%\n",
                       (int) (100.0*duration+150.0*margin), 5*(i-lo), useful);

        (void) fflush(plot);
        (void) fclose(file);
    }
    return 0;
}