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_MALLOC_DUMP_PROVIDER_H_
6 #define BASE_TRACE_EVENT_MALLOC_DUMP_PROVIDER_H_
7 
8 #include "base/macros.h"
9 #include "base/memory/singleton.h"
10 #include "base/synchronization/lock.h"
11 #include "base/trace_event/memory_dump_provider.h"
12 #include "build/build_config.h"
13 
14 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_WIN) || \
15     (defined(OS_MACOSX) && !defined(OS_IOS))
16 #define MALLOC_MEMORY_TRACING_SUPPORTED
17 #endif
18 
19 namespace base {
20 namespace trace_event {
21 
22 // Dump provider which collects process-wide memory stats.
23 class BASE_EXPORT MallocDumpProvider : public MemoryDumpProvider {
24  public:
25   // Name of the allocated_objects dump. Use this to declare suballocator dumps
26   // from other dump providers.
27   static const char kAllocatedObjects[];
28 
29   static MallocDumpProvider* GetInstance();
30 
31   // MemoryDumpProvider implementation.
32   bool OnMemoryDump(const MemoryDumpArgs& args,
33                     ProcessMemoryDump* pmd) override;
34 
35   // Used by out-of-process heap-profiling. When malloc is profiled by an
36   // external process, that process will be responsible for emitting metrics on
37   // behalf of this one. Thus, MallocDumpProvider should not do anything.
38   void EnableMetrics();
39   void DisableMetrics();
40 
41  private:
42   friend struct DefaultSingletonTraits<MallocDumpProvider>;
43 
44   MallocDumpProvider();
45   ~MallocDumpProvider() override;
46 
47   bool emit_metrics_on_memory_dump_ = true;
48   base::Lock emit_metrics_on_memory_dump_lock_;
49 
50   DISALLOW_COPY_AND_ASSIGN(MallocDumpProvider);
51 };
52 
53 }  // namespace trace_event
54 }  // namespace base
55 
56 #endif  // BASE_TRACE_EVENT_MALLOC_DUMP_PROVIDER_H_
57