feat add exposed_var
Some checks failed
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (push) Successful in 1m29s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (push) Successful in 1m48s
linux-arm-gcc / linux-gcc-arm (Debug) (push) Successful in 1m45s
linux-arm-gcc / linux-gcc-arm (Release) (push) Successful in 1m41s
linux-arm-gcc / linux-gcc-armhf (Debug) (push) Successful in 2m0s
linux-arm-gcc / linux-gcc-armhf (Release) (push) Successful in 1m50s
linux-mips-gcc / linux-gcc-mipsel (Debug) (push) Successful in 1m28s
linux-mips-gcc / linux-gcc-mipsel (Release) (push) Successful in 1m39s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m30s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m39s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (push) Successful in 3m11s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (push) Successful in 1m44s
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 1m6s
linux-x64-gcc / linux-gcc (Release) (push) Failing after 1m28s
linux-x86-gcc / linux-gcc (Debug) (push) Failing after 1m16s
linux-x86-gcc / linux-gcc (Release) (push) Failing after 1m38s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (pull_request) Successful in 1m27s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (pull_request) Successful in 1m50s
linux-arm-gcc / linux-gcc-arm (Debug) (pull_request) Successful in 1m44s
linux-arm-gcc / linux-gcc-arm (Release) (pull_request) Successful in 1m38s
linux-arm-gcc / linux-gcc-armhf (Debug) (pull_request) Successful in 1m59s
linux-arm-gcc / linux-gcc-armhf (Release) (pull_request) Successful in 1m49s
linux-mips-gcc / linux-gcc-mipsel (Debug) (pull_request) Successful in 1m29s
linux-mips-gcc / linux-gcc-mipsel (Release) (pull_request) Successful in 1m38s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (pull_request) Successful in 1m26s
linux-mips64-gcc / linux-gcc-mips64el (Release) (pull_request) Successful in 1m39s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (pull_request) Successful in 3m11s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (pull_request) Successful in 1m45s
linux-x64-gcc / linux-gcc (Debug) (pull_request) Failing after 1m6s
linux-x64-gcc / linux-gcc (Release) (pull_request) Failing after 1m27s
linux-x86-gcc / linux-gcc (Debug) (pull_request) Failing after 1m17s
linux-x86-gcc / linux-gcc (Release) (pull_request) Failing after 1m39s

This commit is contained in:
tqcq 2024-07-06 01:59:04 +08:00
parent dd4b6cf8ea
commit 16b1ac13a1
10 changed files with 154 additions and 18 deletions

View File

@ -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")

View File

@ -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)

View File

@ -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<Json::Value()> value) {
@ -296,5 +297,4 @@ std::optional<Json::Value> ExposedVarDynamicTree::TryGet(Slice rel_path) const {
return *ptr;
}
} // namespace exposed_var
} // namespace tile

View File

@ -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 <typename T>
auto ToJsonValue(const T &t)
-> enable_if_t<std::is_same<std::string,
decltype(format_as(std::declval<T>()))>::value,
Json::Value> {
-> enable_if_t<
!std::is_integral<T>::value && !std::is_floating_point<T>::value &&
!std::is_same<T, std::string>::value &&
!std::is_same<T, const char *>::value &&
!std::is_same<T, bool>::value &&
std::is_same<std::string,
decltype(format_as(std::declval<T>()))>::value,
Json::Value> {
return Json::Value(format_as(t));
}
template <class T> Json::Value ToJsonValue(const std::atomic<T> &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 <typename T> 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 <typename T, typename F = detail::IdentityTime<T>>
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<std::string, T> 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 <typename T> using ExposedCounter = ExposedVar<T>;
template <typename T> using ExposedGauge = ExposedVar<T>;
template <typename T> using ExposedMiner = ExposedVar<T>;
template <typename T> using ExposedMaxer = ExposedVar<T>;
template <typename T> using ExposedAverager = ExposedVar<T>;
} // namespace tile
#endif // TILE_BASE_EXPOSED_VAR_H

View File

@ -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<int> v1("v1", 5);
ExposedVar<double> f1("f1", 6.2, ExposedVarGroup::FindOrCreate("/x/y/z"));
auto GreatGroup = ExposedVarGroup::FindOrCreate("/a/b");
// `/a/b/ds1`
ExposedVarDynamic<std::string>
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

0
tile/base/write_mostly.h Normal file
View File

View File

@ -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 <cstddef>
#include <cstdint>
#include <limits>
#include <utility>
namespace tile {
namespace write_mostly {
namespace detail {
template <typename T> struct MetricsStats;
}
} // namespace write_mostly
template <typename T> struct MetricsStats {
using SumType = internal::conditional_t<
std::is_integral<T>::value,
internal::conditional_t<std::is_signed<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

View File

@ -0,0 +1,18 @@
#ifndef TILE_BASE_WRITE_MOSTLY_WRITE_MOSTLY_H
#define TILE_BASE_WRITE_MOSTLY_WRITE_MOSTLY_H
#pragma once
#include <chrono>
#include <memory>
#include <mutex>
namespace tile {
template <typename Traits> class WriteMostly {
using T = typename Traits::Type;
public:
};
} // namespace tile
#endif // TILE_BASE_WRITE_MOSTLY_WRITE_MOSTLY_H

View File

@ -53,17 +53,22 @@ int Start(int argc, char **argv, std::function<int(int, char **)> 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;

View File

@ -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(); });
}