Skip to content

Commit

Permalink
CPU affinity feature work with trace aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhaowu committed Jul 30, 2023
1 parent 2e45028 commit f9a4a17
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
15 changes: 9 additions & 6 deletions examples/tracing_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ int main() {
cactus_rt::FifoThreadConfig fifo_config;
fifo_config.priority = 80;

cactus_rt::CyclicThreadConfig config;
config.name = "ExampleRTThread";
config.period_ns = 1'000'000;
config.scheduler_config = fifo_config;
cactus_rt::CyclicThreadConfig thread_config;
thread_config.name = "ExampleRTThread";
thread_config.period_ns = 1'000'000;
thread_config.scheduler_config = fifo_config;

auto thread = std::make_shared<ExampleRTThread>(config);
App app;
cactus_rt::AppConfig app_config;
app_config.tracer_config.trace_aggregator_cpu_affinity = {1};

auto thread = std::make_shared<ExampleRTThread>(thread_config);
App app(app_config);
app.RegisterThread(thread);

std::cout << "Testing RT loop for 15 seconds with two trace sessions.\n";
Expand Down
2 changes: 2 additions & 0 deletions include/cactus_rt/tracing/trace_aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class TraceAggregator {
void Run();
bool StopRequested() const noexcept;

void SetupCPUAffinityIfNecessary() const;

Trace CreateProcessDescriptorPacket() const;
Trace CreateThreadDescriptorPacket(const ThreadTracer& thread_tracer) const;
void AddTrackEventPacketToTrace(Trace& trace, const ThreadTracer& thread_tracer, const TrackEventInternal& track_event_internal) const;
Expand Down
31 changes: 31 additions & 0 deletions src/cactus_rt/tracing/trace_aggregator.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include "cactus_rt/tracing/trace_aggregator.h"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <pthread.h>
#include <sched.h>

#include <chrono>
#include <cstring>
#include <string>

using Trace = cactus_tracing::vendor::perfetto::protos::Trace;
using TrackDescriptor = cactus_tracing::vendor::perfetto::protos::TrackDescriptor;
Expand Down Expand Up @@ -89,6 +98,8 @@ quill::Logger* TraceAggregator::Logger() noexcept {
}

void TraceAggregator::Run() {
SetupCPUAffinityIfNecessary();

// TODO: major refactor required

while (!StopRequested()) {
Expand Down Expand Up @@ -171,6 +182,26 @@ bool TraceAggregator::StopRequested() const noexcept {
return stop_requested_.load(std::memory_order_relaxed);
}

void TraceAggregator::SetupCPUAffinityIfNecessary() const {
if (cpu_affinity_.empty()) {
return;
}

cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (auto cpu : cpu_affinity_) {
CPU_SET(cpu, &cpuset);
}

pthread_t current_thread = pthread_self();
int ret = pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
if (ret == 0) {
return;
}

throw std::runtime_error{std::string("cannot set affinity for trace aggregator: ") + std::strerror(errno)};
}

Trace TraceAggregator::CreateProcessDescriptorPacket() const {
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
Trace trace;
Expand Down

0 comments on commit f9a4a17

Please sign in to comment.