void test_perf() { ::taskstats stat; int tid = TaskStatsInfoGetter::getCurrentTID(); TaskStatsInfoGetter get_info; rusage rusage; constexpr size_t num_samples = 1000000; { Stopwatch watch; for (size_t i = 0; i < num_samples; ++i) getrusage(RUSAGE_THREAD, &rusage); auto ms = watch.elapsedMilliseconds(); if (ms > 0) std::cerr << "RUsage: " << double(ms) / num_samples << " ms per call, " << 1000 * num_samples / ms << " calls per second\n"; } { Stopwatch watch; for (size_t i = 0; i < num_samples; ++i) get_info.getStat(stat, tid); auto ms = watch.elapsedMilliseconds(); if (ms > 0) std::cerr << "Netlink: " << double(ms) / num_samples << " ms per call, " << 1000 * num_samples / ms << " calls per second\n"; } std::cerr << stat << "\n"; }
void BackgroundSchedulePool::TaskInfo::execute() { Stopwatch watch; CurrentMetrics::Increment metric_increment{CurrentMetrics::BackgroundSchedulePoolTask}; std::lock_guard lock_exec(exec_mutex); { std::lock_guard lock_schedule(schedule_mutex); if (deactivated) return; scheduled = false; executing = true; } function(); UInt64 milliseconds = watch.elapsedMilliseconds(); /// If the task is executed longer than specified time, it will be logged. static const int32_t slow_execution_threshold_ms = 200; if (milliseconds >= slow_execution_threshold_ms) LOG_TRACE(&Logger::get(log_name), "Execution took " << milliseconds << " ms."); { std::lock_guard lock_schedule(schedule_mutex); executing = false; /// In case was scheduled while executing (including a scheduleAfter which expired) we schedule the task /// on the queue. We don't call the function again here because this way all tasks /// will have their chance to execute if (scheduled) pool.queue.enqueueNotification(new TaskNotification(shared_from_this())); } }