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