rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
histogram.cpp
Go to the documentation of this file.
1/**
2 * @file histogram.c
3 * @brief This data structure is used to analyze CPU performance
4 *
5 * Histogram is a data structure which simplifies CPU performance monitoring and trobleshooting by tracking the min, max
6 * and a couple of median values for a series of measurments.
7 *
8 * @date Dec 18, 2013
9 * @author Andrey Belomutskiy, (c) 2012-2020
10 */
11
12#include "pch.h"
13
14#include <string.h>
15#include "histogram.h"
16
17#if defined(HAS_OS_ACCESS)
18#error "Unexpected OS ACCESS HERE"
19#endif
20
21#if EFI_HISTOGRAMS || EFI_UNIT_TEST
22
23#define H_ACCURACY 0.05
24#define H_CONFIDENCE 0.8
25#define LONG_MAX_INT 0x7fffffffffffffffL
26#define SBI_SIZE 1000
27
28static float confidence_bounds[] = { 0.5 - H_CONFIDENCE * 0.5, 0.5, 0.5 + H_CONFIDENCE * 0.5 };
29
30/**
31 * magic curve lookup table
32 */
33static int64_t bounds[BOUND_LENGTH] CCM_OPTIONAL;
34/**
35 * just an optimization - faster lookup for small values
36 */
37static int small_bounds_index[SBI_SIZE];
38
39static int initialized = FALSE;
40
41/**
42 * @breif Internal histogram data structure
43 */
45 bounds[0] = 0;
46 for (int i = 1; i < BOUND_LENGTH; i++) {
47 int64_t prev = bounds[i - 1];
48 int64_t next = prev + (int64_t) ((double) prev * H_ACCURACY);
49 if (next == prev) // Ensure minimum step for small numbers.
50 next = prev + 1;
51 if (next < prev) // Overflow over Long.MAX_VALUE occurred.
52 next = LONG_MAX_INT;
53 bounds[i] = next;
54 }
55 bounds[BOUND_LENGTH - 1] = LONG_MAX_INT;
56
57 for (int i = 0, j = 0; j < SBI_SIZE; i++)
58 while (j < bounds[i + 1] && j < SBI_SIZE)
59 small_bounds_index[j++] = i;
60 initialized = TRUE;
61}
62
63/**
64 * @brief This internal method is only public so that we can test it.
65 */
66int histogramGetIndex(int64_t value) {
67 efiAssert(ObdCode::CUSTOM_ERR_ASSERT, initialized, "histo initialized", 0);
68 if (value < 0)
69 return 0;
70 if (value < SBI_SIZE)
71 return small_bounds_index[(int) value];
72 int l = small_bounds_index[SBI_SIZE - 1];
73 int r = BOUND_LENGTH - 1;
74 while (l < r) {
75 int m = (l + r) >> 1;
76 if (bounds[m] > value)
77 r = m - 1;
78 else if (bounds[m + 1] <= value)
79 l = m + 1;
80 else
81 return m;
82 }
83 return l;
84}
85
86/**
87 * @brief Reset histogram_s to orignal state
88 */
89void initHistogram(histogram_s *h, const char *name) {
90 if (std::strlen(name) > sizeof(h->name) - 1) {
91 firmwareError(ObdCode::ERROR_HISTO_NAME, "Histogram name [%s] too long", name);
92 }
93 strcpy(h->name, name);
94 h->total_value = 0;
95 h->total_count = 0;
96 memset(h->values, 0, sizeof(h->values));
97}
98
99/**
100 * @breif Add a new value into histogram_s
101 */
102void hsAdd(histogram_s *h, int64_t value) {
103 int index = histogramGetIndex(value);
104 int count = 1;
105 h->total_value += value;
106 h->total_count += count;
107 efiAssertVoid(ObdCode::CUSTOM_ERR_6670, index < BOUND_LENGTH, "histogram issue");
108
109 h->values[index] += count;
110}
111
112/**
113 * @brief Prepare histogram report
114 * @note This report should be displayed using 'printHistogram' method
115 */
116int hsReport(histogram_s *h, int* report) {
117 int index = 0;
118
119 if (h->total_count <= 5) {
120 for (int j = 0; j < BOUND_LENGTH; j++) {
121 for (int k = 0; k < h->values[j]; k++) {
122 report[index++] = (bounds[j] + bounds[j + 1]) / 2;
123 }
124 }
125 return index;
126 }
127
128 int minIndex = 0;
129 while (h->values[minIndex] == 0) {
130 minIndex++;
131 }
132 report[index++] = h->values[minIndex];
133
134 int64_t acc = 0;
135 // 'acc' is accumulated number of samples in [0, min - 1].
136 for (int j = 0; j < 3; j++) {
137 int64_t k = confidence_bounds[j] * h->total_count;
138 // Always drop at least 1 'non-confident' sample...
139 if (k == 0) {
140 k = 1;
141 }
142 if (k == h->total_count) {
143 k = h->total_count - 1;
144 }
145 // 'k' is desired number of samples.
146 while (acc + h->values[minIndex] < k)
147 acc += h->values[minIndex++];
148 if (k < h->total_count / 2) // Converge to median (from left).
149 while (acc + h->values[minIndex] <= k)
150 acc += h->values[minIndex++];
151 // Now: acc <= k <= acc + st.histogram[min]
152 // And desired number of samples is within [min, min + 1)
153 float d = bounds[minIndex];
154 if (acc != k)
155 d += (bounds[minIndex + 1] - 1 - bounds[minIndex]) * (k - acc) / h->values[minIndex];
156 report[index++] = (int) d;
157 }
158
159 int maxIndex = BOUND_LENGTH - 1;
160 while (h->values[maxIndex] == 0)
161 maxIndex--;
162 int64_t maxValue = bounds[maxIndex + 1] - 1;
163 report[index++] = maxValue;
164
165 return index;
166}
167
168#endif /* EFI_HISTOGRAMS */
void firmwareError(ObdCode code, const char *fmt,...)
static float confidence_bounds[]
Definition histogram.cpp:28
int histogramGetIndex(int64_t value)
This internal method is only public so that we can test it.
Definition histogram.cpp:66
void initHistogram(histogram_s *h, const char *name)
Reset histogram_s to orignal state.
Definition histogram.cpp:89
void hsAdd(histogram_s *h, int64_t value)
static int64_t bounds[BOUND_LENGTH] CCM_OPTIONAL
Definition histogram.cpp:33
int hsReport(histogram_s *h, int *report)
Prepare histogram report.
static int initialized
Definition histogram.cpp:39
static int small_bounds_index[SBI_SIZE]
Definition histogram.cpp:37
void initHistogramsModule(void)
Definition histogram.cpp:44
This data structure is used to analyze CPU performance.
@ ERROR_HISTO_NAME
@ CUSTOM_ERR_ASSERT
@ CUSTOM_ERR_6670
int64_t total_value
Definition histogram.h:29
char name[16]
Definition histogram.h:28
int values[BOUND_LENGTH]
Definition histogram.h:31
int64_t total_count
Definition histogram.h:30
uint16_t count
Definition tunerstudio.h:1