rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Data Structures | Functions
histogram.h File Reference

Detailed Description

This data structure is used to analyze CPU performance.

User can report data into a histogram and later get an aggregated represenation, i.e. a histogram, of these values.

A typicsl use-case would be reporting the times it took to execure a particular section of code - and later analyzing the histogram.

Date
Dec 18, 2013
Author
Andrey Belomutskiy, (c) 2012-2020

Definition in file histogram.h.

Data Structures

struct  histogram_s
 

Functions

void initHistogramsModule (void)
 
int histogramGetIndex (int64_t value)
 This internal method is only public so that we can test it.
 
void initHistogram (histogram_s *h, const char *name)
 Reset histogram_s to orignal state.
 
void hsAdd (histogram_s *h, int64_t value)
 
int hsReport (histogram_s *h, int *report)
 Prepare histogram report.
 

Function Documentation

◆ histogramGetIndex()

int histogramGetIndex ( int64_t  value)

This internal method is only public so that we can test it.

Definition at line 66 of file histogram.cpp.

66 {
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}
static int initialized
Definition histogram.cpp:39
static int small_bounds_index[SBI_SIZE]
Definition histogram.cpp:37
@ CUSTOM_ERR_ASSERT

Referenced by hsAdd().

Here is the caller graph for this function:

◆ hsAdd()

void hsAdd ( histogram_s h,
int64_t  value 
)

@breif Add a new value into histogram_s

Definition at line 102 of file histogram.cpp.

102 {
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}
int histogramGetIndex(int64_t value)
This internal method is only public so that we can test it.
Definition histogram.cpp:66
@ CUSTOM_ERR_6670
int64_t total_value
Definition histogram.h:29
int values[BOUND_LENGTH]
Definition histogram.h:31
int64_t total_count
Definition histogram.h:30
uint16_t count
Definition tunerstudio.h:1
Here is the call graph for this function:

◆ hsReport()

int hsReport ( histogram_s h,
int *  report 
)

Prepare histogram report.

Note
This report should be displayed using 'printHistogram' method

Definition at line 116 of file histogram.cpp.

116 {
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}
static float confidence_bounds[]
Definition histogram.cpp:28

Referenced by printHistogram().

Here is the caller graph for this function:

◆ initHistogram()

void initHistogram ( histogram_s h,
const char name 
)

Reset histogram_s to orignal state.

Definition at line 89 of file histogram.cpp.

89 {
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}
void firmwareError(ObdCode code, const char *fmt,...)
@ ERROR_HISTO_NAME
char name[16]
Definition histogram.h:28

Referenced by initWaveChart().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initHistogramsModule()

void initHistogramsModule ( void  )

@breif Internal histogram data structure

Definition at line 44 of file histogram.cpp.

44 {
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}

Referenced by initHardwareNoConfig().

Here is the caller graph for this function:

Go to the source code of this file.