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

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

Issue 1980743002: Track thread activities in order to diagnose hangs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@readwrite-mmf
Patch Set: rebased 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 //
Mark Mentovai 2016/08/01 15:59:37 Make this a blank line instead of a // so that I’m
bcwhite 2016/08/01 17:36:11 Done.
5 // Activity tracking provides a low-overhead method of collecting information
6 // about the state of the application for analysis both while it is running
7 // and after it has terminated unexpectedly. Its primary purpose is to help
8 // locate reasons the browser becomes unresponsive by providing insight into
9 // what all the various threads and processes are (or were) doing.
10
11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_
12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_
13
14 // std::atomic is undesired due to performance issues when used as global
15 // variables. There are no such instances here. This module uses the
16 // PersistentMemoryAllocator which also uses std::atomic and is written
17 // by the same author.
18 #include <atomic>
19 #include <memory>
20 #include <string>
21 #include <vector>
22
23 #include "base/base_export.h"
24 #include "base/location.h"
25 #include "base/metrics/persistent_memory_allocator.h"
26 #include "base/threading/thread_checker.h"
27 #include "base/threading/thread_local_storage.h"
28
29 namespace base {
30
31 struct PendingTask;
32
33 class FilePath;
34 class Lock;
35 class MemoryMappedFile;
36 class PlatformThreadHandle;
37 class Process;
38 class WaitableEvent;
39
40 namespace debug {
41
42 #if !defined(OS_NACL) // NACL doesn't support any kind of file access in build.
43 // Enables the global activity tracker according to a field trial setting,
44 // using the specified |file| (without extension) for storing information
45 // from this run.
46 BASE_EXPORT void SetupGlobalActivityTrackerFieldTrial(const FilePath& file);
47 #endif // !defined(OS_NACL)
48
49
50 // This class manages tracking a stack of activities for a single thread in
51 // a persistent manner, implementing a bounded-size stack in a fixed-size
52 // memory allocation. In order to support an operational mode where another
53 // thread is analyzing this data in real-time, atomic operations are used
54 // where necessary to guarantee a consistent view from the outside.
55 //
56 // This class is not generally used directly but instead managed by the
57 // GlobalActivityTracker instance and updated using Scoped*Activity local
58 // objects.
59 class BASE_EXPORT ThreadActivityTracker {
60 public:
61 enum : int {
62 // The maximum number of call-stack addresses stored per activity. This
63 // cannot be changed without also changing the version number of the
64 // structure. See kTypeIdActivityTracker in GlobalActivityTracker.
65 kActivityCallStackSize = 10
66 };
67
68 // The type of an activity on the stack. Activities are broken into
69 // categories with the category ID taking the top 4 bits and the lower
70 // bits representing an action within that category. This combination
71 // makes it easy to "switch" based on the type during analysis.
72 enum ActivityType : uint8_t {
73 // This "null" constant is used to indicate "do not change" in calls.
74 ACT_NULL = 0,
75
76 // Task activities involve callbacks posted to a thread or thread-pool
77 // using the PostTask() method or any of its friends.
78 ACT_TASK = 1 << 4,
79 ACT_TASK_RUN = ACT_TASK,
80
81 // Lock activities involve the acquisition of "mutex" locks.
82 ACT_LOCK = 2 << 4,
83 ACT_LOCK_ACQUIRE = ACT_LOCK,
84 ACT_LOCK_RELEASE,
85
86 // Event activities involve operations on a WaitableEvent.
87 ACT_EVENT = 3 << 4,
88 ACT_EVENT_WAIT = ACT_EVENT,
89 ACT_EVENT_SIGNAL,
90
91 // Thread activities involve the life management of threads.
92 ACT_THREAD = 4 << 4,
93 ACT_THREAD_START = ACT_THREAD,
94 ACT_THREAD_JOIN,
95
96 // Process activities involve the life management of processes.
97 ACT_PROCESS = 5 << 4,
98 ACT_PROCESS_START = ACT_PROCESS,
99 ACT_PROCESS_WAIT,
100
101 // Generic activities are user defined and can be anything.
102 ACT_GENERIC = 15 << 4,
103
104 // These constants can be used to separate the category and action from
105 // a combined activity type.
106 ACT_CATEGORY_MASK = 0xF << 4,
107 ACT_ACTION_MASK = 0xF
108 };
109
110 // The data associated with an activity is dependent upon the activity type.
111 // This union defines all of the various fields. All fields must be explicitly
112 // sized types to ensure no interoperability problems between 32-bit and
113 // 64-bit systems.
114 union ActivityData {
115 // Generic activities don't have any defined structure.
116 struct {
117 uint32_t id; // An arbitrary identifier used for association.
118 int32_t info; // An arbitrary value used for information purposes.
119 } generic;
120 struct {
121 uint64_t sequence_id; // The sequence identifier of the posted task.
122 } task;
123 struct {
124 uint64_t lock_address; // The memory address of the lock object.
125 } lock;
126 struct {
127 uint64_t event_address; // The memory address of the event object.
128 } event;
129 struct {
130 int64_t thread_id; // A unique identifier for a thread within a process.
131 } thread;
132 struct {
133 int64_t process_id; // A unique identifier for a process.
134 } process;
135
136 // These methods create an ActivityData object from the appropriate
137 // parameters. Objects of this type should always be created this way to
138 // ensure that no fields remain unpopulated should the set of recorded
139 // fields change. They're defined inline where practical because they
140 // reduce to loading a small local structure with a few values, roughly
141 // the same as loading all those values into parameters.
142
143 static ActivityData ForGeneric(uint32_t id, int32_t info) {
144 ActivityData data;
145 data.generic.id = id;
146 data.generic.info = info;
147 return data;
148 }
149
150 static ActivityData ForTask(uint64_t sequence) {
151 ActivityData data;
152 data.task.sequence_id = sequence;
153 return data;
154 }
155
156 static ActivityData ForLock(const void* lock) {
157 ActivityData data;
158 data.lock.lock_address = reinterpret_cast<uintptr_t>(lock);
159 return data;
160 }
161
162 static ActivityData ForEvent(const void* event) {
163 ActivityData data;
164 data.event.event_address = reinterpret_cast<uintptr_t>(event);
165 return data;
166 }
167
168 static ActivityData ForThread(const PlatformThreadHandle& handle);
169 static ActivityData ForThread(const int64_t id) {
170 ActivityData data;
171 data.thread.thread_id = id;
172 return data;
173 }
174
175 static ActivityData ForProcess(const int64_t id) {
176 ActivityData data;
177 data.process.process_id = id;
178 return data;
179 }
180 };
181
182 // This structure is the full contents recorded for every activity pushed
183 // onto the stack. The |activity_type| indicates what is actually stored in
184 // the |data| field. All fields must be explicitly sized types to ensure no
185 // interoperability problems between 32-bit and 64-bit systems.
186 struct Activity {
187 // Internal representation of time. During collection, this is in "ticks"
188 // but when returned in a snapshot, it is "wall time".
189 int64_t time_internal;
190
191 // The address that is the origin of the activity if it not obvious from
192 // the call stack. This is useful for things like tasks that are posted
193 // from a completely different thread though most activities will leave
194 // it null.
195 uint64_t origin_address;
196
197 // Array of program-counters that make up the top of the call stack.
198 // Despite the fixed size, this list is always null-terminated. Entries
199 // after the terminator have no meaning and may or may not also be null.
200 // The list will be completely empty if call-stack collection is not
201 // enabled.
202 uint64_t call_stack[kActivityCallStackSize];
203
204 // The (enumerated) type of the activity. This defines what fields of the
205 // |data| record are valid.
206 uint8_t activity_type;
207
208 // Padding to ensure that the next member begins on a 64-bit boundary
209 // even on 32-bit builds which ensures inter-operability between CPU
210 // architectures. New fields can be taken from this space.
211 uint8_t padding[7];
212
213 // Information specific to the |activity_type|.
214 ActivityData data;
215 };
216
217 // This structure holds a copy of all the internal data at the moment the
218 // "snapshot" operation is done. It is disconnected from the live tracker
219 // so that continued operation of the thread will not cause changes here.
220 struct BASE_EXPORT ActivitySnapshot {
221 // Explicit constructor/destructor are needed because of complex types
222 // with non-trivial default constructors and destructors.
223 ActivitySnapshot();
224 ~ActivitySnapshot();
225
226 // The name of the thread as set when it was created. The name may be
227 // truncated due to internal length limitations.
228 std::string thread_name;
229
230 // The process and thread IDs. These values have no meaning other than
231 // they uniquely identify a running process and a running thread within
232 // that process. Thread-IDs can be re-used across different processes
233 // and both can be re-used after the process/thread exits.
234 int64_t process_id = 0;
235 int64_t thread_id = 0;
236
237 // The current stack of activities that are underway for this thread. It
238 // is limited in its maximum size with later entries being left off.
239 std::vector<Activity> activity_stack;
240
241 // The current total depth of the activity stack, including those later
242 // entries not recorded in the |activity_stack| vector.
243 uint32_t activity_stack_depth = 0;
244 };
245
246 // This is the base class for having the compiler manage an activity on the
247 // tracker's stack. It does nothing but call methods on the passed |tracker|
248 // if it is not null, making it safe (and cheap) to create these objects
249 // even if activity tracking is not enabled.
250 class BASE_EXPORT ScopedActivity {
251 public:
252 ScopedActivity(ThreadActivityTracker* tracker,
253 const void* origin,
254 ActivityType type,
255 const ActivityData& data)
256 : tracker_(tracker) {
257 if (tracker_)
258 tracker_->PushActivity(origin, type, data);
259 }
260
261 ~ScopedActivity() {
262 if (tracker_)
263 tracker_->PopActivity();
264 }
265
266 void ChangeTypeAndData(ActivityType type, const ActivityData& data) {
267 if (tracker_)
268 tracker_->ChangeActivity(type, data);
269 }
270
271 private:
272 // The thread tracker to which this object reports. It can be null if
273 // activity tracking is not (yet) enabled.
274 ThreadActivityTracker* const tracker_;
275 };
276
277 // A ThreadActivityTracker runs on top of memory that is managed externally.
278 // It must be large enough for the internal header and a few Activity
279 // blocks. See SizeForStackDepth().
280 ThreadActivityTracker(void* base, size_t size);
281 virtual ~ThreadActivityTracker();
282
283 // Indicates that an activity has started from a given |origin| address in
284 // the code, though it can be null if the creator's address is not known.
285 // The |type| and |data| describe the activity.
286 void PushActivity(const void* origin,
287 ActivityType type,
288 const ActivityData& data);
289
290 // Changes the activity |type| and |data| of the top-most entry on the stack.
291 // This is useful if the information has changed and it is desireable to
292 // track that change without creating a new stack entry. If the type is
293 // ACT_NULL or the data is kNullActivityData then that value will remain
294 // unchanged. The type, if changed, must remain in the same category.
295 // Changing both is not atomic so a snapshot operation could occur between
296 // the update of |type| and |data| or between update of |data| fields.
297 void ChangeActivity(ActivityType type, const ActivityData& data);
298
299 // Indicates that an activity has completed.
300 void PopActivity();
301
302 // Returns whether the current data is valid or not. It is not valid if
303 // corruption has been detected in the header or other data structures.
304 bool IsValid() const;
305
306 // Gets a copy of the tracker contents for analysis. Returns false if a
307 // snapshot was not possible, perhaps because the data is not valid; the
308 // contents of |output_snapshot| are undefined in that case. The current
309 // implementation does not support concurrent snapshot operations.
310 bool Snapshot(ActivitySnapshot* output_snapshot) const;
311
312 // Calculates the memory size required for a given stack depth, including
313 // the internal header structure for the stack.
314 static size_t SizeForStackDepth(int stack_depth);
315
316 // A "null" activity-data that can be passed to indicate "do not change".
317 static const ActivityData kNullActivityData;
318
319 private:
320 friend class ActivityTrackerTest;
321
322 // This structure contains all the common information about the thread so
323 // it doesn't have to be repeated in every entry on the stack. It is defined
324 // and used completely within the .cc file.
325 struct Header;
326
327 Header* const header_; // Pointer to the Header structure.
328 Activity* const stack_; // The stack of activities.
329 const uint32_t stack_slots_; // The total number of stack slots.
330
331 bool valid_ = false; // Tracks whether the data is valid or not.
332
333 base::ThreadChecker thread_checker_;
334
335 DISALLOW_COPY_AND_ASSIGN(ThreadActivityTracker);
336 };
337
338
339 // The global tracker manages all the individual thread trackers. Memory for
340 // the thread trackers is taken from a PersistentMemoryAllocator which allows
341 // for the data to be analyzed by a parallel process or even post-mortem.
342 class BASE_EXPORT GlobalActivityTracker {
343 public:
344 // Type identifiers used when storing in persistent memory so they can be
345 // identified during extraction; the first 4 bytes of the SHA1 of the name
346 // is used as a unique integer. A "version number" is added to the base
347 // so that, if the structure of that object changes, stored older versions
348 // will be safely ignored. These are public so that an external process
349 // can recognize records of this type within an allocator.
350 enum : uint32_t {
351 kTypeIdActivityTracker = 0x5D7381AF + 1, // SHA1(ActivityTracker) v1
352 kTypeIdActivityTrackerFree = 0x3F0272FB + 1, // SHA1(ActivityTrackerFree)
353 };
354
355 // This is a thin wrapper around the thread-tracker's ScopedActivity that
356 // accesses the global tracker to provide some of the information, notably
357 // which thread-tracker to use. It is safe to create even if activity
358 // tracking is not enabled.
359 class BASE_EXPORT ScopedThreadActivity
360 : public ThreadActivityTracker::ScopedActivity {
361 public:
362 ScopedThreadActivity(const void* origin,
363 ThreadActivityTracker::ActivityType type,
364 const ThreadActivityTracker::ActivityData& data,
365 bool lock_allowed)
366 : ThreadActivityTracker::ScopedActivity(
367 GetOrCreateTracker(lock_allowed),
368 origin,
369 type,
370 data) {}
371
372 private:
373 // Gets (or creates) a tracker for the current thread. If locking is not
374 // allowed (because a lock is being tracked which would cause recursion)
375 // then the attempt to create one if none found will be skipped. Once
376 // the tracker for this thread has been created for other reasons, locks
377 // will be tracked. The thread-tracker uses locks.
378 static ThreadActivityTracker* GetOrCreateTracker(bool lock_allowed) {
379 GlobalActivityTracker* global_tracker = Get();
380 if (!global_tracker)
381 return nullptr;
382 if (lock_allowed)
383 return global_tracker->GetOrCreateTrackerForCurrentThread();
384 else
385 return global_tracker->GetTrackerForCurrentThread();
386 }
387 };
388
389 ~GlobalActivityTracker();
390
391 // Creates a global tracker using a given persistent-memory |allocator| and
392 // providing the given |stack_depth| to each thread tracker it manages. The
393 // created object is activated so tracking will begin immediately upon return.
394 static void CreateWithAllocator(
395 std::unique_ptr<PersistentMemoryAllocator> allocator,
396 int stack_depth);
397
398 #if !defined(OS_NACL)
399 // Like above but internally creates an allocator around a disk file with
400 // the specified |size| at the given |file_path|. Any existing file will be
401 // overwritten. The |id| and |name| are arbitrary and stored in the allocator
402 // for reference by whatever process reads it.
403 static void CreateWithFile(const FilePath& file_path,
404 size_t size,
405 uint64_t id,
406 StringPiece name,
407 int stack_depth);
408 #endif // !defined(OS_NACL)
409
410 // Like above but internally creates an allocator using local heap memory of
411 // the specified size. This is used primarily for unit tests.
412 static void CreateWithLocalMemory(size_t size,
413 uint64_t id,
414 StringPiece name,
415 int stack_depth);
416
417 // Gets the global activity-tracker or null if none exists.
418 static GlobalActivityTracker* Get() { return g_tracker_; }
419
420 // Gets the persistent-memory-allocator in which data is stored. Callers
421 // can store additional records here to pass more information to the
422 // analysis process.
423 PersistentMemoryAllocator* allocator() { return allocator_.get(); }
424
425 // Gets the thread's activity-tracker if it exists. This is inline for
426 // performance reasons and it uses thread-local-storage (TLS) so that there
427 // is no significant lookup time required to find the one for the calling
428 // thread. Ownership remains with the global tracker.
429 ThreadActivityTracker* GetTrackerForCurrentThread() {
430 return reinterpret_cast<ThreadActivityTracker*>(this_thread_tracker_.Get());
431 }
432
433 // Gets the thread's activity-tracker or creates one if none exists. This
434 // is inline for performance reasons. Ownership remains with the global
435 // tracker.
436 ThreadActivityTracker* GetOrCreateTrackerForCurrentThread() {
437 ThreadActivityTracker* tracker = GetTrackerForCurrentThread();
438 if (tracker)
439 return tracker;
440 return CreateTrackerForCurrentThread();
441 }
442
443 // Creates an activity-tracker for the current thread.
444 ThreadActivityTracker* CreateTrackerForCurrentThread();
445
446 // Releases the activity-tracker for the current thread (for testing only).
447 void ReleaseTrackerForCurrentThreadForTesting();
448
449 private:
450 friend class ActivityTrackerTest;
451
452 enum : int {
453 // The maximum number of threads that can be tracked within a process. If
454 // more than this number run concurrently, tracking of new ones may cease.
455 kMaxThreadCount = 100,
456 };
457
458 // A thin wrapper around the main thread-tracker that keeps additional
459 // information that the global tracker needs to handle joined threads.
460 class ManagedActivityTracker : public ThreadActivityTracker {
461 public:
462 ManagedActivityTracker(PersistentMemoryAllocator::Reference mem_reference,
463 void* base,
464 size_t size);
465 ~ManagedActivityTracker() override;
466
467 // The reference into persistent memory from which the thread-tracker's
468 // memory was created.
469 const PersistentMemoryAllocator::Reference mem_reference_;
470
471 // The physical address used for the thread-tracker's memory.
472 void* const mem_base_;
473 };
474
475 // Creates a global tracker using a given persistent-memory |allocator| and
476 // providing the given |stack_depth| to each thread tracker it manages. The
477 // created object is activated so tracking has already started upon return.
478 GlobalActivityTracker(std::unique_ptr<PersistentMemoryAllocator> allocator,
479 int stack_depth);
480
481 // Returns the memory used by an activity-tracker managed by this class.
482 // It is called during the destruction of a ManagedActivityTracker object.
483 void ReturnTrackerMemory(ManagedActivityTracker* tracker);
484
485 // Releases the activity-tracker associcated with thread. It is called
486 // automatically when a thread is joined and thus there is nothing more to
487 // be tracked. |value| is a pointer to a ManagedActivityTracker.
488 static void OnTLSDestroy(void* value);
489
490 // The persistent-memory allocator from which the memory for all trackers
491 // is taken.
492 std::unique_ptr<PersistentMemoryAllocator> allocator_;
493
494 // The size (in bytes) of memory required by a ThreadActivityTracker to
495 // provide the stack-depth requested during construction.
496 const size_t stack_memory_size_;
497
498 // The activity tracker for the currently executing thread.
499 base::ThreadLocalStorage::Slot this_thread_tracker_;
500
501 // These have to be lock-free because lock activity is tracked and causes
502 // re-entry problems.
503 std::atomic<int> thread_tracker_count_;
504 std::atomic<int> available_memories_count_;
505 std::atomic<PersistentMemoryAllocator::Reference>
506 available_memories_[kMaxThreadCount];
507
508 // The active global activity tracker.
509 static GlobalActivityTracker* g_tracker_;
510 };
511
512
513 // Record entry in to and out of an arbitrary block of code.
514 class BASE_EXPORT ScopedActivity
515 : public GlobalActivityTracker::ScopedThreadActivity {
516 public:
517 // Track activity at the specified FROM_HERE location for an arbitrary
518 // 4-bit |action|, an arbitrary 32-bit |id|, and 32-bits of arbitrary
519 // |info|. None of these values affect operation; they're all purely
520 // for association and analysis. To have unique identifiers across a
521 // diverse code-base, create the number by taking the first 8 characters
522 // of the hash of the activity being tracked.
523 //
524 // For example:
525 // Tracking method: void MayNeverExit(uint32_t foo) {...}
526 // echo -n "MayNeverExit" | sha1sum => e44873ccab21e2b71270da24aa1...
527 //
528 // void MayNeverExit(int32_t foo) {
529 // base::debug::ScopedActivity track_me(FROM_HERE, 0, 0xE44873CC, foo);
530 // ...
531 // }
532 ScopedActivity(const tracked_objects::Location& location,
533 uint8_t action,
534 uint32_t id,
535 int32_t info);
536
537 // Because this is inline, the FROM_HERE macro will resolve the current
538 // program-counter as the location in the calling code.
539 ScopedActivity() : ScopedActivity(FROM_HERE, 0, 0, 0) {}
540
541 // Changes the |action| and/or |info| of this activity on the stack. This
542 // is useful for tracking progress through a function, updating the action
543 // to indicate "milestones" in the block (max 16 milestones: 0-15) or the
544 // info to reflect other changes. Changing both is not atomic so a snapshot
545 // operation could occur between the update of |action| and |info|.
546 void ChangeAction(uint8_t action);
547 void ChangeInfo(int32_t info);
548 void ChangeActionAndInfo(uint8_t action, int32_t info);
549
550 private:
551 // A copy of the ID code so it doesn't have to be passed by the caller when
552 // changing the |info| field.
553 uint32_t id_;
554 };
555
556
557 // These "scoped" classes provide easy tracking of various blocking actions.
558
559 class BASE_EXPORT ScopedTaskRunActivity
560 : public GlobalActivityTracker::ScopedThreadActivity {
561 public:
562 explicit ScopedTaskRunActivity(const base::PendingTask& task);
563 };
564
565 class BASE_EXPORT ScopedLockAcquireActivity
566 : public GlobalActivityTracker::ScopedThreadActivity {
567 public:
568 explicit ScopedLockAcquireActivity(const base::internal::LockImpl* lock);
569 };
570
571 class BASE_EXPORT ScopedEventWaitActivity
572 : public GlobalActivityTracker::ScopedThreadActivity {
573 public:
574 explicit ScopedEventWaitActivity(const base::WaitableEvent* event);
575 };
576
577 class BASE_EXPORT ScopedThreadJoinActivity
578 : public GlobalActivityTracker::ScopedThreadActivity {
579 public:
580 explicit ScopedThreadJoinActivity(const base::PlatformThreadHandle* thread);
581 };
582
583 // Some systems don't have base::Process
584 #if !defined(OS_NACL) && !defined(OS_IOS)
585 class BASE_EXPORT ScopedProcessWaitActivity
586 : public GlobalActivityTracker::ScopedThreadActivity {
587 public:
588 explicit ScopedProcessWaitActivity(const base::Process* process);
589 };
590 #endif
591
592 } // namespace debug
593 } // namespace base
594
595 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698