rusEFI
An attempt to build an Engine Control Unit
Functions | Variables
histogram.c File Reference

Detailed Description

This data structure is used to analyze CPU performance.

Histogram is a data structure which simplifies CPU performance monitoring and trobleshooting by tracking the min, max and a couple of median values for a series of measurments.

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

Definition in file histogram.c.

Functions

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

Variables

static float confidence_bounds [] = { 0.5 - H_CONFIDENCE * 0.5, 0.5, 0.5 + H_CONFIDENCE * 0.5 }
 
static int64_t bounds [BOUND_LENGTH] CCM_OPTIONAL
 
static int small_bounds_index [SBI_SIZE]
 
static int initialized = FALSE
 

Function Documentation

◆ histogramGetIndex()

int histogramGetIndex ( int64_t  value)

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

Definition at line 65 of file histogram.c.

65  {
66  efiAssert(CUSTOM_ERR_ASSERT, initialized, "histo initialized", 0);
67  if (value < 0)
68  return 0;
69  if (value < SBI_SIZE)
70  return small_bounds_index[(int) value];
71  int l = small_bounds_index[SBI_SIZE - 1];
72  int r = BOUND_LENGTH - 1;
73  while (l < r) {
74  int m = (l + r) >> 1;
75  if (bounds[m] > value)
76  r = m - 1;
77  else if (bounds[m + 1] <= value)
78  l = m + 1;
79  else
80  return m;
81  }
82  return l;
83 }
static int small_bounds_index[SBI_SIZE]
Definition: histogram.c:36
static int initialized
Definition: histogram.c:38

Referenced by hsAdd().

◆ hsAdd()

void hsAdd ( histogram_s h,
int64_t  value 
)

@breif Add a new value into histogram_s

Definition at line 101 of file histogram.c.

101  {
102  int index = histogramGetIndex(value);
103  int count = 1;
104  h->total_value += value;
105  h->total_count += count;
106  efiAssertVoid(CUSTOM_ERR_6670, index < BOUND_LENGTH, "histogram issue");
107 
108  h->values[index] += count;
109 }
int histogramGetIndex(int64_t value)
This internal method is only public so that we can test it.
Definition: histogram.c:65
int64_t total_value
Definition: histogram.h:29
int values[BOUND_LENGTH]
Definition: histogram.h:31
int64_t total_count
Definition: histogram.h:30
static int count
Definition: bench_test.cpp:121
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 115 of file histogram.c.

115  {
116  int index = 0;
117 
118  if (h->total_count <= 5) {
119  for (int j = 0; j < BOUND_LENGTH; j++) {
120  for (int k = 0; k < h->values[j]; k++) {
121  report[index++] = (bounds[j] + bounds[j + 1]) / 2;
122  }
123  }
124  return index;
125  }
126 
127  int minIndex = 0;
128  while (h->values[minIndex] == 0) {
129  minIndex++;
130  }
131  report[index++] = h->values[minIndex];
132 
133  int64_t acc = 0;
134  // 'acc' is accumulated number of samples in [0, min - 1].
135  for (int j = 0; j < 3; j++) {
136  int64_t k = confidence_bounds[j] * h->total_count;
137  // Always drop at least 1 'non-confident' sample...
138  if (k == 0) {
139  k = 1;
140  }
141  if (k == h->total_count) {
142  k = h->total_count - 1;
143  }
144  // 'k' is desired number of samples.
145  while (acc + h->values[minIndex] < k)
146  acc += h->values[minIndex++];
147  if (k < h->total_count / 2) // Converge to median (from left).
148  while (acc + h->values[minIndex] <= k)
149  acc += h->values[minIndex++];
150  // Now: acc <= k <= acc + st.histogram[min]
151  // And desired number of samples is within [min, min + 1)
152  float d = bounds[minIndex];
153  if (acc != k)
154  d += (bounds[minIndex + 1] - 1 - bounds[minIndex]) * (k - acc) / h->values[minIndex];
155  report[index++] = (int) d;
156  }
157 
158  int maxIndex = BOUND_LENGTH - 1;
159  while (h->values[maxIndex] == 0)
160  maxIndex--;
161  int64_t maxValue = bounds[maxIndex + 1] - 1;
162  report[index++] = maxValue;
163 
164  return index;
165 }
int values[BOUND_LENGTH]
Definition: histogram.h:31
int64_t total_count
Definition: histogram.h:30
static float confidence_bounds[]
Definition: histogram.c:27

Referenced by printHistogram().

◆ initHistogram()

void initHistogram ( histogram_s h,
const char name 
)

Reset histogram_s to orignal state.

Definition at line 88 of file histogram.c.

88  {
89  if (efiStrlen(name) > sizeof(h->name) - 1) {
90  firmwareError(ERROR_HISTO_NAME, "Histogram name [%s] too long", name);
91  }
92  strcpy(h->name, name);
93  h->total_value = 0;
94  h->total_count = 0;
95  memset(h->values, 0, sizeof(h->values));
96 }
void firmwareError(obd_code_e, const char *,...)
int64_t total_value
Definition: histogram.h:29
uint32_t efiStrlen(const char *param)
Definition: efilib.cpp:70
char name[16]
Definition: histogram.h:28
int values[BOUND_LENGTH]
Definition: histogram.h:31
int64_t total_count
Definition: histogram.h:30

Referenced by initWaveChart().

Here is the call graph for this function:

◆ initHistogramsModule()

void initHistogramsModule ( void  )

@breif Internal histogram data structure

Definition at line 43 of file histogram.c.

43  {
44  bounds[0] = 0;
45  for (int i = 1; i < BOUND_LENGTH; i++) {
46  int64_t prev = bounds[i - 1];
47  int64_t next = prev + (int64_t) ((double) prev * H_ACCURACY);
48  if (next == prev) // Ensure minimum step for small numbers.
49  next = prev + 1;
50  if (next < prev) // Overflow over Long.MAX_VALUE occurred.
51  next = LONG_MAX_INT;
52  bounds[i] = next;
53  }
54  bounds[BOUND_LENGTH - 1] = LONG_MAX_INT;
55 
56  for (int i = 0, j = 0; j < SBI_SIZE; i++)
57  while (j < bounds[i + 1] && j < SBI_SIZE)
58  small_bounds_index[j++] = i;
59  initialized = TRUE;
60 }
static int small_bounds_index[SBI_SIZE]
Definition: histogram.c:36
static int initialized
Definition: histogram.c:38

Referenced by adc_callback_fast_internal().

Variable Documentation

◆ CCM_OPTIONAL

int64_t bounds [BOUND_LENGTH] CCM_OPTIONAL
static

magic curve lookup table

Definition at line 32 of file histogram.c.

◆ confidence_bounds

float confidence_bounds[] = { 0.5 - H_CONFIDENCE * 0.5, 0.5, 0.5 + H_CONFIDENCE * 0.5 }
static

Definition at line 27 of file histogram.c.

Referenced by hsReport().

◆ initialized

int initialized = FALSE
static

Definition at line 38 of file histogram.c.

Referenced by histogramGetIndex(), and initHistogramsModule().

◆ small_bounds_index

int small_bounds_index[SBI_SIZE]
static

just an optimization - faster lookup for small values

Definition at line 36 of file histogram.c.

Referenced by histogramGetIndex(), and initHistogramsModule().

Go to the source code of this file.