blob: 503d805d43eb4f31640108004c933c121e1f697f [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Isabella Gottardic64f5062022-01-21 15:27:13 +00002 * Copyright (c) 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://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,
13 * WITHOUT 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#ifndef APP_PROFILER_HPP
18#define APP_PROFILER_HPP
19
20#include "hal.h"
21
22#include <string>
23#include <map>
24#include <vector>
25
26namespace arm {
27namespace app {
28
Isabella Gottardi8df12f32021-04-07 17:15:31 +010029 /** Statistics for a profiling metric. */
30 struct Statistics {
31 std::string name;
32 std::string unit;
33 std::uint64_t total;
34 double avrg;
35 std::uint64_t min;
36 std::uint64_t max;
37 };
38
39 /** Profiling results with calculated statistics. */
40 struct ProfileResult {
41 std::string name;
42 std::uint32_t samplesNum;
43 std::vector<Statistics> data;
44 };
45
alexander3c798932021-03-26 21:42:19 +000046 /** A single profiling unit definition. */
47 struct ProfilingUnit {
48 uint64_t npuCycles = 0;
49 uint64_t activeNpuCycles = 0;
Isabella Gottardi8df12f32021-04-07 17:15:31 +010050 uint64_t idleNpuCycles = 0;
51 uint64_t axi0writes = 0;
52 uint64_t axi0reads = 0;
53 uint64_t axi1reads = 0;
alexander3c798932021-03-26 21:42:19 +000054 uint64_t cpuCycles = 0;
55 time_t time = 0;
56 };
57
58 /* A collection of profiling units. */
59 using ProfilingSeries = std::vector<arm::app::ProfilingUnit>;
60
61 /* A map for string identifiable profiling series. */
62 using ProfilingMap = std::map<std::string, ProfilingSeries>;
63
64 /**
65 * @brief A very simple profiler example using the platform timer
66 * implementation.
67 */
68 class Profiler {
69 public:
70 /**
71 * @brief Constructor for profiler.
72 * @param[in] platform Pointer to a valid, initialised hal platform.
73 * @param[in] name A friendly name for this profiler.
74 **/
75 Profiler(hal_platform* platform, const char* name);
76
77 /** Block the default constructor. */
78 Profiler() = delete;
79
80 /** Default destructor. */
81 ~Profiler() = default;
82
83 /** @brief Start profiling => get starting time-stamp. */
84 bool StartProfiling(const char* name = nullptr);
85
86 /** @brief Stop profiling => get the ending time-stamp. */
87 bool StopProfiling();
88
89 /** @brief Stops the profiling and internally resets the
90 * platform timers. */
91 bool StopProfilingAndReset();
92
93 /** @brief Reset the platform timers. */
94 void Reset();
95
96 /**
Isabella Gottardi8df12f32021-04-07 17:15:31 +010097 * @brief Collects profiling results statistics and resets the profiler.
alexander3c798932021-03-26 21:42:19 +000098 **/
Isabella Gottardi8df12f32021-04-07 17:15:31 +010099 void GetAllResultsAndReset(std::vector<ProfileResult>& results);
100
101 /**
102 * @brief Prints collected profiling results and resets the profiler.
103 **/
104 void PrintProfilingResult(bool printFullStat = false);
alexander3c798932021-03-26 21:42:19 +0000105
106 /** @brief Set the profiler name. */
107 void SetName(const char* str);
108
109 private:
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100110 ProfilingMap m_series; /* Profiling series map. */
111 time_counter m_tstampSt{}; /* Container for a current starting timestamp. */
112 time_counter m_tstampEnd{}; /* Container for a current ending timestamp. */
113 hal_platform * m_pPlatform = nullptr; /* Platform pointer - to get the timer. */
alexander3c798932021-03-26 21:42:19 +0000114
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100115 bool m_started = false; /* Indicates profiler has been started. */
alexander3c798932021-03-26 21:42:19 +0000116
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100117 std::string m_name; /* Name given to this profiler. */
alexander3c798932021-03-26 21:42:19 +0000118
119 /**
120 * @brief Appends the profiling unit computed by the "start" and
121 * "end" timestamps to the profiling series identified by
122 * the name provided.
123 * @param[in] start Starting time-stamp.
124 * @param[in] end Ending time-stamp.
125 * @param[in] name Name for the profiling unit series to be
126 * appended to.
127 **/
alexanderc350cdc2021-04-29 20:36:09 +0100128 void AddProfilingUnit(time_counter start, time_counter end,
129 const std::string& name);
alexander3c798932021-03-26 21:42:19 +0000130 };
131
132} /* namespace app */
133} /* namespace arm */
134
135#endif /* APP_PROFILER_HPP */