From 80464525c760d76e970ea1da5a304605b3d1afc2 Mon Sep 17 00:00:00 2001 From: Ammar ELWazir Date: Tue, 10 May 2022 22:53:41 -0500 Subject: [PATCH] Flush function fix Using std::thread instead of pthreads and also atomic_bool to identify the end of the flush function so that the unload_tool can wait for it Change-Id: Iea00d7e16c65d51db2d222e8b42f03f9caeb2067 --- test/tool/tracer_tool.cpp | 48 ++++++++++++--------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/test/tool/tracer_tool.cpp b/test/tool/tracer_tool.cpp index 51c7ca1d0c..b546791948 100644 --- a/test/tool/tracer_tool.cpp +++ b/test/tool/tracer_tool.cpp @@ -21,6 +21,9 @@ #include #include #include +#include +#include +#include #include /* names denangle */ #include @@ -32,6 +35,7 @@ #include #include /* usleep */ + #include #include "src/util/exception.h" #include @@ -218,26 +222,16 @@ void* control_thr_fun(void*) { // Flushing control thread uint32_t control_flush_us = 0; -pthread_t flush_thread; -bool flush_thread_started = false; -std::mutex flush_thread_mutex; -; +std::thread* flush_thread = nullptr; +std::atomic_bool stop_flush_thread = false; -void* flush_thr_fun(void*) { - const uint32_t dist_sec = control_flush_us / 1000000; - const uint32_t dist_us = control_flush_us % 1000000; - while (1) { - sleep(dist_sec); - usleep(dist_us); - std::lock_guard lock(flush_thread_mutex); - if (!flush_thread_started) - while (1) sleep(1); +void flush_thr_fun() { + while (!stop_flush_thread) { ROCTRACER_CALL(roctracer_flush_activity()); roctracer::TraceBufferBase::FlushAll(); + std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::microseconds(control_flush_us)); } - - return NULL; } /////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -702,14 +696,11 @@ void tool_unload() { if (is_loaded == false) return; is_loaded = false; - if (flush_thread_started) { - flush_thread_mutex.lock(); - flush_thread_started = false; - flush_thread_mutex.unlock(); - PTHREAD_CALL(pthread_cancel(flush_thread)); - void* res; - PTHREAD_CALL(pthread_join(flush_thread, &res)); - if (res != PTHREAD_CANCELED) fatal("flush thread wasn't stopped correctly"); + if (flush_thread) { + stop_flush_thread = true; + flush_thread->join(); + delete flush_thread; + flush_thread = nullptr; } if (trace_roctx) { @@ -898,16 +889,7 @@ void tool_load() { fprintf(stdout, "ROCTracer: trace control flush rate(%uus)\n", control_flush_us); fflush(stdout); - pthread_attr_t attr; - int err = pthread_attr_init(&attr); - if (err) { - errno = err; - perror("pthread_attr_init"); - abort(); - } - std::lock_guard lock(flush_thread_mutex); - PTHREAD_CALL(pthread_create(&flush_thread, &attr, flush_thr_fun, NULL)); - flush_thread_started = true; + flush_thread = new std::thread(flush_thr_fun); } ONLOAD_TRACE_END();