Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | a78c7a8 | 2022-02-10 14:17:24 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "tensorflow/lite/kernels/internal/compatibility.h" |
| 20 | #include "tensorflow/lite/micro/micro_error_reporter.h" |
| 21 | #include "tensorflow/lite/micro/micro_time.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "arm_profiler.hpp" |
| 26 | #include <inttypes.h> |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | namespace tflite { |
| 30 | |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 31 | ArmProfiler::ArmProfiler(size_t max_events) : max_events_(max_events), num_events_(0) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 32 | tags_ = std::make_unique<const char *[]>(max_events_); |
| 33 | start_ticks_ = std::make_unique<int32_t[]>(max_events_); |
| 34 | end_ticks_ = std::make_unique<int32_t[]>(max_events_); |
| 35 | } |
| 36 | |
| 37 | uint32_t ArmProfiler::BeginEvent(const char *tag) { |
| 38 | if (num_events_ == max_events_) { |
| 39 | tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_); |
| 40 | num_events_ = 0; |
| 41 | } |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 42 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 43 | tags_[num_events_] = tag; |
| 44 | start_ticks_[num_events_] = GetCurrentTimeTicks(); |
| 45 | end_ticks_[num_events_] = start_ticks_[num_events_] - 1; |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 46 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 47 | return num_events_++; |
| 48 | } |
| 49 | |
| 50 | void ArmProfiler::EndEvent(uint32_t event_handle) { |
| 51 | TFLITE_DCHECK(event_handle < max_events_); |
| 52 | end_ticks_[event_handle] = GetCurrentTimeTicks(); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Kristofer Jonsson | a78c7a8 | 2022-02-10 14:17:24 +0100 | [diff] [blame] | 55 | uint64_t ArmProfiler::GetTotalTicks() const { |
| 56 | uint64_t ticks = 0; |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 57 | |
| 58 | for (size_t i = 0; i < num_events_; ++i) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 59 | ticks += end_ticks_[i] - start_ticks_[i]; |
| 60 | } |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 61 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 62 | return ticks; |
| 63 | } |
| 64 | |
Jonny Svärd | 2ebaac7 | 2022-05-10 17:29:30 +0200 | [diff] [blame] | 65 | void ArmProfiler::ReportResults() const { |
| 66 | tflite::GetMicroErrorReporter()->Report("Profiler report, CPU cycles per operator:"); |
| 67 | for (size_t i = 0; i < num_events_; ++i) { |
| 68 | tflite::GetMicroErrorReporter()->Report( |
| 69 | "%s : cycle_cnt : %u cycles", tags_[i], end_ticks_[i] - start_ticks_[i]); |
| 70 | } |
| 71 | } |
| 72 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 73 | } // namespace tflite |