Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:01
The new copyright header (>= 2014 IIRC) doesn't ha
Ruud van Asseldonk
2015/09/21 10:10:05
You are right. Fixed.
| |
| 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_ALLOCATION_CONTEXT_H_ | |
| 6 #define BASE_TRACE_EVENT_ALLOCATION_CONTEXT_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/atomicops.h" | |
| 12 #include "base/base_export.h" | |
| 13 #include "base/trace_event/trace_log.h" | |
| 14 | |
| 15 namespace base { | |
| 16 namespace trace_event { | |
| 17 | |
| 18 // The allocation context is attached to every allocation when heap profiling | |
| 19 // is enabled. It is generated by the |AllocationContextManager| which keeps | |
| 20 // stacks of context in TLS. The manager is initialized lazily. Every time heap | |
| 21 // profiling is enabled, a global generation counter is incremented. This | |
| 22 // allows for context left over from a previous profiling session to be | |
| 23 // discarded. | |
| 24 | |
| 25 // A simple stack of |const char*| that unlike |std::stack| allows iterating | |
| 26 // the stack and guards for underflow. | |
| 27 class BASE_EXPORT Stack { | |
| 28 public: | |
| 29 typedef std::vector<const char*>::const_reverse_iterator ConstIterator; | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:01
should this be named just const_iterator (see reas
Primiano Tucci (use gerrit)
2015/09/18 16:11:14
AH also I'd probably encapsulate the const char* i
Ruud van Asseldonk
2015/09/21 10:10:05
The name of the iterator type is irrelevant to ran
| |
| 30 | |
| 31 Stack(); | |
| 32 ~Stack(); | |
| 33 | |
| 34 inline ConstIterator Top() const { return stack_.rbegin(); } | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:01
should you just stick with begin() and end() names
Ruud van Asseldonk
2015/09/21 10:10:05
I don’t use the range-based for because I need to
| |
| 35 inline ConstIterator Bottom() const { return stack_.rend(); } | |
| 36 | |
| 37 inline void Push(const char* item) { stack_.push_back(item); } | |
| 38 inline void Pop() { | |
| 39 if (!stack_.empty()) | |
| 40 stack_.pop_back(); | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 std::vector<const char*> stack_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(Stack); | |
| 47 }; | |
| 48 | |
| 49 class BASE_EXPORT AllocationContext { | |
| 50 // TODO(ruuda): Fill this in a follow-up CL. | |
| 51 }; | |
| 52 | |
| 53 class BASE_EXPORT AllocationContextManager { | |
| 54 public: | |
| 55 // Globally enables or disables capturing allocation context. | |
| 56 static void set_capture_enabled(bool enabled); | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:01
nit: SetCaptureEnabled. lower_with_under is only f
Ruud van Asseldonk
2015/09/21 10:10:05
Done. Should it be |SetCaptureEnabled| for the set
Primiano Tucci (use gerrit)
2015/09/22 10:15:00
You will hate this answer but... yes :)
| |
| 57 | |
| 58 // Returns whether capturing allocation context is enabled globally. | |
| 59 inline static bool capture_enabled() { | |
| 60 // A little lag after heap profiling is enabled or disabled is fine, it is | |
| 61 // more important that the check is as cheap as possible when capturing is | |
| 62 // not enabled, so issue no memory barrier. | |
| 63 return subtle::NoBarrier_Load(&capture_enabled_) != 0; | |
| 64 } | |
| 65 | |
| 66 // Returns the thread-local allocation context manager for the current | |
| 67 // generation, creating it if it does not exist. | |
| 68 static AllocationContextManager* GetInstance(); | |
| 69 | |
| 70 // Returns the pseudo stack where frames are |TRACE_EVENT| names. These macros | |
| 71 // automatically push and pop, the stack should not be modified manually. | |
| 72 inline Stack& pseudo_stack() { return pseudo_stack_; } | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:01
as a convention (see [1]) we allow either const re
Ruud van Asseldonk
2015/09/21 10:10:05
Done for the pointer. No, I need mutability to be
| |
| 73 | |
| 74 inline void SetContextField(const char* key, const char* value) { | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:00
I'd remove "Field" from the name (also below), the
Ruud van Asseldonk
2015/09/21 10:10:05
Naming is hard ... I would like to call a snapshot
| |
| 75 context_[key] = value; | |
| 76 } | |
| 77 | |
| 78 inline void UnsetContextField(const char* key) { | |
| 79 auto pos = context_.find(key); | |
| 80 if (pos != context_.end()) | |
| 81 context_.erase(pos); | |
| 82 } | |
| 83 | |
| 84 // Returns a snapshot of the current context. | |
| 85 AllocationContext GetContext() const; | |
| 86 | |
| 87 ~AllocationContextManager(); | |
| 88 | |
| 89 private: | |
| 90 AllocationContextManager(int32_t generation); | |
| 91 | |
| 92 // The context in this manager is only valid when its generation matches the | |
| 93 // current generation. | |
| 94 const int32_t generation_; | |
| 95 | |
| 96 // The pseudo stack where frames are |TRACE_EVENT| names. | |
| 97 Stack pseudo_stack_; | |
| 98 | |
| 99 // A dictionary of arbitrary context. | |
| 100 std::map<const char*, const char*> context_; | |
|
Primiano Tucci (use gerrit)
2015/09/18 16:10:00
I'd probably use base/containers/small_map.h here,
Ruud van Asseldonk
2015/09/21 10:10:05
Done. The header warns about increased code size t
Primiano Tucci (use gerrit)
2015/09/22 10:15:00
Well, this is going to be in the middle of each in
| |
| 101 | |
| 102 static subtle::Atomic32 capture_enabled_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(AllocationContextManager); | |
| 105 }; | |
| 106 | |
| 107 } // namespace trace_event | |
| 108 } // namespace base | |
| 109 | |
| 110 #endif // BASE_TRACE_EVENT_ALLOCATION_CONTEXT_H_ | |
| OLD | NEW |