|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2025-Present Couchbase, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "rcb_hdr_histogram.hxx" |
| 19 | +#include "rcb_exceptions.hxx" |
| 20 | + |
| 21 | +#include <hdr/hdr_histogram.h> |
| 22 | +#include <ruby.h> |
| 23 | + |
| 24 | +#include <mutex> |
| 25 | +#include <shared_mutex> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +namespace couchbase::ruby |
| 29 | +{ |
| 30 | +namespace |
| 31 | +{ |
| 32 | +struct cb_hdr_histogram_data { |
| 33 | + hdr_histogram* histogram{ nullptr }; |
| 34 | + std::shared_mutex mutex{}; |
| 35 | +}; |
| 36 | + |
| 37 | +void |
| 38 | +cb_hdr_histogram_close(cb_hdr_histogram_data* hdr_histogram_data) |
| 39 | +{ |
| 40 | + if (hdr_histogram_data->histogram != nullptr) { |
| 41 | + hdr_close(hdr_histogram_data->histogram); |
| 42 | + hdr_histogram_data->histogram = nullptr; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void |
| 47 | +cb_HdrHistogramC_mark(void* /* ptr */) |
| 48 | +{ |
| 49 | + /* no embedded ruby objects -- no mark */ |
| 50 | +} |
| 51 | + |
| 52 | +void |
| 53 | +cb_HdrHistogramC_free(void* ptr) |
| 54 | +{ |
| 55 | + auto* hdr_histogram_data = static_cast<cb_hdr_histogram_data*>(ptr); |
| 56 | + cb_hdr_histogram_close(hdr_histogram_data); |
| 57 | + hdr_histogram_data->~cb_hdr_histogram_data(); |
| 58 | + ruby_xfree(hdr_histogram_data); |
| 59 | +} |
| 60 | + |
| 61 | +std::size_t |
| 62 | +cb_HdrHistogramC_memsize(const void* ptr) |
| 63 | +{ |
| 64 | + const auto* hdr_histogram_data = static_cast<const cb_hdr_histogram_data*>(ptr); |
| 65 | + return sizeof(*hdr_histogram_data); |
| 66 | +} |
| 67 | + |
| 68 | +const rb_data_type_t cb_hdr_histogram_type{ |
| 69 | + "Couchbase/Utils/HdrHistogramC", |
| 70 | + { |
| 71 | + cb_HdrHistogramC_mark, |
| 72 | + cb_HdrHistogramC_free, |
| 73 | + cb_HdrHistogramC_memsize, |
| 74 | +// only one reserved field when GC.compact implemented |
| 75 | +#ifdef T_MOVED |
| 76 | + nullptr, |
| 77 | +#endif |
| 78 | + {}, |
| 79 | + }, |
| 80 | +#ifdef RUBY_TYPED_FREE_IMMEDIATELY |
| 81 | + nullptr, |
| 82 | + nullptr, |
| 83 | + RUBY_TYPED_FREE_IMMEDIATELY, |
| 84 | +#endif |
| 85 | +}; |
| 86 | + |
| 87 | +VALUE |
| 88 | +cb_HdrHistogramC_allocate(VALUE klass) |
| 89 | +{ |
| 90 | + cb_hdr_histogram_data* hdr_histogram = nullptr; |
| 91 | + VALUE obj = |
| 92 | + TypedData_Make_Struct(klass, cb_hdr_histogram_data, &cb_hdr_histogram_type, hdr_histogram); |
| 93 | + new (hdr_histogram) cb_hdr_histogram_data(); |
| 94 | + return obj; |
| 95 | +} |
| 96 | + |
| 97 | +VALUE |
| 98 | +cb_HdrHistogramC_initialize(VALUE self, |
| 99 | + VALUE lowest_discernible_value, |
| 100 | + VALUE highest_trackable_value, |
| 101 | + VALUE significant_figures) |
| 102 | +{ |
| 103 | + Check_Type(lowest_discernible_value, T_FIXNUM); |
| 104 | + Check_Type(highest_trackable_value, T_FIXNUM); |
| 105 | + Check_Type(significant_figures, T_FIXNUM); |
| 106 | + |
| 107 | + std::int64_t lowest = NUM2LL(lowest_discernible_value); |
| 108 | + std::int64_t highest = NUM2LL(highest_trackable_value); |
| 109 | + int sigfigs = NUM2INT(significant_figures); |
| 110 | + |
| 111 | + cb_hdr_histogram_data* hdr_histogram; |
| 112 | + TypedData_Get_Struct(self, cb_hdr_histogram_data, &cb_hdr_histogram_type, hdr_histogram); |
| 113 | + |
| 114 | + int res; |
| 115 | + { |
| 116 | + const std::unique_lock lock(hdr_histogram->mutex); |
| 117 | + res = hdr_init(lowest, highest, sigfigs, &hdr_histogram->histogram); |
| 118 | + } |
| 119 | + if (res != 0) { |
| 120 | + rb_raise(exc_couchbase_error(), "failed to initialize HDR histogram"); |
| 121 | + return self; |
| 122 | + } |
| 123 | + |
| 124 | + return self; |
| 125 | +} |
| 126 | + |
| 127 | +VALUE |
| 128 | +cb_HdrHistogramC_close(VALUE self) |
| 129 | +{ |
| 130 | + cb_hdr_histogram_data* hdr_histogram; |
| 131 | + TypedData_Get_Struct(self, cb_hdr_histogram_data, &cb_hdr_histogram_type, hdr_histogram); |
| 132 | + { |
| 133 | + const std::unique_lock lock(hdr_histogram->mutex); |
| 134 | + cb_hdr_histogram_close(hdr_histogram); |
| 135 | + } |
| 136 | + return Qnil; |
| 137 | +} |
| 138 | + |
| 139 | +VALUE |
| 140 | +cb_HdrHistogramC_record_value(VALUE self, VALUE value) |
| 141 | +{ |
| 142 | + Check_Type(value, T_FIXNUM); |
| 143 | + |
| 144 | + std::int64_t val = NUM2LL(value); |
| 145 | + |
| 146 | + cb_hdr_histogram_data* hdr_histogram; |
| 147 | + TypedData_Get_Struct(self, cb_hdr_histogram_data, &cb_hdr_histogram_type, hdr_histogram); |
| 148 | + |
| 149 | + { |
| 150 | + const std::shared_lock lock(hdr_histogram->mutex); |
| 151 | + hdr_record_value_atomic(hdr_histogram->histogram, val); |
| 152 | + } |
| 153 | + return Qnil; |
| 154 | +} |
| 155 | + |
| 156 | +VALUE |
| 157 | +cb_HdrHistogramC_get_percentiles_and_reset(VALUE self, VALUE percentiles) |
| 158 | +{ |
| 159 | + Check_Type(percentiles, T_ARRAY); |
| 160 | + |
| 161 | + cb_hdr_histogram_data* hdr_histogram; |
| 162 | + TypedData_Get_Struct(self, cb_hdr_histogram_data, &cb_hdr_histogram_type, hdr_histogram); |
| 163 | + |
| 164 | + std::vector<std::int64_t> percentile_values{}; |
| 165 | + std::int64_t total_count; |
| 166 | + { |
| 167 | + const std::unique_lock lock(hdr_histogram->mutex); |
| 168 | + total_count = hdr_histogram->histogram->total_count; |
| 169 | + for (std::size_t i = 0; i < static_cast<std::size_t>(RARRAY_LEN(percentiles)); ++i) { |
| 170 | + VALUE entry = rb_ary_entry(percentiles, static_cast<long>(i)); |
| 171 | + Check_Type(entry, T_FLOAT); |
| 172 | + double perc = NUM2DBL(entry); |
| 173 | + std::int64_t value_at_perc = hdr_value_at_percentile(hdr_histogram->histogram, perc); |
| 174 | + percentile_values.push_back(value_at_perc); |
| 175 | + } |
| 176 | + hdr_reset(hdr_histogram->histogram); |
| 177 | + } |
| 178 | + |
| 179 | + static const VALUE sym_total_count = rb_id2sym(rb_intern("total_count")); |
| 180 | + static const VALUE sym_percentiles = rb_id2sym(rb_intern("percentiles")); |
| 181 | + VALUE res = rb_hash_new(); |
| 182 | + rb_hash_aset(res, sym_total_count, LL2NUM(total_count)); |
| 183 | + VALUE perc_array = rb_ary_new_capa(static_cast<long>(percentile_values.size())); |
| 184 | + for (const auto& val : percentile_values) { |
| 185 | + rb_ary_push(perc_array, LL2NUM(val)); |
| 186 | + } |
| 187 | + rb_hash_aset(res, sym_percentiles, perc_array); |
| 188 | + return res; |
| 189 | +} |
| 190 | + |
| 191 | +VALUE |
| 192 | +cb_HdrHistogramC_bin_count(VALUE self) |
| 193 | +{ |
| 194 | + cb_hdr_histogram_data* hdr_histogram; |
| 195 | + TypedData_Get_Struct(self, cb_hdr_histogram_data, &cb_hdr_histogram_type, hdr_histogram); |
| 196 | + |
| 197 | + std::int32_t bin_count; |
| 198 | + { |
| 199 | + const std::unique_lock lock(hdr_histogram->mutex); |
| 200 | + bin_count = hdr_histogram->histogram->bucket_count; |
| 201 | + } |
| 202 | + return LONG2NUM(bin_count); |
| 203 | +} |
| 204 | +} // namespace |
| 205 | + |
| 206 | +void |
| 207 | +init_hdr_histogram(VALUE mCouchbase) |
| 208 | +{ |
| 209 | + VALUE mUtils = rb_define_module_under(mCouchbase, "Utils"); |
| 210 | + VALUE cHdrHistogramC = rb_define_class_under(mUtils, "HdrHistogramC", rb_cObject); |
| 211 | + rb_define_alloc_func(cHdrHistogramC, cb_HdrHistogramC_allocate); |
| 212 | + rb_define_method(cHdrHistogramC, "initialize", cb_HdrHistogramC_initialize, 3); |
| 213 | + rb_define_method(cHdrHistogramC, "close", cb_HdrHistogramC_close, 0); |
| 214 | + rb_define_method(cHdrHistogramC, "record_value", cb_HdrHistogramC_record_value, 1); |
| 215 | + rb_define_method(cHdrHistogramC, "bin_count", cb_HdrHistogramC_bin_count, 0); |
| 216 | + rb_define_method( |
| 217 | + cHdrHistogramC, "get_percentiles_and_reset", cb_HdrHistogramC_get_percentiles_and_reset, 1); |
| 218 | +} |
| 219 | +} // namespace couchbase::ruby |
0 commit comments