Home | History | Annotate | Download | only in trace_event
      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 
     10 #include "base/base_export.h"
     11 #include "base/containers/hash_tables.h"
     12 #include "base/containers/small_map.h"
     13 #include "base/macros.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   TraceEventMemoryOverhead();
     28   ~TraceEventMemoryOverhead();
     29 
     30   // Use this method to account the overhead of an object for which an estimate
     31   // is known for both the allocated and resident memory.
     32   void Add(const char* object_type,
     33            size_t allocated_size_in_bytes,
     34            size_t resident_size_in_bytes);
     35 
     36   // Similar to Add() above, but assumes that
     37   // |resident_size_in_bytes| == |allocated_size_in_bytes|.
     38   void Add(const char* object_type, size_t allocated_size_in_bytes);
     39 
     40   // Specialized profiling functions for commonly used object types.
     41   void AddString(const std::string& str);
     42   void AddValue(const Value& value);
     43   void AddRefCountedString(const RefCountedString& str);
     44 
     45   // Call this after all the Add* methods above to account the memory used by
     46   // this TraceEventMemoryOverhead instance itself.
     47   void AddSelf();
     48 
     49   // Retrieves the count, that is, the count of Add*(|object_type|, ...) calls.
     50   size_t GetCount(const char* object_type) const;
     51 
     52   // Adds up and merges all the values from |other| to this instance.
     53   void Update(const TraceEventMemoryOverhead& other);
     54 
     55   void DumpInto(const char* base_name, ProcessMemoryDump* pmd) const;
     56 
     57  private:
     58   struct ObjectCountAndSize {
     59     size_t count;
     60     size_t allocated_size_in_bytes;
     61     size_t resident_size_in_bytes;
     62   };
     63   using map_type = SmallMap<hash_map<const char*, ObjectCountAndSize>, 16>;
     64   map_type allocated_objects_;
     65 
     66   void AddOrCreateInternal(const char* object_type,
     67                            size_t count,
     68                            size_t allocated_size_in_bytes,
     69                            size_t resident_size_in_bytes);
     70 
     71   DISALLOW_COPY_AND_ASSIGN(TraceEventMemoryOverhead);
     72 };
     73 
     74 }  // namespace trace_event
     75 }  // namespace base
     76 
     77 #endif  // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_OVERHEAD_H_
     78