1 // Copyright 2015 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 BASE_TRACE_EVENT_MEMORY_DUMP_REQUEST_ARGS_H_
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_REQUEST_ARGS_H_
7 
8 // This file defines the types and structs used to issue memory dump requests.
9 // These are also used in the IPCs for coordinating inter-process memory dumps.
10 
11 #include <stdint.h>
12 #include <map>
13 #include <memory>
14 #include <string>
15 
16 #include "base/base_export.h"
17 #include "base/callback.h"
18 #include "base/optional.h"
19 #include "base/process/process_handle.h"
20 
21 namespace base {
22 namespace trace_event {
23 
24 class ProcessMemoryDump;
25 
26 // Captures the reason why a memory dump is being requested. This is to allow
27 // selective enabling of dumps, filtering and post-processing. Keep this
28 // consistent with memory_instrumentation.mojo and
29 // memory_instrumentation_struct_traits.{h,cc}
30 enum class MemoryDumpType {
31   PERIODIC_INTERVAL,     // Dumping memory at periodic intervals.
32   EXPLICITLY_TRIGGERED,  // Non maskable dump request.
33   SUMMARY_ONLY,          // Calculate just the summary & don't add to the trace.
34   LAST = SUMMARY_ONLY
35 };
36 
37 // Tells the MemoryDumpProvider(s) how much detailed their dumps should be.
38 // Keep this consistent with memory_instrumentation.mojo and
39 // memory_instrumentation_struct_traits.{h,cc}
40 enum class MemoryDumpLevelOfDetail : uint32_t {
41   FIRST,
42 
43   // For background tracing mode. The dump time is quick, and typically just the
44   // totals are expected. Suballocations need not be specified. Dump name must
45   // contain only pre-defined strings and string arguments cannot be added.
46   BACKGROUND = FIRST,
47 
48   // For the levels below, MemoryDumpProvider instances must guarantee that the
49   // total size reported in the root node is consistent. Only the granularity of
50   // the child MemoryAllocatorDump(s) differs with the levels.
51 
52   // Few entries, typically a fixed number, per dump.
53   LIGHT,
54 
55   // Unrestricted amount of entries per dump.
56   DETAILED,
57 
58   LAST = DETAILED
59 };
60 
61 // Tells the MemoryDumpProvider(s) if they should try to make the result
62 // more deterministic by forcing garbage collection.
63 // Keep this consistent with memory_instrumentation.mojo and
64 // memory_instrumentation_struct_traits.{h,cc}
65 enum class MemoryDumpDeterminism : uint32_t { NONE, FORCE_GC };
66 
67 // Keep this consistent with memory_instrumentation.mojo and
68 // memory_instrumentation_struct_traits.{h,cc}
69 struct BASE_EXPORT MemoryDumpRequestArgs {
70   // Globally unique identifier. In multi-process dumps, all processes issue a
71   // local dump with the same guid. This allows the trace importers to
72   // reconstruct the global dump.
73   uint64_t dump_guid;
74 
75   MemoryDumpType dump_type;
76   MemoryDumpLevelOfDetail level_of_detail;
77   MemoryDumpDeterminism determinism;
78 };
79 
80 // Args for ProcessMemoryDump and passed to OnMemoryDump calls for memory dump
81 // providers. Dump providers are expected to read the args for creating dumps.
82 struct MemoryDumpArgs {
83   // Specifies how detailed the dumps should be.
84   MemoryDumpLevelOfDetail level_of_detail;
85   // Specifies whether the dumps should be more deterministic.
86   MemoryDumpDeterminism determinism;
87 
88   // Globally unique identifier. In multi-process dumps, all processes issue a
89   // local dump with the same guid. This allows the trace importers to
90   // reconstruct the global dump.
91   uint64_t dump_guid;
92 };
93 
94 using ProcessMemoryDumpCallback = OnceCallback<
95     void(bool success, uint64_t dump_guid, std::unique_ptr<ProcessMemoryDump>)>;
96 
97 BASE_EXPORT const char* MemoryDumpTypeToString(const MemoryDumpType& dump_type);
98 
99 BASE_EXPORT MemoryDumpType StringToMemoryDumpType(const std::string& str);
100 
101 BASE_EXPORT const char* MemoryDumpLevelOfDetailToString(
102     const MemoryDumpLevelOfDetail& level_of_detail);
103 
104 BASE_EXPORT MemoryDumpLevelOfDetail
105 StringToMemoryDumpLevelOfDetail(const std::string& str);
106 
107 }  // namespace trace_event
108 }  // namespace base
109 
110 #endif  // BASE_TRACE_EVENT_MEMORY_DUMP_REQUEST_ARGS_H_
111