• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
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_TRACE_EVENT_MEMORY_OVERHEAD_H_
6 #define BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_OVERHEAD_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <string>
12 
13 #include "base/base_export.h"
14 
15 namespace base {
16 
17 class RefCountedString;
18 class Value;
19 
20 namespace trace_event {
21 
22 class ProcessMemoryDump;
23 
24 // Used to estimate the memory overhead of the tracing infrastructure.
25 class BASE_EXPORT TraceEventMemoryOverhead {
26  public:
27   enum ObjectType : uint32_t {
28     kOther = 0,
29     kTraceBuffer,
30     kTraceBufferChunk,
31     kTraceEvent,
32     kUnusedTraceEvent,
33     kTracedValue,
34     kConvertableToTraceFormat,
35     kHeapProfilerAllocationRegister,
36     kHeapProfilerTypeNameDeduplicator,
37     kHeapProfilerStackFrameDeduplicator,
38     kStdString,
39     kBaseValue,
40     kTraceEventMemoryOverhead,
41     kFrameMetrics,
42     kLast
43   };
44 
45   TraceEventMemoryOverhead();
46   TraceEventMemoryOverhead(const TraceEventMemoryOverhead&) = delete;
47   TraceEventMemoryOverhead& operator=(const TraceEventMemoryOverhead&) = delete;
48   ~TraceEventMemoryOverhead();
49 
50   // Use this method to account the overhead of an object for which an estimate
51   // is known for both the allocated and resident memory.
52   void Add(ObjectType object_type,
53            size_t allocated_size_in_bytes,
54            size_t resident_size_in_bytes);
55 
56   // Similar to Add() above, but assumes that
57   // |resident_size_in_bytes| == |allocated_size_in_bytes|.
58   void Add(ObjectType object_type, size_t allocated_size_in_bytes);
59 
60   // Specialized profiling functions for commonly used object types.
61   void AddString(const std::string& str);
62   void AddValue(const Value& value);
63   void AddRefCountedString(const RefCountedString& str);
64 
65   // Call this after all the Add* methods above to account the memory used by
66   // this TraceEventMemoryOverhead instance itself.
67   void AddSelf();
68 
69   // Retrieves the count, that is, the count of Add*(|object_type|, ...) calls.
70   size_t GetCount(ObjectType object_type) const;
71 
72   // Adds up and merges all the values from |other| to this instance.
73   void Update(const TraceEventMemoryOverhead& other);
74 
75   void DumpInto(const char* base_name, ProcessMemoryDump* pmd) const;
76 
77  private:
78   struct ObjectCountAndSize {
79     size_t count;
80     size_t allocated_size_in_bytes;
81     size_t resident_size_in_bytes;
82   };
83   ObjectCountAndSize allocated_objects_[ObjectType::kLast];
84 
85   void AddInternal(ObjectType object_type,
86                    size_t count,
87                    size_t allocated_size_in_bytes,
88                    size_t resident_size_in_bytes);
89 };
90 
91 }  // namespace trace_event
92 }  // namespace base
93 
94 #endif  // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_OVERHEAD_H_
95