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

Side by Side Diff: base/debug/scoped_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 a brain****, may even compile now. Created 4 years, 4 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_HEAP_USAGE_H_
6 #define BASE_DEBUG_SCOPED_HEAP_USAGE_H_
7
8 #include <stdint.h>
9
10 #include "base/allocator/features.h"
11
12 namespace base {
13 namespace debug {
14
15 // By keeping a tally on heap operations, it's possible to track:
16 // - the number of alloc/free operations (realloc is one of each?)
17 // - the number of bytes allocated/freed.
18 // - the number of estimated bytes of heap overhead used.
19 // - the high-watermark amount of bytes allocated in the scope.
20 // This in turn allows measuring the memory usage and memory usage churn over
21 // a scope. Note that scopes must be cleanly nested, and each scope must be
22 // destroyed on the thread where it's created.
23 // TODO(siggi): Should this perhaps leave the class declaration, cut the
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 Yeah I think you really just need this in the Init
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
24 // implementation and return zeros? That would make it easier to use
25 // as there wouldn't be a need to ifdef every use of the class.
26 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
27 class ScopedHeapUsage {
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 After reading all this CL I think it would help if
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 you could probably use a threadchecker to check in
Sigurður Ásgeirsson 2016/09/01 15:18:18 Good idea, done.
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
28 public:
29 struct AllocatorUsage {
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 Similarly I'd put thread in the name here as well.
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
30 // The cumulative number of allocation operations.
31 uint64_t alloc_ops;
32 // The cumulative number of allocated bytes. Where available, this is
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 nit: newlines here and below between comments and
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
33 // inclusive heap padding and estimated or actual heap overhead.
34 uint64_t alloc_bytes;
35 // Where available, cumulative number of heap padding heap
36 // and overhead bytes.
37 uint64_t alloc_overhead_bytes;
38
39 // The cumulative number of free operations.
40 uint64_t free_ops;
41 // The cumulative number of bytes freed.
42 // Only recorded if the underlying heap shim can return the size of an
43 // allocation.
44 uint64_t free_bytes;
45
46 // The maximal value of alloc_bytes - free_bytes seen for this thread.
47 // Only recorded if the underlying heap shim supports returning the size of
48 // an allocation.
49 uint64_t max_allocated_bytes;
50 };
51
52 ScopedHeapUsage();
53 ~ScopedHeapUsage();
54
55 const AllocatorUsage& usage_at_creation() const { return usage_at_creation_; }
56
57 // Returns this thread's current allocator usage.
58 static AllocatorUsage Now();
59
60 // Initializes the underlying heap shim. Must be called zero or one times.
61 static void Initialize();
62
63 protected:
64 // Exposed for testing only - note that it's safe to re-Initialize() after
65 // tearing down in tests.
66 static void TearDownForTesting();
67
68 private:
69 // The allocator usage for this thread, if any.
70 AllocatorUsage* thread_usage_;
71
72 // The allocator usage captured at creation of this instance.
73 AllocatorUsage usage_at_creation_;
74 };
75 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
76
77 } // namespace debug
78 } // namespace base
79
80 #endif // BASE_DEBUG_SCOPED_HEAP_USAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698