CodesKernelHelpers.c 9.98 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1
2
3
4
5
6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7
#include "codes/CodesKernelHelpers.h"
8
9
10
11
12
13
14

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
15
#include <libgen.h>
16
17
18
19
20
21
22
23
24
25

#define CK_LINE_LIMIT 8192
#define CL_DEFAULT_GID 0

char * code_kernel_helpers_kinstToStr(int inst)
{
    switch(inst)
    {
        case WRITEAT:
            return "WRITEAT";
26
27
	case READAT:
	    return "READAT";
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
        case GETGROUPRANK:
            return "GETGROUPRANK";
        case GETGROUPSIZE:
            return "GETGROUPSIZE";
        case CLOSE:
            return "CLOSE";
        case OPEN:
            return "OPEN";
        case SYNC:
            return "SYNC";
        case SLEEP:
            return "SLEEP";
        case EXIT:
            return "EXIT";
        case DELETE:
            return "DELETE";
        default:
            return "UNKNOWN";
    };
    
    return "UNKNOWN";
}

char * code_kernel_helpers_cleventToStr(int inst)
{
    switch(inst)
    {
        case CL_WRITEAT:
            return "CL_WRITEAT";
57
58
	case CL_READAT:
	    return "CL_READAT";
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
        case CL_GETRANK:
            return "CL_GETRANK";
        case CL_GETSIZE:
            return "CL_GETSIZE";
        case CL_CLOSE:
            return "CL_CLOSE";
        case CL_OPEN:
            return "CL_OPEN";
        case CL_SYNC:
            return "CL_SYNC";
        case CL_SLEEP:
            return "CL_SLEEP";
        case CL_EXIT:
            return "CL_EXIT";
        case CL_DELETE:
            return "CL_DELETE";
        default:
            return "CL_UNKNOWN";
    };
    
    return "CL_UNKNOWN";
}

static int convertKLInstToEvent(int inst)
{
    switch(inst)
    {
        case WRITEAT:
            return CL_WRITEAT;
88
89
	case READAT:
	    return CL_READAT;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        case GETGROUPRANK:
            return CL_GETRANK;
        case GETGROUPSIZE:
            return CL_GETSIZE;
        case CLOSE:
            return CL_CLOSE;
        case OPEN:
            return CL_OPEN;
        case SYNC:
            return CL_SYNC;
        case SLEEP:
            return CL_SLEEP;
        case EXIT:
            return CL_EXIT;
        case DELETE:
            return CL_DELETE;
        default:
            return CL_UNKNOWN;
    };

    return CL_UNKNOWN;
}

static void codes_kernel_helper_parse_cf(char * io_kernel_path, char *
114
        io_kernel_def_path, char * io_kernel_meta_path, int task_rank, codes_workload_info * task_info, int use_relpath)
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
{
       int foundit = 0;
       char line[CK_LINE_LIMIT];
       FILE * ikmp = NULL;

       /* open the config file */
       ikmp = fopen(io_kernel_meta_path, "r");
       if(ikmp == NULL)
       {
           fprintf(stderr, "%s:%i could not open meta file (%s)... bail\n", __func__,
                   __LINE__, io_kernel_meta_path);
           exit(1);
       }

       /* for each line in the config file */
       while(fgets(line, CK_LINE_LIMIT, ikmp) != NULL)
       {
               char * token = NULL;
               int min = 0;
               int max = 0;
               int gid = 0;
               char * ctx = NULL;

               /* parse the first element... the gid */
               token = strtok_r(line, " \n", &ctx);
               if(token)
                   gid = atoi(token);

               if(gid == CL_DEFAULT_GID)
               {
                   fprintf(stderr, "%s:%i incorrect GID detected in kernel meta\
                           file. Cannot use the reserved GID\
                           CL_DEFAULT_GID(%i)\n", __func__, __LINE__,
                           CL_DEFAULT_GID);
               }

               /* parse the second element... min rank */
               token = strtok_r(NULL, " \n", &ctx);
               if(token)
                       min = atoi(token);

               /* parse the third element... max rank */
               token = strtok_r(NULL, " \n", &ctx);
               if(token)
                       max = atoi(token);

               /* parse the last element... kernel path */
               token = strtok_r(NULL, " \n", &ctx);
163
164
               if(token) {
                       if (use_relpath){
165
166
167
168
169
                           /* posix dirname overwrites argument :(, need to
                            * prevent that */
                           char *tmp_path = strdup(io_kernel_meta_path);
                           sprintf(io_kernel_path, "%s/%s", dirname(tmp_path), token);
                           free(tmp_path);
170
171
172
173
174
                       }
                       else{
                           strcpy(io_kernel_path, token);
                       }
               }
175
176
177
178
179

               /* if our rank is on this range... end processing of the config
                * file */
               if(task_rank >= min && task_rank <= max)
               {
180
181
182
183
                       task_info->group_id = gid;
                       task_info->min_rank = min;
                       task_info->max_rank = max;
                       task_info->local_rank = task_rank - min;
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                       task_info->num_lrank = max - min + 1;

                       foundit = 1;

                       break;
               }
       }

       /* close the config file */
       fclose(ikmp);

       /* if we did not find the config file, set it to the default */
       if(foundit == 0)
       {
               /* default kernel */
               strcpy(io_kernel_path, io_kernel_def_path);

               /* default gid and task attrs */
               /* TODO can we detect the gaps instead of -1 */
203
204
205
206
               task_info->group_id = CL_DEFAULT_GID;
               task_info->min_rank = -1;
               task_info->max_rank = -1;
               task_info->local_rank = -1;
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
               task_info->num_lrank = -1;
       }

       return;
}

int codes_kernel_helper_parse_input(CodesIOKernel_pstate * ps, CodesIOKernelContext * c,
                          codeslang_inst * inst)
{
    int yychar;
    int status;
    int codes_inst = CL_NOOP;

    /* swap in the symbol table for the current LP context */
    CodesIOKernelScannerSetSymTable(c);

    do
    {
        c->locval = CodesIOKernel_get_lloc(*((yyscan_t *)c->scanner_));
        yychar = CodesIOKernel_lex(c->lval, c->locval, c->scanner_);
        c->locval = NULL;
      
        /* for each instructions */
        switch(yychar)
        {
            /* for each instrunction that triggers a simulator event */
            case WRITEAT:
234
            case READAT:
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
            case GETGROUPRANK:
            case GETGROUPSIZE:
            case CLOSE:
            case OPEN:
            case SYNC:
            case SLEEP:
            case EXIT:
            case DELETE:
            {
                c->inst_ready = 0;
                status = CodesIOKernel_push_parse(ps, yychar, c->lval, c->locval, c);
                codes_inst = convertKLInstToEvent(yychar);
		break;
            }
            /* not a simulator event */
            default:
            {
                status = CodesIOKernel_push_parse(ps, yychar, c->lval, c->locval, c);
		break;
            }
        };

        /* if the instruction is ready (all preq data is ready ) */
        if(c->inst_ready)
        {
            /* reset instruction ready state */
            c->inst_ready = 0;

            switch(codes_inst)
            {
                case CL_GETRANK:
                case CL_GETSIZE:
                case CL_WRITEAT:
268
	        case CL_READAT:
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
                case CL_OPEN:
                case CL_CLOSE:
                case CL_SYNC:
                case CL_EXIT:
                case CL_DELETE:
                case CL_SLEEP:
                {
                    int i = 0;

                    inst->event_type = codes_inst;
                    inst->num_var = c->var[0];
                    for(i = 0 ; i < inst->num_var ; i++)
                    {
                        inst->var[i] = c->var[i + 1];
                    }

		    break;
                }
                /* we don't need to store the instructions args */
                default:
                {
                    continue;
                }
            };
            
	    /* we have all of the data for the instruction... bail */
            break;
        }
    /* while there are more instructions to parse in the stream */
    }while(status == YYPUSH_MORE);

    /* return the simulator instruction */
    return codes_inst;
}

int codes_kernel_helper_bootstrap(char * io_kernel_path, char *
        io_kernel_def_path, char * io_kernel_meta_path,
306
        int rank, int use_relpath, CodesIOKernelContext * c,
307
        CodesIOKernel_pstate ** ps, codes_workload_info * task_info,
308
309
310
311
312
313
314
315
316
        codeslang_inst * next_event)
{
    int t = CL_NOOP;
    int ret = 0;
    char * kbuffer = NULL;
    int fd = 0;
    off_t ksize = 0;
    struct stat info;

317
    temp_group_rank = rank;
318
319
    /* get the kernel from the file */
    codes_kernel_helper_parse_cf(io_kernel_path, io_kernel_def_path,
320
            io_kernel_meta_path, rank, task_info, use_relpath);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

    /* stat the kernel file */
    ret = stat(io_kernel_path, &info);
    if(ret != 0)
    {
        fprintf(stderr, "%s:%i could not stat kernel file (%s), exiting\n",
                __func__, __LINE__, io_kernel_path);
        perror("stat() error: ");
        exit(1);
    }

    /* get the size of the file */
    ksize = info.st_size;

    /* allocate a buffer for the kernel */
    kbuffer = (char *)malloc(sizeof(char) * ksize);

    /* get data from the kernel file */
    fd = open(io_kernel_path, O_RDONLY);
    ret = pread(fd, kbuffer, ksize, 0);
    close(fd);

    /* init the scanner */
    CodesIOKernelScannerInit(c);

    if(ret <= ksize)
    {
        CodesIOKernel__scan_string(kbuffer, c->scanner_);
	
	*ps = CodesIOKernel_pstate_new();

        /* get the first instruction */
        t = codes_kernel_helper_parse_input(*ps, c, next_event);
    }
    else
    {
        fprintf(stderr, "not enough buffer space... bail\n");
        exit(1);
    }

    /* cleanup */
    free(kbuffer);

    return t;
}
Philip Carns's avatar
Philip Carns committed
366
367
368
369
370
371
372
373
374

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */