blob: b8f9089e56af641a544be2521b0992030e023389 [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 {
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010048 pmu_counters counters;
alexander3c798932021-03-26 21:42:19 +000049 };
50
51 /* A collection of profiling units. */
52 using ProfilingSeries = std::vector<arm::app::ProfilingUnit>;
53
54 /* A map for string identifiable profiling series. */
55 using ProfilingMap = std::map<std::string, ProfilingSeries>;
56
57 /**
58 * @brief A very simple profiler example using the platform timer
59 * implementation.
60 */
61 class Profiler {
62 public:
63 /**
64 * @brief Constructor for profiler.
65 * @param[in] platform Pointer to a valid, initialised hal platform.
66 * @param[in] name A friendly name for this profiler.
67 **/
68 Profiler(hal_platform* platform, const char* name);
69
70 /** Block the default constructor. */
71 Profiler() = delete;
72
73 /** Default destructor. */
74 ~Profiler() = default;
75
76 /** @brief Start profiling => get starting time-stamp. */
77 bool StartProfiling(const char* name = nullptr);
78
79 /** @brief Stop profiling => get the ending time-stamp. */
80 bool StopProfiling();
81
82 /** @brief Stops the profiling and internally resets the
83 * platform timers. */
84 bool StopProfilingAndReset();
85
86 /** @brief Reset the platform timers. */
87 void Reset();
88
89 /**
Isabella Gottardi8df12f32021-04-07 17:15:31 +010090 * @brief Collects profiling results statistics and resets the profiler.
alexander3c798932021-03-26 21:42:19 +000091 **/
Isabella Gottardi8df12f32021-04-07 17:15:31 +010092 void GetAllResultsAndReset(std::vector<ProfileResult>& results);
93
94 /**
95 * @brief Prints collected profiling results and resets the profiler.
96 **/
97 void PrintProfilingResult(bool printFullStat = false);
alexander3c798932021-03-26 21:42:19 +000098
99 /** @brief Set the profiler name. */
100 void SetName(const char* str);
101
102 private:
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100103 ProfilingMap m_series; /* Profiling series map. */
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100104 pmu_counters m_tstampSt{}; /* Container for a current starting timestamp. */
105 pmu_counters m_tstampEnd{}; /* Container for a current ending timestamp. */
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100106 hal_platform * m_pPlatform = nullptr; /* Platform pointer - to get the timer. */
alexander3c798932021-03-26 21:42:19 +0000107
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100108 bool m_started = false; /* Indicates profiler has been started. */
alexander3c798932021-03-26 21:42:19 +0000109
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100110 std::string m_name; /* Name given to this profiler. */
alexander3c798932021-03-26 21:42:19 +0000111
112 /**
113 * @brief Appends the profiling unit computed by the "start" and
114 * "end" timestamps to the profiling series identified by
115 * the name provided.
116 * @param[in] start Starting time-stamp.
117 * @param[in] end Ending time-stamp.
118 * @param[in] name Name for the profiling unit series to be
119 * appended to.
120 **/
Kshitij Sisodiada2ec062022-04-01 14:43:53 +0100121 void AddProfilingUnit(pmu_counters start, pmu_counters end,
alexanderc350cdc2021-04-29 20:36:09 +0100122 const std::string& name);
alexander3c798932021-03-26 21:42:19 +0000123 };
124
125} /* namespace app */
126} /* namespace arm */
127
128#endif /* APP_PROFILER_HPP */