|
|
|
@ -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_capacity);
|
|
|
|
|
memset(perf_window->by_termination, 0, sizeof(uint16_t) * perf_window_capacity);
|
|
|
|
|
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 < perf_window_capacity);
|
|
|
|
|
assert(second_by_duration_idx < perf_window_capacity);
|
|
|
|
|
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;
|
|
|
|
@ -70,12 +70,12 @@ perf_window_swap(struct perf_window *perf_window, uint16_t first_by_duration_idx
|
|
|
|
|
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++) {
|
|
|
|
|
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;
|
|
|
|
|
perf_window->count = PERF_WINDOW_CAPACITY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -104,7 +104,7 @@ perf_window_add(struct perf_window *perf_window, uint64_t newest_execution_time)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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];
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
@ -112,7 +112,7 @@ perf_window_add(struct perf_window *perf_window, uint64_t newest_execution_time)
|
|
|
|
|
* 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
|
|
|
|
|
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);
|
|
|
|
@ -127,13 +127,13 @@ perf_window_add(struct perf_window *perf_window, uint64_t newest_execution_time)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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]]
|
|
|
|
|
assert(perf_window->by_duration[perf_window->by_termination[perf_window->count % PERF_WINDOW_CAPACITY]]
|
|
|
|
|
.execution_time
|
|
|
|
|
== newest_execution_time);
|
|
|
|
|
|
|
|
|
|
/* The by_duration array should be ordered by execution time */
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
for (int i = 1; i < perf_window_capacity; 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
|
|
|
|
@ -160,7 +160,7 @@ perf_window_get_percentile(struct perf_window *perf_window, uint8_t percentile,
|
|
|
|
|
if (unlikely(perf_window->count == 0)) return 0;
|
|
|
|
|
|
|
|
|
|
int idx = precomputed_index;
|
|
|
|
|
if (unlikely(perf_window->count < perf_window_capacity)) idx = perf_window->count * percentile / 100;
|
|
|
|
|
if (unlikely(perf_window->count < PERF_WINDOW_CAPACITY)) idx = perf_window->count * percentile / 100;
|
|
|
|
|
|
|
|
|
|
return perf_window->by_duration[idx].execution_time;
|
|
|
|
|
}
|
|
|
|
|