Skip to content

Commit

Permalink
Minor optimization to not get time 4 times in 1 loop
Browse files Browse the repository at this point in the history
Also fixed tests under 20.04
  • Loading branch information
shuhaowu committed Sep 7, 2023
1 parent 17831be commit 6b9308d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ build-test-debug:
cmake --build build/test -j $$(nproc)

test-debug: build-test-debug
ctest --test-dir build/test -V
cd build/test && ctest -V

test: test-debug

Expand Down
3 changes: 2 additions & 1 deletion docker/scripts/02-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

set -xe

ctest -V --test-dir ${CACTUS_RT_BUILD_DIR}
cd ${CACTUS_RT_BUILD_DIR}
ctest -V
6 changes: 3 additions & 3 deletions include/cactus_rt/tracing/thread_tracer.disabled.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class ThreadTracer {
ThreadTracer(ThreadTracer&&) = delete;
ThreadTracer& operator=(ThreadTracer&&) = delete;

bool StartSpan(const char* /* name */, const char* /* category */ = nullptr) noexcept {
bool StartSpan(const char* /* name */, const char* /* category */ = nullptr, int64_t /* now */ = 0) noexcept {
return false;
}

bool EndSpan() noexcept {
bool EndSpan(int64_t /* now */ = 0) noexcept {
return false;
}

Expand All @@ -61,7 +61,7 @@ class ThreadTracer {
return span;
}

bool InstantEvent(const char* /* name */, const char* /* category */ = nullptr) noexcept {
bool InstantEvent(const char* /* name */, const char* /* category */ = nullptr, int64_t /* now */ = 0) noexcept {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions include/cactus_rt/tracing/thread_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class ThreadTracer {
ThreadTracer(ThreadTracer&&) = delete;
ThreadTracer& operator=(ThreadTracer&&) = delete;

bool StartSpan(const char* name, const char* category = nullptr) noexcept;
bool EndSpan() noexcept;
bool StartSpan(const char* name, const char* category = nullptr, int64_t now = 0) noexcept;
bool EndSpan(int64_t now = 0) noexcept;
TraceSpan WithSpan(const char* name, const char* category = nullptr, bool enabled = true) noexcept;
bool InstantEvent(const char* name, const char* category = nullptr) noexcept;
bool InstantEvent(const char* name, const char* category = nullptr, int64_t now = 0) noexcept;

inline EventCountData EventCount() const noexcept { return event_count_.Read(); }
inline size_t QueueCapacity() const noexcept { return queue_capacity_; }
Expand Down
12 changes: 9 additions & 3 deletions src/cactus_rt/cyclic_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ void CyclicThread::Run() noexcept {
wakeup_latency = loop_start - should_have_woken_up_at;

bool should_stop;
{
auto span = Tracer().WithSpan("CyclicThread::Loop", "cactusrt", tracer_config.trace_loop);
should_stop = Loop(loop_start - Thread::StartMonotonicTimeNs());
if (tracer_config.trace_loop) {
Tracer().StartSpan("CyclicThread::Loop", "cactusrt", loop_start);
}

should_stop = Loop(loop_start - Thread::StartMonotonicTimeNs());

loop_end = NowNs();

if (tracer_config.trace_loop) {
Tracer().EndSpan(loop_end);
}

loop_latency = loop_end - loop_start;
TrackLatency(wakeup_latency, loop_latency);

Expand Down
19 changes: 13 additions & 6 deletions src/cactus_rt/tracing/thread_tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,34 @@ ThreadTracer::ThreadTracer(const char* name, uint32_t queue_capacity)
queue_capacity_ = queue_.max_capacity();
}

bool ThreadTracer::StartSpan(const char* name, const char* category) noexcept {
bool ThreadTracer::StartSpan(const char* name, const char* category, int64_t now) noexcept {
if (IsTracingEnabled()) {
auto now = NowNs();
if (now == 0) {
now = NowNs();
}
return Emit(now, TrackEvent_Type_TYPE_SLICE_BEGIN, name, category);
}

return false;
}

bool ThreadTracer::EndSpan() noexcept {
bool ThreadTracer::EndSpan(int64_t now) noexcept {
if (IsTracingEnabled()) {
auto now = NowNs();
if (now == 0) {
now = NowNs();
}
return Emit(now, TrackEvent_Type_TYPE_SLICE_END);
}

return false;
}

bool ThreadTracer::InstantEvent(const char* name, const char* category) noexcept {
bool ThreadTracer::InstantEvent(const char* name, const char* category, int64_t now) noexcept {
if (IsTracingEnabled()) {
return Emit(NowNs(), TrackEvent_Type_TYPE_INSTANT, name, category);
if (now == 0) {
now = NowNs();
}
return Emit(now, TrackEvent_Type_TYPE_INSTANT, name, category);
}

return false;
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
find_package(GTest REQUIRED)

enable_testing()

add_executable(cactus_rt_tests
utils_test.cc
)
Expand Down

0 comments on commit 6b9308d

Please sign in to comment.