From 16b1ac13a185fac4284a7305db33986cc246e3c1 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sat, 6 Jul 2024 01:59:04 +0800 Subject: [PATCH] feat add exposed_var --- CMakeLists.txt | 4 +- third_party/glog/CMakeLists.txt | 6 +-- tile/base/exposed_var.cc | 10 ++--- tile/base/exposed_var.h | 53 +++++++++++++++++++++++++-- tile/base/exposed_var_test.cc | 34 ++++++++++++++++- tile/base/write_mostly.h | 0 tile/base/write_mostly/metrics.h | 36 ++++++++++++++++++ tile/base/write_mostly/write_mostly.h | 18 +++++++++ tile/init.cc | 5 +++ tile/testing/main.cc | 6 +-- 10 files changed, 154 insertions(+), 18 deletions(-) create mode 100644 tile/base/write_mostly.h create mode 100644 tile/base/write_mostly/metrics.h create mode 100644 tile/base/write_mostly/write_mostly.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c3253ab..cdf0287 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -279,13 +279,13 @@ if(TILE_BUILD_TESTS) target_sources(${PROJECT_NAME}_test_all PRIVATE ${test_file}) endmacro() - tile_add_test(base_exposed_var_test "tile/base/exposed_var_test.cc") macro(tile_add_test test_name test_file) tile_add_custom_test(${test_name} ${test_file}) add_test(NAME ${test_name} COMMAND ${test_name}) endmacro() -tile_add_test(fiber_detail_scheduler_test "tile/fiber/detail/scheduler_test.cc") + tile_add_test(base_exposed_var_test "tile/base/exposed_var_test.cc") + tile_add_test(fiber_detail_scheduler_test "tile/fiber/detail/scheduler_test.cc") tile_add_test(base_internal_meta_test "tile/base/internal/meta_test.cc") # tile_add_test(net_internal_http_engine_test # "tile/net/internal/http_engine_test.cc") diff --git a/third_party/glog/CMakeLists.txt b/third_party/glog/CMakeLists.txt index e260194..c1c3ac5 100644 --- a/third_party/glog/CMakeLists.txt +++ b/third_party/glog/CMakeLists.txt @@ -39,7 +39,7 @@ option(PRINT_UNSYMBOLIZED_STACK_TRACES "Print file offsets in traces instead of symbolizing" OFF) option(WITH_CUSTOM_PREFIX "Enable support for user-generated message prefixes" ON) -option(WITH_GFLAGS "Use gflags" ON) +option(WITH_GFLAGS "Use gflags" OFF) option(WITH_GTEST "Use Google Test" OFF) option(WITH_PKGCONFIG "Enable pkg-config support" OFF) option(WITH_SYMBOLIZE "Enable symbolize module" ON) @@ -699,8 +699,8 @@ endif(HAVE_PTHREAD) if(gflags_FOUND) # Prefer the gflags target that uses double colon convention - # target_link_libraries(glog PRIVATE gflags::gflags) - # if (TARGET + # target_link_libraries(glog PRIVATE gflags::gflags) + # if (TARGET # gflags::gflags) target_link_libraries (glog PUBLIC gflags::gflags) else # (TARGET gflags::gflags) target_link_libraries (glog PUBLIC gflags) endif # (TARGET gflags::gflags) diff --git a/tile/base/exposed_var.cc b/tile/base/exposed_var.cc index 5f403a6..cd032e2 100644 --- a/tile/base/exposed_var.cc +++ b/tile/base/exposed_var.cc @@ -5,7 +5,7 @@ #include "tile/base/string.h" namespace tile { -namespace exposed_var { +// namespace exposed_var { namespace { #define CHECK_PATH(path) \ @@ -63,19 +63,20 @@ std::string JoinPath(Slice a, Slice b) { std::string SubstituteEscapedSlashForZero(Slice path) { TILE_CHECK(path.find('\0') == tile::Slice::npos); - return Replace(path, "\\/", "\0"); + return Replace(path, "\\/", '\0'); } std::string SubstituteZeroForEscapedSlash(Slice path) { - return Replace(path, "\0", "\\/"); + return Replace(path, '\0', "\\/"); } std::string UnescapeZeroToPlainSlash(Slice path) { - return Replace(path, "\0", "/"); + return Replace(path, '\0', '/'); } } // namespace +// } // namespace exposed_var ExposedVarGroup::Handle ExposedVarGroup::Add(Slice rel_path, std::function value) { @@ -296,5 +297,4 @@ std::optional ExposedVarDynamicTree::TryGet(Slice rel_path) const { return *ptr; } -} // namespace exposed_var } // namespace tile diff --git a/tile/base/exposed_var.h b/tile/base/exposed_var.h index a8b6417..85d347c 100644 --- a/tile/base/exposed_var.h +++ b/tile/base/exposed_var.h @@ -3,6 +3,7 @@ #pragma once +#include "tile/base/chrono.h" #include "tile/base/deferred.h" #include "tile/base/internal/meta.h" #include "tile/base/optional.h" @@ -46,15 +47,21 @@ auto ToJsonValue(const T &t) template auto ToJsonValue(const T &t) - -> enable_if_t()))>::value, - Json::Value> { + -> enable_if_t< + !std::is_integral::value && !std::is_floating_point::value && + !std::is_same::value && + !std::is_same::value && + !std::is_same::value && + std::is_same()))>::value, + Json::Value> { return Json::Value(format_as(t)); } template Json::Value ToJsonValue(const std::atomic &v) { return ToJsonValue(v.load(std::memory_order_relaxed)); } +} // namespace exposed_var class ExposedVarDynamicTree; @@ -142,7 +149,45 @@ private: ExposedVarGroup::Handle handle_; }; -} // namespace exposed_var +namespace detail { +template struct IdentityTime { + std::uint64_t operator()(const T &val) const { + return (ReadSteadyClock() - std::chrono::steady_clock::time_point()) / + std::chrono::nanoseconds(1); + } +}; +} // namespace detail + +template > +class ExposedMetrics { +public: + explicit ExposedMetrics(Slice rel_path) { + LinkToParent(rel_path, ExposedVarGroup::FindOrCreate("/")); + } + +private: + Json::Value ToJsonValue(const T &v) { + Json::Value result; + std::unordered_map m = {{"1s", 1}}; + for (auto &&item : m) { + } + } + + void LinkToParent(Slice rel_path, ExposedVarGroup *parent) { + handle_ = parent->Add(rel_path, [this] { return ToJsonValue(obj_); }); + } + +private: + T obj_{}; + ExposedVarGroup::Handle handle_; +}; + +template using ExposedCounter = ExposedVar; +template using ExposedGauge = ExposedVar; +template using ExposedMiner = ExposedVar; +template using ExposedMaxer = ExposedVar; +template using ExposedAverager = ExposedVar; + } // namespace tile #endif // TILE_BASE_EXPOSED_VAR_H diff --git a/tile/base/exposed_var_test.cc b/tile/base/exposed_var_test.cc index 25b1e25..2c078c7 100644 --- a/tile/base/exposed_var_test.cc +++ b/tile/base/exposed_var_test.cc @@ -3,4 +3,36 @@ #include "gtest/gtest.h" #include "json/json.h" -namespace tile {} +namespace tile { +ExposedVarGroup *GetFancyGroup() { + return ExposedVarGroup::FindOrCreate("/a/b"); +} +Json::Value GetTree() { + Json::Value jsv; + jsv["dir"]["sub-dir"]["key"] = 5; + jsv["key"] = "6"; + return jsv; +} + +ExposedVar v1("v1", 5); + +ExposedVar f1("f1", 6.2, ExposedVarGroup::FindOrCreate("/x/y/z")); + +auto GreatGroup = ExposedVarGroup::FindOrCreate("/a/b"); + +// `/a/b/ds1` +ExposedVarDynamic + ds1("ds1", [] { return "test_str"; }, GetFancyGroup()); + +TEST(ExposedVar, Mutate) { + auto opt = ExposedVarGroup::TryGet("/"); + ASSERT_TRUE(opt); + auto &&jsv = *opt; + ASSERT_EQ(5, jsv["v1"].asInt()); + *v1 = 6; + jsv = *ExposedVarGroup::TryGet("/"); + ASSERT_EQ(6, jsv["v1"].asInt()); + *v1 = 5; +} + +} // namespace tile diff --git a/tile/base/write_mostly.h b/tile/base/write_mostly.h new file mode 100644 index 0000000..e69de29 diff --git a/tile/base/write_mostly/metrics.h b/tile/base/write_mostly/metrics.h new file mode 100644 index 0000000..e702109 --- /dev/null +++ b/tile/base/write_mostly/metrics.h @@ -0,0 +1,36 @@ +#ifndef TILE_BASE_WRITE_MOSTLY_METRICS_H +#define TILE_BASE_WRITE_MOSTLY_METRICS_H + +#pragma once + +#include "tile/base/internal/meta.h" +#include +#include +#include +#include + +namespace tile { + +namespace write_mostly { +namespace detail { +template struct MetricsStats; +} +} // namespace write_mostly + +template struct MetricsStats { + using SumType = internal::conditional_t< + std::is_integral::value, + internal::conditional_t::value, std::int64_t, + std::uint64_t>, + long double>; + + SumType sum; + std::size_t cnt; + T max; + T min; + T average; +}; + +} // namespace tile + +#endif // TILE_BASE_WRITE_MOSTLY_METRICS_H diff --git a/tile/base/write_mostly/write_mostly.h b/tile/base/write_mostly/write_mostly.h new file mode 100644 index 0000000..f17ee86 --- /dev/null +++ b/tile/base/write_mostly/write_mostly.h @@ -0,0 +1,18 @@ +#ifndef TILE_BASE_WRITE_MOSTLY_WRITE_MOSTLY_H +#define TILE_BASE_WRITE_MOSTLY_WRITE_MOSTLY_H + +#pragma once + +#include +#include +#include + +namespace tile { +template class WriteMostly { + using T = typename Traits::Type; + +public: +}; +} // namespace tile + +#endif // TILE_BASE_WRITE_MOSTLY_WRITE_MOSTLY_H diff --git a/tile/init.cc b/tile/init.cc index 4afaccf..f4b225b 100644 --- a/tile/init.cc +++ b/tile/init.cc @@ -53,17 +53,22 @@ int Start(int argc, char **argv, std::function cb, google::InstallFailureSignalHandler(); } + // Init gflags gflags::SetVersionString("0.1.0"); gflags::ParseCommandLineFlags(&argc, &argv, true); detail::ApplyFlagOverrider(); + // Init Glog google::InitGoogleLogging(argv[0]); TILE_LOG_INFO("Tile started."); TILE_PCHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR); + // Init BasicRuntime InitializeBasicRuntime(); + + // Run all initializers detail::RunAllInitializers(); int rc = 0; diff --git a/tile/testing/main.cc b/tile/testing/main.cc index a69639c..69ffa25 100644 --- a/tile/testing/main.cc +++ b/tile/testing/main.cc @@ -11,9 +11,9 @@ namespace testing { int InitAndRunAllTests(int *argc, char **argv) { ::testing::InitGoogleTest(argc, argv); - if (gflags::GetCommandLineFlagInfoOrDie("logtostderr").is_default) { - FLAGS_logtostderr = true; - } + // if (gflags::GetCommandLineFlagInfoOrDie("logtostderr").is_default) { + // FLAGS_logtostderr = true; + // } return Start(*argc, argv, [](int, char **) { return ::RUN_ALL_TESTS(); }); }