TimeProfiler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/TimeProfiler.h - Hierarchical Time Profiler -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This provides lightweight and dependency-free machinery to trace execution
  15. // time around arbitrary code. Two API flavors are available.
  16. //
  17. // The primary API uses a RAII object to trigger tracing:
  18. //
  19. // \code
  20. // {
  21. // TimeTraceScope scope("my_event_name");
  22. // ...my code...
  23. // }
  24. // \endcode
  25. //
  26. // If the code to be profiled does not have a natural lexical scope then
  27. // it is also possible to start and end events with respect to an implicit
  28. // per-thread stack of profiling entries:
  29. //
  30. // \code
  31. // timeTraceProfilerBegin("my_event_name");
  32. // ...my code...
  33. // timeTraceProfilerEnd(); // must be called on all control flow paths
  34. // \endcode
  35. //
  36. // Time profiling entries can be given an arbitrary name and, optionally,
  37. // an arbitrary 'detail' string. The resulting trace will include 'Total'
  38. // entries summing the time spent for each name. Thus, it's best to choose
  39. // names to be fairly generic, and rely on the detail field to capture
  40. // everything else of interest.
  41. //
  42. // To avoid lifetime issues name and detail strings are copied into the event
  43. // entries at their time of creation. Care should be taken to make string
  44. // construction cheap to prevent 'Heisenperf' effects. In particular, the
  45. // 'detail' argument may be a string-returning closure:
  46. //
  47. // \code
  48. // int n;
  49. // {
  50. // TimeTraceScope scope("my_event_name",
  51. // [n]() { return (Twine("x=") + Twine(n)).str(); });
  52. // ...my code...
  53. // }
  54. // \endcode
  55. // The closure will not be called if tracing is disabled. Otherwise, the
  56. // resulting string will be directly moved into the entry.
  57. //
  58. // The main process should begin with a timeTraceProfilerInitialize, and
  59. // finish with timeTraceProfileWrite and timeTraceProfilerCleanup calls.
  60. // Each new thread should begin with a timeTraceProfilerInitialize, and
  61. // finish with a timeTraceProfilerFinishThread call.
  62. //
  63. // Timestamps come from std::chrono::stable_clock. Note that threads need
  64. // not see the same time from that clock, and the resolution may not be
  65. // the best available.
  66. //
  67. // Currently, there are a number of compatible viewers:
  68. // - chrome://tracing is the original chromium trace viewer.
  69. // - http://ui.perfetto.dev is the replacement for the above, under active
  70. // development by Google as part of the 'Perfetto' project.
  71. // - https://www.speedscope.app/ has also been reported as an option.
  72. //
  73. // Future work:
  74. // - Support akin to LLVM_DEBUG for runtime enable/disable of named tracing
  75. // families for non-debug builds which wish to support optional tracing.
  76. // - Evaluate the detail closures at profile write time to avoid
  77. // stringification costs interfering with tracing.
  78. //
  79. //===----------------------------------------------------------------------===//
  80. #ifndef LLVM_SUPPORT_TIMEPROFILER_H
  81. #define LLVM_SUPPORT_TIMEPROFILER_H
  82. #include "llvm/ADT/STLFunctionalExtras.h"
  83. #include "llvm/Support/Error.h"
  84. namespace llvm {
  85. class raw_pwrite_stream;
  86. struct TimeTraceProfiler;
  87. TimeTraceProfiler *getTimeTraceProfilerInstance();
  88. /// Initialize the time trace profiler.
  89. /// This sets up the global \p TimeTraceProfilerInstance
  90. /// variable to be the profiler instance.
  91. void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
  92. StringRef ProcName);
  93. /// Cleanup the time trace profiler, if it was initialized.
  94. void timeTraceProfilerCleanup();
  95. /// Finish a time trace profiler running on a worker thread.
  96. void timeTraceProfilerFinishThread();
  97. /// Is the time trace profiler enabled, i.e. initialized?
  98. inline bool timeTraceProfilerEnabled() {
  99. return getTimeTraceProfilerInstance() != nullptr;
  100. }
  101. /// Write profiling data to output stream.
  102. /// Data produced is JSON, in Chrome "Trace Event" format, see
  103. /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
  104. void timeTraceProfilerWrite(raw_pwrite_stream &OS);
  105. /// Write profiling data to a file.
  106. /// The function will write to \p PreferredFileName if provided, if not
  107. /// then will write to \p FallbackFileName appending .time-trace.
  108. /// Returns a StringError indicating a failure if the function is
  109. /// unable to open the file for writing.
  110. Error timeTraceProfilerWrite(StringRef PreferredFileName,
  111. StringRef FallbackFileName);
  112. /// Manually begin a time section, with the given \p Name and \p Detail.
  113. /// Profiler copies the string data, so the pointers can be given into
  114. /// temporaries. Time sections can be hierarchical; every Begin must have a
  115. /// matching End pair but they can nest.
  116. void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
  117. void timeTraceProfilerBegin(StringRef Name,
  118. llvm::function_ref<std::string()> Detail);
  119. /// Manually end the last time section.
  120. void timeTraceProfilerEnd();
  121. /// The TimeTraceScope is a helper class to call the begin and end functions
  122. /// of the time trace profiler. When the object is constructed, it begins
  123. /// the section; and when it is destroyed, it stops it. If the time profiler
  124. /// is not initialized, the overhead is a single branch.
  125. struct TimeTraceScope {
  126. TimeTraceScope() = delete;
  127. TimeTraceScope(const TimeTraceScope &) = delete;
  128. TimeTraceScope &operator=(const TimeTraceScope &) = delete;
  129. TimeTraceScope(TimeTraceScope &&) = delete;
  130. TimeTraceScope &operator=(TimeTraceScope &&) = delete;
  131. TimeTraceScope(StringRef Name) {
  132. if (getTimeTraceProfilerInstance() != nullptr)
  133. timeTraceProfilerBegin(Name, StringRef(""));
  134. }
  135. TimeTraceScope(StringRef Name, StringRef Detail) {
  136. if (getTimeTraceProfilerInstance() != nullptr)
  137. timeTraceProfilerBegin(Name, Detail);
  138. }
  139. TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
  140. if (getTimeTraceProfilerInstance() != nullptr)
  141. timeTraceProfilerBegin(Name, Detail);
  142. }
  143. ~TimeTraceScope() {
  144. if (getTimeTraceProfilerInstance() != nullptr)
  145. timeTraceProfilerEnd();
  146. }
  147. };
  148. } // end namespace llvm
  149. #endif
  150. #ifdef __GNUC__
  151. #pragma GCC diagnostic pop
  152. #endif