Skip to content

Commit

Permalink
Wrap HANDLE_EINTR around open() and write() in trace_event_android.cc
Browse files Browse the repository at this point in the history
BUG=547825

Review URL: https://codereview.chromium.org/1421283002

Cr-Commit-Position: refs/heads/master@{#356873}
  • Loading branch information
wangxianzhu authored and Commit bot committed Oct 29, 2015
1 parent b2e7ac0 commit 5fd175b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions base/trace_event/trace_event_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "base/format_macros.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/trace_event/trace_event.h"
Expand All @@ -20,6 +21,21 @@ namespace {
int g_atrace_fd = -1;
const char kATraceMarkerFile[] = "/sys/kernel/debug/tracing/trace_marker";

void WriteToATrace(int fd, const char* buffer, size_t size) {
size_t total_written = 0;
while (total_written < size) {
ssize_t written = HANDLE_EINTR(write(
fd, buffer + size, size - total_written));
if (written <= 0)
break;
total_written += written;
}
if (total_written < size) {
PLOG(WARNING) << "Failed to write buffer '" << std::string(buffer, size)
<< "' to " << kATraceMarkerFile;
}
}

void WriteEvent(
char phase,
const char* category_group,
Expand Down Expand Up @@ -57,7 +73,7 @@ void WriteEvent(

out += '|';
out += category_group;
write(g_atrace_fd, out.c_str(), out.size());
WriteToATrace(g_atrace_fd, out.c_str(), out.size());
}

void NoOpOutputCallback(WaitableEvent* complete_event,
Expand Down Expand Up @@ -90,7 +106,7 @@ void TraceLog::StartATrace() {
if (g_atrace_fd != -1)
return;

g_atrace_fd = open(kATraceMarkerFile, O_WRONLY);
g_atrace_fd = HANDLE_EINTR(open(kATraceMarkerFile, O_WRONLY));
if (g_atrace_fd == -1) {
PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
return;
Expand Down Expand Up @@ -152,7 +168,7 @@ void TraceEvent::SendToATrace() {
WriteEvent('B', category_group, name_, id_,
arg_names_, arg_types_, arg_values_, convertable_values_,
flags_);
write(g_atrace_fd, "E", 1);
WriteToATrace(g_atrace_fd, "E", 1);
break;

case TRACE_EVENT_PHASE_COUNTER:
Expand All @@ -164,7 +180,7 @@ void TraceEvent::SendToATrace() {
StringAppendF(&out, "-%" PRIx64, static_cast<uint64>(id_));
StringAppendF(&out, "|%d|%s",
static_cast<int>(arg_values_[i].as_int), category_group);
write(g_atrace_fd, out.c_str(), out.size());
WriteToATrace(g_atrace_fd, out.c_str(), out.size());
}
break;

Expand All @@ -175,7 +191,7 @@ void TraceEvent::SendToATrace() {
}

void TraceLog::AddClockSyncMetadataEvent() {
int atrace_fd = open(kATraceMarkerFile, O_WRONLY | O_APPEND);
int atrace_fd = HANDLE_EINTR(open(kATraceMarkerFile, O_WRONLY | O_APPEND));
if (atrace_fd == -1) {
PLOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
return;
Expand All @@ -188,8 +204,7 @@ void TraceLog::AddClockSyncMetadataEvent() {
double now_in_seconds = (TraceTicks::Now() - TraceTicks()).InSecondsF();
std::string marker = StringPrintf(
"trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
if (write(atrace_fd, marker.c_str(), marker.size()) == -1)
PLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile;
WriteToATrace(atrace_fd, marker.c_str(), marker.size());
close(atrace_fd);
}

Expand Down

0 comments on commit 5fd175b

Please sign in to comment.