blob: e6244ffa6f8734175a821f50a553d7b36681dfa4 [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;
Richard Burton2c2cacd2022-05-13 09:46:57 +010037 std::uint32_t samplesNum = 0;
Isabella Gottardi8df12f32021-04-07 17:15:31 +010038 };
39
40 /** Profiling results with calculated statistics. */
41 struct ProfileResult {
42 std::string name;
43 std::uint32_t samplesNum;
44 std::vector<Statistics> data;
45 };
46
alexander3c798932021-03-26 21:42:19 +000047 /** A single profiling unit definition. */
48 struct ProfilingUnit {
Kshitij Sisodiada2ec062022-04-01 14:43:53 +010049 pmu_counters counters;
alexander3c798932021-03-26 21:42:19 +000050 };
51
Richard Burton2c2cacd2022-05-13 09:46:57 +010052 /* A map for string identifiable profiling statistics. */
53 using ProfilingStats = std::map<std::string, std::vector<Statistics>>;
alexander3c798932021-03-26 21:42:19 +000054
55 /**
56 * @brief A very simple profiler example using the platform timer
57 * implementation.
58 */
59 class Profiler {
60 public:
61 /**
62 * @brief Constructor for profiler.
alexander3c798932021-03-26 21:42:19 +000063 * @param[in] name A friendly name for this profiler.
64 **/
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010065 Profiler(const char* name);
alexander3c798932021-03-26 21:42:19 +000066
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010067 /** Default constructor. */
68 Profiler();
alexander3c798932021-03-26 21:42:19 +000069
70 /** Default destructor. */
71 ~Profiler() = default;
72
73 /** @brief Start profiling => get starting time-stamp. */
74 bool StartProfiling(const char* name = nullptr);
75
76 /** @brief Stop profiling => get the ending time-stamp. */
77 bool StopProfiling();
78
79 /** @brief Stops the profiling and internally resets the
80 * platform timers. */
81 bool StopProfilingAndReset();
82
83 /** @brief Reset the platform timers. */
84 void Reset();
85
86 /**
Isabella Gottardi8df12f32021-04-07 17:15:31 +010087 * @brief Collects profiling results statistics and resets the profiler.
alexander3c798932021-03-26 21:42:19 +000088 **/
Isabella Gottardi8df12f32021-04-07 17:15:31 +010089 void GetAllResultsAndReset(std::vector<ProfileResult>& results);
90
91 /**
92 * @brief Prints collected profiling results and resets the profiler.
93 **/
94 void PrintProfilingResult(bool printFullStat = false);
alexander3c798932021-03-26 21:42:19 +000095
96 /** @brief Set the profiler name. */
97 void SetName(const char* str);
98
99 private:
Richard Burton2c2cacd2022-05-13 09:46:57 +0100100 ProfilingStats m_profStats; /* Profiling stats map. */
101 pmu_counters m_tstampSt{}; /* Container for a current starting timestamp. */
102 pmu_counters m_tstampEnd{}; /* Container for a current ending timestamp. */
103 bool m_started = false; /* Indicates profiler has been started. */
104 std::string m_name; /* Name given to this profiler. */
105
alexander3c798932021-03-26 21:42:19 +0000106
107 /**
Richard Burton2c2cacd2022-05-13 09:46:57 +0100108 * @brief Updates the running average stats with those computed
109 * by the "start" and "end" timestamps for the profiling
110 * stats identified by the name provided.
alexander3c798932021-03-26 21:42:19 +0000111 * @param[in] start Starting time-stamp.
112 * @param[in] end Ending time-stamp.
Richard Burton2c2cacd2022-05-13 09:46:57 +0100113 * @param[in] name Name for the profiling running stats series to be
114 * updated.
alexander3c798932021-03-26 21:42:19 +0000115 **/
Richard Burton2c2cacd2022-05-13 09:46:57 +0100116 void UpdateRunningStats(pmu_counters start, pmu_counters end,
117 const std::string& name);
alexander3c798932021-03-26 21:42:19 +0000118 };
119
120} /* namespace app */
121} /* namespace arm */
122
123#endif /* APP_PROFILER_HPP */