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 #ifndef SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_TRACING_OBSERVER_H_
6 #define SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_TRACING_OBSERVER_H_
7 
8 #include "base/component_export.h"
9 #include "base/macros.h"
10 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/trace_event.h"
12 #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
13 #include "third_party/perfetto/include/perfetto/ext/tracing/core/trace_writer.h"
14 
15 namespace memory_instrumentation {
16 
17 // Observes TraceLog for Enable/Disable events and when they occur Enables and
18 // Disables the MemoryDumpManager with the correct state based on reading the
19 // trace log. Also provides a method for adding a dump to the trace.
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION)20 class COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION)
21     TracingObserver : public base::trace_event::TraceLog::EnabledStateObserver {
22  public:
23   TracingObserver(base::trace_event::TraceLog*,
24                   base::trace_event::MemoryDumpManager*);
25   ~TracingObserver() override;
26 
27   // TraceLog::EnabledStateObserver implementation.
28   void OnTraceLogEnabled() override;
29   void OnTraceLogDisabled() override;
30 
31   virtual bool AddChromeDumpToTraceIfEnabled(
32       const base::trace_event::MemoryDumpRequestArgs&,
33       const base::ProcessId pid,
34       const base::trace_event::ProcessMemoryDump*,
35       const base::TimeTicks& timestamp);
36 
37   virtual bool AddOsDumpToTraceIfEnabled(
38       const base::trace_event::MemoryDumpRequestArgs& args,
39       const base::ProcessId pid,
40       const mojom::OSMemDump& os_dump,
41       const std::vector<mojom::VmRegionPtr>& memory_maps,
42       const base::TimeTicks& timestamp);
43 
44   // TODO(lalitm): make these private again after TracingObserver is moved
45   // to private space.
46   bool ShouldAddToTrace(const base::trace_event::MemoryDumpRequestArgs&);
47 
48  protected:
49   static std::string ApplyPathFiltering(const std::string& file,
50                                         bool is_argument_filtering_enabled);
51 
52  private:
53   // Returns true if the dump mode is allowed for current tracing session.
54   bool IsDumpModeAllowed(base::trace_event::MemoryDumpLevelOfDetail) const;
55 
56   base::trace_event::MemoryDumpManager* const memory_dump_manager_;
57   base::trace_event::TraceLog* const trace_log_;
58   std::unique_ptr<base::trace_event::TraceConfig::MemoryDumpConfig>
59       memory_dump_config_;
60 
61   DISALLOW_COPY_AND_ASSIGN(TracingObserver);
62 };
63 
64 }  // namespace memory_instrumentation
65 
66 #endif  // SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_TRACING_OBSERVER_H_
67