CodesKernelHelpers.c 9.94 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 "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
        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;
}

113 114
static void codes_kernel_helper_parse_cf(char * io_kernel_path,
        char * io_kernel_meta_path, int task_rank, int max_ranks_default,
115
        iolang_workload_info * task_info, int use_relpath)
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
{
       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);
164 165
               if(token) {
                       if (use_relpath){
166 167 168 169 170
                           /* 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);
171 172 173 174 175
                       }
                       else{
                           strcpy(io_kernel_path, token);
                       }
               }
176 177 178

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

                       foundit = 1;

                       break;
               }
       }

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

       /* if we did not find the config file, set it to the default */
197 198 199 200 201 202
       if(foundit == 0) {
           fprintf(stderr,
                   "ERROR: Unable to find iolang workload file "
                   "from given metadata file %s... exiting\n",
                   io_kernel_meta_path);
           exit(1);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
       }

       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_));
221
        yychar = CodesIOKernel_lex((codesYYType*)c->lval, (YYLTYPE*)c->locval, c->scanner_);
222 223 224 225 226 227 228
        c->locval = NULL;
      
        /* for each instructions */
        switch(yychar)
        {
            /* for each instrunction that triggers a simulator event */
            case WRITEAT:
229
            case READAT:
230 231 232 233 234 235 236 237 238 239
            case GETGROUPRANK:
            case GETGROUPSIZE:
            case CLOSE:
            case OPEN:
            case SYNC:
            case SLEEP:
            case EXIT:
            case DELETE:
            {
                c->inst_ready = 0;
240
                status = CodesIOKernel_push_parse(ps, yychar, (codesYYType*)c->lval, (YYLTYPE*)c->locval, c);
241 242 243 244 245 246
                codes_inst = convertKLInstToEvent(yychar);
		break;
            }
            /* not a simulator event */
            default:
            {
247
                status = CodesIOKernel_push_parse(ps, yychar, (codesYYType*)c->lval, (YYLTYPE*)c->locval, c);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		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:
263
	        case CL_READAT:
264 265 266 267 268 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
                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;
}

299 300 301
int codes_kernel_helper_bootstrap(char * io_kernel_path,
        char * io_kernel_meta_path, int rank, int num_ranks, int use_relpath,
        CodesIOKernelContext * c, CodesIOKernel_pstate ** ps,
302
        iolang_workload_info * task_info, codeslang_inst * next_event)
303 304 305 306 307 308 309 310
{
    int t = CL_NOOP;
    int ret = 0;
    char * kbuffer = NULL;
    int fd = 0;
    off_t ksize = 0;
    struct stat info;

311
    temp_group_rank = rank;
312
    /* get the kernel from the file */
313
    codes_kernel_helper_parse_cf(io_kernel_path,
314
            io_kernel_meta_path, rank, num_ranks, task_info, use_relpath);
315
    temp_group_size = task_info->num_lrank;
316 317 318 319 320 321 322 323 324 325 326 327 328 329
    /* 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 */
330 331
    kbuffer = (char *)malloc(sizeof(char) * (ksize+1));
    kbuffer[ksize] = '\0';
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

    /* 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
361 362 363 364 365 366 367 368 369

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