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 <array> 12 #include <string> 13 14 #include "base/base_export.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(const TraceEventMemoryOverhead&) = delete; 48 TraceEventMemoryOverhead& operator=(const TraceEventMemoryOverhead&) = delete; 49 ~TraceEventMemoryOverhead(); 50 51 // Use this method to account the overhead of an object for which an estimate 52 // is known for both the allocated and resident memory. 53 void Add(ObjectType object_type, 54 size_t allocated_size_in_bytes, 55 size_t resident_size_in_bytes); 56 57 // Similar to Add() above, but assumes that 58 // |resident_size_in_bytes| == |allocated_size_in_bytes|. 59 void Add(ObjectType object_type, size_t allocated_size_in_bytes); 60 61 // Specialized profiling functions for commonly used object types. 62 void AddString(const std::string& str); 63 void AddValue(const Value& value); 64 void AddRefCountedString(const RefCountedString& str); 65 66 // Call this after all the Add* methods above to account the memory used by 67 // this TraceEventMemoryOverhead instance itself. 68 void AddSelf(); 69 70 // Retrieves the count, that is, the count of Add*(|object_type|, ...) calls. 71 size_t GetCount(ObjectType object_type) const; 72 73 // Adds up and merges all the values from |other| to this instance. 74 void Update(const TraceEventMemoryOverhead& other); 75 76 void DumpInto(const char* base_name, ProcessMemoryDump* pmd) const; 77 78 private: 79 struct ObjectCountAndSize { 80 size_t count; 81 size_t allocated_size_in_bytes; 82 size_t resident_size_in_bytes; 83 }; 84 std::array<ObjectCountAndSize, ObjectType::kLast> allocated_objects_; 85 86 void AddInternal(ObjectType object_type, 87 size_t count, 88 size_t allocated_size_in_bytes, 89 size_t resident_size_in_bytes); 90 }; 91 92 } // namespace trace_event 93 } // namespace base 94 95 #endif // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_OVERHEAD_H_ 96