/* This, and hence fuse_reply, are called on a different thread to the one the * request came in on. This doesn't seem to matter. */ static void chunk_done (void *read_ctxt, int rc, size_t size) { read_context_t *ctxt = (read_context_t *)read_ctxt; if (size != ctxt->size) { read_trace("bytes read != request size => EOF / error\n"); } /* As a result of this read() operation we can say certain things * about the direntry. These professions can be made with confidence * because we've just performed an actual network transaction, so * our information is "live": * - If we actually read data from the file then it definitely still * exists, now. * - If we tried to read from it and found it didn't exist, then it * definitely doesn't exist any more. * - Any other error doesn't give enough info. */ if (!rc) { direntry_still_exists(ctxt->de); } else if (rc == ENOENT) { direntry_no_longer_exists(ctxt->de); } if (!rc) { assert(!fuse_reply_buf(ctxt->req, ctxt->buf, size)); } else { assert(!fuse_reply_err(ctxt->req, rc)); } direntry_delete(CALLER_INFO ctxt->de); free(ctxt->buf); free(read_ctxt); }
int GraphBuilder::get_trace(const char* pathfile, syscall_t** trace) { int fd, status, ret; pid_t child; /* open system call interceptor driver */ fd = open(SCID_DEVICE_PATH, O_RDWR); DIE(fd < 0, "scid device open"); /* create process and get pid */ child = start_child(pathfile); /* set process pid to monitor */ ret = ioctl(fd, IOCTL_SET_PID, &child); if(ret != 0) { std::cerr<<"failed to set pid"<<std::endl; return -1; } ret = sem_post(child_sem); DIE(ret < 0, "error posting semaphore"); /* wait until process child ends to get a full trace of it's * execution */ ret = waitpid(child, &status, 0); /* read the program system call trace from driver */ ret = read_trace(fd, trace); /* close device */ close(fd); return ret; }
int add_trace (Dbptr db, double tstart, double tend, Trace *trace, Trace **traceo) { Trace *tr, *trn; trn = read_trace (db, tstart, tend); if (trn == NULL) { fprintf (stderr, "add_trace: read_trace() error.\n"); return (0); } if (trn->raw_data == NULL) { SCV_free_trace (trn); *traceo = trace; return (1); } if (trace == NULL) { *traceo = trn; return (1); } for (tr=trace; tr->next!=NULL; tr=tr->next); tr->next = trn; trn->prev = tr; *traceo = trace; return (1); }
void read_traces_from_dir(char * directory) { int res; DIR *traces_dir; struct dirent *directory_entry; struct stat file_stats; char *key, *message, *ciphertext; trace_s *trace; res = chdir(directory); if (res < 0) { perror("Cannot chdir to traces directory"); abort(); } traces_dir = opendir("./"); if (traces_dir == NULL) { perror("Cannot open to traces directory"); abort(); } while ((directory_entry = readdir(traces_dir))) { stat(directory_entry->d_name, &file_stats); if (! S_ISREG(file_stats.st_mode)) continue; // Extract key, message and ciphertext from the filename key = strstr(directory_entry->d_name, "k="); message = strstr(directory_entry->d_name, "m="); ciphertext = strstr(directory_entry->d_name, "c="); if ((key == NULL) || (message == NULL) || (ciphertext == NULL)) continue; trace = (trace_s *) malloc(sizeof(trace_s)); sscanf(key + 2, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", &trace->key[0], &trace->key[1], &trace->key[2], &trace->key[3], &trace->key[4], &trace->key[5], &trace->key[6], &trace->key[7]); sscanf(message + 2, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", &trace->message[0], &trace->message[1], &trace->message[2], &trace->message[3], &trace->message[4], &trace->message[5], &trace->message[6], &trace->message[7]); sscanf(ciphertext + 2, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", &trace->ciphertext[0], &trace->ciphertext[1], &trace->ciphertext[2], &trace->ciphertext[3], &trace->ciphertext[4], &trace->ciphertext[5], &trace->ciphertext[6], &trace->ciphertext[7]); read_trace(directory_entry->d_name, trace); verify_des(trace); if (trace->num_samples > 0) add_trace_to_list(trace); else free(trace); } }
int main(int argc, char **argv) { char *sift_expr, *wfdir, *format, *dbin, *dbout, *chan_maps; double calper; int overwrite=0; int ndec_stages; char **dec_stages; Tbl *chan_in_tbl, *chan_out_tbl; Dbptr dbi, dbo; Dbvalue dbv; Response *resp; Tbl *ncoefs, *coefs, *dec_fac; int dec_factor; int nwi, n, i, j, nchans; char string[512]; char expr[1024]; Dbptr dbwfi; char dbbase[1024]; char dir[128]; char dfile[128]; char fnamei[1024]; char fnameo[1024]; char wfdir1[512]; char wfdir2[512]; double time, tref; /* Get command line args */ if (!getargs(argc, argv, &sift_expr, &calper, &wfdir, &format, &dbin, &dbout, &chan_maps, &ndec_stages, &dec_stages)) { usage(); exit (1); } if (calper >= 0.0) { fprintf (stderr, "dbdec: -c option not operational.\n"); usage(); exit (1); } /* Parse channel maps and wfdir */ if (!parse_chan_maps(chan_maps, &chan_in_tbl, &chan_out_tbl)) { fprintf (stderr, "dbdec: Unable to parse channel maps, '%s'\n", chan_maps); usage(); exit (1); } if (!parse_wfdir(wfdir, wfdir1, wfdir2)) { fprintf (stderr, "dbdec: Unable to parse wfdir, '%s'\n", wfdir); usage(); exit (1); } /* Open and read decimation stage files */ if (!read_dec_files(ndec_stages, dec_stages, &resp, &dec_factor, &ncoefs, &coefs, &dec_fac)) { fprintf (stderr, "dbdec: Unable to read decimation stage(s).\n"); usage(); exit (1); } printf ("Total decimation factor = %d\n", dec_factor); /* Open the input database */ if (dbopen (dbin, "r+", &dbi) == dbINVALID) { elog_clear_register(1); fprintf (stderr, "dbdec: Unable to open input database '%s'.\n", dbin); exit (1); } dbi = dblookup (dbi, 0, "wfdisc", 0, 0); dbquery (dbi, dbRECORD_COUNT, &nwi); if (nwi < 1) { fprintf (stderr, "dbdec: No wfdisc rows for input database '%s'.\n", dbin); exit (1); } /* Compose a subset expression for the input channel mappings */ nchans = maxtbl (chan_in_tbl); for (i=0; i<nchans; i++) { sprintf (string, "chan == \"%s\"", gettbl(chan_in_tbl, i)); if (i == 0) { strcpy (expr, "("); strcat (expr, string); } else { strcat (expr, " || "); strcat (expr, string); } } strcat (expr, ")"); if (sift_expr) { strcat (expr, " && ("); strcat (expr, sift_expr); strcat (expr, ")"); } /* Subset the input wfdisc table */ dbwfi = dblookup (dbi, 0, "wfdisc", 0, 0); dbwfi = dbsubset (dbwfi, expr, 0); dbquery (dbwfi, dbRECORD_COUNT, &n); if (n < 1) { fprintf (stderr, "dbdec: No input channels for database '%s'.\n", dbin); exit (1); } printf ("Processing %d out of %d waveform segments.\n", n, nwi); /* Open the output database */ if (dbopen (dbout, "r+", &dbo) == dbINVALID) { elog_clear_register(1); fprintf (stderr, "dbdec: Unable to open output database '%s'.\n", dbout); exit (1); } dbo = dblookup (dbo, 0, "wfdisc", 0, 0); dbquery (dbo, dbTABLE_DIRNAME, &dbv); strcpy (dbbase, dbv.t); /* Make a pass through the input wfdiscs to look for wf file conflicts */ if (!overwrite) { printf ("Looking for waveform file conflicts..."); fflush (stdout); } tref = 1.e30; for (dbwfi.record=0; dbwfi.record<n; dbwfi.record++) { if (!overwrite && (!strcmp(wfdir1,wfdir2)) ){ if (!makeoutfname (dbwfi, wfdir1, wfdir2, dbbase, dir, dfile, fnameo)) { fprintf (stderr, "dbdec: makeoutfname() error.\n"); exit (1); } if (zaccess(fnameo, F_OK) != -1) { fprintf (stderr, "\ndbdec(Warning): wf file conflict for '%s'.\n", fnameo); fprintf(stderr,"Will append but may waste space\n"); } } dbgetv (dbwfi, 0, "time", &time, 0); if (time < tref) tref = time; } if (!overwrite) { printf ("OK\n"); } tref = (double)((int)tref); /* Loop through and do the decimation */ for (dbwfi.record=0; dbwfi.record<n; dbwfi.record++) { Trace *trace=NULL; /* Read in trace and convert to float */ SCV_free_trace (trace); trace = NULL; trace = read_trace (dbwfi); if (trace == NULL) { fprintf (stderr, "dbdec: read_trace() error.\n"); continue; } /* Decimate float trace */ if (!decimate_trace (trace, ncoefs, coefs, dec_fac, tref)) { fprintf (stderr, "dbdec: decimate_trace() error.\n"); exit (1); } /* Convert trace to output units and put back in data gaps */ trace = convert_trace (trace, format); if (trace == NULL) { fprintf (stderr, "dbdec: convert_trace() error.\n"); exit (1); } /* Write out decimated trace and wfdisc */ makeoutfname (dbwfi, wfdir1, wfdir2, dbbase, dir, dfile, fnameo); if (!write_trace (dbwfi, dbo, dir, dfile, fnameo, chan_in_tbl, chan_out_tbl, trace, overwrite)) { fprintf (stderr, "dbdec: write_trace() error.\n"); exit (1); } } /* Fix up the output sensor, sitechan, etc. tables */ if (!fixup_tables (dbi, dbo, chan_in_tbl, chan_out_tbl, calper, resp, dec_factor)) { fprintf (stderr, "dbdec: fixup_tables() error.\n"); exit (1); } /* Normal exit */ exit (0); }
/************** * Main routine **************/ int main(int argc, char **argv) { int i; char c; char **tracefiles = NULL; /* null-terminated array of trace file names */ int num_tracefiles = 0; /* the number of traces in that array */ trace_t *trace = NULL; /* stores a single trace file in memory */ range_t *ranges = NULL; /* keeps track of block extents for one trace */ stats_t *libc_stats = NULL;/* libc stats for each trace */ stats_t *mm_stats = NULL; /* mm (i.e. student) stats for each trace */ speed_t speed_params; /* input parameters to the xx_speed routines */ int run_libc = 0; /* If set, run libc malloc (set by -l) */ int autograder = 0; /* If set, emit summary info for autograder (-g) */ /* temporaries used to compute the performance index */ double secs, ops, util, avg_mm_util, avg_mm_throughput, p1, p2, perfindex; int numcorrect; /* * Read and interpret the command line arguments */ while ((c = getopt(argc, argv, "f:t:hvVgal")) != EOF) { switch (c) { case 'g': /* Generate summary info for the autograder */ autograder = 1; break; case 'f': /* Use one specific trace file only (relative to curr dir) */ num_tracefiles = 1; if ((tracefiles = realloc(tracefiles, 2*sizeof(char *))) == NULL) unix_error("ERROR: realloc failed in main"); strcpy(tracedir, "./"); tracefiles[0] = strdup(optarg); tracefiles[1] = NULL; break; case 't': /* Directory where the traces are located */ if (num_tracefiles == 1) /* ignore if -f already encountered */ break; strcpy(tracedir, optarg); if (tracedir[strlen(tracedir)-1] != '/') strcat(tracedir, "/"); /* path always ends with "/" */ break; case 'l': /* Run libc malloc */ run_libc = 1; break; case 'v': /* Print per-trace performance breakdown */ verbose = 1; break; case 'V': /* Be more verbose than -v */ verbose = 2; break; case 'h': /* Print this message */ usage(); exit(0); default: usage(); exit(1); } } /* * If no -f command line arg, then use the entire set of tracefiles * defined in default_traces[] */ if (tracefiles == NULL) { tracefiles = default_tracefiles; num_tracefiles = sizeof(default_tracefiles) / sizeof(char *) - 1; printf("Using default tracefiles in %s\n", tracedir); } /* Initialize the timing package */ init_fsecs(); /* * Optionally run and evaluate the libc malloc package */ if (run_libc) { if (verbose > 1) printf("\nTesting libc malloc\n"); /* Allocate libc stats array, with one stats_t struct per tracefile */ libc_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t)); if (libc_stats == NULL) unix_error("libc_stats calloc in main failed"); /* Evaluate the libc malloc package using the K-best scheme */ for (i=0; i < num_tracefiles; i++) { trace = read_trace(tracedir, tracefiles[i]); libc_stats[i].ops = trace->num_ops; if (verbose > 1) printf("Checking libc malloc for correctness, "); libc_stats[i].valid = eval_libc_valid(trace, i); if (libc_stats[i].valid) { speed_params.trace = trace; if (verbose > 1) printf("and performance.\n"); libc_stats[i].secs = fsecs(eval_libc_speed, &speed_params); } free_trace(trace); } /* Display the libc results in a compact table */ if (verbose) { printf("\nResults for libc malloc:\n"); printresults(num_tracefiles, libc_stats); } } /* * Always run and evaluate the student's mm package */ if (verbose > 1) printf("\nTesting mm malloc\n"); /* Allocate the mm stats array, with one stats_t struct per tracefile */ mm_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t)); if (mm_stats == NULL) unix_error("mm_stats calloc in main failed"); /* Initialize the simulated memory system in memlib.c */ mem_init(); /* Evaluate student's mm malloc package using the K-best scheme */ for (i=0; i < num_tracefiles; i++) { trace = read_trace(tracedir, tracefiles[i]); strncpy(mm_stats[i].trace_name, trace->trace_name, 1024); mm_stats[i].ops = trace->num_ops; if (verbose > 1) printf("Checking mm_malloc for correctness, "); mm_stats[i].valid = eval_mm_valid(trace, i, &ranges); if (mm_stats[i].valid) { if (verbose > 1) printf("efficiency, "); mm_stats[i].util = eval_mm_util(trace, i, &ranges); speed_params.trace = trace; speed_params.ranges = ranges; if (verbose > 1) printf("and performance.\n"); mm_stats[i].secs = fsecs(eval_mm_speed, &speed_params); } free_trace(trace); } /* Display the mm results in a compact table */ if (verbose) { printf("\nResults for mm malloc:\n"); printresults(num_tracefiles, mm_stats); printf("\n"); } /* * Accumulate the aggregate statistics for the student's mm package */ secs = 0; ops = 0; util = 0; numcorrect = 0; for (i=0; i < num_tracefiles; i++) { secs += mm_stats[i].secs; ops += mm_stats[i].ops; util += mm_stats[i].util; if (mm_stats[i].valid) numcorrect++; } avg_mm_util = util/num_tracefiles; /* * Compute and print the performance index */ if (errors == 0) { avg_mm_throughput = ops/secs; p1 = UTIL_WEIGHT * avg_mm_util; if (avg_mm_throughput > AVG_LIBC_THRUPUT) { p2 = (double)(1.0 - UTIL_WEIGHT); } else { p2 = ((double) (1.0 - UTIL_WEIGHT)) * (avg_mm_throughput/AVG_LIBC_THRUPUT); } perfindex = (p1 + p2)*100.0; printf("Perf index = %.0f (util) + %.0f (thru) = %.0f/100\n", p1*100, p2*100, perfindex); } else { /* There were errors */ perfindex = 0.0; printf("Terminated with %d errors\n", errors); } if (autograder) { printf("correct:%d\n", numcorrect); printf("perfidx:%.0f\n", perfindex); } exit(0); }
void trace_stack (int argc, char **argv) { enum stack_type trace_type = STACK_REPORT; int c; if (argc < 2) usage(argv); if (strcmp(argv[1], "stack") != 0) usage(argv); for (;;) { int option_index = 0; static struct option long_options[] = { {"start", no_argument, NULL, OPT_start}, {"stop", no_argument, NULL, OPT_stop}, {"reset", no_argument, NULL, OPT_reset}, {"help", no_argument, NULL, '?'}, {NULL, 0, NULL, 0} }; c = getopt_long (argc-1, argv+1, "+h?", long_options, &option_index); if (c == -1) break; switch (c) { case 'h': usage(argv); break; case OPT_start: trace_type = STACK_START; break; case OPT_stop: trace_type = STACK_STOP; break; case OPT_reset: trace_type = STACK_RESET; break; default: usage(argv); } } test_available(); switch (trace_type) { case STACK_START: start_trace(); break; case STACK_STOP: stop_trace(); break; case STACK_RESET: reset_trace(); break; default: read_trace(); break; } return; }
int trace_main(char *hostIp, int portNum, char *imsi, int outflag, char *path) { int fd_stdin, peersock, maxfd, status, readsize; int selStatus; fd_set fdset_Read; FILE *fp = NULL; char buff[128]; struct timeval timeout; fd_stdin = fileno(stdin); peersock = connect_to_peer(hostIp, portNum); if(peersock < 0) return 1; if(outflag){ fp = open_trace_file(path, imsi); if(!fp) exit(1); } timeout.tv_sec = 0; timeout.tv_usec = 100; while(1){ FD_ZERO(&fdset_Read); FD_SET(fd_stdin, &fdset_Read); FD_SET(peersock, &fdset_Read); maxfd = (fd_stdin >= peersock)?fd_stdin:peersock; maxfd += 1; selStatus = select(maxfd, &fdset_Read, NULL, NULL, &timeout); if(selStatus < 0){ fprintf(stderr, "[ERROR] select - %s\n", strerror(errno)); }else if(selStatus > 0){ if(FD_ISSET(fd_stdin, &fdset_Read)){ memset(buff, 0x00, sizeof(buff)); read_from_fd(fd_stdin, buff, 1); if(buff[0] == 'q' || buff[0] == 'Q'){ close(peersock); fclose(fp); return 1; } } if(FD_ISSET(peersock, &fdset_Read)){ status = read_trace(peersock, fp, imsi); if(status < 0){ // try to reconnect to peer. peersock = connect_to_peer(hostIp, portNum); if(peersock < 0) return 1; } } } #ifdef DEBUG_1 else{ printf("."); } #endif } return 0; }