|
|
|
@ -21,8 +21,8 @@ perf_window_initialize(struct perf_window *perf_window)
|
|
|
|
|
|
|
|
|
|
lock_init(&perf_window->lock);
|
|
|
|
|
perf_window->count = 0;
|
|
|
|
|
memset(perf_window->by_duration, 0, sizeof(struct execution_node) * PERF_WINDOW_BUFFER_SIZE);
|
|
|
|
|
memset(perf_window->by_termination, 0, sizeof(uint16_t) * PERF_WINDOW_BUFFER_SIZE);
|
|
|
|
|
memset(perf_window->by_duration, 0, sizeof(struct execution_node) * perf_window_capacity);
|
|
|
|
|
memset(perf_window->by_termination, 0, sizeof(uint16_t) * perf_window_capacity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -38,8 +38,8 @@ perf_window_swap(struct perf_window *perf_window, uint16_t first_by_duration_idx
|
|
|
|
|
{
|
|
|
|
|
assert(lock_is_locked(&perf_window->lock));
|
|
|
|
|
assert(perf_window != NULL);
|
|
|
|
|
assert(first_by_duration_idx >= 0 && first_by_duration_idx < PERF_WINDOW_BUFFER_SIZE);
|
|
|
|
|
assert(second_by_duration_idx >= 0 && second_by_duration_idx < PERF_WINDOW_BUFFER_SIZE);
|
|
|
|
|
assert(first_by_duration_idx < perf_window_capacity);
|
|
|
|
|
assert(second_by_duration_idx < perf_window_capacity);
|
|
|
|
|
|
|
|
|
|
uint16_t first_by_termination_idx = perf_window->by_duration[first_by_duration_idx].by_termination_idx;
|
|
|
|
|
uint16_t second_by_termination_idx = perf_window->by_duration[second_by_duration_idx].by_termination_idx;
|
|
|
|
@ -51,11 +51,11 @@ perf_window_swap(struct perf_window *perf_window, uint16_t first_by_duration_idx
|
|
|
|
|
uint64_t first_execution_time = perf_window->by_duration[first_by_duration_idx].execution_time;
|
|
|
|
|
uint64_t second_execution_time = perf_window->by_duration[second_by_duration_idx].execution_time;
|
|
|
|
|
|
|
|
|
|
/* Swap Indices in Buffer*/
|
|
|
|
|
/* Swap indices */
|
|
|
|
|
perf_window->by_termination[first_by_termination_idx] = second_by_duration_idx;
|
|
|
|
|
perf_window->by_termination[second_by_termination_idx] = first_by_duration_idx;
|
|
|
|
|
|
|
|
|
|
/* Swap by_termination_idx */
|
|
|
|
|
/* Swap nodes */
|
|
|
|
|
struct execution_node tmp_node = perf_window->by_duration[first_by_duration_idx];
|
|
|
|
|
perf_window->by_duration[first_by_duration_idx] = perf_window->by_duration[second_by_duration_idx];
|
|
|
|
|
perf_window->by_duration[second_by_duration_idx] = tmp_node;
|
|
|
|
@ -67,66 +67,73 @@ perf_window_swap(struct perf_window *perf_window, uint16_t first_by_duration_idx
|
|
|
|
|
== second_execution_time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
perf_window_fill(struct perf_window *perf_window, uint64_t newest_execution_time)
|
|
|
|
|
{
|
|
|
|
|
for (uint16_t i = 0; i < perf_window_capacity; i++) {
|
|
|
|
|
perf_window->by_termination[i] = i;
|
|
|
|
|
perf_window->by_duration[i] = (struct execution_node){ .execution_time = newest_execution_time,
|
|
|
|
|
.by_termination_idx = i };
|
|
|
|
|
}
|
|
|
|
|
perf_window->count = perf_window_capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a new value to the perf window
|
|
|
|
|
* Adds newest_execution_time to the perf window
|
|
|
|
|
* Not intended to be called directly!
|
|
|
|
|
* @param perf_window
|
|
|
|
|
* @param value
|
|
|
|
|
* @param newest_execution_time
|
|
|
|
|
*/
|
|
|
|
|
static inline void
|
|
|
|
|
perf_window_add(struct perf_window *perf_window, uint64_t value)
|
|
|
|
|
perf_window_add(struct perf_window *perf_window, uint64_t newest_execution_time)
|
|
|
|
|
{
|
|
|
|
|
assert(perf_window != NULL);
|
|
|
|
|
/* Assumption: A successful invocation should run for a non-zero amount of time */
|
|
|
|
|
assert(newest_execution_time > 0);
|
|
|
|
|
|
|
|
|
|
uint16_t idx_of_oldest;
|
|
|
|
|
uint16_t idx_to_replace;
|
|
|
|
|
uint64_t previous_execution_time;
|
|
|
|
|
bool check_up;
|
|
|
|
|
|
|
|
|
|
if (unlikely(!lock_is_locked(&perf_window->lock))) panic("lock not held when calling perf_window_add\n");
|
|
|
|
|
|
|
|
|
|
/* A successful invocation should run for a non-zero amount of time */
|
|
|
|
|
assert(value > 0);
|
|
|
|
|
|
|
|
|
|
/* If count is 0, then fill entire array with initial execution times */
|
|
|
|
|
/* If perf window is empty, fill all elements with newest_execution_time */
|
|
|
|
|
if (perf_window->count == 0) {
|
|
|
|
|
for (int i = 0; i < PERF_WINDOW_BUFFER_SIZE; i++) {
|
|
|
|
|
perf_window->by_termination[i] = i;
|
|
|
|
|
perf_window->by_duration[i] = (struct execution_node){ .execution_time = value,
|
|
|
|
|
.by_termination_idx = i };
|
|
|
|
|
}
|
|
|
|
|
perf_window->count = PERF_WINDOW_BUFFER_SIZE;
|
|
|
|
|
perf_window_fill(perf_window, newest_execution_time);
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Otherwise, replace the oldest value, and then sort */
|
|
|
|
|
idx_of_oldest = perf_window->by_termination[perf_window->count % PERF_WINDOW_BUFFER_SIZE];
|
|
|
|
|
check_up = value > perf_window->by_duration[idx_of_oldest].execution_time;
|
|
|
|
|
|
|
|
|
|
perf_window->by_duration[idx_of_oldest].execution_time = value;
|
|
|
|
|
/* If full, replace the oldest execution_time. Save the old execution time to know which direction to swap */
|
|
|
|
|
idx_to_replace = perf_window->by_termination[perf_window->count % perf_window_capacity];
|
|
|
|
|
previous_execution_time = perf_window->by_duration[idx_to_replace].execution_time;
|
|
|
|
|
perf_window->by_duration[idx_to_replace].execution_time = newest_execution_time;
|
|
|
|
|
|
|
|
|
|
if (check_up) {
|
|
|
|
|
for (uint16_t i = idx_of_oldest;
|
|
|
|
|
i + 1 < PERF_WINDOW_BUFFER_SIZE
|
|
|
|
|
/* At this point, the by_duration array is partially sorted. The node we overwrote needs to be shifted left or
|
|
|
|
|
* right. We can determine which direction to shift by comparing with the previous execution time. */
|
|
|
|
|
if (newest_execution_time > previous_execution_time) {
|
|
|
|
|
for (uint16_t i = idx_to_replace;
|
|
|
|
|
i + 1 < perf_window_capacity
|
|
|
|
|
&& perf_window->by_duration[i + 1].execution_time < perf_window->by_duration[i].execution_time;
|
|
|
|
|
i++) {
|
|
|
|
|
perf_window_swap(perf_window, i, i + 1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (int i = idx_of_oldest;
|
|
|
|
|
i - 1 >= 0
|
|
|
|
|
for (uint16_t i = idx_to_replace;
|
|
|
|
|
i >= 1
|
|
|
|
|
&& perf_window->by_duration[i - 1].execution_time > perf_window->by_duration[i].execution_time;
|
|
|
|
|
i--) {
|
|
|
|
|
perf_window_swap(perf_window, i, i - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The idx that we replaces should still point to the same value */
|
|
|
|
|
assert(perf_window->by_duration[perf_window->by_termination[perf_window->count % PERF_WINDOW_BUFFER_SIZE]]
|
|
|
|
|
/* The idx that we replaces should still point to the same newest_execution_time */
|
|
|
|
|
assert(perf_window->by_duration[perf_window->by_termination[perf_window->count % perf_window_capacity]]
|
|
|
|
|
.execution_time
|
|
|
|
|
== value);
|
|
|
|
|
== newest_execution_time);
|
|
|
|
|
|
|
|
|
|
/* The by_duration array should be ordered by execution time */
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
for (int i = 1; i < PERF_WINDOW_BUFFER_SIZE; i++) {
|
|
|
|
|
for (int i = 1; i < perf_window_capacity; i++) {
|
|
|
|
|
assert(perf_window->by_duration[i - 1].execution_time <= perf_window->by_duration[i].execution_time);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
@ -151,10 +158,10 @@ perf_window_get_percentile(struct perf_window *perf_window, uint8_t percentile,
|
|
|
|
|
assert(percentile >= 50 && percentile <= 99);
|
|
|
|
|
assert(perf_window->count > 0);
|
|
|
|
|
|
|
|
|
|
if (likely(perf_window->count >= PERF_WINDOW_BUFFER_SIZE))
|
|
|
|
|
return perf_window->by_duration[precomputed_index].execution_time;
|
|
|
|
|
int idx = precomputed_index;
|
|
|
|
|
if (unlikely(perf_window->count < perf_window_capacity)) idx = perf_window->count * percentile / 100;
|
|
|
|
|
|
|
|
|
|
return perf_window->by_duration[perf_window->count * percentile / 100].execution_time;
|
|
|
|
|
return perf_window->by_duration[idx].execution_time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|