micro_profiler.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright 2020 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. #include "tensorflow/lite/micro/micro_profiler.h"
  13. #include <cinttypes>
  14. #include <cstdint>
  15. #include <cstring>
  16. #include "tensorflow/lite/kernels/internal/compatibility.h"
  17. #include "tensorflow/lite/micro/micro_error_reporter.h"
  18. #include "tensorflow/lite/micro/micro_time.h"
  19. namespace tflite {
  20. uint32_t MicroProfiler::BeginEvent(const char* tag) {
  21. if (num_events_ == kMaxEvents) {
  22. num_events_ = 0;
  23. }
  24. tags_[num_events_] = tag;
  25. start_ticks_[num_events_] = GetCurrentTimeTicks();
  26. end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
  27. return num_events_++;
  28. }
  29. void MicroProfiler::EndEvent(uint32_t event_handle) {
  30. TFLITE_DCHECK(event_handle < kMaxEvents);
  31. end_ticks_[event_handle] = GetCurrentTimeTicks();
  32. }
  33. uint32_t MicroProfiler::GetTotalTicks() const {
  34. int32_t ticks = 0;
  35. for (int i = 0; i < num_events_; ++i) {
  36. ticks += end_ticks_[i] - start_ticks_[i];
  37. }
  38. return ticks;
  39. }
  40. void MicroProfiler::Log() const {
  41. #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
  42. for (int i = 0; i < num_events_; ++i) {
  43. uint32_t ticks = end_ticks_[i] - start_ticks_[i];
  44. MicroPrintf("%s took %" PRIu32 " ticks (%d ms).", tags_[i], ticks,
  45. TicksToMs(ticks));
  46. }
  47. #endif
  48. }
  49. void MicroProfiler::LogCsv() const {
  50. #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
  51. MicroPrintf("\"Event\",\"Tag\",\"Ticks\"");
  52. for (int i = 0; i < num_events_; ++i) {
  53. uint32_t ticks = end_ticks_[i] - start_ticks_[i];
  54. MicroPrintf("%d,%s,%" PRIu32, i, tags_[i], ticks);
  55. }
  56. #endif
  57. }
  58. void MicroProfiler::LogTicksPerTagCsv() {
  59. #if !defined(TF_LITE_STRIP_ERROR_STRINGS)
  60. MicroPrintf(
  61. "\"Unique Tag\",\"Total ticks across all events with that tag.\"");
  62. int total_ticks = 0;
  63. for (int i = 0; i < num_events_; ++i) {
  64. uint32_t ticks = end_ticks_[i] - start_ticks_[i];
  65. TFLITE_DCHECK(tags_[i] != nullptr);
  66. int position = FindExistingOrNextPosition(tags_[i]);
  67. TFLITE_DCHECK(position >= 0);
  68. total_ticks_per_tag[position].tag = tags_[i];
  69. total_ticks_per_tag[position].ticks =
  70. total_ticks_per_tag[position].ticks + ticks;
  71. total_ticks += ticks;
  72. }
  73. for (int i = 0; i < num_events_; ++i) {
  74. TicksPerTag each_tag_entry = total_ticks_per_tag[i];
  75. if (each_tag_entry.tag == nullptr) {
  76. break;
  77. }
  78. MicroPrintf("%s, %d", each_tag_entry.tag, each_tag_entry.ticks);
  79. }
  80. MicroPrintf("total number of ticks, %d", total_ticks);
  81. #endif
  82. }
  83. // This method finds a particular array element in the total_ticks_per_tag array
  84. // with the matching tag_name passed in the method. If it can find a
  85. // matching array element that has the same tag_name, then it will return the
  86. // position of the matching element. But if it unable to find a matching element
  87. // with the given tag_name, it will return the next available empty position
  88. // from the array.
  89. int MicroProfiler::FindExistingOrNextPosition(const char* tag_name) {
  90. int pos = 0;
  91. for (; pos < num_events_; pos++) {
  92. TicksPerTag each_tag_entry = total_ticks_per_tag[pos];
  93. if (each_tag_entry.tag == nullptr ||
  94. strcmp(each_tag_entry.tag, tag_name) == 0) {
  95. return pos;
  96. }
  97. }
  98. return pos < num_events_ ? pos : -1;
  99. }
  100. } // namespace tflite