AceRoutine  1.5.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
LogBinProfiler.h
1 /*
2 MIT License
3 
4 Copyright (c) 2022 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_ROUTINE_LOG_BIN_PROFILER_H
26 #define ACE_ROUTINE_LOG_BIN_PROFILER_H
27 
28 #include <stdint.h> // uint8_t, uint32_t
29 #include <stdio.h> // memset()
30 #include "Coroutine.h"
31 #include "CoroutineProfiler.h"
32 
33 
34 namespace ace_routine {
35 
53 template <typename T_COROUTINE>
55  public:
57  static const uint8_t kNumBins = 32;
58 
59  public:
62  clear();
63  }
64 
66  void clear() {
67  memset(mBins, 0, sizeof(mBins));
68  }
69 
79  void updateElapsedMicros(uint32_t micros) override {
80  uint8_t index = 0; // [0, 31]
81 
82  while (true) {
83  micros >>= 1;
84  if (micros == 0) break;
85  index++;
86  }
87  uint16_t count = mBins[index];
88  if (count < UINT16_MAX) {
89  mBins[index]++;
90  }
91  }
92 
106  static void createProfilers() {
107  T_COROUTINE** root = T_COROUTINE::getRoot();
108  for (T_COROUTINE** p = root; (*p) != nullptr; p = (*p)->getNext()) {
109  auto* profiler = new LogBinProfilerTemplate();
110  (*p)->setProfiler(profiler);
111  }
112  }
113 
115  static void deleteProfilers() {
116  T_COROUTINE** root = T_COROUTINE::getRoot();
117  for (T_COROUTINE** p = root; (*p) != nullptr; p = (*p)->getNext()) {
118  auto* profiler = (LogBinProfilerTemplate*) (*p)->getProfiler();
119  if (profiler) {
120  delete profiler;
121  (*p)->setProfiler(nullptr);
122  }
123  }
124  }
125 
127  static void clearProfilers() {
128  T_COROUTINE** root = T_COROUTINE::getRoot();
129  for (T_COROUTINE** p = root; (*p) != nullptr; p = (*p)->getNext()) {
130  auto* profiler = (LogBinProfilerTemplate*) (*p)->getProfiler();
131  if (profiler) {
132  profiler->clear();
133  }
134  }
135  }
136 
137  public:
139  uint16_t mBins[kNumBins];
140 };
141 
143 
144 namespace internal {
145 
155 void rollupExteriorBins(
156  uint16_t dst[],
157  const uint16_t src[],
158  uint8_t numBins,
159  uint8_t startBin,
160  uint8_t endBin
161 );
162 
163 }
164 
165 }
166 
167 #endif
ace_routine::LogBinProfilerTemplate::updateElapsedMicros
void updateElapsedMicros(uint32_t micros) override
Update the count for the calculated elapsed time bin.
Definition: LogBinProfiler.h:79
ace_routine::LogBinProfilerTemplate::LogBinProfilerTemplate
LogBinProfilerTemplate()
Constructor.
Definition: LogBinProfiler.h:61
ace_routine::CoroutineProfiler
An interface class for profiling classes that can track the elapsed time consumed by Coroutine::runCo...
Definition: CoroutineProfiler.h:36
ace_routine::LogBinProfilerTemplate::clearProfilers
static void clearProfilers()
Clear counters for all profilers.
Definition: LogBinProfiler.h:127
ace_routine::LogBinProfilerTemplate
Class that maintains the frequency count of the elapsed time of runCoroutine() in an array of bins wh...
Definition: LogBinProfiler.h:54
ace_routine::LogBinProfilerTemplate::kNumBins
static const uint8_t kNumBins
Number of event counter bins used by this class.
Definition: LogBinProfiler.h:57
ace_routine::LogBinProfilerTemplate::clear
void clear()
Clear the bins.
Definition: LogBinProfiler.h:66
ace_routine::LogBinProfilerTemplate::deleteProfilers
static void deleteProfilers()
Delete the profilers created by createProfilers().
Definition: LogBinProfiler.h:115
ace_routine::LogBinProfilerTemplate::createProfilers
static void createProfilers()
Create a new profiler on the heap and attach it to each coroutine.
Definition: LogBinProfiler.h:106
ace_routine::LogBinProfilerTemplate::mBins
uint16_t mBins[kNumBins]
Event count bins.
Definition: LogBinProfiler.h:139
Coroutine.h