Skip to content

Commit

Permalink
Flushing trace at dump.
Browse files Browse the repository at this point in the history
  • Loading branch information
khuck committed Feb 3, 2024
1 parent 431bad0 commit 396b3fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/apex/trace_event_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ trace_event_listener::trace_event_listener (void) : _terminate(false),
}

trace_event_listener::~trace_event_listener (void) {
close_trace();
if (!_terminate) {
end_trace_time();
close_trace();
_terminate = true;
}
}

void trace_event_listener::end_trace_time(void) {
Expand Down Expand Up @@ -62,6 +66,8 @@ void trace_event_listener::on_startup(startup_event_data &data) {

void trace_event_listener::on_dump(dump_event_data &data) {
APEX_UNUSED(data);
// force a flush
flush_trace_if_necessary(true);
return;
}

Expand All @@ -73,7 +79,6 @@ void trace_event_listener::on_shutdown(shutdown_event_data &data) {
APEX_UNUSED(data);
if (!_terminate) {
end_trace_time();
flush_trace(this);
close_trace();
_terminate = true;
}
Expand Down Expand Up @@ -475,7 +480,7 @@ std::ofstream& trace_event_listener::get_trace_file() {
if (!_trace_file.is_open()) {
_trace_file.open(get_file_name());
_trace_file << fixed << "{\n";
_trace_file << "\"displayTimeUnit\": \"ms\",\n";
//_trace_file << "\"displayTimeUnit\": \"ms\",\n";
_trace_file << "\"traceEvents\": [\n";
}
return _trace_file;
Expand All @@ -496,8 +501,6 @@ void trace_event_listener::flush_trace(trace_event_listener* listener) {
// reset the buffer
listener->trace.str("");
listener->_vthread_mutex.unlock();
// flush the trace
trace_file << std::flush;
#else
listener->_vthread_mutex.lock();
size_t count = listener->streams.size();
Expand All @@ -515,22 +518,26 @@ void trace_event_listener::flush_trace(trace_event_listener* listener) {
// flush the trace
trace_file << ss.rdbuf() << std::flush;
#endif
// flush the trace
trace_file << std::flush;
flushing = false;
}

void trace_event_listener::flush_trace_if_necessary(void) {
void trace_event_listener::flush_trace_if_necessary(bool force) {
auto tmp = ++num_events;
/* flush after every 100k events */
if (tmp % 1000000 == 0) {
if (tmp % 1000000 == 0 || force) {
//flush_trace(this);
//std::async(std::launch::async, flush_trace, this);
std::cout << "Flushing APEX trace..." << std::endl;
std::thread(flush_trace, this).detach();
}
}

void trace_event_listener::close_trace(void) {
static bool closed{false};
if (closed) return;
flush_trace(this);
auto& trace_file = get_trace_file();
std::stringstream ss;
ss.precision(3);
Expand All @@ -542,7 +549,6 @@ void trace_event_listener::close_trace(void) {
ss << "]\n";
ss << "}\n" << std::endl;
write_to_trace(ss);
flush_trace(this);
//printf("Closing trace...\n"); fflush(stdout);
trace_file.close();
closed = true;
Expand Down
2 changes: 1 addition & 1 deletion src/apex/trace_event_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class trace_event_listener : public event_listener {
bool _terminate;
static void flush_trace(trace_event_listener* listener);
void close_trace(void);
void flush_trace_if_necessary(void);
void flush_trace_if_necessary(bool force = false);
void _common_start(std::shared_ptr<task_wrapper> &tt_ptr);
void _common_stop(std::shared_ptr<profiler> &p);
std::string make_tid (base_thread_node &node);
Expand Down

0 comments on commit 396b3fe

Please sign in to comment.