TimeProfiler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //===- llvm/Support/TimeProfiler.h - Hierarchical Time Profiler -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_SUPPORT_TIMEPROFILER_H
  9. #define LLVM_SUPPORT_TIMEPROFILER_H
  10. #include "llvm/Support/Error.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. namespace llvm {
  13. struct TimeTraceProfiler;
  14. TimeTraceProfiler *getTimeTraceProfilerInstance();
  15. /// Initialize the time trace profiler.
  16. /// This sets up the global \p TimeTraceProfilerInstance
  17. /// variable to be the profiler instance.
  18. void timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
  19. StringRef ProcName);
  20. /// Cleanup the time trace profiler, if it was initialized.
  21. void timeTraceProfilerCleanup();
  22. /// Finish a time trace profiler running on a worker thread.
  23. void timeTraceProfilerFinishThread();
  24. /// Is the time trace profiler enabled, i.e. initialized?
  25. inline bool timeTraceProfilerEnabled() {
  26. return getTimeTraceProfilerInstance() != nullptr;
  27. }
  28. /// Write profiling data to output stream.
  29. /// Data produced is JSON, in Chrome "Trace Event" format, see
  30. /// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
  31. void timeTraceProfilerWrite(raw_pwrite_stream &OS);
  32. /// Write profiling data to a file.
  33. /// The function will write to \p PreferredFileName if provided, if not
  34. /// then will write to \p FallbackFileName appending .time-trace.
  35. /// Returns a StringError indicating a failure if the function is
  36. /// unable to open the file for writing.
  37. Error timeTraceProfilerWrite(StringRef PreferredFileName,
  38. StringRef FallbackFileName);
  39. /// Manually begin a time section, with the given \p Name and \p Detail.
  40. /// Profiler copies the string data, so the pointers can be given into
  41. /// temporaries. Time sections can be hierarchical; every Begin must have a
  42. /// matching End pair but they can nest.
  43. void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
  44. void timeTraceProfilerBegin(StringRef Name,
  45. llvm::function_ref<std::string()> Detail);
  46. /// Manually end the last time section.
  47. void timeTraceProfilerEnd();
  48. /// The TimeTraceScope is a helper class to call the begin and end functions
  49. /// of the time trace profiler. When the object is constructed, it begins
  50. /// the section; and when it is destroyed, it stops it. If the time profiler
  51. /// is not initialized, the overhead is a single branch.
  52. struct TimeTraceScope {
  53. TimeTraceScope() = delete;
  54. TimeTraceScope(const TimeTraceScope &) = delete;
  55. TimeTraceScope &operator=(const TimeTraceScope &) = delete;
  56. TimeTraceScope(TimeTraceScope &&) = delete;
  57. TimeTraceScope &operator=(TimeTraceScope &&) = delete;
  58. TimeTraceScope(StringRef Name) {
  59. if (getTimeTraceProfilerInstance() != nullptr)
  60. timeTraceProfilerBegin(Name, StringRef(""));
  61. }
  62. TimeTraceScope(StringRef Name, StringRef Detail) {
  63. if (getTimeTraceProfilerInstance() != nullptr)
  64. timeTraceProfilerBegin(Name, Detail);
  65. }
  66. TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
  67. if (getTimeTraceProfilerInstance() != nullptr)
  68. timeTraceProfilerBegin(Name, Detail);
  69. }
  70. ~TimeTraceScope() {
  71. if (getTimeTraceProfilerInstance() != nullptr)
  72. timeTraceProfilerEnd();
  73. }
  74. };
  75. } // end namespace llvm
  76. #endif