@@ -90,6 +90,26 @@ bool validate(const ContextRoot *Root) {
9090 }
9191 return true ;
9292}
93+
94+ inline ContextNode *allocContextNode (char *Place, GUID Guid,
95+ uint32_t NrCounters, uint32_t NrCallsites,
96+ ContextNode *Next = nullptr ) {
97+ assert (reinterpret_cast <uint64_t >(Place) % ExpectedAlignment == 0 );
98+ return new (Place) ContextNode (Guid, NrCounters, NrCallsites, Next);
99+ }
100+
101+ void resetContextNode (ContextNode &Node) {
102+ // FIXME(mtrofin): this is std::memset, which we can probably use if we
103+ // drop/reduce the dependency on sanitizer_common.
104+ for (uint32_t I = 0 ; I < Node.counters_size (); ++I)
105+ Node.counters ()[I] = 0 ;
106+ for (uint32_t I = 0 ; I < Node.callsites_size (); ++I)
107+ for (auto *Next = Node.subContexts ()[I]; Next; Next = Next->next ())
108+ resetContextNode (*Next);
109+ }
110+
111+ void onContextEnter (ContextNode &Node) { ++Node.counters ()[0 ]; }
112+
93113} // namespace
94114
95115// the scratch buffer - what we give when we can't produce a real context (the
@@ -134,27 +154,9 @@ void Arena::freeArenaList(Arena *&A) {
134154 A = nullptr ;
135155}
136156
137- inline ContextNode *ContextNode::alloc (char *Place, GUID Guid,
138- uint32_t NrCounters,
139- uint32_t NrCallsites,
140- ContextNode *Next) {
141- assert (reinterpret_cast <uint64_t >(Place) % ExpectedAlignment == 0 );
142- return new (Place) ContextNode (Guid, NrCounters, NrCallsites, Next);
143- }
144-
145- void ContextNode::reset () {
146- // FIXME(mtrofin): this is std::memset, which we can probably use if we
147- // drop/reduce the dependency on sanitizer_common.
148- for (uint32_t I = 0 ; I < NrCounters; ++I)
149- counters ()[I] = 0 ;
150- for (uint32_t I = 0 ; I < NrCallsites; ++I)
151- for (auto *Next = subContexts ()[I]; Next; Next = Next->Next )
152- Next->reset ();
153- }
154-
155157// If this is the first time we hit a callsite with this (Guid) particular
156158// callee, we need to allocate.
157- ContextNode *getCallsiteSlow (uint64_t Guid, ContextNode **InsertionPoint,
159+ ContextNode *getCallsiteSlow (GUID Guid, ContextNode **InsertionPoint,
158160 uint32_t NrCounters, uint32_t NrCallsites) {
159161 auto AllocSize = ContextNode::getAllocSize (NrCounters, NrCallsites);
160162 auto *Mem = __llvm_ctx_profile_current_context_root->CurrentMem ;
@@ -169,8 +171,8 @@ ContextNode *getCallsiteSlow(uint64_t Guid, ContextNode **InsertionPoint,
169171 Mem->allocateNewArena (getArenaAllocSize (AllocSize), Mem);
170172 AllocPlace = Mem->tryBumpAllocate (AllocSize);
171173 }
172- auto *Ret = ContextNode::alloc (AllocPlace, Guid, NrCounters, NrCallsites,
173- *InsertionPoint);
174+ auto *Ret = allocContextNode (AllocPlace, Guid, NrCounters, NrCallsites,
175+ *InsertionPoint);
174176 *InsertionPoint = Ret;
175177 return Ret;
176178}
@@ -224,7 +226,7 @@ ContextNode *__llvm_ctx_profile_get_context(void *Callee, GUID Guid,
224226 " Context: %p, Asked: %lu %u %u, Got: %lu %u %u \n " ,
225227 Ret, Guid, NrCallsites, NrCounters, Ret->guid (),
226228 Ret->callsites_size (), Ret->counters_size ());
227- Ret-> onEntry ( );
229+ onContextEnter (*Ret );
228230 return Ret;
229231}
230232
@@ -241,8 +243,8 @@ void setupContext(ContextRoot *Root, GUID Guid, uint32_t NrCounters,
241243 auto *M = Arena::allocateNewArena (getArenaAllocSize (Needed));
242244 Root->FirstMemBlock = M;
243245 Root->CurrentMem = M;
244- Root->FirstNode = ContextNode::alloc (M->tryBumpAllocate (Needed), Guid,
245- NrCounters, NrCallsites);
246+ Root->FirstNode = allocContextNode (M->tryBumpAllocate (Needed), Guid,
247+ NrCounters, NrCallsites);
246248 AllContextRoots.PushBack (Root);
247249}
248250
@@ -254,7 +256,7 @@ ContextNode *__llvm_ctx_profile_start_context(
254256 }
255257 if (Root->Taken .TryLock ()) {
256258 __llvm_ctx_profile_current_context_root = Root;
257- Root->FirstNode -> onEntry ( );
259+ onContextEnter (* Root->FirstNode );
258260 return Root->FirstNode ;
259261 }
260262 // If this thread couldn't take the lock, return scratch context.
@@ -281,13 +283,13 @@ void __llvm_ctx_profile_start_collection() {
281283 for (auto *Mem = Root->FirstMemBlock ; Mem; Mem = Mem->next ())
282284 ++NrMemUnits;
283285
284- Root->FirstNode -> reset ( );
286+ resetContextNode (* Root->FirstNode );
285287 }
286288 __sanitizer::Printf (" [ctxprof] Initial NrMemUnits: %zu \n " , NrMemUnits);
287289}
288290
289- bool __llvm_ctx_profile_fetch (
290- void *Data, bool (*Writer)(void *W, const __ctx_profile:: ContextNode &)) {
291+ bool __llvm_ctx_profile_fetch (void *Data,
292+ bool (*Writer)(void *W, const ContextNode &)) {
291293 assert (Writer);
292294 __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock (
293295 &AllContextsMutex);
0 commit comments