/* this function creates a random realization of the session duration of a background-session input: output: - random realization of D_s (ms) */ int backgroundSessionDurationRnd(){ int duration; duration = ceil(pow(10,exponential_dist(1/0.3591))*1000); /*ms*/ if(duration<0){ duration=0; }else if(duration>1<<30){ duration=1<<30; } return duration; }
/* Simulate power law with cut-off x^(-alpha)*exp(-lambda*x) To simulate power-law with cutoff , one can generate an exponentially distributed random number using the formula above (as k>0 and integer, so k start at 1) and then accept or reject it with probability p or 1 - p respectively (i.e accept U1 <p or reject U1>p, and U1 is a uniform [0,1] random variable), where p = (x/x_min)^(-alpha) and x_min=1. http://www.santafe.edu/~aaronc/powerlaws/ */ double rand_gen::powerlaw_dist(double alpha, double lamda) { double x; do{ x = exponential_dist(lamda); } while (pow(x,-1*alpha) < uniform_dist(0.,1.)); return (x); }
void generate_taskset(char *path, int nr_tasksets, char *dist, char *deadline, char *period, int workload, double sep, double prob, double mean, double umin, double umax, int pmin, int pmax) { int i, k; char dir[MAX_BUF]; char file[MAX_BUF]; char cmd[MAX_BUF]; char str[MAX_BUF]; FILE *fp; int nr_tasks; double u, utotal, usys = (double)workload / 100.0; taskdata_t *taskdata; /* create taskset files, only if they do not exist. */ system("mkdir ./taskset >& .error.log"); /* create taskset/@dist. */ sprintf(path, "./taskset/%s", dist); sprintf(cmd, "mkdir %s >& .error.log", path);; system(cmd); /* create taskset/@dist/@deadline. */ sprintf(path, "%s/%s", path, deadline); sprintf(cmd, "mkdir %s >& .error.log", path);; system(cmd); /* create taskset/@dist/@deadline/@period. */ sprintf(path, "%s/%s", path, period); sprintf(cmd, "mkdir %s >& .error.log", path); system(cmd); /* create taskset/@dist/@deadline/@parameters. */ if (strcmp(dist, "uniform") == 0) { sprintf(path, "%s/pmin%d_pmax%d_umin%.2f_umax%.2f", path, pmin, pmax, umin, umax); sprintf(cmd, "mkdir %s >& .error.log", path); } else if (strcmp(dist, "bimodal") == 0) { sprintf(path, "%s/pmin%d_pmax%d_umin%.2f_umax%.2f_sep%.2f_prob%.2f", path, pmin, pmax, umin, umax, sep, prob); sprintf(cmd, "mkdir %s >& .error.log", path); } else if (strcmp(deadline, "arbitrary") == 0) { sprintf(path, "%s/pmin%d_pmax%d_mean%.2f", path, pmin, pmax, mean); sprintf(cmd, "mkdir %s >& .error.log", path); } else { printf("Error: undefined deadline type!\n"); exit(1); } system(cmd); /* create taskset/@dist/@deadline/@period/workload?. */ sprintf(dir, "%s/workload%d", path, workload); sprintf(cmd, "mkdir %s >& .error.log", dir); if (system(cmd) == 0) { /* initialize random function seed. */ srand((unsigned int)time(NULL) + workload); /* if --period=harmonic is chosen, make harmonic periods. */ if (strcmp(period, "harmonic") == 0) { make_harmonic_periods(pmin, pmax); } /* generate tasksets for each workload. */ for (k = 1; k <= nr_tasksets; k++) { /* determine utilizations based on a uniform distribution. results will be written to a tempral file ".util". */ fp = fopen(".util", "w"); nr_tasks = 0; utotal = 0.0; while (1) { nr_tasks++; if (strcmp(dist, "uniform") == 0) { u = uniform_dist(umin, umax); } else if (strcmp(dist, "bimodal") == 0) { u = bimodal_dist(umin, umax, sep, prob); } else if (strcmp(dist, "exponential") == 0) { u = exponential_dist(mean); } else { printf("Error: undefined distribution!\n"); exit(1); } utotal += u; if (utotal <= usys) { fprintf(fp, "%f\n", u); } else { u -= (utotal - usys); fprintf(fp, "%f\n", u); break; } } fclose(fp); /* task data will be stored here. */ taskdata = (taskdata_t*) malloc(sizeof(taskdata_t) * nr_tasks); /* reopen the temporal file to determine C, T, and D. */ fp = fopen(".util", "r"); /* determine computation times and periods. */ for (i = 0; i < nr_tasks; i++) { fgets(str, MAX_BUF, fp); taskdata[i].U = atof(str); if (strcmp(period, "harmonic") == 0) { taskdata[i].T = harmonic_periods[rand()%nr_periods]; } else { taskdata[i].T = pmin + rand()%(pmax-pmin); } taskdata[i].C = taskdata[i].U * taskdata[i].T; } /* determine relative deadlines. */ if (strcmp(deadline, "implicit") == 0) { for (i = 0; i < nr_tasks; i++) { taskdata[i].D = taskdata[i].T; } } else if (strcmp(deadline, "constrained") == 0) { for (i = 0; i < nr_tasks; i++) { taskdata[i].D = taskdata[i].C + rand()%(taskdata[i].T - taskdata[i].C); } } else if (strcmp(deadline, "arbitrary") == 0) { for (i = 0; i < nr_tasks; i++) { taskdata[i].D = taskdata[i].C + rand()%(4*taskdata[i].T - taskdata[i].C); } } else { printf("Error: undefined deadline type!\n"); exit(1); } fclose(fp); /* generate a taskset file. */ sprintf(file, "%s/ts%d", dir, k); if ((fp = fopen(file, "w")) == NULL) { printf("Cannot open %s\n", file); return; } fprintf(fp, "# the number of tasks\n"); fprintf(fp, "%d\n", nr_tasks); fprintf(fp, "# tasks (name, C, T, D)\n"); for (i = 0; i < nr_tasks; i++) { fprintf(fp, "task%d, %d, %d, %d\n", i, taskdata[i].C, taskdata[i].T, taskdata[i].D); } fclose(fp); free(taskdata); } /* if --period=harmonic is chosen, free harmonic periods. */ if (strcmp(period, "harmonic") == 0) { free(harmonic_periods); } } }