void luasandbox_timer_stop(luasandbox_timer_set * lts) { struct timespec usage, delta; if (lts->is_running) { lts->is_running = 0; } else { return; } // Make sure timers aren't paused, and extract the delta luasandbox_timer_unpause(lts); delta = lts->pause_delta; luasandbox_timer_zero(<s->pause_delta); // Stop the interval timers and save the time remaining if (lts->emergency_running) { luasandbox_timer_stop_one(<s->emergency_timer, <s->emergency_remaining); lts->emergency_running = 0; } if (lts->normal_running) { luasandbox_timer_stop_one(<s->normal_timer, <s->normal_remaining); lts->normal_running = 0; luasandbox_timer_add(<s->normal_remaining, &delta); } // Update the usage luasandbox_update_usage(lts); clock_gettime(LUASANDBOX_CLOCK_ID, &usage); luasandbox_timer_subtract(&usage, <s->usage_start); luasandbox_timer_add(<s->usage, &usage); luasandbox_timer_subtract(<s->usage, &delta); }
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; }
void luasandbox_timer_destroy(luasandbox_timer_set * lts) { luasandbox_timer_stop(lts); if (lts->profiler_running) { luasandbox_timer_stop_one(lts->profiler_timer, NULL); lts->profiler_running = 0; } if (lts->function_counts) { zend_hash_destroy(lts->function_counts); lts->function_counts = NULL; } }
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; }