auto_open_close_event.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef BASE_TRACE_EVENT_AUTO_OPEN_CLOSE_EVENT_H_
  5. #define BASE_TRACE_EVENT_AUTO_OPEN_CLOSE_EVENT_H_
  6. #include "base/macros.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/threading/thread_checker.h"
  9. #include "base/time/time.h"
  10. #include "base/trace_event/trace_event.h"
  11. namespace base {
  12. namespace trace_event {
  13. // Class for tracing events that support "auto-opening" and "auto-closing".
  14. // "auto-opening" = if the trace event is started (call Begin() before
  15. // tracing is started,the trace event will be opened, with the start time
  16. // being the time that the trace event was actually started.
  17. // "auto-closing" = if the trace event is started but not ended by the time
  18. // tracing ends, then the trace event will be automatically closed at the
  19. // end of tracing.
  20. // |category| must be known at compile-time in order to be used in trace macros.
  21. // Hence, it's passed as a class templace argument.
  22. template <const char* category>
  23. class AutoOpenCloseEvent : public TraceLog::AsyncEnabledStateObserver {
  24. public:
  25. enum Type {
  26. ASYNC
  27. };
  28. // As in the rest of the tracing macros, the const char* arguments here
  29. // must be pointers to indefinitely lived strings (e.g. hard-coded string
  30. // literals are okay, but not strings created by c_str())
  31. AutoOpenCloseEvent(Type type, const char* event_name)
  32. : event_name_(event_name) {
  33. base::trace_event::TraceLog::GetInstance()->AddAsyncEnabledStateObserver(
  34. weak_factory_.GetWeakPtr());
  35. }
  36. ~AutoOpenCloseEvent() override {
  37. DCHECK(thread_checker_.CalledOnValidThread());
  38. base::trace_event::TraceLog::GetInstance()->RemoveAsyncEnabledStateObserver(
  39. this);
  40. }
  41. void Begin() {
  42. DCHECK(thread_checker_.CalledOnValidThread());
  43. start_time_ = TRACE_TIME_TICKS_NOW();
  44. TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
  45. category, event_name_, static_cast<void*>(this), start_time_);
  46. }
  47. void End() {
  48. DCHECK(thread_checker_.CalledOnValidThread());
  49. TRACE_EVENT_ASYNC_END0(category, event_name_, static_cast<void*>(this));
  50. start_time_ = base::TimeTicks();
  51. }
  52. // AsyncEnabledStateObserver implementation
  53. void OnTraceLogEnabled() override {
  54. DCHECK(thread_checker_.CalledOnValidThread());
  55. if (!start_time_.is_null()) {
  56. TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
  57. category, event_name_, static_cast<void*>(this), start_time_);
  58. }
  59. }
  60. void OnTraceLogDisabled() override {}
  61. private:
  62. const char* const event_name_;
  63. base::TimeTicks start_time_;
  64. base::ThreadChecker thread_checker_;
  65. WeakPtrFactory<AutoOpenCloseEvent> weak_factory_{this};
  66. DISALLOW_COPY_AND_ASSIGN(AutoOpenCloseEvent);
  67. };
  68. } // namespace trace_event
  69. } // namespace base
  70. #endif // BASE_TRACE_EVENT_AUTO_OPEN_CLOSE_EVENT_H_