Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[metric][improve]improve metric #693

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ inline T &g_io_context_pool(
return *_g_io_context_pool;
}

template <typename T = io_context_pool>
inline std::shared_ptr<T> create_io_context_pool(
unsigned pool_size = std::thread::hardware_concurrency()) {
auto pool = std::make_shared<T>(pool_size);
std::thread thrd{[pool] {
pool->run();
}};
thrd.detach();

return pool;
}

template <typename T = io_context_pool>
inline T &g_block_io_context_pool(
unsigned pool_size = std::thread::hardware_concurrency()) {
Expand Down
35 changes: 17 additions & 18 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ class counter_t : public metric_t {
}
}

std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map() override {
std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
map;
metric_hash_map<double> value_map() override {
metric_hash_map<double> map;
if (use_atomic_) {
map = {atomic_value_map_.begin(), atomic_value_map_.end()};
}
Expand Down Expand Up @@ -192,9 +188,7 @@ class counter_t : public metric_t {
}
}

std::map<std::vector<std::string>, std::atomic<double>,
std::less<std::vector<std::string>>>
&atomic_value_map() {
metric_hash_map<std::atomic<double>> &atomic_value_map() {
return atomic_value_map_;
}

Expand Down Expand Up @@ -267,7 +261,12 @@ class counter_t : public metric_t {
label_val += value;
}
#else
label_val += value;
if constexpr (is_atomic) {
label_val.fetch_add(value, std::memory_order_relaxed);
}
else {
label_val += value;
}
#endif
} break;
case op_type_t::DEC:
Expand All @@ -278,9 +277,13 @@ class counter_t : public metric_t {
else {
label_val -= value;
}

#else
label_val -= value;
if constexpr (is_atomic) {
label_val.fetch_sub(value, std::memory_order_relaxed);
}
else {
label_val -= value;
}
#endif
break;
case op_type_t::SET:
Expand All @@ -289,14 +292,10 @@ class counter_t : public metric_t {
}
}

std::map<std::vector<std::string>, std::atomic<double>,
std::less<std::vector<std::string>>>
atomic_value_map_;
metric_hash_map<std::atomic<double>> atomic_value_map_;
std::atomic<double> default_lable_value_ = 0;

std::mutex mtx_;
std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map_;
metric_hash_map<double> value_map_;
};
} // namespace ylt::metric
6 changes: 1 addition & 5 deletions include/ylt/metric/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ class histogram_t : public metric_t {

auto get_bucket_counts() { return bucket_counts_; }

std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map() override {
return sum_->value_map();
}
metric_hash_map<double> value_map() override { return sum_->value_map(); }

void serialize(std::string &str) override {
if (!sum_->labels_name().empty()) {
Expand Down
38 changes: 32 additions & 6 deletions include/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SyncAwait.h"
#include "cinatra/cinatra_log_wrapper.hpp"
#if __has_include("ylt/coro_io/coro_io.hpp")
#include "ylt/coro_io/coro_io.hpp"
#else
#include "cinatra/ylt/coro_io/coro_io.hpp"
#endif

#ifdef CINATRA_ENABLE_METRIC_JSON
namespace iguana {
Expand Down Expand Up @@ -42,11 +47,33 @@ struct metric_filter_options {
bool is_white = true;
};

struct vector_hash {
size_t operator()(const std::vector<std::string>& vec) const {
unsigned int seed = 131;
unsigned int hash = 0;

for (const auto& str : vec) {
for (auto ch : str) {
hash = hash * seed + ch;
}
}

return (hash & 0x7FFFFFFF);
}
};

template <typename T>
using metric_hash_map =
std::unordered_map<std::vector<std::string>, T, vector_hash>;

class metric_t {
public:
metric_t() = default;
metric_t(MetricType type, std::string name, std::string help)
: type_(type), name_(std::move(name)), help_(std::move(help)) {}
: type_(type),
name_(std::move(name)),
help_(std::move(help)),
metric_created_time_(std::chrono::system_clock::now()) {}
metric_t(MetricType type, std::string name, std::string help,
std::vector<std::string> labels_name)
: metric_t(type, std::move(name), std::move(help)) {
Expand All @@ -70,6 +97,8 @@ class metric_t {

MetricType metric_type() { return type_; }

auto get_created_time() { return metric_created_time_; }

std::string_view metric_name() {
switch (type_) {
case MetricType::Counter:
Expand All @@ -92,11 +121,7 @@ class metric_t {
return static_labels_;
}

virtual std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map() {
return {};
}
virtual metric_hash_map<double> value_map() { return {}; }

virtual void serialize(std::string& str) {}

Expand Down Expand Up @@ -173,6 +198,7 @@ class metric_t {
std::vector<std::string> labels_name_; // read only
std::vector<std::string> labels_value_; // read only
bool use_atomic_ = false;
std::chrono::system_clock::time_point metric_created_time_{};
};

template <size_t ID = 0>
Expand Down
Loading
Loading