int main (int argc, char ** argv) { int ret, status, i = 0; pid_t rt_child; timer_t tid; struct sigaction sa; struct sigevent sigev; struct itimerspec new_tmr; struct sched_param s_param; cpu_set_t mask; scheduler_check_arguments(argc, argv); //--------------------------------PAPI initilization------------------------------// init_papi(); scheduler_init_papi(); set_option(); scheduler_set_option(); add_events(); scheduler_add_event(); //---------------------------Timer initialization and sigaction----------------------// sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = timer_handler; sigemptyset(&sa.sa_mask); if(sigaction(SIGRTMIN, &sa, NULL) == -1){ perror("Sigaction"); exit(2); } if(sigaction(SIGCONT, &sa, NULL) == -1){ perror("Sigaction"); exit(2); } sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGRTMIN; sigev.sigev_value.sival_ptr = &tid; if(timer_create(CLOCK_REALTIME, &sigev, &tid) != -1){ new_tmr.it_value.tv_sec = 0; new_tmr.it_value.tv_nsec = 25000000; new_tmr.it_interval.tv_sec = 0; new_tmr.it_interval.tv_nsec = 25000000; } /**********************************************************************************************/ //Launch the attackers printf("Launching %d attackers\n", atoi(argv[2])); for(i=0; i < atoi(argv[2]); i++){ if((pid_attacker[nb_attackers++] = fork()) == 0){ CPU_ZERO(&mask); CPU_SET(i+2, &mask); printf("Core %d\n", i+2); if(sched_setaffinity(getpid(), sizeof(mask), &mask)){ fprintf(stderr, "Sched error: set affinity\n"); exit(16); } char c[2]; sprintf(c, "%d", i+2); if(execl("/usr/bin/xterm", "xterm", "-hold", "-e", "./bin/attack_task", "1", c, NULL) == -1){ exit(17); } exit(0); } } sleep(10); /**********************************************************************************************/ /**********************************************************************************************/ //Child executes the RT task in one core if((rt_child = fork())==0){ int stdin_fd = -1; //Setting the affinity of the child //Using only one CPU with max priority CPU_ZERO(&mask); CPU_SET(1, &mask); stdin_fd = open("/dev/null", O_RDONLY); if(stdin_fd == -1) exit(127); dup2(stdin_fd, 1); dup2(stdin_fd, 2); close(stdin_fd); s_param.sched_priority = sched_get_priority_max(SCHED_FIFO); if(sched_setaffinity(getpid(), sizeof(mask), &mask)){ fprintf(stderr, "Sched error: set affinity\n"); exit(16); } if(sched_setscheduler(getpid(), SCHED_FIFO, &s_param)){ fprintf(stderr, "Sched error: sched_setscheduler\n"); exit(17); } if(execl(argv[1], "RT task", NB_RT_ITERATION, NULL) == -1){ fprintf(stderr, "Execl error: Couldn't launch RT exec\n"); exit(17); } exit(0); }else if(rt_child == -1){ fprintf(stderr, "Fork: couldn't create the RT child.\n"); exit(16); /**********************************************************************************************/ //The scheduler }else{ sleep(1); //<= Waiting for stationnary period if(timer_settime(tid, 0, &new_tmr, NULL) == -1){ perror("Timer_settime"); exit(3); } // Launch the counters and the timer if((ret = PAPI_start(PAPI_EventSet)) != PAPI_OK){ fprintf(stderr, "PAPI error: failed to start counters: %s\n", PAPI_strerror(ret)); exit(16); } if((ret = PAPI_start(scheduler_eventset)) != PAPI_OK){ fprintf(stderr, "PAPI error: failed to start counters: %s\n", PAPI_strerror(ret)); exit(16); } struct timeval tv1, tv2; gettimeofday(&tv1, NULL); // Wait for RT child to finish, then kill attackers ret = -1; while(ret != rt_child){ ret = waitpid(rt_child, &status, 0); } if (!WIFEXITED(status)) { fprintf(stderr, "scheduler: Child exited with wrong status\n"); exit(16); } for(i=0; i<nb_attackers; i++){ fprintf(stderr, "\nScheduler (%d) > Sending signal to %d\n", getpid(), pid_attacker[i]); kill(pid_attacker[i], SIGKILL); } gettimeofday(&tv2, NULL); char time_to_char[20]; printf ("\nTotal time = %f seconds\n", (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 + (double) (tv2.tv_sec - tv1.tv_sec)); sprintf(time_to_char, "%f\n", (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 + (double) (tv2.tv_sec - tv1.tv_sec)); // Stopping and reading for Gnuplot if((ret = PAPI_stop(PAPI_EventSet, papi_values))!= PAPI_OK){ fprintf(stderr, "PAPI error: Couldn't stop the counters %s\n", PAPI_strerror(ret)); exit(17); } if((ret = PAPI_stop(scheduler_eventset, &scheduler_value))!= PAPI_OK){ fprintf(stderr, "PAPI error: Couldn't stop the counters %s\n", PAPI_strerror(ret)); exit(17); } if((ret = PAPI_read(PAPI_EventSet, papi_values)) != PAPI_OK){ fprintf(stderr, "PAPI error : Couldn't read the values %s\n", PAPI_strerror(ret)); exit(18); } // Printing and writting for Gnuplot print_counters(papi_values); write_miss_values(1, nb_attackers, papi_values); // Writting the time for Gnuplot int fic_time; char fic_name[50]; switch(atoi(argv[2])){ case 0: strcpy(fic_name, "plot/mesures_execution_0_scheduler.data"); break; case 1: strcpy(fic_name, "plot/mesures_execution_1_scheduler.data"); break; case 2: strcpy(fic_name, "plot/mesures_execution_2_scheduler.data"); break; default: strcpy(fic_name, "plot/mesures_execution.data"); break; } if ((fic_time = open(fic_name, O_RDWR | O_APPEND))==-1){ perror("Open error on fic_time\n"); exit(19); } if (write(fic_time, time_to_char, strlen(time_to_char))==0){ fprintf(stderr, "Write exec_time error\n"); exit(20); } // Cleaning event sets if((ret=PAPI_cleanup_eventset(PAPI_EventSet))!=PAPI_OK){ fprintf(stderr, "PAPI error: Couldn't clean the Event Set %s\n", PAPI_strerror(ret)); exit(19); } if((ret=PAPI_cleanup_eventset(scheduler_eventset))!=PAPI_OK){ fprintf(stderr, "PAPI error: Couldn't clean the Event Set %s\n", PAPI_strerror(ret)); exit(19); } if((ret=PAPI_destroy_eventset(&PAPI_EventSet))!=PAPI_OK){ fprintf(stderr, "PAPI error: Couldn't destroy the Event Set %s\n", PAPI_strerror(ret)); exit(20); } if((ret=PAPI_destroy_eventset(&scheduler_eventset))!=PAPI_OK){ fprintf(stderr, "PAPI error: Couldn't destroy the Event Set %s\n", PAPI_strerror(ret)); exit(20); } } return EXIT_SUCCESS; }
int case1(void) { int retval, i, EventSet = PAPI_NULL, j = 0, k = 0, allvalid = 1; int max_mux, nev, *events; long long *values; PAPI_event_info_t pset; char evname[PAPI_MAX_STR_LEN]; init_papi(); init_multiplex(); retval = PAPI_create_eventset(&EventSet); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval); /* In Component PAPI, EventSets must be assigned a component index before you can fiddle with their internals. 0 is always the cpu component */ retval = PAPI_assign_eventset_component(EventSet, 0); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component", retval); retval = PAPI_set_multiplex(EventSet); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_set_multiplex", retval); max_mux = PAPI_get_opt(PAPI_MAX_MPX_CTRS, NULL); if (max_mux > 32) max_mux = 32; /* Fill up the event set with as many non-derived events as we can */ printf("\nFilling the event set with as many non-derived events as we can...\n"); i = PAPI_PRESET_MASK; do { if (PAPI_get_event_info(i, &pset) == PAPI_OK) { if (pset.count && (strcmp(pset.derived,"NOT_DERIVED") == 0)) { retval = PAPI_add_event(EventSet, pset.event_code); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_add_event", retval); else { printf("Added %s\n", pset.symbol); j++; } } } } while ((PAPI_enum_event(&i, PAPI_PRESET_ENUM_AVAIL) == PAPI_OK) && (j < max_mux)); events = (int *) malloc(j * sizeof(int)); if (events == NULL) test_fail(__FILE__, __LINE__, "malloc events", 0); values = (long long *) malloc(j * sizeof(long long)); if (values == NULL) test_fail(__FILE__, __LINE__, "malloc values", 0); do_stuff(); if (PAPI_start(EventSet) != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_start", retval); do_stuff(); retval = PAPI_stop(EventSet, values); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_stop", retval); nev =j; retval = PAPI_list_events(EventSet, events, &nev); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_list_events", retval); printf("\nEvent Counts:\n"); for (i = 0, allvalid = 0; i < j; i++) { PAPI_event_code_to_name(events[i], evname); printf(TAB1, evname, values[i]); if (values[i] == 0) allvalid++; } printf("\n"); if (allvalid){ printf("Caution: %d counters had zero values\n", allvalid); } for (i = 0, allvalid = 0; i < j; i++) { for (k = i+1; k < j; k++) { if ((i != k) && (values[i] == values[k])) { allvalid++; break; } } } if (allvalid){ printf("Caution: %d counter pair(s) had identical values\n", allvalid); } free(events); free(values); retval = PAPI_cleanup_eventset(EventSet); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset", retval); retval = PAPI_destroy_eventset(&EventSet); if (retval != PAPI_OK) test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset", retval); return (SUCCESS); }