Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: base/debug/scoped_thread_heap_usage.h

Issue 2163783003: Implement a ScopedThreadHeapUsage class to allow profiling per-thread heap usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shim-default
Patch Set: Fix memory leak found by ASAN bot. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_
6 #define BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_
7
8 #include <stdint.h>
9
10 #include "base/allocator/features.h"
11 #include "base/base_export.h"
12 #include "base/threading/thread_checker.h"
13
14 namespace base {
15 namespace allocator {
16 struct AllocatorDispatch;
17 } // namespace allocator
18
19 namespace debug {
20
21 // By keeping a tally on heap operations, it's possible to track:
22 // - the number of alloc/free operations, where a realloc is zero or one
23 // of each, depending on the input parameters (see man realloc).
24 // - the number of bytes allocated/freed.
25 // - the number of estimated bytes of heap overhead used.
26 // - the high-watermark amount of bytes allocated in the scope.
27 // This in turn allows measuring the memory usage and memory usage churn over
28 // a scope. Scopes must be cleanly nested, and each scope must be
29 // destroyed on the thread where it's created.
30 //
31 // Note that this depends on the capabilities of the underlying heap shim. If
32 // that shim can not yield a size estimate for an allocation, it's impossible to
33 // keep track of overhead, freed bytes and the allocation high water mark.
34 class BASE_EXPORT ScopedThreadHeapUsage {
35 public:
36 struct ThreadAllocatorUsage {
37 // The cumulative number of allocation operations.
38 uint64_t alloc_ops;
39
40 // The cumulative number of allocated bytes. Where available, this is
41 // inclusive heap padding and estimated or actual heap overhead.
42 uint64_t alloc_bytes;
43
44 // Where available, cumulative number of heap padding heap
45 // and overhead bytes.
46 uint64_t alloc_overhead_bytes;
47
48 // The cumulative number of free operations.
49 uint64_t free_ops;
50
51 // The cumulative number of bytes freed.
52 // Only recorded if the underlying heap shim can return the size of an
53 // allocation.
54 uint64_t free_bytes;
55
56 // The maximal value of alloc_bytes - free_bytes seen for this thread.
57 // Only recorded if the underlying heap shim supports returning the size of
58 // an allocation.
59 uint64_t max_allocated_bytes;
60 };
61
62 ScopedThreadHeapUsage();
63 ~ScopedThreadHeapUsage();
64
65 const ThreadAllocatorUsage& usage_at_creation() const {
66 return usage_at_creation_;
67 }
68
69 // Returns this thread's current allocator usage.
70 static ThreadAllocatorUsage Now();
71
72 // Initializes the TLS machinery this class uses. Must be called before
73 // creating instances of this class.
74 static void Initialize();
75
76 // Enables the heap intercept. May only be called once, and only if the heap
77 // shim is available, e.g. if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) is
78 // true.
79 static void EnableHeapTracking();
80
81 protected:
82 // Exposed for testing only - note that it's safe to re-EnableHeapTracking()
83 // after calling this function in tests.
84 static void DisableHeapTrackingForTesting();
85
86 // Exposed to allow testing the shim without inserting it in the allocator
87 // shim chain.
88 static base::allocator::AllocatorDispatch* GetDispatchForTesting();
89
90 private:
91 static void EnsureTLSInitialized();
92
93 ThreadChecker thread_checker_;
94 // The allocator usage captured at creation of this instance.
95 ThreadAllocatorUsage usage_at_creation_;
96 };
97
98 } // namespace debug
99 } // namespace base
100
101 #endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698