task_annotator.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 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. #ifndef BASE_TASK_COMMON_TASK_ANNOTATOR_H_
  5. #define BASE_TASK_COMMON_TASK_ANNOTATOR_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/base_export.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/macros.h"
  11. #include "base/pending_task.h"
  12. #include "base/strings/string_piece.h"
  13. namespace base {
  14. // Implements common debug annotations for posted tasks. This includes data
  15. // such as task origins, IPC message contexts, queueing durations and memory
  16. // usage.
  17. class BASE_EXPORT TaskAnnotator {
  18. public:
  19. class ObserverForTesting {
  20. public:
  21. // Invoked just before RunTask() in the scope in which the task is about to
  22. // be executed.
  23. virtual void BeforeRunTask(const PendingTask* pending_task) = 0;
  24. };
  25. // This is used to set the |ipc_hash| field for PendingTasks. It is intended
  26. // to be used only from within generated IPC handler dispatch code.
  27. class ScopedSetIpcHash;
  28. static const PendingTask* CurrentTaskForThread();
  29. TaskAnnotator();
  30. ~TaskAnnotator();
  31. // Called to indicate that a task is about to be queued to run in the future,
  32. // giving one last chance for this TaskAnnotator to add metadata to
  33. // |pending_task| before it is moved into the queue. |task_queue_name| must
  34. // live for the duration of the process.
  35. void WillQueueTask(const char* trace_event_name,
  36. PendingTask* pending_task,
  37. const char* task_queue_name);
  38. // Run a previously queued task.
  39. void NOT_TAIL_CALLED RunTask(const char* trace_event_name,
  40. PendingTask* pending_task);
  41. // Creates a process-wide unique ID to represent this task in trace events.
  42. // This will be mangled with a Process ID hash to reduce the likelyhood of
  43. // colliding with TaskAnnotator pointers on other processes. Callers may use
  44. // this when generating their own flow events (i.e. when passing
  45. // |queue_function == nullptr| in above methods).
  46. uint64_t GetTaskTraceID(const PendingTask& task) const;
  47. private:
  48. friend class TaskAnnotatorBacktraceIntegrationTest;
  49. // Registers an ObserverForTesting that will be invoked by all TaskAnnotators'
  50. // RunTask(). This registration and the implementation of BeforeRunTask() are
  51. // responsible to ensure thread-safety.
  52. static void RegisterObserverForTesting(ObserverForTesting* observer);
  53. static void ClearObserverForTesting();
  54. DISALLOW_COPY_AND_ASSIGN(TaskAnnotator);
  55. };
  56. class BASE_EXPORT TaskAnnotator::ScopedSetIpcHash {
  57. public:
  58. explicit ScopedSetIpcHash(uint32_t ipc_hash);
  59. // Compile-time-const string identifying the current IPC context. Not always
  60. // available due to binary size constraints, so IPC hash might be set instead.
  61. explicit ScopedSetIpcHash(const char* ipc_interface_name);
  62. ~ScopedSetIpcHash();
  63. uint32_t GetIpcHash() const { return ipc_hash_; }
  64. const char* GetIpcInterfaceName() const { return ipc_interface_name_; }
  65. static uint32_t MD5HashMetricName(base::StringPiece name);
  66. private:
  67. ScopedSetIpcHash(uint32_t ipc_hash, const char* ipc_interface_name);
  68. ScopedSetIpcHash* old_scoped_ipc_hash_ = nullptr;
  69. uint32_t ipc_hash_ = 0;
  70. const char* ipc_interface_name_ = nullptr;
  71. DISALLOW_COPY_AND_ASSIGN(ScopedSetIpcHash);
  72. };
  73. } // namespace base
  74. #endif // BASE_TASK_COMMON_TASK_ANNOTATOR_H_