Skip to content

Commit

Permalink
improved buffering. (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
hariharan-devarajan authored Aug 31, 2024
1 parent 24b912a commit c374308
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
27 changes: 21 additions & 6 deletions src/dftracer/df_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ class DFTLogger {

inline void enter_event() {
index++;
level++;
index_stack.push_back(index.load());
if (include_metadata) {
level++;
index_stack.push_back(index.load());
}
}

inline void exit_event() {
level--;
index_stack.pop_back();
if (include_metadata) {
level--;
index_stack.pop_back();
}
}

inline TimeResolution get_time() {
Expand All @@ -121,6 +125,10 @@ class DFTLogger {
if (dftracer_tid) {
tid = df_gettid() + this->process_id;
}
int local_index;
if (!include_metadata) {
local_index = index.load();
}
if (metadata != nullptr) {
metadata->insert_or_assign("level", level);
int parent_index_value = -1;
Expand Down Expand Up @@ -152,8 +160,15 @@ class DFTLogger {
#endif

if (this->writer != nullptr) {
this->writer->log(index_stack[level - 1], event_name, category,
start_time, duration, metadata, this->process_id, tid);
if (include_metadata) {
this->writer->log(index_stack[level - 1], event_name, category,
start_time, duration, metadata, this->process_id,
tid);
} else {
this->writer->log(local_index, event_name, category, start_time,
duration, metadata, this->process_id, tid);
}

has_entry = true;
} else {
DFTRACER_LOG_ERROR("DFTLogger.log writer not initialized", "");
Expand Down
14 changes: 7 additions & 7 deletions src/dftracer/writer/chrome_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void dftracer::ChromeWriter::initialize(char *filename, bool throw_error) {
DFTRACER_LOG_ERROR("unable to create log file %s",
filename); // GCOVR_EXCL_LINE
} else {
setvbuf(fh, NULL, _IOLBF, MAX_LINE_SIZE);
setvbuf(fh, NULL, _IOFBF, MAX_BUFFER);
DFTRACER_LOG_INFO("created log file %s", filename);
}
}
Expand Down Expand Up @@ -58,6 +58,9 @@ void dftracer::ChromeWriter::finalize(bool has_entry) {
DFTRACER_LOG_DEBUG("ChromeWriter.finalize", "");
if (fh != nullptr) {
DFTRACER_LOG_INFO("Profiler finalizing writer %s", filename.c_str());
if (current_index > 0) {
write_buffer_op(true);
}
fflush(fh);
int last_off = ftell(fh);
(void)last_off;
Expand Down Expand Up @@ -118,11 +121,7 @@ void dftracer::ChromeWriter::finalize(bool has_entry) {
hwloc_topology_destroy(topology);
#endif
}
{
if (current_index > 0) {
write_buffer_op(true);
}
}

{
std::unique_lock<std::shared_mutex> lock(mtx);
if (buffer) {
Expand All @@ -140,6 +139,7 @@ void dftracer::ChromeWriter::convert_json(
std::unordered_map<std::string, std::any> *metadata, ProcessID process_id,
ThreadID thread_id) {
auto previous_index = current_index;
(void)previous_index;
char is_first_char[3] = " ";
if (!is_first_write) is_first_char[0] = '\0';
if (include_metadata) {
Expand Down Expand Up @@ -220,7 +220,7 @@ void dftracer::ChromeWriter::convert_json(
std::unique_lock<std::shared_mutex> lock(mtx);
auto written_size = sprintf(
buffer + current_index,
R"(%s{"id":%d,"name":"%s","cat":"%s","pid":%lu,"tid":%lu,"ts":%llu,"dur":%llu,"ph":"X","args":{}})",
R"(%s{"id":%d,"name":"%s","cat":"%s","pid":%lu,"tid":%lu,"ts":%llu,"dur":%llu,"ph":"X"})",
is_first_char, index, event_name, category, process_id, thread_id,
start_time, duration);
current_index += written_size;
Expand Down
11 changes: 5 additions & 6 deletions src/dftracer/writer/chrome_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,23 @@ class ChromeWriter {

bool is_first_write;
inline size_t write_buffer_op(bool force = false) {
if (!force && current_index > MAX_BUFFER) return 0;
if (!force && current_index < MAX_BUFFER) return 0;
DFTRACER_LOG_DEBUG("ChromeWriter.write_buffer_op %s",
this->filename.c_str());
size_t written_elements = 0;
auto previous_index = current_index;
{
std::unique_lock<std::shared_mutex> lock(mtx);
flockfile(fh);
written_elements = fwrite(buffer, sizeof(char), current_index, fh);
written_elements = fwrite(buffer, current_index, sizeof(char), fh);
funlockfile(fh);
current_index = 0;
}

if (written_elements != previous_index) { // GCOVR_EXCL_START
if (written_elements != 1) { // GCOVR_EXCL_START
DFTRACER_LOG_ERROR(
"unable to log write for a+ written only %ld of %ld with error code "
"unable to log write for a+ written only %ld of %d with error code "
"%d",
written_elements, previous_index, errno);
written_elements, 1, errno);
} // GCOVR_EXCL_STOP
return written_elements;
}
Expand Down

0 comments on commit c374308

Please sign in to comment.