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