Skip to content

Commit 9095d98

Browse files
Revert LATENCY_HDR_SEC_SIGDIGTS to 2. Optimize summarize_current_second by using hdr_value_at_percentiles() instead of N x hdr_value_at_percentile(). (#295)
1 parent ae4c12b commit 9095d98

File tree

7 files changed

+83
-21
lines changed

7 files changed

+83
-21
lines changed

deps/hdr_histogram/.dirstamp

Whitespace-only changes.

deps/hdr_histogram/hdr_histogram.c

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,28 +643,67 @@ int64_t hdr_min(const struct hdr_histogram* h)
643643
return non_zero_min(h);
644644
}
645645

646+
static int64_t get_value_from_idx_up_to_count(const struct hdr_histogram* h, int64_t count_at_percentile)
647+
{
648+
int64_t count_to_idx = 0;
649+
650+
count_at_percentile = 0 < count_at_percentile ? count_at_percentile : 1;
651+
for (int32_t idx = 0; idx < h->counts_len; idx++)
652+
{
653+
count_to_idx += h->counts[idx];
654+
if (count_to_idx >= count_at_percentile)
655+
{
656+
return hdr_value_at_index(h, idx);
657+
}
658+
}
659+
660+
return 0;
661+
}
662+
646663
int64_t hdr_value_at_percentile(const struct hdr_histogram* h, double percentile)
647664
{
648-
struct hdr_iter iter;
649-
int64_t total = 0;
650665
double requested_percentile = percentile < 100.0 ? percentile : 100.0;
651666
int64_t count_at_percentile =
652667
(int64_t) (((requested_percentile / 100) * h->total_count) + 0.5);
653-
count_at_percentile = count_at_percentile > 1 ? count_at_percentile : 1;
668+
int64_t value_from_idx = get_value_from_idx_up_to_count(h, count_at_percentile);
669+
if (percentile == 0.0)
670+
{
671+
return lowest_equivalent_value(h, value_from_idx);
672+
}
673+
return highest_equivalent_value(h, value_from_idx);
674+
}
654675

655-
hdr_iter_init(&iter, h);
676+
int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percentiles, int64_t *values, size_t length)
677+
{
678+
if (NULL == percentiles || NULL == values)
679+
{
680+
return EINVAL;
681+
}
656682

657-
while (hdr_iter_next(&iter))
683+
struct hdr_iter iter;
684+
const int64_t total_count = h->total_count;
685+
// to avoid allocations we use the values array for intermediate computation
686+
// i.e. to store the expected cumulative count at each percentile
687+
for (size_t i = 0; i < length; i++)
658688
{
659-
total += iter.count;
689+
const double requested_percentile = percentiles[i] < 100.0 ? percentiles[i] : 100.0;
690+
const int64_t count_at_percentile =
691+
(int64_t) (((requested_percentile / 100) * total_count) + 0.5);
692+
values[i] = count_at_percentile > 1 ? count_at_percentile : 1;
693+
}
660694

661-
if (total >= count_at_percentile)
695+
hdr_iter_init(&iter, h);
696+
int64_t total = 0;
697+
size_t at_pos = 0;
698+
while (hdr_iter_next(&iter) && at_pos < length)
699+
{
700+
total += iter.count;
701+
while (at_pos < length && total >= values[at_pos])
662702
{
663-
int64_t value_from_index = iter.value;
664-
return highest_equivalent_value(h, value_from_index);
703+
values[at_pos] = highest_equivalent_value(h, iter.value);
704+
at_pos++;
665705
}
666706
}
667-
668707
return 0;
669708
}
670709

deps/hdr_histogram/hdr_histogram.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ int64_t hdr_max(const struct hdr_histogram* h);
284284
*/
285285
int64_t hdr_value_at_percentile(const struct hdr_histogram* h, double percentile);
286286

287+
/**
288+
* Get the values at the given percentiles.
289+
*
290+
* @param h "This" pointer.
291+
* @param percentiles The ordered percentiles array to get the values for.
292+
* @param length Number of elements in the arrays.
293+
* @param values Destination array containing the values at the given percentiles.
294+
* The values array should be allocated by the caller.
295+
* @return 0 on success, ENOMEM if the provided destination array is null.
296+
*/
297+
int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percentiles, int64_t *values, size_t length);
298+
287299
/**
288300
* Gets the standard deviation for the values in the histogram.
289301
*

run_stats.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ run_stats::run_stats(benchmark_config *config) :
117117
{
118118
memset(&m_start_time, 0, sizeof(m_start_time));
119119
memset(&m_end_time, 0, sizeof(m_end_time));
120-
quantiles_list = config->print_percentiles.quantile_list;
121-
120+
std::vector<float> quantiles_list_float = config->print_percentiles.quantile_list;
121+
std::sort(quantiles_list_float.begin(), quantiles_list_float.end());
122+
quantiles_list = std::vector<double>(quantiles_list_float.begin(), quantiles_list_float.end());
122123
if (config->arbitrary_commands->is_defined()) {
123124
setup_arbitrary_commands(config->arbitrary_commands->size());
124125
}
@@ -882,7 +883,7 @@ void run_stats::summarize(totals& result) const
882883
void result_print_to_json(json_handler * jsonhandler, const char * type, double ops_sec,
883884
double hits, double miss, double moved, double ask, double kbs, double kbs_rx, double kbs_tx,
884885
double latency, long m_total_latency, long ops,
885-
std::vector<float> quantile_list, struct hdr_histogram* latency_histogram,
886+
std::vector<double> quantile_list, struct hdr_histogram* latency_histogram,
886887
std::vector<unsigned int> timestamps,
887888
std::vector<one_sec_cmd_stats> timeserie_stats )
888889
{

run_stats.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class run_stats {
9797
totals m_totals;
9898

9999
std::list<one_second_stats> m_stats;
100-
std::vector<float> quantiles_list;
100+
std::vector<double> quantiles_list;
101101

102102
// current second stats ( appended to m_stats and reset every second )
103103
one_second_stats m_cur_stats;

run_stats_types.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ void one_sec_cmd_stats::merge(const one_sec_cmd_stats& other) {
7272
m_min_latency = other.m_min_latency < m_min_latency ? other.m_min_latency : m_min_latency;
7373
}
7474

75-
void one_sec_cmd_stats::summarize_quantiles(safe_hdr_histogram histogram, std::vector<float> quantiles) {
76-
for (std::size_t i = 0; i < quantiles.size(); i++){
77-
const float quantile = quantiles[i];
78-
const double value = hdr_value_at_percentile(histogram, quantile)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER;
79-
summarized_quantile_values.push_back(value);
75+
void one_sec_cmd_stats::summarize_quantiles(safe_hdr_histogram histogram, std::vector<double> sorted_quantiles) {
76+
std::vector<int64_t> values(sorted_quantiles.size());
77+
int result = hdr_value_at_percentiles(histogram, sorted_quantiles.data(), values.data(), sorted_quantiles.size());
78+
if (result != 0) {
79+
return;
80+
}
81+
for (std::size_t i = 0; i < sorted_quantiles.size(); i++) {
82+
summarized_quantile_values.push_back(values[i] / static_cast<double>(LATENCY_HDR_RESULTS_MULTIPLIER));
8083
}
8184
}
8285

run_stats_types.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define LATENCY_HDR_SIGDIGTS 3
2525
#define LATENCY_HDR_SEC_MIN_VALUE 10
2626
#define LATENCY_HDR_SEC_MAX_VALUE 600000000 ## LL
27-
#define LATENCY_HDR_SEC_SIGDIGTS 3
27+
#define LATENCY_HDR_SEC_SIGDIGTS 2
2828
#define LATENCY_HDR_RESULTS_MULTIPLIER 1000
2929
#define LATENCY_HDR_GRANULARITY 10
3030

@@ -90,7 +90,14 @@ class one_sec_cmd_stats {
9090
one_sec_cmd_stats();
9191
void reset();
9292
void merge(const one_sec_cmd_stats& other);
93-
void summarize_quantiles(safe_hdr_histogram histogram, std::vector<float> quantiles);
93+
/**
94+
* Summarizes quantiles from the given histogram.
95+
*
96+
* @param histogram The histogram from which quantile values are extracted.
97+
* @param sorted_quantiles A sorted (ascending order) vector of quantiles for which values will be computed.
98+
* The caller must ensure the vector is sorted from smallest to largest.
99+
*/
100+
void summarize_quantiles(safe_hdr_histogram histogram, std::vector<double> sorted_quantiles);
94101
void update_op(unsigned int bytes_rx, unsigned int bytes_tx, unsigned int latency);
95102
void update_op(unsigned int bytes_rx, unsigned int bytes_tx, unsigned int latency, unsigned int hits, unsigned int misses);
96103
void update_moved_op(unsigned int bytes_rx, unsigned int bytes_tx, unsigned int latency);

0 commit comments

Comments
 (0)