TimeProfiler.h 3.7 KB

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