void luasandbox_timer_start(luasandbox_timer_set * lts) { if (lts->is_running) { // Already running return; } lts->is_running = 1; // Initialise usage timer clock_gettime(LUASANDBOX_CLOCK_ID, <s->usage_start); // Create normal timer if requested if (!luasandbox_timer_is_zero(<s->normal_remaining)) { lts->normal_running = 1; luasandbox_timer_create_one(<s->normal_timer, lts->sandbox, LUASANDBOX_TIMER_NORMAL); luasandbox_timer_set_one_time(<s->normal_timer, <s->normal_remaining); } else { lts->normal_running = 0; } // Create emergency timer if requested if (!luasandbox_timer_is_zero(<s->emergency_remaining)) { lts->emergency_running = 1; luasandbox_timer_create_one(<s->emergency_timer, lts->sandbox, LUASANDBOX_TIMER_EMERGENCY); luasandbox_timer_set_one_time(<s->emergency_timer, <s->emergency_remaining); } else { lts->emergency_running = 0; } }
int luasandbox_timer_start(luasandbox_timer_set * lts) { if (lts->is_running) { // Already running return 1; } lts->is_running = 1; // Initialise usage timer clock_gettime(LUASANDBOX_CLOCK_ID, <s->usage_start); // Create limiter timer if requested if (!luasandbox_timer_is_zero(<s->limiter_remaining)) { luasandbox_timer * timer = luasandbox_timer_create_one( lts->sandbox, LUASANDBOX_TIMER_LIMITER); if (!timer) { lts->limiter_running = 0; return 0; } lts->limiter_timer = timer; lts->limiter_running = 1; luasandbox_timer_set_one_time(timer, <s->limiter_remaining); } else { lts->limiter_running = 0; } return 1; }
int luasandbox_timer_enable_profiler(luasandbox_timer_set * lts, struct timespec * period) { if (lts->profiler_running) { luasandbox_timer_stop_one(lts->profiler_timer, NULL); lts->profiler_running = 0; } lts->profiler_period = *period; if (lts->function_counts) { zend_hash_destroy(lts->function_counts); FREE_HASHTABLE(lts->function_counts); lts->function_counts = NULL; } lts->total_count = 0; lts->overrun_count = 0; if (period->tv_sec || period->tv_nsec) { ALLOC_HASHTABLE(lts->function_counts); zend_hash_init(lts->function_counts, 0, NULL, NULL, 0); luasandbox_timer * timer = luasandbox_timer_create_one( lts->sandbox, LUASANDBOX_TIMER_PROFILER); if (!timer) { return 0; } lts->profiler_running = 1; lts->profiler_timer = timer; luasandbox_timer_set_periodic(timer, <s->profiler_period); } return 1; }
int luasandbox_timer_enable_profiler(luasandbox_timer_set * lts, struct timespec * period) { if (lts->profiler_running) { luasandbox_timer_stop_one(lts->profiler_timer, NULL); lts->profiler_running = 0; } lts->profiler_period = *period; if (lts->function_counts) { zend_hash_destroy(lts->function_counts); lts->function_counts = NULL; } lts->total_count = 0; lts->overrun_count = 0; if (period->tv_sec || period->tv_nsec) { int start, cur; luasandbox_timer *profiler_timers; TSRMLS_FETCH(); start = cur = LUASANDBOX_G(profiler_timer_idx) % MAX_PROFILING_CLOCKS; profiler_timers = LUASANDBOX_G(profiler_timers); while (1) { if (!profiler_timers[cur].cbdata.sandbox) { break; } cur = (cur + 1) % MAX_PROFILING_CLOCKS; if (cur == start) { return 0; } } LUASANDBOX_G(profiler_timer_idx) = cur; lts->function_counts = emalloc(sizeof(HashTable)); zend_hash_init(lts->function_counts, 0, NULL, NULL, 0); lts->profiler_running = 1; lts->profiler_timer = &profiler_timers[cur]; luasandbox_timer_create_one(lts->profiler_timer, lts->sandbox, LUASANDBOX_TIMER_PROFILER); luasandbox_timer_set_periodic(lts->profiler_timer, <s->profiler_period); } return 1; }