aboutsummaryrefslogtreecommitdiff
path: root/lib/arm_profiler/src/arm_profiler.cpp
blob: 7648584e5e79ae5aec740674bd7eb53f792f9644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_time.h"

#include <string.h>

#include "arm_profiler.hpp"
#include <inttypes.h>
#include <stdio.h>

namespace tflite {

ArmProfiler::ArmProfiler(size_t max_events) : max_events_(max_events), num_events_(0) {
    tags_        = std::make_unique<const char *[]>(max_events_);
    start_ticks_ = std::make_unique<int32_t[]>(max_events_);
    end_ticks_   = std::make_unique<int32_t[]>(max_events_);
}

uint32_t ArmProfiler::BeginEvent(const char *tag) {
    if (num_events_ == max_events_) {
        tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_);
        num_events_ = 0;
    }

    tags_[num_events_]        = tag;
    start_ticks_[num_events_] = GetCurrentTimeTicks();
    end_ticks_[num_events_]   = start_ticks_[num_events_] - 1;

    return num_events_++;
}

void ArmProfiler::EndEvent(uint32_t event_handle) {
    TFLITE_DCHECK(event_handle < max_events_);
    end_ticks_[event_handle] = GetCurrentTimeTicks();
}

uint64_t ArmProfiler::GetTotalTicks() const {
    uint64_t ticks = 0;

    for (size_t i = 0; i < num_events_; ++i) {
        ticks += end_ticks_[i] - start_ticks_[i];
    }

    return ticks;
}

void ArmProfiler::ReportResults() const {
    tflite::GetMicroErrorReporter()->Report("Profiler report, CPU cycles per operator:");
    for (size_t i = 0; i < num_events_; ++i) {
        tflite::GetMicroErrorReporter()->Report(
            "%s : cycle_cnt : %u cycles", tags_[i], end_ticks_[i] - start_ticks_[i]);
    }
}

} // namespace tflite