micro_profiler.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_
  13. #define TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_
  14. #include <cstdint>
  15. #include "tensorflow/lite/micro/compatibility.h"
  16. namespace tflite {
  17. // MicroProfiler creates a common way to gain fine-grained insight into runtime
  18. // performance. Bottleck operators can be identified along with slow code
  19. // sections. This can be used in conjunction with running the relevant micro
  20. // benchmark to evaluate end-to-end performance.
  21. class MicroProfiler {
  22. public:
  23. MicroProfiler() = default;
  24. virtual ~MicroProfiler() = default;
  25. // Marks the start of a new event and returns an event handle that can be used
  26. // to mark the end of the event via EndEvent. The lifetime of the tag
  27. // parameter must exceed that of the MicroProfiler.
  28. virtual uint32_t BeginEvent(const char* tag);
  29. // Marks the end of an event associated with event_handle. It is the
  30. // responsibility of the caller to ensure than EndEvent is called once and
  31. // only once per event_handle.
  32. //
  33. // If EndEvent is called more than once for the same event_handle, the last
  34. // call will be used as the end of event marker.If EndEvent is called 0 times
  35. // for a particular event_handle, the duration of that event will be 0 ticks.
  36. virtual void EndEvent(uint32_t event_handle);
  37. // Clears all the events that have been currently profiled.
  38. void ClearEvents() { num_events_ = 0; }
  39. // Returns the sum of the ticks taken across all the events. This number
  40. // is only meaningful if all of the events are disjoint (the end time of
  41. // event[i] <= start time of event[i+1]).
  42. uint32_t GetTotalTicks() const;
  43. // Prints the profiling information of each of the events in human readable
  44. // form.
  45. void Log() const;
  46. // Prints the profiling information of each of the events in CSV (Comma
  47. // Separated Value) form.
  48. void LogCsv() const;
  49. // Prints total ticks for each unique tag in CSV format.
  50. // Output will have one row for each unique tag along with the
  51. // total ticks summed across all events with that particular tag.
  52. void LogTicksPerTagCsv();
  53. private:
  54. // Maximum number of events that this class can keep track of. If we call
  55. // AddEvent more than kMaxEvents number of times, then the oldest event's
  56. // profiling information will be overwritten.
  57. static constexpr int kMaxEvents = 1024;
  58. const char* tags_[kMaxEvents];
  59. uint32_t start_ticks_[kMaxEvents];
  60. uint32_t end_ticks_[kMaxEvents];
  61. int num_events_ = 0;
  62. struct TicksPerTag {
  63. const char* tag;
  64. uint32_t ticks;
  65. };
  66. // In practice, the number of tags will be much lower than the number of
  67. // events. But it is theoretically possible that each event to be unique and
  68. // hence we allow total_ticks_per_tag to have kMaxEvents entries.
  69. TicksPerTag total_ticks_per_tag[kMaxEvents] = {};
  70. int FindExistingOrNextPosition(const char* tag_name);
  71. TF_LITE_REMOVE_VIRTUAL_DELETE;
  72. };
  73. #if defined(TF_LITE_STRIP_ERROR_STRINGS)
  74. // For release builds, the ScopedMicroProfiler is a noop.
  75. //
  76. // This is done because the ScipedProfiler is used as part of the
  77. // MicroInterpreter and we want to ensure zero overhead for the release builds.
  78. class ScopedMicroProfiler {
  79. public:
  80. explicit ScopedMicroProfiler(const char* tag, MicroProfiler* profiler) {}
  81. };
  82. #else
  83. // This class can be used to add events to a MicroProfiler object that span the
  84. // lifetime of the ScopedMicroProfiler object.
  85. // Usage example:
  86. //
  87. // MicroProfiler profiler();
  88. // ...
  89. // {
  90. // ScopedMicroProfiler scoped_profiler("custom_tag", profiler);
  91. // work_to_profile();
  92. // }
  93. class ScopedMicroProfiler {
  94. public:
  95. explicit ScopedMicroProfiler(const char* tag, MicroProfiler* profiler)
  96. : profiler_(profiler) {
  97. if (profiler_ != nullptr) {
  98. event_handle_ = profiler_->BeginEvent(tag);
  99. }
  100. }
  101. ~ScopedMicroProfiler() {
  102. if (profiler_ != nullptr) {
  103. profiler_->EndEvent(event_handle_);
  104. }
  105. }
  106. private:
  107. uint32_t event_handle_ = 0;
  108. MicroProfiler* profiler_ = nullptr;
  109. };
  110. #endif // !defined(TF_LITE_STRIP_ERROR_STRINGS)
  111. } // namespace tflite
  112. #endif // TENSORFLOW_LITE_MICRO_MICRO_PROFILER_H_