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

Side by Side Diff: base/trace_event/category_registry.h

Issue 2495173002: tracing: fix races in CategoryRegistry (Closed)
Patch Set: . Created 4 years, 1 month 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
« no previous file with comments | « no previous file | base/trace_event/category_registry.cc » ('j') | base/trace_event/trace_log.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_TRACE_EVENT_CATEGORY_H_ 5 #ifndef BASE_TRACE_EVENT_CATEGORY_H_
6 #define BASE_TRACE_EVENT_CATEGORY_H_ 6 #define BASE_TRACE_EVENT_CATEGORY_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include "base/base_export.h" 11 #include "base/base_export.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 13
14 namespace base { 14 namespace base {
15 namespace trace_event { 15 namespace trace_event {
16 16
17 struct TraceCategory; 17 struct TraceCategory;
18 class TraceCategoryTest; 18 class TraceCategoryTest;
19 class TraceLog; 19 class TraceLog;
20 20
21 // Keeps track of the state of all tracing categories. The reason why this 21 // Allows fast and thread-safe acces to the state of all tracing categories.
Lei Zhang 2016/11/21 19:11:13 (Maybe later since it's already in CQ) typo: acces
22 // is a fully static class with global state is to allow to statically define 22 // All the methods in this class can be concurrently called on multiple threads,
23 // known categories as global linker-initialized structs, without requiring 23 // unless otherwise noted (e.g., GetOrCreateCategoryLocked).
24 // static initializers. 24 // The reason why this is a fully static class with global state is to allow to
25 // statically define known categories as global linker-initialized structs,
26 // without requiring static initializers.
25 class BASE_EXPORT CategoryRegistry { 27 class BASE_EXPORT CategoryRegistry {
26 public: 28 public:
27 // Allows for-each iterations over a slice of the categories array. 29 // Allows for-each iterations over a slice of the categories array.
28 class Range { 30 class Range {
29 public: 31 public:
30 Range(TraceCategory* begin, TraceCategory* end) : begin_(begin), end_(end) { 32 Range(TraceCategory* begin, TraceCategory* end) : begin_(begin), end_(end) {
31 DCHECK_LE(begin, end); 33 DCHECK_LE(begin, end);
32 } 34 }
33 TraceCategory* begin() const { return begin_; } 35 TraceCategory* begin() const { return begin_; }
34 TraceCategory* end() const { return end_; } 36 TraceCategory* end() const { return end_; }
(...skipping 10 matching lines...) Expand all
45 47
46 // Returns a category entry from the Category.state_ptr() pointer. 48 // Returns a category entry from the Category.state_ptr() pointer.
47 // TODO(primiano): trace macros should just keep a pointer to the entire 49 // TODO(primiano): trace macros should just keep a pointer to the entire
48 // TraceCategory, not just the enabled state pointer. That would remove the 50 // TraceCategory, not just the enabled state pointer. That would remove the
49 // need for this function and make everything cleaner at no extra cost (as 51 // need for this function and make everything cleaner at no extra cost (as
50 // long as the |state_| is the first field of the struct, which can be 52 // long as the |state_| is the first field of the struct, which can be
51 // guaranteed via static_assert, see TraceCategory ctor). 53 // guaranteed via static_assert, see TraceCategory ctor).
52 static const TraceCategory* GetCategoryByStatePtr( 54 static const TraceCategory* GetCategoryByStatePtr(
53 const uint8_t* category_state); 55 const uint8_t* category_state);
54 56
57 // Returns a category from its name or nullptr if not found.
58 // The output |category| argument is an undefinitely lived pointer to the
59 // TraceCategory owned by the registry. TRACE_EVENTx macros will cache this
60 // pointer and use it for checks in their fast-paths.
61 static TraceCategory* GetCategoryByName(const char* category_name);
62
55 static bool IsBuiltinCategory(const TraceCategory*); 63 static bool IsBuiltinCategory(const TraceCategory*);
56 64
57 private: 65 private:
58 friend class TraceCategoryTest; 66 friend class TraceCategoryTest;
59 friend class TraceLog; 67 friend class TraceLog;
68 using CategoryInitializerFn = void (*)(TraceCategory*);
60 69
61 // Only for debugging/testing purposes, is a no-op on release builds. 70 // Only for debugging/testing purposes, is a no-op on release builds.
62 static void Initialize(); 71 static void Initialize();
63 72
64 // Resets the state of all categories, to clear up the state between tests. 73 // Resets the state of all categories, to clear up the state between tests.
65 static void ResetForTesting(); 74 static void ResetForTesting();
66 75
67 // The output |category| argument is an undefinitely lived pointer to the 76 // Used to get/create a category in the slow-path. If the category exists
68 // TraceCategory owned by the registry. TRACE_EVENTx macros will cache this 77 // already, this has the same effect of GetCategoryByName and returns false.
69 // pointer and use it for checks in their fast-paths. 78 // If not, a new category is created and the CategoryInitializerFn is invoked
70 // Returns false if the category was already present, true if the category 79 // before retuning true. The caller must guarantee serialization: either call
71 // has just been added and hence requires initialization. 80 // this method from a single thread or hold a lock when calling this.
72 static bool GetOrCreateCategoryByName(const char* category_name, 81 static bool GetOrCreateCategoryLocked(const char* category_name,
73 TraceCategory** category); 82 CategoryInitializerFn,
83 TraceCategory**);
74 84
75 // Allows to iterate over the valid categories in a for-each loop. 85 // Allows to iterate over the valid categories in a for-each loop.
76 // This includes builtin categories such as __metadata. 86 // This includes builtin categories such as __metadata.
77 static Range GetAllCategories(); 87 static Range GetAllCategories();
78 }; 88 };
79 89
80 } // namespace trace_event 90 } // namespace trace_event
81 } // namespace base 91 } // namespace base
82 92
83 #endif // BASE_TRACE_EVENT_CATEGORY_H_ 93 #endif // BASE_TRACE_EVENT_CATEGORY_H_
OLDNEW
« no previous file with comments | « no previous file | base/trace_event/category_registry.cc » ('j') | base/trace_event/trace_log.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698