struct busy_loop_data * busy_init(double running_time) { long long loop = 500; double time; struct busy_loop_data * busy = busy_loop_create(1000); if (busy == NULL) return NULL; /* stick to a cpu */ os_affinity_set(0); time = os_time(); /* run for 1s to be sure to be at max frequency */ while (os_time() < time + 2) assert(busy_loop_run(busy)); do { busy_loop_destroy(busy); loop = loop * 2; busy = busy_loop_create(loop); if (busy == NULL) { os_affinity_set(-1); return NULL; } time = os_time(); assert(busy_loop_run(busy)); } while (os_time() - time < running_time); /* no more stick to any CPU */ os_affinity_set(-1); return busy; }
/* * Launch all process */ bool main_launch(struct busy_data * busy) { double start_user; double start_system; busy->pids_nb = 0; busy->time = os_time() + 1; /* because everyone will do a os_sleep(1) at the start */ busy->time_user = 0; if (!os_usage_get(&start_system, &start_user)) { printf("# Can't get ressource usage\n"); return false; } busy->time_user = start_system + start_user;; for (busy->pids_nb = 0; busy->pids_nb < busy->tasks_max; busy->pids_nb++) { busy->nice = atol(busy->tasks_nice[busy->pids_nb]); busy->pids[busy->pids_nb] = os_child_create(main_child, busy); if (busy->pids[busy->pids_nb] < 0) { printf("Can't create more than %d process\n", busy->pids_nb); return false; } } return true; }
/* * Wait end of all process */ bool main_wait(struct busy_data * busy) { int j; int tasks = busy->pids_nb; double stop_user; double stop_system; bool success_all = true; while (tasks > 0) { os_sleep(1); /* Waiting from sigalarm + sigchld may be better */ for (j = 0;j < busy->pids_nb; j++) { bool success; bool exited; int cpu; if (busy->pids[j] <= 0) continue; os_child_check(busy->pids[j], &exited, &success, &cpu); if (!exited) continue; if (!success) { printf("Task %d nice %s unsuccesfull\n", busy->pids[j], busy->tasks_nice[j]); success_all = false; } tasks--; busy->pids[j] = -1; busy->times[j] = os_time() - busy->time; } if (os_usage_get(&stop_system, &stop_user)) printf("%lf %lf %d\n", os_time(), stop_system + stop_user - busy->time_user, tasks); else exit(1); /* worked in main_start(0 and not here ???? */ } busy->time_user = stop_system + stop_user - busy->time_user; busy->time = os_time() - busy->time; return success_all; }
void taskctrl_update() { PTASKINFO header = tsk_getlist(); if (!header) return; PTASKINFO tmp = header; while(tmp) { if (tmp->status == TASK_STAT_RUNNING && !os_isrunning(tmp->pid)) { tmp->status = TASK_STAT_END; arraycopy(tmp->time, 0, os_time(), 0, PKG_ALL_TIME_LEN); } tmp = tmp->next; } }
/* * Log a string with a given length */ void vm_log(VMG_ const char *str, size_t len) { /* open the system log file */ osfildef *fp = osfoprwt(G_syslogfile, OSFTTEXT); if (fp != 0) { /* wrap it in a data source */ CVmFileSource ds(fp); /* get a printable timestamp */ os_time_t timer = os_time(0); struct tm *tblk = os_localtime(&timer); char *tmsg = asctime(tblk); /* remove the trailing '\n' from the asctime message */ size_t tmsgl = strlen(tmsg); if (tmsgl > 0 && tmsg[tmsgl-1] == '\n') tmsg[--tmsgl] = '\0'; /* build the full message: [<timestamp>] <message> <newline> */ char *msg = t3sprintf_alloc("[%s] %.*s\n", tmsg, (int)len, str); size_t msglen = strlen(msg); /* seek to the end of the file */ ds.seek(0, OSFSK_END); /* if we can convert to a local character set, do so */ if (G_cmap_to_log != 0) { /* write the message in the local character set */ G_cmap_to_file->write_file(&ds, msg, msglen); } else { /* write the message with no character set conversion */ (void)osfwb(fp, (unsigned char*)msg, msglen); } /* done with the formatted text string */ t3free(msg); } }
char *TadsServerManager::gen_rand_id(void *obj) { /* set up a hashing buffer */ sha256_ctx s; sha256_begin(&s); /* add the current date/time to the hash */ os_time_t timer = os_time(0); struct tm *tblock = os_localtime(&timer); sha256_hash((unsigned char *)tblock, sizeof(*tblock), &s); /* add the system timer to the hash */ long systime = os_get_sys_clock_ms(); sha256_hash((unsigned char *)&systime, sizeof(systime), &s); /* add the object address to the hash */ sha256_hash((unsigned char *)obj, sizeof(obj), &s); /* add the current stack location to the hash */ sha256_hash((unsigned char *)&obj, sizeof(void *), &s); /* add some random bytes from the operating system */ unsigned char rbuf[128]; os_gen_rand_bytes(rbuf, sizeof(rbuf)); sha256_hash(rbuf, sizeof(rbuf), &s); /* compute the hash */ unsigned char hval[32]; sha256_end(hval, &s); /* convert it to hex, but just keep the low nybbles, for 32 digits */ char *ret = lib_alloc_str(32); int i; for (i = 0 ; i < 32 ; ++i) ret[i] = nybble2hex(hval[i]); /* null-terminate the string */ ret[i] = '\0'; /* return the allocated string */ return ret; }