Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: base/trace_event/memory_tracing_observer.cc

Issue 2820433005: memory-infra: Start disentangling tracing from memory-infra (Closed)
Patch Set: add todo Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/trace_event/memory_tracing_observer.h ('k') | tools/gn/bootstrap/bootstrap.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/trace_event/memory_tracing_observer.h"
6
7 #include "base/trace_event/memory_dump_manager.h"
8 #include "base/trace_event/trace_event_argument.h"
9
10 namespace base {
11 namespace trace_event {
12
13 namespace {
14
15 const int kTraceEventNumArgs = 1;
16 const char* kTraceEventArgNames[] = {"dumps"};
17 const unsigned char kTraceEventArgTypes[] = {TRACE_VALUE_TYPE_CONVERTABLE};
18
19 bool IsMemoryInfraTracingEnabled() {
20 bool enabled;
21 TRACE_EVENT_CATEGORY_GROUP_ENABLED(MemoryDumpManager::kTraceCategory,
22 &enabled);
23 return enabled;
24 }
25
26 }; // namespace
27
28 MemoryTracingObserver::MemoryTracingObserver(
29 TraceLog* trace_log,
30 MemoryDumpManager* memory_dump_manager)
31 : memory_dump_manager_(memory_dump_manager), trace_log_(trace_log) {
32 // If tracing was enabled before initializing MemoryDumpManager, we missed the
33 // OnTraceLogEnabled() event. Synthetize it so we can late-join the party.
34 // IsEnabled is called before adding observer to avoid calling
35 // OnTraceLogEnabled twice.
36 bool is_tracing_already_enabled = trace_log_->IsEnabled();
37 trace_log_->AddEnabledStateObserver(this);
38 if (is_tracing_already_enabled)
39 OnTraceLogEnabled();
40 }
41
42 MemoryTracingObserver::~MemoryTracingObserver() {
43 trace_log_->RemoveEnabledStateObserver(this);
44 }
45
46 void MemoryTracingObserver::OnTraceLogEnabled() {
47 if (!IsMemoryInfraTracingEnabled())
48 return;
49
50 // Initialize the TraceLog for the current thread. This is to avoids that the
51 // TraceLog memory dump provider is registered lazily during the MDM Enable()
52 TraceLog::GetInstance()->InitializeThreadLocalEventBufferIfSupported();
53
54 const TraceConfig& trace_config =
55 TraceLog::GetInstance()->GetCurrentTraceConfig();
56 const TraceConfig::MemoryDumpConfig& memory_dump_config =
57 trace_config.memory_dump_config();
58
59 memory_dump_manager_->Enable(memory_dump_config);
60 }
61
62 void MemoryTracingObserver::OnTraceLogDisabled() {
63 memory_dump_manager_->Disable();
64 }
65
66 bool MemoryTracingObserver::AddDumpToTraceIfEnabled(
67 const MemoryDumpRequestArgs* req_args,
68 const ProcessId pid,
69 const ProcessMemoryDump* process_memory_dump) {
70 // If tracing has been disabled early out to avoid the cost of serializing the
71 // dump then ignoring the result.
72 if (!IsMemoryInfraTracingEnabled())
73 return false;
74
75 const uint64_t dump_guid = req_args->dump_guid;
76
77 std::unique_ptr<TracedValue> traced_value(new TracedValue);
78 process_memory_dump->AsValueInto(traced_value.get());
79 traced_value->SetString("level_of_detail", MemoryDumpLevelOfDetailToString(
80 req_args->level_of_detail));
81 const char* const event_name = MemoryDumpTypeToString(req_args->dump_type);
82
83 std::unique_ptr<ConvertableToTraceFormat> event_value(
84 std::move(traced_value));
85 TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID(
86 TRACE_EVENT_PHASE_MEMORY_DUMP,
87 TraceLog::GetCategoryGroupEnabled(MemoryDumpManager::kTraceCategory),
88 event_name, trace_event_internal::kGlobalScope, dump_guid, pid,
89 kTraceEventNumArgs, kTraceEventArgNames, kTraceEventArgTypes,
90 nullptr /* arg_values */, &event_value, TRACE_EVENT_FLAG_HAS_ID);
91
92 return true;
93 }
94
95 } // namespace trace_event
96 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/memory_tracing_observer.h ('k') | tools/gn/bootstrap/bootstrap.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698