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

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

Powered by Google App Engine
This is Rietveld 408576698