refactor: PERF_WINDOW_CAPACITY

master
Sean McBride 2 years ago
parent cd73d7d172
commit 1981c01338

@ -21,8 +21,8 @@ perf_window_initialize(struct perf_window *perf_window)
lock_init(&perf_window->lock); lock_init(&perf_window->lock);
perf_window->count = 0; perf_window->count = 0;
memset(perf_window->by_duration, 0, sizeof(struct execution_node) * 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); 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(lock_is_locked(&perf_window->lock));
assert(perf_window != NULL); assert(perf_window != NULL);
assert(first_by_duration_idx < perf_window_capacity); assert(first_by_duration_idx < PERF_WINDOW_CAPACITY);
assert(second_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 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; 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 static inline void
perf_window_fill(struct perf_window *perf_window, uint64_t newest_execution_time) 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_termination[i] = i;
perf_window->by_duration[i] = (struct execution_node){ .execution_time = newest_execution_time, perf_window->by_duration[i] = (struct execution_node){ .execution_time = newest_execution_time,
.by_termination_idx = i }; .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 */ /* 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; previous_execution_time = perf_window->by_duration[idx_to_replace].execution_time;
perf_window->by_duration[idx_to_replace].execution_time = newest_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. */ * right. We can determine which direction to shift by comparing with the previous execution time. */
if (newest_execution_time > previous_execution_time) { if (newest_execution_time > previous_execution_time) {
for (uint16_t i = idx_to_replace; 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; && perf_window->by_duration[i + 1].execution_time < perf_window->by_duration[i].execution_time;
i++) { i++) {
perf_window_swap(perf_window, i, i + 1); 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 */ /* 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 .execution_time
== newest_execution_time); == newest_execution_time);
/* The by_duration array should be ordered by execution time */ /* The by_duration array should be ordered by execution time */
#ifndef NDEBUG #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); assert(perf_window->by_duration[i - 1].execution_time <= perf_window->by_duration[i].execution_time);
} }
#endif #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; if (unlikely(perf_window->count == 0)) return 0;
int idx = precomputed_index; 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; return perf_window->by_duration[idx].execution_time;
} }

@ -7,13 +7,13 @@
enum enum
{ {
perf_window_capacity = 32 PERF_WINDOW_CAPACITY = 32
}; };
static_assert(perf_window_capacity && !(perf_window_capacity & (perf_window_capacity - 1)), static_assert(PERF_WINDOW_CAPACITY && !(PERF_WINDOW_CAPACITY & (PERF_WINDOW_CAPACITY - 1)),
"perf_window_capacity must be power of 2!"); "PERF_WINDOW_CAPACITY must be power of 2!");
static_assert(perf_window_capacity <= UINT16_MAX, "perf_window_capacity must be indexable by a 16-bit unsigned int"); static_assert(PERF_WINDOW_CAPACITY <= UINT16_MAX, "PERF_WINDOW_CAPACITY must be indexable by a 16-bit unsigned int");
/* /*
* The by_duration array sorts the last N executions by execution time * The by_duration array sorts the last N executions by execution time
@ -28,8 +28,8 @@ struct execution_node {
}; };
struct perf_window { struct perf_window {
struct execution_node by_duration[perf_window_capacity]; struct execution_node by_duration[PERF_WINDOW_CAPACITY];
uint16_t by_termination[perf_window_capacity]; uint16_t by_termination[PERF_WINDOW_CAPACITY];
uint64_t count; uint64_t count;
lock_t lock; lock_t lock;
}; };

@ -24,7 +24,7 @@ admissions_info_initialize(struct admissions_info *admissions_info, uint8_t perc
if (unlikely(percentile < 50 || percentile > 99)) panic("Invalid admissions percentile"); if (unlikely(percentile < 50 || percentile > 99)) panic("Invalid admissions percentile");
admissions_info->percentile = percentile; admissions_info->percentile = percentile;
admissions_info->control_index = perf_window_capacity * percentile / 100; admissions_info->control_index = PERF_WINDOW_CAPACITY * percentile / 100;
#ifdef LOG_ADMISSIONS_CONTROL #ifdef LOG_ADMISSIONS_CONTROL
debuglog("Percentile: %u\n", admissions_info->percentile); debuglog("Percentile: %u\n", admissions_info->percentile);
debuglog("Control Index: %d\n", admissions_info->control_index); debuglog("Control Index: %d\n", admissions_info->control_index);

@ -6,8 +6,8 @@
// tenant_database_foreach_cb_t // tenant_database_foreach_cb_t
static const int p50_idx = perf_window_capacity * 50 / 100; static const int p50_idx = PERF_WINDOW_CAPACITY * 50 / 100;
static const int p90_idx = perf_window_capacity * 90 / 100; static const int p90_idx = PERF_WINDOW_CAPACITY * 90 / 100;
void void
render_routes(struct route *route, void *arg_one, void *arg_two) render_routes(struct route *route, void *arg_one, void *arg_two)

Loading…
Cancel
Save