| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/debug/trace_event_impl.h" | 5 #include "base/debug/trace_event_impl.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/file_util.h" | |
| 11 #include "base/format_macros.h" | |
| 12 #include "base/logging.h" | 10 #include "base/logging.h" |
| 13 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 14 | 12 |
| 15 namespace { | 13 namespace { |
| 16 | 14 |
| 17 int g_atrace_fd = -1; | 15 int g_atrace_fd = -1; |
| 18 const char* kATraceMarkerFile = "/sys/kernel/debug/tracing/trace_marker"; | 16 const char* kATraceMarkerFile = "/sys/kernel/debug/tracing/trace_marker"; |
| 19 | 17 |
| 20 } // namespace | 18 } // namespace |
| 21 | 19 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 write(g_atrace_fd, out.c_str(), out.size()); | 80 write(g_atrace_fd, out.c_str(), out.size()); |
| 83 } | 81 } |
| 84 break; | 82 break; |
| 85 | 83 |
| 86 default: | 84 default: |
| 87 // Do nothing. | 85 // Do nothing. |
| 88 break; | 86 break; |
| 89 } | 87 } |
| 90 } | 88 } |
| 91 | 89 |
| 92 void TraceLog::AddClockSyncMetadataEvents() { | |
| 93 // Since Android does not support sched_setaffinity, we cannot establish clock | |
| 94 // sync unless the scheduler clock is set to global. If the trace_clock file | |
| 95 // can't be read, we will assume the kernel doesn't support tracing and do | |
| 96 // nothing. | |
| 97 std::string clock_mode; | |
| 98 if (!file_util::ReadFileToString( | |
| 99 FilePath("/sys/kernel/debug/tracing/trace_clock"), &clock_mode)) | |
| 100 return; | |
| 101 | |
| 102 if (clock_mode != "local [global]\n") { | |
| 103 DLOG(WARNING) << | |
| 104 "The kernel's tracing clock must be set to global in order for " << | |
| 105 "trace_event to be synchronized with . Do this by\n" << | |
| 106 " echo global > /sys/kerel/debug/tracing/trace_clock"; | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 int atrace_fd = g_atrace_fd; | |
| 111 if (atrace_fd == -1) { | |
| 112 // This function may be called when atrace is not enabled. | |
| 113 atrace_fd = open(kATraceMarkerFile, O_WRONLY | O_APPEND); | |
| 114 if (atrace_fd == -1) { | |
| 115 LOG(WARNING) << "Couldn't open " << kATraceMarkerFile; | |
| 116 return; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 // Android's kernel trace system has a trace_marker feature: this is a file on | |
| 121 // debugfs that takes the written data and pushes it onto the trace | |
| 122 // buffer. So, to establish clock sync, we write our monotonic clock into that | |
| 123 // trace buffer. | |
| 124 TimeTicks now = TimeTicks::NowFromSystemTraceTime(); | |
| 125 double now_in_seconds = now.ToInternalValue() / 1000000.0; | |
| 126 std::string marker = StringPrintf( | |
| 127 "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds); | |
| 128 if (write(atrace_fd, marker.c_str(), marker.size()) != 0) { | |
| 129 DLOG(WARNING) << "Couldn't write to " << kATraceMarkerFile << ": " | |
| 130 << strerror(errno); | |
| 131 } | |
| 132 | |
| 133 if (g_atrace_fd == -1) | |
| 134 close(atrace_fd); | |
| 135 } | |
| 136 | |
| 137 // Must be called with lock_ locked. | 90 // Must be called with lock_ locked. |
| 138 void TraceLog::ApplyATraceEnabledFlag(unsigned char* category_enabled) { | 91 void TraceLog::ApplyATraceEnabledFlag(unsigned char* category_enabled) { |
| 139 if (g_atrace_fd != -1) | 92 if (g_atrace_fd != -1) |
| 140 *category_enabled |= ATRACE_ENABLED; | 93 *category_enabled |= ATRACE_ENABLED; |
| 141 } | 94 } |
| 142 | 95 |
| 143 } // namespace debug | 96 } // namespace debug |
| 144 } // namespace base | 97 } // namespace base |
| OLD | NEW |