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

Side by Side Diff: base/debug/activity_tracker.cc

Issue 1980743002: Track thread activities in order to diagnose hangs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@readwrite-mmf
Patch Set: fixed thread-id discovery Created 4 years, 6 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
« no previous file with comments | « base/debug/activity_tracker.h ('k') | base/debug/activity_tracker_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/debug/activity_tracker.h"
6
7 #include <atomic>
8
9 #include "base/debug/stack_trace.h"
10 #include "base/feature_list.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/memory_mapped_file.h"
14 #include "base/logging.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/metrics/histogram_macros.h"
18 #include "base/pending_task.h"
19 #include "base/process/process.h"
20 #include "base/process/process_handle.h"
21 #include "base/stl_util.h"
22 #include "base/strings/string_util.h"
23 #include "base/threading/platform_thread.h"
24
25 namespace base {
26 namespace debug {
27
28 namespace {
29
30 // A number that identifies the memory as having been initialized. It's
31 // arbitrary but happens to be the first 8 bytes of SHA1(ThreadActivityTracker).
32 // A version number is added on so that major structure changes won't try to
33 // read an older version (since the cookie won't match).
34 const uint64_t kHeaderCookie = 0xC0029B240D4A3092ULL + 1; // v1
35
36 // The minimum depth a stack should support.
37 const int kMinStackDepth = 2;
38
39 } // namespace
40
41
42 #if !defined(OS_NACL) // NACL doesn't support any kind of file access in build.
43 void SetupGlobalActivityTrackerFieldTrial(const FilePath& file) {
44 const Feature kActivityTrackerFeature{
45 "ActivityTracking", FEATURE_DISABLED_BY_DEFAULT
46 };
47
48 if (!base::FeatureList::IsEnabled(kActivityTrackerFeature))
49 return;
50
51 // TODO(bcwhite): Adjust these numbers once there is real data to show
52 // just how much of an arena is necessary.
53 const size_t kMemorySize = 1 << 20; // 1 MiB
54 const int kStackDepth = 4;
55 const uint64_t kAllocatorId = 0;
56 const char kAllocatorName[] = "ActivityTracker";
57
58 GlobalActivityTracker::CreateWithFile(
59 file.AddExtension(PersistentMemoryAllocator::kFileExtension),
60 kMemorySize, kAllocatorId, kAllocatorName, kStackDepth);
61 }
62 #endif // !defined(OS_NACL)
63
64
65 // This information is kept for every thread that is tracked. It is filled
66 // the very first time the thread is seen. All fields must be of exact sizes
67 // so there is no issue moving between 32 and 64-bit builds.
68 struct ThreadActivityTracker::Header {
69 // This unique number indicates a valid initialization of the memory.
70 uint64_t cookie;
71
72 // The process-id and thread-id to which this data belongs. These identifiers
73 // are not guaranteed to mean anything but are unique, in combination, among
74 // all active trackers.
75 int64_t process_id;
76 union {
77 int64_t as_id;
78 #if defined(OS_WIN)
79 // On Windows, the handle itself is often a pseudo-handle with a common
80 // value meaning "this thread" and so the thread-id is used. The former
81 // is can be converted to a thread-id with a system call.
82 PlatformThreadId as_tid;
83 #elif defined(OS_POSIX)
84 // On Posix, the handle is always a unique identifier so no conversion
85 // needs to be done. However, it's value is officially opaque so there
86 // is no one correct way to convert it to a numerical identifier.
87 PlatformThreadHandle::Handle as_handle;
88 #endif
89 } thread_ref;
90
91 // The start-time and start-ticks when the data was created. Each activity
92 // record has a |time_internal| value that can be converted to a "wall time"
93 // with these two values.
94 int64_t start_time;
95 int64_t start_ticks;
96
97 // The number of Activity slots in the data.
98 uint32_t stack_slots;
99
100 // The current depth of the stack. This may be greater than the number of
101 // slots. If the depth exceeds the number of slots, the newest entries
102 // won't be recorded.
103 std::atomic<uint32_t> current_depth;
104
105 // A memory location used to indicate if changes have been made to the stack
106 // that would invalidate an in-progress read of its contents. The active
107 // tracker will zero the value whenever something gets popped from the
108 // stack. A monitoring tracker can write a non-zero value here, copy the
109 // stack contents, and read the value to know, if it is still non-zero, that
110 // the contents didn't change while being copied.
111 std::atomic<uint32_t> stack_unchanged;
112
113 // The name of the thread (up to a maximum length). Dynamic-length names
114 // are not practical since the memory has to come from the same persistent
115 // allocator that holds this structure and to which this object has no
116 // reference.
117 char thread_name[32];
118 };
119
120 // It doesn't matter what is contained in this (though it will be all zeros)
121 // as only the address of it is important.
122 const ThreadActivityTracker::ActivityData
123 ThreadActivityTracker::kNullActivityData = {};
124
125 ThreadActivityTracker::ActivityData
126 ThreadActivityTracker::ActivityData::ForThread(
127 const PlatformThreadHandle& handle) {
128 // Header already has a conversion union; reuse that.
129 ThreadActivityTracker::Header header;
130 header.thread_ref.as_id = 0; // Zero the union in case other is smaller.
131 #if defined(OS_WIN)
132 header.thread_ref.as_tid = ::GetThreadId(handle.platform_handle());
133 #elif defined(OS_POSIX)
134 header.thread_ref.as_handle = handle.platform_handle();
135 #endif
136 return ForThread(header.thread_ref.as_id);
137 }
138
139 ThreadActivityTracker::ActivitySnapshot::ActivitySnapshot() {}
140 ThreadActivityTracker::ActivitySnapshot::~ActivitySnapshot() {}
141
142
143 ThreadActivityTracker::ThreadActivityTracker(void* base, size_t size)
144 : header_(static_cast<Header*>(base)),
145 stack_(reinterpret_cast<Activity*>(reinterpret_cast<char*>(base) +
146 sizeof(Header))),
147 stack_slots_(
148 static_cast<uint32_t>((size - sizeof(Header)) / sizeof(Activity))) {
149 DCHECK(thread_checker_.CalledOnValidThread());
150
151 // Verify the parameters but fail gracefully if they're not valid so that
152 // production code based on external inputs will not crash. IsValid() will
153 // return false in this case.
154 if (!base ||
155 // Ensure there is enough space for the header and at least a few records.
156 size < sizeof(Header) + kMinStackDepth * sizeof(Activity) ||
157 // Ensure that the |stack_slots_| calculation didn't overflow.
158 (size - sizeof(Header)) / sizeof(Activity) >
159 std::numeric_limits<uint32_t>::max()) {
160 NOTREACHED();
161 return;
162 }
163
164 // Ensure that the thread reference doesn't exceed the size of the ID number.
165 // This won't compile at the global scope because Header is a private struct.
166 static_assert(
167 sizeof(header_->thread_ref) == sizeof(header_->thread_ref.as_id),
168 "PlatformThreadHandle::Handle is too big to hold in 64-bit ID");
169
170 // Provided memory should either be completely initialized or all zeros.
171 if (header_->cookie == 0) {
172 // This is a new file. Double-check other fields and then initialize.
173 DCHECK_EQ(0, header_->process_id);
174 DCHECK_EQ(0, header_->thread_ref.as_id);
175 DCHECK_EQ(0, header_->start_time);
176 DCHECK_EQ(0, header_->start_ticks);
177 DCHECK_EQ(0U, header_->stack_slots);
178 DCHECK_EQ(0U, header_->current_depth.load(std::memory_order_relaxed));
179 DCHECK_EQ(0U, header_->stack_unchanged.load(std::memory_order_relaxed));
180 DCHECK_EQ(0, stack_[0].time_internal);
181 DCHECK_EQ(0U, stack_[0].call_stack[0]);
182 DCHECK_EQ(0U, stack_[0].data.task.sequence_id);
183
184 header_->process_id = GetCurrentProcId();
185 #if defined(OS_WIN)
186 header_->thread_ref.as_tid = PlatformThread::CurrentId();
187 #elif defined(OS_POSIX)
188 header_->thread_ref.as_handle =
189 PlatformThread::CurrentHandle().platform_handle();
190 #endif
191 header_->start_time = base::Time::Now().ToInternalValue();
192 header_->start_ticks = base::TimeTicks::Now().ToInternalValue();
193 header_->stack_slots = stack_slots_;
194 strlcpy(header_->thread_name, PlatformThread::GetName(),
195 sizeof(header_->thread_name));
196 header_->cookie = kHeaderCookie;
brucedawson 2016/06/17 22:46:55 Is this supposed to be the last thing initialized?
bcwhite 2016/06/20 13:48:40 Since at this point, no data is shared elsewhere,
brucedawson 2016/06/21 00:41:44 Makes sense.
197 valid_ = true;
198 DCHECK(IsValid());
199 } else {
200 // This is a file with existing data. Perform basic consistency checks.
201 valid_ = true;
202 valid_ = IsValid();
203 }
204 }
205
206 ThreadActivityTracker::~ThreadActivityTracker() {}
207
208 void ThreadActivityTracker::PushActivity(const void* source,
209 ActivityType type,
210 const ActivityData& data) {
211 // A thread-checker creates a lock to check the thread-id which means
212 // re-entry into this code if lock acquisitions are being tracked.
213 DCHECK(type == ACT_LOCK_ACQUIRE || thread_checker_.CalledOnValidThread());
214
215 // Get the current depth of the stack. No access to other memory guarded
216 // by this variable is done here so a "relaxed" load is acceptable.
217 uint32_t depth = header_->current_depth.load(std::memory_order_relaxed);
218
219 // Handle the case where the stack depth has exceeded the storage capacity.
220 // Extra entries will be lost leaving only the base of the stack.
221 if (depth >= stack_slots_) {
222 // Since no other threads modify the data, no compare/exchange is needed.
223 // Since no other memory is being modified, a "relaxed" store is acceptable.
224 header_->current_depth.store(depth + 1, std::memory_order_relaxed);
225 return;
226 }
227
228 // Get a pointer to the next activity and load it. No atomicity is required
229 // here because the memory is known only to this thread. It will be made
230 // known to other threads once the depth is incremented.
231 Activity* activity = &stack_[depth];
232 activity->time_internal = base::TimeTicks::Now().ToInternalValue();
233 activity->activity_type = type;
234 activity->data = data;
235
236 #if defined(SYZYASAN)
237 // Create a stacktrace from the current location and get the addresses.
238 StackTrace stack_trace;
239 size_t stack_depth;
240 const void* const* stack_addrs = stack_trace.Addresses(&stack_depth);
241 // Copy the stack addresses, ignoring the first one (here).
242 size_t i;
243 for (i = 1; i < stack_depth && i < kActivityCallStackSize; ++i) {
244 activity->call_stack[i - 1] = reinterpret_cast<uintptr_t>(stack_addrs[i]);
245 }
246 activity->call_stack[i - 1] = 0;
247 #else
248 activity->call_stack[0] = reinterpret_cast<uintptr_t>(source);
249 activity->call_stack[1] = 0;
250 #endif
251
252 // Save the incremented depth. Because this guards |activity| memory filled
253 // above that may be read by another thread once the recorded depth changes,
254 // a "release" store is required.
255 header_->current_depth.store(depth + 1, std::memory_order_release);
brucedawson 2016/06/17 22:46:55 Good analysis.
bcwhite 2016/06/20 13:48:40 Acknowledged.
256 }
257
258 void ThreadActivityTracker::ChangeActivity(ActivityType type,
259 const ActivityData& data) {
260 DCHECK(thread_checker_.CalledOnValidThread());
261 DCHECK(type != ACT_NULL || &data != &kNullActivityData);
262
263 // Get the current depth of the stack.
264 uint32_t depth = header_->current_depth.load(std::memory_order_relaxed);
brucedawson 2016/06/17 22:46:55 I'm confused as to why this isn't memory_order_acq
bcwhite 2016/06/20 13:48:40 My reasoning was that because |depth| is used to d
brucedawson 2016/06/21 00:41:44 The best way to make that explicit would be to use
bcwhite 2016/06/21 12:50:44 Cool. Done.
265 DCHECK_LT(0U, depth);
266
267 // Update the information if it is being recorded (i.e. within slot limit).
268 if (depth <= stack_slots_) {
269 Activity* activity = &stack_[depth - 1];
270
271 if (type != ACT_NULL) {
272 DCHECK_EQ(activity->activity_type & ACT_CATEGORY_MASK,
273 type & ACT_CATEGORY_MASK);
274 activity->activity_type = type;
275 }
276
277 if (&data != &kNullActivityData)
278 activity->data = data;
279 }
280 }
281
282 void ThreadActivityTracker::PopActivity() {
283 // Do an atomic decrement of the depth. No changes to stack entries guarded
284 // by this variable are done here so a "relaxed" operation is acceptable.
285 // |depth| will receive the value BEFORE it was modified.
286 uint32_t depth =
287 header_->current_depth.fetch_sub(1, std::memory_order_relaxed);
288
289 // Validate that everything is running correctly.
290 DCHECK_LT(0U, depth);
291
292 // The stack has shrunk meaning that some other thread trying to copy the
293 // contents for reporting purposes could get bad data. That thread would
294 // have written a non-zero value into |stack_unchanged|; clearing it here
295 // will let that thread detect that something did change. This needs to
296 // happen after the atomic |depth| operation above so a "release" store
297 // is required.
298 header_->stack_unchanged.store(0, std::memory_order_release);
brucedawson 2016/06/17 22:46:55 Is any guarantee of the timing of this write neede
bcwhite 2016/06/20 13:48:40 It's safe because the contents being guarded haven
brucedawson 2016/06/21 00:41:44 Adding to the comment sounds good. It occurs to m
bcwhite 2016/06/21 12:50:44 I don't follow.
brucedawson 2016/06/21 17:16:56 With InterlockedIncrement you only get the desired
bcwhite 2016/06/21 17:23:31 Maybe the CST should be just acq_rel? According t
299 }
300
301 bool ThreadActivityTracker::IsValid() const {
302 if (header_->cookie != kHeaderCookie ||
303 header_->process_id == 0 ||
304 header_->thread_ref.as_id == 0 ||
305 header_->start_time == 0 ||
306 header_->start_ticks == 0 ||
307 header_->stack_slots != stack_slots_ ||
308 header_->thread_name[sizeof(header_->thread_name) - 1] != '\0') {
309 return false;
310 }
311
312 return valid_;
313 }
314
315 bool ThreadActivityTracker::Snapshot(ActivitySnapshot* output_snapshot) const {
316 DCHECK(output_snapshot);
317
318 // There is no "called on valid thread" check for this method as it can be
319 // called from other threads or even other processes. It is also the reason
320 // why atomic operations must be used in certain places above.
321
322 // It's possible for the data to change while reading it in such a way that it
323 // invalidates the read. Make several attempts but don't try forever.
324 const int kMaxAttempts = 10;
325 uint32_t depth;
326
327 // Stop here if the data isn't valid.
328 if (!IsValid())
329 return false;
330
331 // Start with an empty return stack.
332 output_snapshot->activity_stack.clear();
333
334 for (int attempt = 0; attempt < kMaxAttempts; ++attempt) {
335 // Remember the process and thread IDs to ensure they aren't replaced
336 // during the snapshot operation.
337 const int64_t starting_process_id = header_->process_id;
338 const int64_t starting_thread_id = header_->thread_ref.as_id;
339
340 // Write a non-zero value to |stack_unchanged| so it's possible to detect
341 // at the end that nothing has changed since copying the data began. A
342 // "cst" operation is required to ensure it occurs before everything else.
343 header_->stack_unchanged.store(1, std::memory_order_seq_cst);
344
345 // Fetching the current depth also "acquires" the contents of the stack.
346 depth = header_->current_depth.load(std::memory_order_acquire);
347 if (depth > 0) {
348 // Copy the existing contents. Memcpy is used for speed.
349 uint32_t count = std::min(depth, stack_slots_);
350 output_snapshot->activity_stack.resize(count);
351 memcpy(&output_snapshot->activity_stack[0], stack_,
352 count * sizeof(Activity));
353 }
354
355 // Retry if something changed during the copy. A "cst" operation ensures
356 // it must happen after all the above operations.
357 if (!header_->stack_unchanged.load(std::memory_order_seq_cst))
brucedawson 2016/06/17 22:46:55 I think that this is assuming that if the update o
bcwhite 2016/06/20 13:48:40 Possible but not a problem. Depth may have decrem
brucedawson 2016/06/21 00:41:44 Okay - I think that makes sense. It's too bad that
bcwhite 2016/06/21 12:50:44 Luckily it is only done by whatever thread is doin
358 continue;
359
360 // Stack copied. Record it's full depth.
361 output_snapshot->activity_stack_depth = depth;
362
363 // TODO(bcwhite): Snapshot other things here.
364
365 // Get the general thread information.
366 output_snapshot->process_id = header_->process_id;
367 output_snapshot->thread_id = header_->thread_ref.as_id;
368 output_snapshot->thread_name =
369 std::string(header_->thread_name, sizeof(header_->thread_name) - 1);
370
371 // All characters of the thread-name buffer were copied so as to not break
372 // if the trailing NUL were missing. Now limit the length if the actual
373 // name is shorter.
374 output_snapshot->thread_name.resize(
375 strlen(output_snapshot->thread_name.c_str()));
376
377 // If the process or thread ID has changed then the tracker has exited and
378 // the memory reused by a new one. Try again.
379 if (output_snapshot->process_id != starting_process_id ||
380 output_snapshot->thread_id != starting_thread_id) {
381 continue;
382 }
383
384 // Only successful if the data is still valid once everything is done since
385 // it's possible for the thread to end somewhere in the middle and all its
386 // values become garbage.
387 if (!IsValid())
388 return false;
389
390 // Change all the timestamps in the activities from "ticks" to "wall" time.
391 const Time start_time = Time::FromInternalValue(header_->start_time);
392 const int64_t start_ticks = header_->start_ticks;
393 for (Activity& activity : output_snapshot->activity_stack) {
394 activity.time_internal =
395 (start_time +
396 TimeDelta::FromInternalValue(activity.time_internal - start_ticks))
397 .ToInternalValue();
398 }
399
400 // Success!
401 return true;
402 }
403
404 // Too many attempts.
405 return false;
406 }
407
408 // static
409 size_t ThreadActivityTracker::SizeForStackDepth(int stack_depth) {
410 return static_cast<size_t>(stack_depth) * sizeof(Activity) + sizeof(Header);
411 }
412
413
414 GlobalActivityTracker* GlobalActivityTracker::g_tracker_ = nullptr;
415
416 GlobalActivityTracker::ManagedActivityTracker::ManagedActivityTracker(
417 PersistentMemoryAllocator::Reference mem_reference,
418 void* base,
419 size_t size)
420 : ThreadActivityTracker(base, size),
421 mem_reference_(mem_reference),
422 mem_base_(base) {}
423
424 GlobalActivityTracker::ManagedActivityTracker::~ManagedActivityTracker() {
425 // The global |g_tracker_| must point to the owner of this class since all
426 // objects of this type must be destructed before |g_tracker_| can be changed
427 // (something that only occurs in tests).
428 DCHECK(g_tracker_);
429 g_tracker_->ReturnTrackerMemory(this);
430 }
431
432 void GlobalActivityTracker::CreateWithAllocator(
433 std::unique_ptr<PersistentMemoryAllocator> allocator,
434 int stack_depth) {
435 // There's no need to do anything with the result. It is self-managing.
436 GlobalActivityTracker* global_tracker =
437 new GlobalActivityTracker(std::move(allocator), stack_depth);
438 // Create a tracker for this thread since it is known.
439 global_tracker->CreateTrackerForCurrentThread();
440 }
441
442 #if !defined(OS_NACL)
443 // static
444 void GlobalActivityTracker::CreateWithFile(const FilePath& file_path,
445 size_t size,
446 uint64_t id,
447 StringPiece name,
448 int stack_depth) {
449 DCHECK(!file_path.empty());
450 DCHECK_GE(static_cast<uint64_t>(std::numeric_limits<int64_t>::max()), size);
451
452 // Create and map the file into memory and make it globally available.
453 std::unique_ptr<MemoryMappedFile> mapped_file(new MemoryMappedFile());
454 bool success =
455 mapped_file->Initialize(File(file_path,
456 File::FLAG_CREATE_ALWAYS | File::FLAG_READ |
457 File::FLAG_WRITE | File::FLAG_SHARE_DELETE),
458 {0, static_cast<int64_t>(size)},
459 MemoryMappedFile::READ_WRITE_EXTEND);
460 DCHECK(success);
461 CreateWithAllocator(WrapUnique(new FilePersistentMemoryAllocator(
462 std::move(mapped_file), size, id, name, false)),
463 stack_depth);
464 }
465 #endif // !defined(OS_NACL)
466
467 // static
468 void GlobalActivityTracker::CreateWithLocalMemory(size_t size,
469 uint64_t id,
470 StringPiece name,
471 int stack_depth) {
472 CreateWithAllocator(
473 WrapUnique(new LocalPersistentMemoryAllocator(size, id, name)),
474 stack_depth);
475 }
476
477 ThreadActivityTracker* GlobalActivityTracker::CreateTrackerForCurrentThread() {
478 DCHECK(!this_thread_tracker_.Get());
479
480 PersistentMemoryAllocator::Reference mem_reference = 0;
481 void* mem_base = nullptr;
482
483 // Get the current count of available memories, acquiring the array values.
484 int count = available_memories_count_.load(std::memory_order_acquire);
485 while (count > 0) {
486 // There is a memory block that was previously released (and zero'd) so
487 // just re-use that rather than allocating a new one. Use "acquire" so
488 // operations below can be re-ordered above.
489 mem_reference =
490 available_memories_[count - 1].load(std::memory_order_acquire);
491 DCHECK(mem_reference);
492
493 // Decrement the count indicating that the value has been taken. If this
494 // fails then something else, another thread doing push or pop, has changed
495 // the stack; retry if so.
496 // NOTE: |count| will be loaded with the existing value and affect the
497 // "while" condition.
498 if (!available_memories_count_.compare_exchange_weak(
499 count, count - 1, std::memory_order_acquire,
500 std::memory_order_acquire)) {
501 continue;
502 }
503
504 // Clear the value just read from the array so that the "push" operation
505 // knows there is no value there and will work correctly.
506 available_memories_[count - 1].store(0, std::memory_order_relaxed);
507
508 // Turn the reference back into one of the activity-tracker type.
509 mem_base = allocator_->GetAsObject<char>(mem_reference,
510 kTypeIdActivityTrackerFree);
511 DCHECK(mem_base);
512 DCHECK_LE(stack_memory_size_, allocator_->GetAllocSize(mem_reference));
513 allocator_->ChangeType(mem_reference, kTypeIdActivityTracker,
514 kTypeIdActivityTrackerFree);
515
516 // Success.
517 break;
518 }
519
520 // Handle the case where no previously-used memories are available.
521 if (count == 0) {
522 // Allocate a block of memory from the persistent segment.
523 mem_reference =
524 allocator_->Allocate(stack_memory_size_, kTypeIdActivityTracker);
525 if (mem_reference) {
526 // Success. Convert the reference to an actual memory address.
527 mem_base =
528 allocator_->GetAsObject<char>(mem_reference, kTypeIdActivityTracker);
529 // Make the allocation iterable so it can be found by other processes.
530 allocator_->MakeIterable(mem_reference);
531 } else {
532 // Failure. This shouldn't happen.
533 NOTREACHED();
534 // But if it does, probably because the allocator wasn't given enough
535 // memory to satisfy all possible requests, handle it gracefully by
536 // allocating the required memory from the heap.
537 mem_base = new char[stack_memory_size_];
538 memset(mem_base, 0, stack_memory_size_);
539 // Report the thread-count at which the allocator was full so that the
540 // failure can be seen and underlying memory resized appropriately.
541 UMA_HISTOGRAM_COUNTS_1000(
542 "UMA.ActivityTracker.ThreadTrackers.MemLimit",
543 thread_tracker_count_.load(std::memory_order_relaxed));
544 }
545 }
546
547 // Create a tracker with the acquired memory and set it as the tracker
548 // for this particular thread in thread-local-storage.
549 DCHECK(mem_base);
550 ManagedActivityTracker* tracker =
551 new ManagedActivityTracker(mem_reference, mem_base, stack_memory_size_);
552 DCHECK(tracker->IsValid());
553 this_thread_tracker_.Set(tracker);
554 int old_count = thread_tracker_count_.fetch_add(1, std::memory_order_relaxed);
555
556 UMA_HISTOGRAM_ENUMERATION("UMA.ActivityTracker.ThreadTrackers.Count",
557 old_count + 1, kMaxThreadCount);
558 return tracker;
559 }
560
561 void GlobalActivityTracker::ReleaseTrackerForCurrentThreadForTesting() {
562 ThreadActivityTracker* tracker =
563 reinterpret_cast<ThreadActivityTracker*>(this_thread_tracker_.Get());
564 if (tracker) {
565 this_thread_tracker_.Free();
566 delete tracker;
567 }
568 }
569
570 GlobalActivityTracker::GlobalActivityTracker(
571 std::unique_ptr<PersistentMemoryAllocator> allocator,
572 int stack_depth)
573 : allocator_(std::move(allocator)),
574 stack_memory_size_(ThreadActivityTracker::SizeForStackDepth(stack_depth)),
575 this_thread_tracker_(&OnTLSDestroy),
576 thread_tracker_count_(0),
577 available_memories_count_(0) {
578 // Clear the available-memories array.
579 memset(available_memories_, 0, sizeof(available_memories_));
580
581 // Ensure the passed memory is valid and empty (iterator finds nothing).
582 uint32_t type;
583 DCHECK(!PersistentMemoryAllocator::Iterator(allocator_.get()).GetNext(&type));
584
585 // Ensure that there is no other global object and then make this one such.
586 DCHECK(!g_tracker_);
587 g_tracker_ = this;
588 }
589
590 GlobalActivityTracker::~GlobalActivityTracker() {
591 DCHECK_EQ(g_tracker_, this);
592 DCHECK_EQ(0, thread_tracker_count_.load(std::memory_order_relaxed));
593 g_tracker_ = nullptr;
594 }
595
596 void GlobalActivityTracker::ReturnTrackerMemory(
597 ManagedActivityTracker* tracker) {
598 PersistentMemoryAllocator::Reference mem_reference = tracker->mem_reference_;
599 void* mem_base = tracker->mem_base_;
600
601 // Zero the memory so that it is ready for use if needed again later. It's
602 // better to clear the memory now, when a thread is exiting, than to do it
603 // when it is first needed by a thread doing actual work.
604 memset(mem_base, 0, stack_memory_size_);
605
606 // Remove the destructed tracker from the set of known ones.
607 DCHECK_LE(1, thread_tracker_count_.load(std::memory_order_relaxed));
608 thread_tracker_count_.fetch_sub(1, std::memory_order_relaxed);
609
610 // Deal with the memory that was used by the tracker.
611 if (mem_reference) {
612 // The memory was within the persistent memory allocator. Change its type
613 // so that iteration won't find it.
614 allocator_->ChangeType(mem_reference, kTypeIdActivityTrackerFree,
615 kTypeIdActivityTracker);
616 // There is no way to free memory from a persistent allocator so instead
617 // push it on the internal list of available memory blocks.
618 while (true) {
619 // Get the existing count of available memories and ensure we won't
620 // burst the array. Acquire the values in the array.
621 int count = available_memories_count_.load(std::memory_order_acquire);
622 if (count >= kMaxThreadCount) {
623 NOTREACHED();
624 // Storage is full. Just forget about this memory. It won't be re-used
625 // but there's no real loss.
626 break;
627 }
628
629 // Write the reference of the memory being returned to this slot in the
630 // array. Empty slots have a value of zero so do an atomic compare-and-
631 // exchange to ensure that a race condition doesn't exist with another
632 // thread doing the same.
633 PersistentMemoryAllocator::Reference mem_expected = 0;
634 if (!available_memories_[count].compare_exchange_weak(
635 mem_expected, mem_reference, std::memory_order_release,
636 std::memory_order_relaxed)) {
637 continue; // Try again.
638 }
639
640 // Increment the count, releasing the value written to the array. This
641 // could fail if a simultaneous "pop" operation decremented the counter.
642 // If that happens, clear the array slot and start over. Do a "strong"
643 // exchange to avoid spurious retries that can occur with a "weak" one.
644 int expected = count; // Updated by compare/exchange.
645 if (!available_memories_count_.compare_exchange_strong(
646 expected, count + 1, std::memory_order_release,
647 std::memory_order_relaxed)) {
648 available_memories_[count].store(0, std::memory_order_relaxed);
649 continue;
650 }
651
652 // Count was successfully incremented to reflect the newly added value.
653 break;
654 }
655 } else {
656 // The memory was allocated from the process heap. This shouldn't happen
657 // because the persistent memory segment should be big enough for all
658 // thread stacks but it's better to support falling back to allocation
659 // from the heap rather than crash. Everything will work as normal but
660 // the data won't be persisted.
661 delete[] reinterpret_cast<char*>(mem_base);
662 }
663 }
664
665 // static
666 void GlobalActivityTracker::OnTLSDestroy(void* value) {
667 delete reinterpret_cast<ManagedActivityTracker*>(value);
668 }
669
670
671 ScopedActivity::ScopedActivity(const tracked_objects::Location& location,
672 uint8_t action,
673 uint32_t id,
674 uint32_t info)
675 : GlobalActivityTracker::ScopedThreadActivity(
676 location.program_counter(),
677 static_cast<ThreadActivityTracker::ActivityType>(
678 ThreadActivityTracker::ACT_GENERIC | action),
679 ThreadActivityTracker::ActivityData::ForGeneric(id, info),
680 /*lock_allowed=*/true),
681 id_(id) {
682 // The action must not affect the category bits of the activity type.
683 DCHECK_EQ(0, action & ThreadActivityTracker::ACT_CATEGORY_MASK);
684 }
685
686 void ScopedActivity::ChangeAction(uint8_t action) {
687 DCHECK_EQ(0, action & ThreadActivityTracker::ACT_CATEGORY_MASK);
688 ChangeTypeAndData(static_cast<ThreadActivityTracker::ActivityType>(
689 ThreadActivityTracker::ACT_GENERIC | action),
690 ThreadActivityTracker::kNullActivityData);
691 }
692
693 void ScopedActivity::ChangeInfo(uint32_t info) {
694 ChangeTypeAndData(ThreadActivityTracker::ACT_NULL,
695 ThreadActivityTracker::ActivityData::ForGeneric(id_, info));
696 }
697
698 void ScopedActivity::ChangeActionAndInfo(uint8_t action, uint32_t info) {
699 DCHECK_EQ(0, action & ThreadActivityTracker::ACT_CATEGORY_MASK);
700 ChangeTypeAndData(static_cast<ThreadActivityTracker::ActivityType>(
701 ThreadActivityTracker::ACT_GENERIC | action),
702 ThreadActivityTracker::ActivityData::ForGeneric(id_, info));
703 }
704
705 ScopedTaskRunActivity::ScopedTaskRunActivity(const base::PendingTask& task)
706 : GlobalActivityTracker::ScopedThreadActivity(
707 task.posted_from.program_counter(),
708 ThreadActivityTracker::ACT_TASK_RUN,
709 ThreadActivityTracker::ActivityData::ForTask(task.sequence_num),
710 /*lock_allowed=*/true) {}
711
712 ScopedLockAcquireActivity::ScopedLockAcquireActivity(
713 const base::internal::LockImpl* lock)
714 : GlobalActivityTracker::ScopedThreadActivity(
715 nullptr, // TODO(bcwhite): Find a real address.
716 ThreadActivityTracker::ACT_LOCK_ACQUIRE,
717 ThreadActivityTracker::ActivityData::ForLock(lock),
718 /*lock_allowed=*/false) {}
719
720 ScopedEventWaitActivity::ScopedEventWaitActivity(
721 const base::WaitableEvent* event)
722 : GlobalActivityTracker::ScopedThreadActivity(
723 nullptr, // TODO(bcwhite): Find a real address.
724 ThreadActivityTracker::ACT_EVENT_WAIT,
725 ThreadActivityTracker::ActivityData::ForEvent(event),
726 /*lock_allowed=*/true) {}
727
728 ScopedThreadJoinActivity::ScopedThreadJoinActivity(
729 const base::PlatformThreadHandle* thread)
730 : GlobalActivityTracker::ScopedThreadActivity(
731 nullptr, // TODO(bcwhite): Find a real address.
732 ThreadActivityTracker::ACT_THREAD_JOIN,
733 ThreadActivityTracker::ActivityData::ForThread(*thread),
734 /*lock_allowed=*/true) {}
735
736 #if !defined(OS_NACL) && !defined(OS_IOS)
737 ScopedProcessWaitActivity::ScopedProcessWaitActivity(
738 const base::Process* process)
739 : GlobalActivityTracker::ScopedThreadActivity(
740 nullptr, // TODO(bcwhite): Find a real address.
741 ThreadActivityTracker::ACT_PROCESS_WAIT,
742 ThreadActivityTracker::ActivityData::ForProcess(process->Pid()),
743 /*lock_allowed=*/true) {}
744 #endif
745
746 } // namespace debug
747 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/activity_tracker.h ('k') | base/debug/activity_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698