long int without_virtual_time() { struct timeval prev; struct timeval next; struct timeval diff; struct timeval tmp; long ret; ret = gettimeofday(&prev, NULL); chk_sys_call_ret(ret, "gettimeofday"); long int i; for (i = 0; i < NR_ROUND; ++i) { ret = gettimeofday(&tmp, NULL); chk_sys_call_ret(ret, "gettimeofday"); } ret = gettimeofday(&next, NULL); chk_sys_call_ret(ret, "gettimeofday"); ret = timeval_substract(&diff, &next, &prev); printf("Elapsed %ld seconds, %ld useconds\n", diff.tv_sec, diff.tv_usec); long int usec = timeval_to_usec(diff); printf("Elapsed %ld useconds without virtual time\n", usec); return usec; }
/** * more straightforward way is: * char cmd[100]; * sprintf(cmd, "echo %d > /proc/%d/dilation", tdf, pid); * system(cmd); */ int set_new_dilation(pid_t pid, float tdf) { char tdf_str[TDF_STR_LEN]; size_t count; ssize_t written_count = 0; #ifdef SHOW_OVHD struct timeval prev, next, ovhd; long int usec; gettimeofday(&prev, NULL); #endif if (tdf >= TDF_MIN && tdf < TDF_MAX){ /* echo 1000*TDF to kernel */ count = sprintf(tdf_str, "%d", (int)(tdf * 1000)); /* count = strlen(tdf_str) */ written_count = write_proc_field(pid, "dilation", tdf_str); //write(proc_file, tdf_str, count); } #ifdef SHOW_OVHD gettimeofday(&next, NULL); timeval_substract(&ovhd, &next, &prev); usec = timeval_to_usec(ovhd); printf("[set dilation ovhd = %ld]\n", usec); #endif return written_count; }
long int with_virtual_time() { struct timeval prev; struct timeval next; struct timeval diff; struct timeval tmp; long ret; int status; int pid = fork(); ret = gettimeofday(&prev, NULL); chk_sys_call_ret(ret, "gettimeofday"); if (pid == -1) { printf("\n[error] clone_time fails with error: %s\n", strerror(errno)); } else if (pid == 0) { ret = virtualtimeunshare(CLONE_NEWNET|CLONE_NEWNS, 4); chk_sys_call_ret(ret, "virtualtimeunshare"); long int i; for (i = 0; i < NR_ROUND; ++i) { ret = gettimeofday(&tmp, NULL); chk_sys_call_ret(ret, "gettimeofday"); } exit(EXIT_SUCCESS); } else { pid = wait(&status); // if (status == -1) // { // perror("wait error"); // return -1; // } // if (WIFEXITED(status) != 0) { // printf("Child process ended normally; status = %d\n", WEXITSTATUS(status)); // } ret = gettimeofday(&next, NULL); chk_sys_call_ret(ret, "gettimeofday"); ret = timeval_substract(&diff, &next, &prev); printf("Elapsed %ld seconds, %ld useconds\n", diff.tv_sec, diff.tv_usec); long int usec = timeval_to_usec(diff); printf("Elapsed %ld useconds with virtual time\n", usec); return usec; } }
void kickoff_pthreads_freeze(pid_t* pid_list, size_t size, void *(* func)(void *), char *action) { pthread_t* threads; pthread_attr_t attr; int i, rc; void *status; #ifdef SHOW_OVHD struct timeval start, end, ovhd; long usec; gettimeofday(&start, NULL); #endif threads = malloc(sizeof(pthread_t) * size); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for (i = 0; i < size; ++i) { if (pthread_create(&threads[i], NULL, func, (void *)(&pid_list[i])) != 0) { fprintf(stderr, "create pthreads failed\n"); } } pthread_attr_destroy(&attr); for (i = 0; i < size; ++i) { rc = pthread_join(threads[i], &status); if (rc) { fprintf(stderr, "join pthread fail with %d\n", rc); exit(-1); } /*printf("%s thread[%d] joined for pid[%d]\n", action, i, *((pid_t*)status));*/ } #ifdef SHOW_OVHD gettimeofday(&end, NULL); timeval_substract(&ovhd, &end, &start); usec = timeval_to_usec(ovhd); if (strcmp(action, "freeze") == 0) { printf("freeze %ld\n", usec); } else if (strcmp(action, "unfreeze") == 0) { printf("unfreeze %ld\n", usec); } else { printf("unknown action %s\n", action); } #endif /* should issue exit on main thread if we do join() */ /*pthread_exit(NULL);*/ }
int unfreeze_proc(pid_t pid) { char *val = "0"; #ifdef SHOW_OVHD struct timeval prev, next, ovhd; long int usec; int result; gettimeofday(&prev, NULL); result = write_proc_field(pid, "freeze", val); //write_proc_freeze(pid, val); gettimeofday(&next, NULL); timeval_substract(&ovhd, &next, &prev); usec = timeval_to_usec(ovhd); printf("[unfreeze proc %d] takes %ld]\n", pid, usec); return result; #else return write_proc_field(pid, "freeze", val); //write_proc_freeze(pid, val); #endif }
static uint64_t elapsed_usec(const struct timeval *start) { struct timeval now; gettimeofday(&now, NULL); return timeval_to_usec(&now) - timeval_to_usec(start); }