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

improve summary #602

Merged
merged 5 commits into from
Jun 18, 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
3 changes: 2 additions & 1 deletion .github/workflows/linux_llvm_cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ jobs:
./test_corofile
./test_http_parse
./test_time_util
./test_metric

llvm-profdata merge -sparse test_cinatra-*.profraw -o test_cinatra.profdata
llvm-cov show test_cinatra -object test_corofile -object test_time_util -object test_http_parse -instr-profile=test_cinatra.profdata -format=html -output-dir=../.coverage_llvm_cov -ignore-filename-regex="example|asio|cmdline|async_simple|tests" -show-instantiations=false
llvm-cov show test_cinatra -object test_corofile -object test_time_util -object test_http_parse -object test_metric -instr-profile=test_cinatra.profdata -format=html -output-dir=../.coverage_llvm_cov -ignore-filename-regex="example|asio|cmdline|async_simple|tests" -show-instantiations=false
echo "Done!"

- name: Upload Coverage Results
Expand Down
1 change: 0 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set(CINATRA_EXAMPLE
)

include_directories(../include)
include_directories(../include/cinatra)
if(ENABLE_METRIC_JSON)
include_directories(../iguana)
endif()
Expand Down
2 changes: 1 addition & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <vector>

#include "../include/cinatra.hpp"
#include "metric_conf.hpp"
#include "cinatra/metric_conf.hpp"

using namespace cinatra;
using namespace ylt::metric;
Expand Down
12 changes: 12 additions & 0 deletions include/cinatra/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
15 changes: 12 additions & 3 deletions include/cinatra/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,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 +283,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 Down
72 changes: 71 additions & 1 deletion include/cinatra/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 @@ -46,7 +51,10 @@ 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 +78,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 Down Expand Up @@ -173,6 +183,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 Expand Up @@ -237,6 +248,14 @@ struct metric_manager_t {
return r;
}

static void set_metric_max_age(std::chrono::steady_clock::duration max_age,
std::chrono::steady_clock::duration
check_duration = std::chrono::minutes(5)) {
metric_max_age_ = max_age;
metric_check_duration_ = check_duration;
start_check();
}

static auto metric_map_static() { return metric_map_impl<false>(); }
static auto metric_map_dynamic() { return metric_map_impl<true>(); }

Expand Down Expand Up @@ -546,12 +565,63 @@ struct metric_manager_t {
return filtered_metrics;
}

static void check_impl() {
check_timer_->expires_after(metric_check_duration_);
check_timer_->async_wait([](std::error_code ec) {
if (ec) {
return;
}

check_clean_metrics();
check_impl();
});
}

static void start_check() {
if (has_start_check_metric_) {
return;
}

has_start_check_metric_ = true;

executor_ = coro_io::create_io_context_pool(1);

check_timer_ =
std::make_shared<coro_io::period_timer>(executor_->get_executor());

check_impl();
}

static void check_clean_metrics() {
auto cur_time = std::chrono::system_clock::now();
{
auto lock = get_lock<true>();
for (auto it = metric_map_.begin(); it != metric_map_.end();) {
if (cur_time - it->second->get_created_time() > metric_max_age_) {
metric_map_.erase(it++);
}
else {
++it;
}
}
}
}

static inline bool has_start_check_metric_ = false;
static inline std::shared_ptr<coro_io::period_timer> check_timer_ = nullptr;
static inline std::shared_ptr<coro_io::io_context_pool> executor_ = nullptr;

static inline std::mutex mtx_;
static inline std::map<std::string, std::shared_ptr<metric_t>> metric_map_;

static inline null_mutex_t null_mtx_;
static inline std::atomic_bool need_lock_ = true;
static inline std::once_flag flag_;

static inline std::chrono::steady_clock::duration metric_max_age_{
std::chrono::hours(24)};
static inline std::chrono::steady_clock::duration metric_check_duration_{
std::chrono::minutes(5)};
};

using default_metric_manager = metric_manager_t<0>;
Expand Down
Loading
Loading