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

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

Powered by Google App Engine
This is Rietveld 408576698