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_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 #include <unordered_map>
13 
14 #include "base/base_export.h"
15 #include "base/macros.h"
16 
17 namespace base {
18 
19 class RefCountedString;
20 class Value;
21 
22 namespace trace_event {
23 
24 class ProcessMemoryDump;
25 
26 // Used to estimate the memory overhead of the tracing infrastructure.
27 class BASE_EXPORT TraceEventMemoryOverhead {
28  public:
29   enum ObjectType : uint32_t {
30     kOther = 0,
31     kTraceBuffer,
32     kTraceBufferChunk,
33     kTraceEvent,
34     kUnusedTraceEvent,
35     kTracedValue,
36     kConvertableToTraceFormat,
37     kHeapProfilerAllocationRegister,
38     kHeapProfilerTypeNameDeduplicator,
39     kHeapProfilerStackFrameDeduplicator,
40     kStdString,
41     kBaseValue,
42     kTraceEventMemoryOverhead,
43     kFrameMetrics,
44     kLast
45   };
46 
47   TraceEventMemoryOverhead();
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   DISALLOW_COPY_AND_ASSIGN(TraceEventMemoryOverhead);
91 };
92 
93 }  // namespace trace_event
94 }  // namespace base
95 
96 #endif  // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_OVERHEAD_H_
97