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

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

Issue 2255503002: New cache for the activity tracker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: forgo lock-free; use regular lock to create thread-safe stack Created 4 years, 3 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 | « no previous file | base/debug/activity_tracker.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Activity tracking provides a low-overhead method of collecting information 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 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 7 // and after it has terminated unexpectedly. Its primary purpose is to help
8 // locate reasons the browser becomes unresponsive by providing insight into 8 // locate reasons the browser becomes unresponsive by providing insight into
9 // what all the various threads and processes are (or were) doing. 9 // what all the various threads and processes are (or were) doing.
10 10
11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_ 11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_
12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_ 12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_
13 13
14 // std::atomic is undesired due to performance issues when used as global 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 15 // variables. There are no such instances here. This module uses the
16 // PersistentMemoryAllocator which also uses std::atomic and is written 16 // PersistentMemoryAllocator which also uses std::atomic and is written
17 // by the same author. 17 // by the same author.
18 #include <atomic> 18 #include <atomic>
19 #include <memory> 19 #include <memory>
20 #include <string> 20 #include <string>
21 #include <vector> 21 #include <vector>
22 22
23 #include "base/base_export.h" 23 #include "base/base_export.h"
24 #include "base/location.h" 24 #include "base/location.h"
25 #include "base/metrics/persistent_memory_allocator.h" 25 #include "base/metrics/persistent_memory_allocator.h"
26 #include "base/threading/platform_thread.h"
26 #include "base/threading/thread_checker.h" 27 #include "base/threading/thread_checker.h"
27 #include "base/threading/thread_local_storage.h" 28 #include "base/threading/thread_local_storage.h"
28 29
29 namespace base { 30 namespace base {
30 31
31 struct PendingTask; 32 struct PendingTask;
32 33
33 class FilePath; 34 class FilePath;
34 class Lock; 35 class Lock;
35 class MemoryMappedFile; 36 class MemoryMappedFile;
36 class PlatformThreadHandle; 37 class PlatformThreadHandle;
37 class Process; 38 class Process;
38 class WaitableEvent; 39 class WaitableEvent;
39 40
40 namespace debug { 41 namespace debug {
41 42
42 class ThreadActivityTracker; 43 class ThreadActivityTracker;
43 44
45
44 enum : int { 46 enum : int {
45 // The maximum number of call-stack addresses stored per activity. This 47 // The maximum number of call-stack addresses stored per activity. This
46 // cannot be changed without also changing the version number of the 48 // cannot be changed without also changing the version number of the
47 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. 49 // structure. See kTypeIdActivityTracker in GlobalActivityTracker.
48 kActivityCallStackSize = 10, 50 kActivityCallStackSize = 10,
49 }; 51 };
50 52
51 // The data associated with an activity is dependent upon the activity type. 53 // The data associated with an activity is dependent upon the activity type.
52 // This union defines all of the various fields. All fields must be explicitly 54 // This union defines all of the various fields. All fields must be explicitly
53 // sized types to ensure no interoperability problems between 32-bit and 55 // sized types to ensure no interoperability problems between 32-bit and
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 base::ThreadChecker thread_checker_; 337 base::ThreadChecker thread_checker_;
336 338
337 DISALLOW_COPY_AND_ASSIGN(ThreadActivityTracker); 339 DISALLOW_COPY_AND_ASSIGN(ThreadActivityTracker);
338 }; 340 };
339 341
340 342
341 // The global tracker manages all the individual thread trackers. Memory for 343 // The global tracker manages all the individual thread trackers. Memory for
342 // the thread trackers is taken from a PersistentMemoryAllocator which allows 344 // the thread trackers is taken from a PersistentMemoryAllocator which allows
343 // for the data to be analyzed by a parallel process or even post-mortem. 345 // for the data to be analyzed by a parallel process or even post-mortem.
344 class BASE_EXPORT GlobalActivityTracker { 346 class BASE_EXPORT GlobalActivityTracker {
347 template <typename T>
348 class ThreadSafeStack {
349 public:
350 ThreadSafeStack(size_t size)
351 : size_(size), values_(new T[size]), used_(0) {}
352 ~ThreadSafeStack() {}
353
354 size_t size() { return size_; }
355 size_t used() {
356 base::AutoLock autolock(lock_);
357 return used_;
358 }
359
360 bool push(T value) {
361 base::AutoLock autolock(lock_);
362 if (used_ == size_)
363 return false;
364 values_[used_++] = value;
365 return true;
366 }
367
368 bool pop(T* out_value) {
369 base::AutoLock autolock(lock_);
370 if (used_ == 0)
371 return false;
372 *out_value = values_[--used_];
373 return true;
374 }
375
376 private:
377 const size_t size_;
378
379 std::unique_ptr<T[]> values_;
380 size_t used_;
381 base::Lock lock_;
382
383 private:
384 DISALLOW_COPY_AND_ASSIGN(ThreadSafeStack);
385 };
386
345 public: 387 public:
346 // Type identifiers used when storing in persistent memory so they can be 388 // Type identifiers used when storing in persistent memory so they can be
347 // identified during extraction; the first 4 bytes of the SHA1 of the name 389 // identified during extraction; the first 4 bytes of the SHA1 of the name
348 // is used as a unique integer. A "version number" is added to the base 390 // is used as a unique integer. A "version number" is added to the base
349 // so that, if the structure of that object changes, stored older versions 391 // so that, if the structure of that object changes, stored older versions
350 // will be safely ignored. These are public so that an external process 392 // will be safely ignored. These are public so that an external process
351 // can recognize records of this type within an allocator. 393 // can recognize records of this type within an allocator.
352 enum : uint32_t { 394 enum : uint32_t {
353 kTypeIdActivityTracker = 0x5D7381AF + 1, // SHA1(ActivityTracker) v1 395 kTypeIdActivityTracker = 0x5D7381AF + 1, // SHA1(ActivityTracker) v1
354 kTypeIdActivityTrackerFree = 0x3F0272FB + 1, // SHA1(ActivityTrackerFree) 396 kTypeIdActivityTrackerFree = 0x3F0272FB + 1, // SHA1(ActivityTrackerFree)
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 // is taken. 540 // is taken.
499 std::unique_ptr<PersistentMemoryAllocator> allocator_; 541 std::unique_ptr<PersistentMemoryAllocator> allocator_;
500 542
501 // The size (in bytes) of memory required by a ThreadActivityTracker to 543 // The size (in bytes) of memory required by a ThreadActivityTracker to
502 // provide the stack-depth requested during construction. 544 // provide the stack-depth requested during construction.
503 const size_t stack_memory_size_; 545 const size_t stack_memory_size_;
504 546
505 // The activity tracker for the currently executing thread. 547 // The activity tracker for the currently executing thread.
506 base::ThreadLocalStorage::Slot this_thread_tracker_; 548 base::ThreadLocalStorage::Slot this_thread_tracker_;
507 549
508 // These have to be lock-free because lock activity is tracked and causes 550 // These have to be lock-free because lock activity is tracked and causes
manzagop (departed) 2016/09/01 15:46:48 Update comment?
bcwhite 2016/09/02 14:20:35 Done.
509 // re-entry problems. 551 // re-entry problems.
510 std::atomic<int> thread_tracker_count_; 552 std::atomic<int> thread_tracker_count_;
511 std::atomic<int> available_memories_count_; 553 ThreadSafeStack<PersistentMemoryAllocator::Reference> available_memories_;
512 std::atomic<PersistentMemoryAllocator::Reference>
513 available_memories_[kMaxThreadCount];
514 554
515 // The active global activity tracker. 555 // The active global activity tracker.
516 static GlobalActivityTracker* g_tracker_; 556 static GlobalActivityTracker* g_tracker_;
517 557
518 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); 558 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker);
519 }; 559 };
520 560
521 561
522 // Record entry in to and out of an arbitrary block of code. 562 // Record entry in to and out of an arbitrary block of code.
523 class BASE_EXPORT ScopedActivity 563 class BASE_EXPORT ScopedActivity
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 explicit ScopedProcessWaitActivity(const base::Process* process); 647 explicit ScopedProcessWaitActivity(const base::Process* process);
608 private: 648 private:
609 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); 649 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity);
610 }; 650 };
611 #endif 651 #endif
612 652
613 } // namespace debug 653 } // namespace debug
614 } // namespace base 654 } // namespace base
615 655
616 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ 656 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_
OLDNEW
« no previous file with comments | « no previous file | base/debug/activity_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698