AceRoutine  1.5.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
LogBinTableRenderer.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_TABLE_RENDERER_H
26 #define ACE_ROUTINE_LOG_BIN_TABLE_RENDERER_H
27 
28 #include <stdint.h> // uint8_t, uint32_t
29 #include <Arduino.h> // Print
30 #include "Coroutine.h" // Coroutine
31 #include "LogBinProfiler.h" // rollupExteriorBins()
32 
33 namespace ace_routine {
34 
35 namespace internal {
36 
38 const uint8_t kNumBinLabels = 32;
39 
41 extern const char* const kBinLabels[kNumBinLabels] PROGMEM;
42 
50 void printHeaderTo( Print& printer, uint8_t startBin, uint8_t endBin);
51 
57 void printPStringTo(Print& printer, const char* s, uint8_t boxSize);
58 
70 void printBinsTo(
71  Print& printer,
72  const uint16_t bins[],
73  uint8_t numBins,
74  uint8_t startBin,
75  uint8_t endBin);
76 
77 } // namespace internal
78 
104 template <typename T_COROUTINE>
106  public:
109 
124  static void printTo(
125  Print& printer,
126  uint8_t startBin,
127  uint8_t endBin,
128  bool clear = true,
129  bool rollup = true
130  ) {
131  if (endBin <= startBin) return;
132 
133  uint16_t bufBins[Profiler::kNumBins];
134 
135  bool isHeaderPrinted = false;
136  T_COROUTINE** root = T_COROUTINE::getRoot();
137  for (T_COROUTINE** p = root; (*p) != nullptr; p = (*p)->getNext()) {
138  auto* profiler = (Profiler*) (*p)->getProfiler();
139  if (! profiler) continue;
140 
141  // Print header if needed.
142  if (! isHeaderPrinted) {
143  printer.print(F("name "));
144  internal::printHeaderTo(printer, startBin, endBin);
145  printer.println();
146  isHeaderPrinted = true;
147  }
148 
149  // Print the coroutine name, truncated to 12 characters. The next column
150  // is a 5-digit number formatted into a 6-character block, so there will
151  // always be a space between the coroutine name and the next number.
152  (*p)->printNameTo(printer, 12);
153 
154  // Roll up the exterior bins in to the first and last bins if requested.
155  const uint16_t* bins;
156  if (rollup) {
157  internal::rollupExteriorBins(
158  bufBins, profiler->mBins, Profiler::kNumBins, startBin, endBin);
159  bins = bufBins;
160  } else {
161  bins = profiler->mBins;
162  }
163 
164  // Print the bins. The number of digits is automatically a log10() of
165  // the counts, so we should be able to visually scan the table and see
166  // which coroutine is taking too long.
167  internal::printBinsTo(
168  printer, bins, Profiler::kNumBins, startBin, endBin);
169  printer.println();
170 
171  if (clear) {
172  profiler->clear();
173  }
174  }
175  }
176 };
177 
178 using LogBinTableRenderer = LogBinTableRendererTemplate<Coroutine>;
179 
180 } // namespace ace_routine
181 
182 #endif
ace_routine::LogBinTableRendererTemplate
Print the information in the LogBinProfiler for each Coroutine in a human-readable table.
Definition: LogBinTableRenderer.h:105
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::LogBinTableRendererTemplate::printTo
static void printTo(Print &printer, uint8_t startBin, uint8_t endBin, bool clear=true, bool rollup=true)
Loop over all coroutines and print the ASCII version of the frequency distribution.
Definition: LogBinTableRenderer.h:124
ace_routine::LogBinProfilerTemplate::kNumBins
static const uint8_t kNumBins
Number of event counter bins used by this class.
Definition: LogBinProfiler.h:57
Coroutine.h