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