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, &lts->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, &lts->profiler_period);
	}
	return 1;
}