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

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

Issue 2511043003: Support for extracting user-data from activity tracking. (Closed)
Patch Set: addressed review comments by Alexei Created 4 years 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
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
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 bool make_iterable); 148 bool make_iterable);
149 ~ActivityTrackerMemoryAllocator(); 149 ~ActivityTrackerMemoryAllocator();
150 150
151 // Gets a reference to an object of the configured type. This can return 151 // Gets a reference to an object of the configured type. This can return
152 // a null reference if it was not possible to allocate the memory. 152 // a null reference if it was not possible to allocate the memory.
153 Reference GetObjectReference(); 153 Reference GetObjectReference();
154 154
155 // Returns an object to the "free" pool. 155 // Returns an object to the "free" pool.
156 void ReleaseObjectReference(Reference ref); 156 void ReleaseObjectReference(Reference ref);
157 157
158 // Helper function to access an object allocated using this instance.
159 template <typename T>
160 T* GetAsObject(Reference ref) {
161 return allocator_->GetAsObject<T>(ref, object_type_);
162 }
163
164 // Similar to GetAsObject() but converts references to arrays of objects.
165 template <typename T>
166 T* GetAsArray(Reference ref, size_t count) {
167 return allocator_->GetAsArray<T>(ref, object_type_, count);
168 }
169
158 // The current "used size" of the internal cache, visible for testing. 170 // The current "used size" of the internal cache, visible for testing.
159 size_t cache_used() const { return cache_used_; } 171 size_t cache_used() const { return cache_used_; }
160 172
161 private: 173 private:
162 PersistentMemoryAllocator* const allocator_; 174 PersistentMemoryAllocator* const allocator_;
163 const uint32_t object_type_; 175 const uint32_t object_type_;
164 const uint32_t object_free_type_; 176 const uint32_t object_free_type_;
165 const size_t object_size_; 177 const size_t object_size_;
166 const size_t cache_size_; 178 const size_t cache_size_;
167 const bool make_iterable_; 179 const bool make_iterable_;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 // it null. 249 // it null.
238 uint64_t origin_address; 250 uint64_t origin_address;
239 251
240 // Array of program-counters that make up the top of the call stack. 252 // Array of program-counters that make up the top of the call stack.
241 // Despite the fixed size, this list is always null-terminated. Entries 253 // Despite the fixed size, this list is always null-terminated. Entries
242 // after the terminator have no meaning and may or may not also be null. 254 // after the terminator have no meaning and may or may not also be null.
243 // The list will be completely empty if call-stack collection is not 255 // The list will be completely empty if call-stack collection is not
244 // enabled. 256 // enabled.
245 uint64_t call_stack[kActivityCallStackSize]; 257 uint64_t call_stack[kActivityCallStackSize];
246 258
247 // Reference to arbitrary user data within the persistent memory segment. 259 // Reference to arbitrary user data within the persistent memory segment
248 uint32_t user_data; 260 // and a unique identifier for it.
261 uint32_t user_data_ref;
262 uint32_t user_data_id;
249 263
250 // The (enumerated) type of the activity. This defines what fields of the 264 // The (enumerated) type of the activity. This defines what fields of the
251 // |data| record are valid. 265 // |data| record are valid.
252 uint8_t activity_type; 266 uint8_t activity_type;
253 267
254 // Padding to ensure that the next member begins on a 64-bit boundary 268 // Padding to ensure that the next member begins on a 64-bit boundary
255 // even on 32-bit builds which ensures inter-operability between CPU 269 // even on 32-bit builds which ensures inter-operability between CPU
256 // architectures. New fields can be taken from this space. 270 // architectures. New fields can be taken from this space.
257 uint8_t padding[3]; 271 uint8_t padding[7];
258 272
259 // Information specific to the |activity_type|. 273 // Information specific to the |activity_type|.
260 ActivityData data; 274 ActivityData data;
261 275
262 static void FillFrom(Activity* activity, 276 static void FillFrom(Activity* activity,
263 const void* program_counter, 277 const void* program_counter,
264 const void* origin, 278 const void* origin,
265 Type type, 279 Type type,
266 const ActivityData& data); 280 const ActivityData& data);
267 }; 281 };
268 282
269 // This structure holds a copy of all the internal data at the moment the
270 // "snapshot" operation is done. It is disconnected from the live tracker
271 // so that continued operation of the thread will not cause changes here.
272 struct BASE_EXPORT ActivitySnapshot {
273 // Explicit constructor/destructor are needed because of complex types
274 // with non-trivial default constructors and destructors.
275 ActivitySnapshot();
276 ~ActivitySnapshot();
277
278 // The name of the thread as set when it was created. The name may be
279 // truncated due to internal length limitations.
280 std::string thread_name;
281
282 // The process and thread IDs. These values have no meaning other than
283 // they uniquely identify a running process and a running thread within
284 // that process. Thread-IDs can be re-used across different processes
285 // and both can be re-used after the process/thread exits.
286 int64_t process_id = 0;
287 int64_t thread_id = 0;
288
289 // The current stack of activities that are underway for this thread. It
290 // is limited in its maximum size with later entries being left off.
291 std::vector<Activity> activity_stack;
292
293 // The current total depth of the activity stack, including those later
294 // entries not recorded in the |activity_stack| vector.
295 uint32_t activity_stack_depth = 0;
296 };
297
298 // This class manages arbitrary user data that can be associated with activities 283 // This class manages arbitrary user data that can be associated with activities
299 // done by a thread by supporting key/value pairs of any type. This can provide 284 // done by a thread by supporting key/value pairs of any type. This can provide
300 // additional information during debugging. It is also used to store arbitrary 285 // additional information during debugging. It is also used to store arbitrary
301 // global data. All updates must be done from the same thread. 286 // global data. All updates must be done from the same thread.
302 class BASE_EXPORT ActivityUserData { 287 class BASE_EXPORT ActivityUserData {
288 public:
303 // List of known value type. REFERENCE types must immediately follow the non- 289 // List of known value type. REFERENCE types must immediately follow the non-
304 // external types. 290 // external types.
305 enum ValueType : uint8_t { 291 enum ValueType : uint8_t {
306 END_OF_VALUES = 0, 292 END_OF_VALUES = 0,
307 RAW_VALUE, 293 RAW_VALUE,
308 RAW_VALUE_REFERENCE, 294 RAW_VALUE_REFERENCE,
309 STRING_VALUE, 295 STRING_VALUE,
310 STRING_VALUE_REFERENCE, 296 STRING_VALUE_REFERENCE,
311 CHAR_VALUE, 297 CHAR_VALUE,
298 BOOL_VALUE,
312 SIGNED_VALUE, 299 SIGNED_VALUE,
313 UNSIGNED_VALUE, 300 UNSIGNED_VALUE,
314 }; 301 };
315 302
316 public: 303 class BASE_EXPORT TypedValue {
304 public:
305 TypedValue();
306 TypedValue(const TypedValue& other);
307 ~TypedValue();
308
309 StringPiece Get() const;
310 StringPiece GetReference() const;
311 StringPiece GetString() const;
312 StringPiece GetStringReference() const;
313 bool GetBool() const;
314 char GetChar() const;
315 int64_t GetInt() const;
316 uint64_t GetUint() const;
317
318 private:
319 friend class ActivityUserData;
320
321 ValueType type;
322 uint64_t short_value; // Used to hold copy of numbers, etc.
323 std::string long_value; // Used to hold copy of raw/string data.
324 StringPiece ref_value; // Used to hold reference to external data.
manzagop (departed) 2016/11/29 22:42:58 I wonder if using StringPiece is risky/error prone
bcwhite 2016/12/01 16:29:39 It would certainly be a problem if any string oper
325 };
326
327 using Snapshot = std::map<std::string, TypedValue>;
328
317 ActivityUserData(void* memory, size_t size); 329 ActivityUserData(void* memory, size_t size);
318 ~ActivityUserData(); 330 ~ActivityUserData();
319 331
332 // Gets the unique ID number for this user data. If this changes then the
333 // contents have been overwritten by another thread. The return value is
334 // always non-zero unless it's actually just a data "sink".
335 uint32_t id() const {
336 return memory_ ? id_->load(std::memory_order_relaxed) : 0;
337 }
338
320 // Writes a |value| (as part of a key/value pair) that will be included with 339 // Writes a |value| (as part of a key/value pair) that will be included with
321 // the activity in any reports. The same |name| can be written multiple times 340 // the activity in any reports. The same |name| can be written multiple times
322 // with each successive call overwriting the previously stored |value|. For 341 // with each successive call overwriting the previously stored |value|. For
323 // raw and string values, the maximum size of successive writes is limited by 342 // raw and string values, the maximum size of successive writes is limited by
324 // the first call. The length of "name" is limited to 255 characters. 343 // the first call. The length of "name" is limited to 255 characters.
325 // 344 //
326 // This information is stored on a "best effort" basis. It may be dropped if 345 // This information is stored on a "best effort" basis. It may be dropped if
327 // the memory buffer is full or the associated activity is beyond the maximum 346 // the memory buffer is full or the associated activity is beyond the maximum
328 // recording depth. 347 // recording depth.
329 void Set(StringPiece name, const void* memory, size_t size) { 348 void Set(StringPiece name, const void* memory, size_t size) {
330 Set(name, RAW_VALUE, memory, size); 349 Set(name, RAW_VALUE, memory, size);
331 } 350 }
332 void SetString(StringPiece name, StringPiece value) { 351 void SetString(StringPiece name, StringPiece value) {
333 Set(name, STRING_VALUE, value.data(), value.length()); 352 Set(name, STRING_VALUE, value.data(), value.length());
334 } 353 }
354 void SetBool(StringPiece name, bool value) {
355 char cvalue = value ? 1 : 0;
356 Set(name, BOOL_VALUE, &cvalue, sizeof(cvalue));
357 }
335 void SetChar(StringPiece name, char value) { 358 void SetChar(StringPiece name, char value) {
336 Set(name, CHAR_VALUE, &value, sizeof(value)); 359 Set(name, CHAR_VALUE, &value, sizeof(value));
337 } 360 }
338 void SetInt(StringPiece name, int64_t value) { 361 void SetInt(StringPiece name, int64_t value) {
339 Set(name, SIGNED_VALUE, &value, sizeof(value)); 362 Set(name, SIGNED_VALUE, &value, sizeof(value));
340 } 363 }
341 void SetUint(StringPiece name, uint64_t value) { 364 void SetUint(StringPiece name, uint64_t value) {
342 Set(name, UNSIGNED_VALUE, &value, sizeof(value)); 365 Set(name, UNSIGNED_VALUE, &value, sizeof(value));
343 } 366 }
344 367
345 // These function as above but don't actually copy the data into the 368 // These function as above but don't actually copy the data into the
346 // persistent memory. They store unaltered pointers along with a size. These 369 // persistent memory. They store unaltered pointers along with a size. These
347 // can be used in conjuction with a memory dump to find certain large pieces 370 // can be used in conjuction with a memory dump to find certain large pieces
348 // of information. 371 // of information.
349 void SetReference(StringPiece name, const void* memory, size_t size) { 372 void SetReference(StringPiece name, const void* memory, size_t size) {
350 SetReference(name, RAW_VALUE_REFERENCE, memory, size); 373 SetReference(name, RAW_VALUE_REFERENCE, memory, size);
351 } 374 }
352 void SetStringReference(StringPiece name, StringPiece value) { 375 void SetStringReference(StringPiece name, StringPiece value) {
353 SetReference(name, STRING_VALUE_REFERENCE, value.data(), value.length()); 376 SetReference(name, STRING_VALUE_REFERENCE, value.data(), value.length());
354 } 377 }
355 378
379 // Create a snapshot of the key/value pairs contained within. The returned
380 // data will be fixed, independent of whatever changes afterward. There is
381 // protection against concurrent modification of the values but no protection
382 // against a complete overwrite of the contents; the caller must ensure that
383 // the memory segment is not going to be re-initialized while this runs.
384 bool CreateSnapshot(Snapshot* output_snapshot) const;
385
356 private: 386 private:
357 FRIEND_TEST_ALL_PREFIXES(ActivityTrackerTest, UserDataTest); 387 FRIEND_TEST_ALL_PREFIXES(ActivityTrackerTest, UserDataTest);
358 388
359 enum : size_t { kMemoryAlignment = sizeof(uint64_t) }; 389 enum : size_t { kMemoryAlignment = sizeof(uint64_t) };
360 390
361 // A structure used to reference data held outside of persistent memory. 391 // A structure used to reference data held outside of persistent memory.
362 struct ReferenceRecord { 392 struct ReferenceRecord {
363 uint64_t address; 393 uint64_t address;
364 uint64_t size; 394 uint64_t size;
365 }; 395 };
(...skipping 19 matching lines...) Expand all
385 std::atomic<uint16_t>* size_ptr; // Address of the actual size of value. 415 std::atomic<uint16_t>* size_ptr; // Address of the actual size of value.
386 size_t extent; // The total storage of the value, 416 size_t extent; // The total storage of the value,
387 }; // typically rounded up for alignment. 417 }; // typically rounded up for alignment.
388 418
389 void Set(StringPiece name, ValueType type, const void* memory, size_t size); 419 void Set(StringPiece name, ValueType type, const void* memory, size_t size);
390 void SetReference(StringPiece name, 420 void SetReference(StringPiece name,
391 ValueType type, 421 ValueType type,
392 const void* memory, 422 const void* memory,
393 size_t size); 423 size_t size);
394 424
395 // TODO(bcwhite): Add Get() methods for Analyzer to use. 425 // Loads any data already in the memory segment. This allows for accessing
426 // records created previously.
427 void ImportExistingData() const;
396 428
397 std::map<StringPiece, ValueInfo> values_; 429 // A map of all the values within the memory block, keyed by name for quick
430 // updates of the values. This is "mutable" because it changes on "const"
431 // objects even when the actual data values can't change.
432 mutable std::map<StringPiece, ValueInfo> values_;
398 433
399 char* memory_; 434 // Information about the memory block in which new data can be stored. These
400 size_t available_; 435 // are "mutable" because they change even on "const" objects that are just
436 // skipping already set values.
437 mutable char* memory_;
438 mutable size_t available_;
439
440 // A pointer to the unique ID for this instance.
441 std::atomic<uint32_t>* const id_;
401 442
402 base::ThreadChecker thread_checker_; 443 base::ThreadChecker thread_checker_;
403 444
445 // This ID is used to create unique indentifiers for user data so that it's
446 // possible to tell if the information has been overwritten.
447 static std::atomic<uint32_t> next_id_;
448
404 DISALLOW_COPY_AND_ASSIGN(ActivityUserData); 449 DISALLOW_COPY_AND_ASSIGN(ActivityUserData);
405 }; 450 };
406 451
452 // This structure holds a copy of all the internal data at the moment the
453 // "snapshot" operation is done. It is disconnected from the live tracker
454 // so that continued operation of the thread will not cause changes here.
455 struct BASE_EXPORT ActivitySnapshot {
456 // Explicit constructor/destructor are needed because of complex types
457 // with non-trivial default constructors and destructors.
458 ActivitySnapshot();
459 ~ActivitySnapshot();
460
461 // The name of the thread as set when it was created. The name may be
462 // truncated due to internal length limitations.
463 std::string thread_name;
464
465 // The process and thread IDs. These values have no meaning other than
466 // they uniquely identify a running process and a running thread within
467 // that process. Thread-IDs can be re-used across different processes
468 // and both can be re-used after the process/thread exits.
469 int64_t process_id = 0;
470 int64_t thread_id = 0;
471
472 // The current stack of activities that are underway for this thread. It
473 // is limited in its maximum size with later entries being left off.
474 std::vector<Activity> activity_stack;
475
476 // The current total depth of the activity stack, including those later
477 // entries not recorded in the |activity_stack| vector.
478 uint32_t activity_stack_depth = 0;
479 };
480
407 // This class manages tracking a stack of activities for a single thread in 481 // This class manages tracking a stack of activities for a single thread in
408 // a persistent manner, implementing a bounded-size stack in a fixed-size 482 // a persistent manner, implementing a bounded-size stack in a fixed-size
409 // memory allocation. In order to support an operational mode where another 483 // memory allocation. In order to support an operational mode where another
410 // thread is analyzing this data in real-time, atomic operations are used 484 // thread is analyzing this data in real-time, atomic operations are used
411 // where necessary to guarantee a consistent view from the outside. 485 // where necessary to guarantee a consistent view from the outside.
412 // 486 //
413 // This class is not generally used directly but instead managed by the 487 // This class is not generally used directly but instead managed by the
414 // GlobalActivityTracker instance and updated using Scoped*Activity local 488 // GlobalActivityTracker instance and updated using Scoped*Activity local
415 // objects. 489 // objects.
416 class BASE_EXPORT ThreadActivityTracker { 490 class BASE_EXPORT ThreadActivityTracker {
(...skipping 14 matching lines...) Expand all
431 ScopedActivity(ThreadActivityTracker* tracker, 505 ScopedActivity(ThreadActivityTracker* tracker,
432 const void* program_counter, 506 const void* program_counter,
433 const void* origin, 507 const void* origin,
434 Activity::Type type, 508 Activity::Type type,
435 const ActivityData& data); 509 const ActivityData& data);
436 ~ScopedActivity(); 510 ~ScopedActivity();
437 511
438 // Changes some basic metadata about the activity. 512 // Changes some basic metadata about the activity.
439 void ChangeTypeAndData(Activity::Type type, const ActivityData& data); 513 void ChangeTypeAndData(Activity::Type type, const ActivityData& data);
440 514
441 // Returns an object for manipulating user data. 515 protected:
442 ActivityUserData& user_data();
443
444 private:
445 // The thread tracker to which this object reports. It can be null if 516 // The thread tracker to which this object reports. It can be null if
446 // activity tracking is not (yet) enabled. 517 // activity tracking is not (yet) enabled.
447 ThreadActivityTracker* const tracker_; 518 ThreadActivityTracker* const tracker_;
448 519
449 // An identifier that indicates a specific activity on the stack. 520 // An identifier that indicates a specific activity on the stack.
450 ActivityId activity_id_; 521 ActivityId activity_id_;
451 522
452 // An object that manages additional user data, created only upon request.
453 std::unique_ptr<ActivityUserData> user_data_;
454
455 DISALLOW_COPY_AND_ASSIGN(ScopedActivity); 523 DISALLOW_COPY_AND_ASSIGN(ScopedActivity);
456 }; 524 };
457 525
458 // A ThreadActivityTracker runs on top of memory that is managed externally. 526 // A ThreadActivityTracker runs on top of memory that is managed externally.
459 // It must be large enough for the internal header and a few Activity 527 // It must be large enough for the internal header and a few Activity
460 // blocks. See SizeForStackDepth(). 528 // blocks. See SizeForStackDepth().
461 ThreadActivityTracker(void* base, size_t size); 529 ThreadActivityTracker(void* base, size_t size);
462 virtual ~ThreadActivityTracker(); 530 virtual ~ThreadActivityTracker();
463 531
464 // Indicates that an activity has started from a given |origin| address in 532 // Indicates that an activity has started from a given |origin| address in
(...skipping 23 matching lines...) Expand all
488 // unchanged. The type, if changed, must remain in the same category. 556 // unchanged. The type, if changed, must remain in the same category.
489 // Changing both is not atomic so a snapshot operation could occur between 557 // Changing both is not atomic so a snapshot operation could occur between
490 // the update of |type| and |data| or between update of |data| fields. 558 // the update of |type| and |data| or between update of |data| fields.
491 void ChangeActivity(ActivityId id, 559 void ChangeActivity(ActivityId id,
492 Activity::Type type, 560 Activity::Type type,
493 const ActivityData& data); 561 const ActivityData& data);
494 562
495 // Indicates that an activity has completed. 563 // Indicates that an activity has completed.
496 void PopActivity(ActivityId id); 564 void PopActivity(ActivityId id);
497 565
498 // Returns an object capable of storing arbitrary user data. 566 // Sets the user-data information for an activity.
499 std::unique_ptr<ActivityUserData> GetUserData(ActivityId id); 567 std::unique_ptr<ActivityUserData> GetUserData(
568 ActivityId id,
569 ActivityTrackerMemoryAllocator* allocator);
570
571 // Returns if there is true use-data associated with a given ActivityId since
572 // it's possible than any returned object is just a sink.
573 bool HasUserData(ActivityId id);
574
575 // Release the user-data information for an activity.
576 void ReleaseUserData(ActivityId id,
577 ActivityTrackerMemoryAllocator* allocator);
500 578
501 // Returns whether the current data is valid or not. It is not valid if 579 // Returns whether the current data is valid or not. It is not valid if
502 // corruption has been detected in the header or other data structures. 580 // corruption has been detected in the header or other data structures.
503 bool IsValid() const; 581 bool IsValid() const;
504 582
505 // Gets a copy of the tracker contents for analysis. Returns false if a 583 // Gets a copy of the tracker contents for analysis. Returns false if a
506 // snapshot was not possible, perhaps because the data is not valid; the 584 // snapshot was not possible, perhaps because the data is not valid; the
507 // contents of |output_snapshot| are undefined in that case. The current 585 // contents of |output_snapshot| are undefined in that case. The current
508 // implementation does not support concurrent snapshot operations. 586 // implementation does not support concurrent snapshot operations.
509 bool Snapshot(ActivitySnapshot* output_snapshot) const; 587 bool CreateSnapshot(ActivitySnapshot* output_snapshot) const;
510 588
511 // Calculates the memory size required for a given stack depth, including 589 // Calculates the memory size required for a given stack depth, including
512 // the internal header structure for the stack. 590 // the internal header structure for the stack.
513 static size_t SizeForStackDepth(int stack_depth); 591 static size_t SizeForStackDepth(int stack_depth);
514 592
515 private: 593 private:
516 friend class ActivityTrackerTest; 594 friend class ActivityTrackerTest;
517 595
518 Header* const header_; // Pointer to the Header structure. 596 Header* const header_; // Pointer to the Header structure.
519 Activity* const stack_; // The stack of activities. 597 Activity* const stack_; // The stack of activities.
(...skipping 12 matching lines...) Expand all
532 // for the data to be analyzed by a parallel process or even post-mortem. 610 // for the data to be analyzed by a parallel process or even post-mortem.
533 class BASE_EXPORT GlobalActivityTracker { 611 class BASE_EXPORT GlobalActivityTracker {
534 public: 612 public:
535 // Type identifiers used when storing in persistent memory so they can be 613 // Type identifiers used when storing in persistent memory so they can be
536 // identified during extraction; the first 4 bytes of the SHA1 of the name 614 // identified during extraction; the first 4 bytes of the SHA1 of the name
537 // is used as a unique integer. A "version number" is added to the base 615 // is used as a unique integer. A "version number" is added to the base
538 // so that, if the structure of that object changes, stored older versions 616 // so that, if the structure of that object changes, stored older versions
539 // will be safely ignored. These are public so that an external process 617 // will be safely ignored. These are public so that an external process
540 // can recognize records of this type within an allocator. 618 // can recognize records of this type within an allocator.
541 enum : uint32_t { 619 enum : uint32_t {
542 kTypeIdActivityTracker = 0x5D7381AF + 3, // SHA1(ActivityTracker) v3 620 kTypeIdActivityTracker = 0x5D7381AF + 3, // SHA1(ActivityTracker) v3
543 kTypeIdUserDataRecord = 0x615EDDD7 + 1, // SHA1(UserDataRecord) v1 621 kTypeIdUserDataRecord = 0x615EDDD7 + 2, // SHA1(UserDataRecord) v2
544 kTypeIdGlobalDataRecord = 0xAFE61ABE + 1, // SHA1(GlobalDataRecord) v1 622 kTypeIdGlobalDataRecord = kTypeIdUserDataRecord + 0x5F1184F7, // Global
manzagop (departed) 2016/11/29 22:42:58 nit: "SHA1(Global)" for consistency
bcwhite 2016/12/01 16:29:39 Makes the line 82 characters. :-(
545 623
546 kTypeIdActivityTrackerFree = ~kTypeIdActivityTracker, 624 kTypeIdActivityTrackerFree = ~kTypeIdActivityTracker,
547 kTypeIdUserDataRecordFree = ~kTypeIdUserDataRecord, 625 kTypeIdUserDataRecordFree = ~kTypeIdUserDataRecord,
548 }; 626 };
549 627
550 // This is a thin wrapper around the thread-tracker's ScopedActivity that 628 // This is a thin wrapper around the thread-tracker's ScopedActivity that
551 // accesses the global tracker to provide some of the information, notably 629 // accesses the global tracker to provide some of the information, notably
552 // which thread-tracker to use. It is safe to create even if activity 630 // which thread-tracker to use. It is safe to create even if activity
553 // tracking is not enabled. 631 // tracking is not enabled.
554 class BASE_EXPORT ScopedThreadActivity 632 class BASE_EXPORT ScopedThreadActivity
555 : public ThreadActivityTracker::ScopedActivity { 633 : public ThreadActivityTracker::ScopedActivity {
556 public: 634 public:
557 ScopedThreadActivity(const void* program_counter, 635 ScopedThreadActivity(const void* program_counter,
558 const void* origin, 636 const void* origin,
559 Activity::Type type, 637 Activity::Type type,
560 const ActivityData& data, 638 const ActivityData& data,
561 bool lock_allowed) 639 bool lock_allowed);
562 : ThreadActivityTracker::ScopedActivity( 640 ~ScopedThreadActivity();
563 GetOrCreateTracker(lock_allowed), 641
564 program_counter, 642 // Returns an object for manipulating user data.
565 origin, 643 ActivityUserData& user_data();
566 type,
567 data) {}
568 644
569 private: 645 private:
570 // Gets (or creates) a tracker for the current thread. If locking is not 646 // Gets (or creates) a tracker for the current thread. If locking is not
571 // allowed (because a lock is being tracked which would cause recursion) 647 // allowed (because a lock is being tracked which would cause recursion)
572 // then the attempt to create one if none found will be skipped. Once 648 // then the attempt to create one if none found will be skipped. Once
573 // the tracker for this thread has been created for other reasons, locks 649 // the tracker for this thread has been created for other reasons, locks
574 // will be tracked. The thread-tracker uses locks. 650 // will be tracked. The thread-tracker uses locks.
575 static ThreadActivityTracker* GetOrCreateTracker(bool lock_allowed) { 651 static ThreadActivityTracker* GetOrCreateTracker(bool lock_allowed) {
576 GlobalActivityTracker* global_tracker = Get(); 652 GlobalActivityTracker* global_tracker = Get();
577 if (!global_tracker) 653 if (!global_tracker)
578 return nullptr; 654 return nullptr;
579 if (lock_allowed) 655 if (lock_allowed)
580 return global_tracker->GetOrCreateTrackerForCurrentThread(); 656 return global_tracker->GetOrCreateTrackerForCurrentThread();
581 else 657 else
582 return global_tracker->GetTrackerForCurrentThread(); 658 return global_tracker->GetTrackerForCurrentThread();
583 } 659 }
584 660
661 // An object that manages additional user data, created only upon request.
662 std::unique_ptr<ActivityUserData> user_data_;
663
585 DISALLOW_COPY_AND_ASSIGN(ScopedThreadActivity); 664 DISALLOW_COPY_AND_ASSIGN(ScopedThreadActivity);
586 }; 665 };
587 666
588 ~GlobalActivityTracker(); 667 ~GlobalActivityTracker();
589 668
590 // Creates a global tracker using a given persistent-memory |allocator| and 669 // Creates a global tracker using a given persistent-memory |allocator| and
591 // providing the given |stack_depth| to each thread tracker it manages. The 670 // providing the given |stack_depth| to each thread tracker it manages. The
592 // created object is activated so tracking will begin immediately upon return. 671 // created object is activated so tracking will begin immediately upon return.
593 static void CreateWithAllocator( 672 static void CreateWithAllocator(
594 std::unique_ptr<PersistentMemoryAllocator> allocator, 673 std::unique_ptr<PersistentMemoryAllocator> allocator,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 return tracker; 717 return tracker;
639 return CreateTrackerForCurrentThread(); 718 return CreateTrackerForCurrentThread();
640 } 719 }
641 720
642 // Creates an activity-tracker for the current thread. 721 // Creates an activity-tracker for the current thread.
643 ThreadActivityTracker* CreateTrackerForCurrentThread(); 722 ThreadActivityTracker* CreateTrackerForCurrentThread();
644 723
645 // Releases the activity-tracker for the current thread (for testing only). 724 // Releases the activity-tracker for the current thread (for testing only).
646 void ReleaseTrackerForCurrentThreadForTesting(); 725 void ReleaseTrackerForCurrentThreadForTesting();
647 726
648 // Gets a reference to memory for holding user-defined activity data. If
649 // the reference is valid, it's memory will be returned. If not, then a
650 // new reference will be created (and stored) and that memory returned.
651 void* GetUserDataMemory(PersistentMemoryAllocator::Reference* reference);
652
653 // Releases memory for user-defined activity data.
654 void ReleaseUserDataMemory(PersistentMemoryAllocator::Reference* reference);
655
656 // Accesses the global data record for storing arbitrary key/value pairs. 727 // Accesses the global data record for storing arbitrary key/value pairs.
657 ActivityUserData& user_data() { return user_data_; } 728 ActivityUserData& user_data() { return user_data_; }
658 729
659 private: 730 private:
731 friend class ScopedThreadActivity;
660 friend class ActivityTrackerTest; 732 friend class ActivityTrackerTest;
661 733
662 enum : int { 734 enum : int {
663 // The maximum number of threads that can be tracked within a process. If 735 // The maximum number of threads that can be tracked within a process. If
664 // more than this number run concurrently, tracking of new ones may cease. 736 // more than this number run concurrently, tracking of new ones may cease.
665 kMaxThreadCount = 100, 737 kMaxThreadCount = 100,
666 kCachedThreadMemories = 10, 738 kCachedThreadMemories = 10,
667 kCachedUserDataMemories = 10, 739 kCachedUserDataMemories = 10,
668 }; 740 };
669 741
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 ScopedProcessWaitActivity(const void* program_counter, 930 ScopedProcessWaitActivity(const void* program_counter,
859 const base::Process* process); 931 const base::Process* process);
860 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); 932 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity);
861 }; 933 };
862 #endif 934 #endif
863 935
864 } // namespace debug 936 } // namespace debug
865 } // namespace base 937 } // namespace base
866 938
867 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ 939 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698