summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorprimiano <primiano@chromium.org>2015-11-03 11:50:49 -0800
committerCommit bot <commit-bot@chromium.org>2015-11-03 19:52:31 +0000
commite3d118d5a217b3ad96b2ac3393b797339bc77d13 (patch)
tree3b434183f36a91577432135660630b76fee72421 /base
parentdcef9fdcde6aa16ca06d1baf32a963e92bcac21c (diff)
downloadchromium_src-e3d118d5a217b3ad96b2ac3393b797339bc77d13.zip
chromium_src-e3d118d5a217b3ad96b2ac3393b797339bc77d13.tar.gz
chromium_src-e3d118d5a217b3ad96b2ac3393b797339bc77d13.tar.bz2
[tracing] Add TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID
Adds a new macro to the tracing subsystem that allows to override the pid of the trace events. So far all the events created from one process are attributed to the pid of the process itself. However, in some cases, we want to generate events on behalf of other processes. A concrete use case is memory-infra, where the browser process dumps some memory stats for the child processes to avoid sandboxing issues (see crrev.com/1404343005, which depends on this). The pid override is rendered by reusing the thread_id and a TRACE_EVENT_FLAG_HAS_PROCESS_ID flag. Rationale for doing this: - In this case, any other use of the thread_id would be pointless. If we are specifying an event for another pid, a local thread_id has no valuable meaning. - This avoids inflating the sizeof(TraceEvent). For more context see: Discussion on tracing@chromium.org: https://goo.gl/N4OG9J Design doc: https://goo.gl/Nvidgi BUG=461788 Review URL: https://codereview.chromium.org/1421223006 Cr-Commit-Position: refs/heads/master@{#357581}
Diffstat (limited to 'base')
-rw-r--r--base/trace_event/common/trace_event_common.h1
-rw-r--r--base/trace_event/trace_event.h20
-rw-r--r--base/trace_event/trace_event_impl.cc19
-rw-r--r--base/trace_event/trace_event_impl.h8
-rw-r--r--base/trace_event/trace_log.cc30
-rw-r--r--base/trace_event/trace_log.h12
6 files changed, 86 insertions, 4 deletions
diff --git a/base/trace_event/common/trace_event_common.h b/base/trace_event/common/trace_event_common.h
index 679e923..9a61b19 100644
--- a/base/trace_event/common/trace_event_common.h
+++ b/base/trace_event/common/trace_event_common.h
@@ -1024,6 +1024,7 @@
#define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8))
#define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9))
#define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10))
+#define TRACE_EVENT_FLAG_HAS_PROCESS_ID (static_cast<unsigned int>(1 << 11))
#define TRACE_EVENT_FLAG_SCOPE_MASK \
(static_cast<unsigned int>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \
diff --git a/base/trace_event/trace_event.h b/base/trace_event/trace_event.h
index 8137251..37320ea 100644
--- a/base/trace_event/trace_event.h
+++ b/base/trace_event/trace_event.h
@@ -125,6 +125,26 @@
#define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_CONTEXT_ID \
base::trace_event::TraceLog::GetInstance()->AddTraceEventWithContextId
+// Add a trace event to the platform tracing system overriding the pid.
+// The resulting event will have tid = pid == (process_id passed here).
+// base::trace_event::TraceEventHandle
+// TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID(
+// char phase,
+// const unsigned char* category_group_enabled,
+// const char* name,
+// unsigned long long id,
+// unsigned long long context_id,
+// int process_id,
+// int num_args,
+// const char** arg_names,
+// const unsigned char* arg_types,
+// const unsigned long long* arg_values,
+// const scoped_refptr<ConvertableToTraceFormat>*
+// convertable_values,
+// unsigned int flags)
+#define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID \
+ base::trace_event::TraceLog::GetInstance()->AddTraceEventWithProcessId
+
// Add a trace event to the platform tracing system.
// base::trace_event::TraceEventHandle
// TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
diff --git a/base/trace_event/trace_event_impl.cc b/base/trace_event/trace_event_impl.cc
index 60ef16b..778ecbf 100644
--- a/base/trace_event/trace_event_impl.cc
+++ b/base/trace_event/trace_event_impl.cc
@@ -6,6 +6,7 @@
#include "base/format_macros.h"
#include "base/json/string_escape.h"
+#include "base/process/process_handle.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
@@ -60,7 +61,10 @@ void TraceEvent::CopyFrom(const TraceEvent& other) {
context_id_ = other.context_id_;
category_group_enabled_ = other.category_group_enabled_;
name_ = other.name_;
- thread_id_ = other.thread_id_;
+ if (other.flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID)
+ process_id_ = other.process_id_;
+ else
+ thread_id_ = other.thread_id_;
phase_ = other.phase_;
flags_ = other.flags_;
parameter_copy_storage_ = other.parameter_copy_storage_;
@@ -270,7 +274,16 @@ void TraceEvent::AppendAsJSON(
std::string* out,
const ArgumentFilterPredicate& argument_filter_predicate) const {
int64 time_int64 = timestamp_.ToInternalValue();
- int process_id = TraceLog::GetInstance()->process_id();
+ int process_id;
+ int thread_id;
+ if ((flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID) &&
+ process_id_ != kNullProcessId) {
+ process_id = process_id_;
+ thread_id = -1;
+ } else {
+ process_id = TraceLog::GetInstance()->process_id();
+ thread_id = thread_id_;
+ }
const char* category_group_name =
TraceLog::GetCategoryGroupName(category_group_enabled_);
@@ -279,7 +292,7 @@ void TraceEvent::AppendAsJSON(
StringAppendF(out, "{\"pid\":%i,\"tid\":%i,\"ts\":%" PRId64
","
"\"ph\":\"%c\",\"cat\":\"%s\",\"name\":\"%s\",\"args\":",
- process_id, thread_id_, time_int64, phase_, category_group_name,
+ process_id, thread_id, time_int64, phase_, category_group_name,
name_);
// Output argument names and values, stop at first NULL argument name.
diff --git a/base/trace_event/trace_event_impl.h b/base/trace_event/trace_event_impl.h
index 59401631..cb8b144 100644
--- a/base/trace_event/trace_event_impl.h
+++ b/base/trace_event/trace_event_impl.h
@@ -168,7 +168,13 @@ class BASE_EXPORT TraceEvent {
const unsigned char* category_group_enabled_;
const char* name_;
scoped_refptr<base::RefCountedString> parameter_copy_storage_;
- int thread_id_;
+ // Depending on TRACE_EVENT_FLAG_HAS_PROCESS_ID the event will have either:
+ // tid: thread_id_, pid: current_process_id (default case).
+ // tid: -1, pid: process_id_ (when flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID).
+ union {
+ int thread_id_;
+ int process_id_;
+ };
unsigned int flags_;
unsigned long long bind_id_;
unsigned char arg_types_[kTraceMaxNumArgs];
diff --git a/base/trace_event/trace_log.cc b/base/trace_event/trace_log.cc
index a8d50ca..c9cb1b3 100644
--- a/base/trace_event/trace_log.cc
+++ b/base/trace_event/trace_log.cc
@@ -1129,6 +1129,36 @@ TraceEventHandle TraceLog::AddTraceEventWithContextId(
flags | TRACE_EVENT_FLAG_HAS_CONTEXT_ID);
}
+TraceEventHandle TraceLog::AddTraceEventWithProcessId(
+ char phase,
+ const unsigned char* category_group_enabled,
+ const char* name,
+ unsigned long long id,
+ int process_id,
+ int num_args,
+ const char** arg_names,
+ const unsigned char* arg_types,
+ const unsigned long long* arg_values,
+ const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
+ unsigned int flags) {
+ base::TraceTicks now = base::TraceTicks::Now();
+ return AddTraceEventWithThreadIdAndTimestamp(
+ phase,
+ category_group_enabled,
+ name,
+ id,
+ trace_event_internal::kNoId, // context_id
+ trace_event_internal::kNoId, // bind_id
+ process_id,
+ now,
+ num_args,
+ arg_names,
+ arg_types,
+ arg_values,
+ convertable_values,
+ flags | TRACE_EVENT_FLAG_HAS_PROCESS_ID);
+}
+
// Handle legacy calls to AddTraceEventWithThreadIdAndTimestamp
// with kNoId as bind_id
TraceEventHandle TraceLog::AddTraceEventWithThreadIdAndTimestamp(
diff --git a/base/trace_event/trace_log.h b/base/trace_event/trace_log.h
index 425c7f3..a68a380 100644
--- a/base/trace_event/trace_log.h
+++ b/base/trace_event/trace_log.h
@@ -202,6 +202,18 @@ class BASE_EXPORT TraceLog : public MemoryDumpProvider {
const unsigned long long* arg_values,
const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
unsigned int flags);
+ TraceEventHandle AddTraceEventWithProcessId(
+ char phase,
+ const unsigned char* category_group_enabled,
+ const char* name,
+ unsigned long long id,
+ int process_id,
+ int num_args,
+ const char** arg_names,
+ const unsigned char* arg_types,
+ const unsigned long long* arg_values,
+ const scoped_refptr<ConvertableToTraceFormat>* convertable_values,
+ unsigned int flags);
TraceEventHandle AddTraceEventWithThreadIdAndTimestamp(
char phase,
const unsigned char* category_group_enabled,