Skip to content
Open
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: 3 additions & 0 deletions pp/bare_bones/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ concept SteadyClockInterface = requires(Clock& clock) {
{ clock.now() };
};

template <class T>
concept arithmetic = std::integral<T> || std::floating_point<T>;

} // namespace BareBones::concepts
4 changes: 2 additions & 2 deletions pp/bare_bones/snug_composite.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class GenericDecodingTable {
const GenericDecodingTable* decoding_table;
PROMPP_ALWAYS_INLINE explicit Hasher(const GenericDecodingTable* _decoding_table = nullptr) noexcept : decoding_table(_decoding_table) {}

PROMPP_ALWAYS_INLINE size_t operator()(const std::string_view& str) const noexcept { return XXHash::hash(str); }
PROMPP_ALWAYS_INLINE size_t operator()(const std::string& str) const noexcept { return XXHash::hash(str); }
PROMPP_ALWAYS_INLINE size_t operator()(const std::string_view& str) const noexcept { return XXHash3::hash(str); }
PROMPP_ALWAYS_INLINE size_t operator()(const std::string& str) const noexcept { return XXHash3::hash(str); }

template <class Class>
PROMPP_ALWAYS_INLINE size_t operator()(const Class& c) const noexcept {
Expand Down
4 changes: 2 additions & 2 deletions pp/bare_bones/tests/snug_composite_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ TYPED_TEST(SnugComposite, should_return_same_id_for_same_data_with_hash) {
this->fill_table_with_random_values(outcomes);

std::string v = "12";
auto hash_val = BareBones::XXHash::hash(v);
auto hash_val = BareBones::XXHash3::hash(v);
auto id = outcomes.find_or_emplace(v, hash_val);

EXPECT_EQ(id, outcomes.find_or_emplace(v, hash_val));
Expand All @@ -151,7 +151,7 @@ TYPED_TEST(SnugComposite, should_return_same_id_for_same_data_with_hash) {
EXPECT_EQ(outcomes.find_or_emplace(v, hash_val), outcomes.find(v));

v = "21";
hash_val = BareBones::XXHash::hash(v);
hash_val = BareBones::XXHash3::hash(v);
id = outcomes.find_or_emplace(v);

EXPECT_EQ(id, outcomes.find_or_emplace(v, hash_val));
Expand Down
19 changes: 15 additions & 4 deletions pp/bare_bones/xxhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PRAGMA_DIAGNOSTIC(pop)

namespace BareBones {

class XXHash {
class XXHash3 {
public:
template <class Number>
requires std::is_arithmetic_v<Number>
Expand All @@ -30,15 +30,15 @@ class XXHash {
[[nodiscard]] PROMPP_ALWAYS_INLINE uint64_t hash() const noexcept { return hash_; }
[[nodiscard]] explicit PROMPP_ALWAYS_INLINE operator uint64_t() const noexcept { return hash_; }

auto operator<=>(const XXHash& other) const noexcept = default;
auto operator<=>(const XXHash3& other) const noexcept = default;

XXHash& operator=(uint64_t hash) noexcept {
XXHash3& operator=(uint64_t hash) noexcept {
hash_ = hash;
return *this;
}

PROMPP_ALWAYS_INLINE static uint64_t hash(const char* data, size_t size) noexcept {
XXHash hasher;
XXHash3 hasher;
hasher.extend(data, size);
return hasher.hash();
}
Expand All @@ -50,4 +50,15 @@ class XXHash {
uint64_t hash_{};
};

class XXHash {
public:
PROMPP_ALWAYS_INLINE XXHash() { XXH64_reset(&state_, 0); }

PROMPP_ALWAYS_INLINE void extend(std::string_view data) noexcept { XXH64_update(&state_, data.data(), data.length()); }
[[nodiscard]] PROMPP_ALWAYS_INLINE uint64_t digest() const noexcept { return XXH64_digest(&state_); }

private:
XXH64_state_t state_;
};

} // namespace BareBones
3 changes: 3 additions & 0 deletions pp/entrypoint/go_constants.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "go_constants.h"

#include "head/serialization.h"
#include "metrics/storage.h"
#include "prometheus/relabeler.h"

namespace {
Expand All @@ -13,4 +14,6 @@ static_assert(sizeof(PromPP::Prometheus::Relabel::InnerSeries) == Sizeof_InnerSe

static_assert(sizeof(entrypoint::head::SerializedDataIterator) == Sizeof_SerializedDataIterator);

static_assert(sizeof(metrics::Storage::Iterator) == Sizeof_MetricsIterator);

} // namespace
2 changes: 2 additions & 0 deletions pp/entrypoint/go_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#define Sizeof_InnerSeries (Sizeof_SizeT + Sizeof_BareBonesVector + Sizeof_RoaringBitset)

#define Sizeof_SerializedDataIterator 192

#define Sizeof_MetricsIterator 24
57 changes: 16 additions & 41 deletions pp/entrypoint/metrics.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,47 @@
#include "metrics.h"

#include <memory>

#include "metrics/counter.h"
#include "metrics/serializer.h"
#include "metrics/storage.h"
#include "primitives/go_model.h"
#include "primitives/go_slice.h"

struct MetricsIterator {
metrics::Storage::Iterator iterator{metrics::storage().begin()};
metrics::Serializer serializer;
};

using MetricsIteratorPtr = std::unique_ptr<MetricsIterator>;

using PromPP::Primitives::Go::Label;
using PromPP::Primitives::Go::SliceView;
using PromPP::Primitives::Go::String;

extern "C" void prompp_metrics_iterator_ctor(void* res) {
struct Result {
MetricsIteratorPtr iterator;
};

extern "C" void prompp_metrics_iterator_ctor(void* args) {
metrics::storage().remove_unused_pages();

new (res) Result{.iterator = std::make_unique<MetricsIterator>()};
std::construct_at(static_cast<metrics::Storage::Iterator*>(args), metrics::storage().begin());
}

extern "C" void prompp_metrics_iterator_dtor(void* args) {
extern "C" void prompp_metrics_iterator_next(void* args, void* res) {
struct Arguments {
MetricsIteratorPtr iterator;
};

static_cast<Arguments*>(args)->~Arguments();
}

extern "C" void prompp_metrics_iterator_serialize(void* args, void* res) {
struct Arguments {
MetricsIteratorPtr iterator;
metrics::Storage::Iterator* iterator;
};
struct Result {
SliceView<const char> buffer;
const PromPP::Primitives::Go::Metric* metric;
};

const auto it = static_cast<Arguments*>(args)->iterator.get();
const auto in = static_cast<Arguments*>(args);
const auto out = static_cast<Result*>(res);

if (it->iterator == metrics::Storage::end()) [[unlikely]] {
out->buffer.reset_to(nullptr, 0, 0);
if (*in->iterator == metrics::Storage::end()) [[unlikely]] {
out->metric = nullptr;
} else {
const auto& buffer = it->serializer.serialize(it->iterator->page->labels(), it->iterator->metric);
out->buffer.reset_to(buffer.c_str(), buffer.size(), buffer.capacity());
++it->iterator;
out->metric = (*in->iterator)->go_metric();
++(*in->iterator);
}
}

struct MetricsPageForTest final : metrics::MetricsPage<MetricsPageForTest> {
using MetricsPage::MetricsPage;

MetricsPageForTest(PromPP::Primitives::LabelViewSet&& label_set, const String& counter_name, uint64_t counter_value)
: MetricsPage(std::move(label_set)), emplace_count(static_cast<std::string_view>(counter_name), counter_value) {}
MetricsPageForTest(const SliceView<Label>& labels, const String& counter_name, uint64_t counter_value)
: emplace_count(labels, static_cast<std::string_view>(counter_name), counter_value),
emplace_gauge(labels, static_cast<std::string_view>(counter_name), counter_value) {}

metrics::Counter<> emplace_count;
metrics::Counter emplace_count;
metrics::Gauge emplace_gauge;
};

extern "C" void prompp_metrics_page_for_test_ctor(void* args, void* res) {
Expand All @@ -78,11 +56,8 @@ extern "C" void prompp_metrics_page_for_test_ctor(void* args, void* res) {

const auto in = static_cast<Arguments*>(args);

PromPP::Primitives::LabelViewSet label_set;
label_set.append_unsorted(in->labels);

new (res) Result{
.page = metrics::CreateMetricsPage<MetricsPageForTest>(std::move(label_set), in->counter_name, in->counter_value),
.page = metrics::CreateMetricsPage<MetricsPageForTest>(in->labels, in->counter_name, in->counter_value),
};
}

Expand Down
23 changes: 6 additions & 17 deletions pp/entrypoint/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@ extern "C" {
#endif

/**
* @brief Create metrics iterator
* @brief Initialize metrics iterator
*
* @param res {
* iterator uintptr // Pointer to constructed iterator
* }
*/
void prompp_metrics_iterator_ctor(void* res);

/**
* @brief Destroy metrics iterator
*
* @param args {
* iterator uintptr // Pointer to constructed iterator
* }
* @param args *MetricIterator
*/
void prompp_metrics_iterator_dtor(void* args);
void prompp_metrics_iterator_ctor(void* args);

/**
* @brief Serialize metric into protobuf and advance iterator to next metric
*
* @param args {
* iterator uintptr // Pointer to constructed iterator
* iterator *MetricIterator // Pointer to constructed iterator
* }
*
* @param res {
* buffer []bytes // serialized data
* metric *cppbridge.CppMetric // Pointer to go metric
* }
*/
void prompp_metrics_iterator_serialize(void* args, void* res);
void prompp_metrics_iterator_next(void* args, void* res);

/**
* @brief Create metrics page for test
Expand Down
33 changes: 10 additions & 23 deletions pp/go/cppbridge/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
CppBareBonesVector = [C.Sizeof_BareBonesVector]byte
CppRoaringBitset = [C.Sizeof_RoaringBitset]byte
CppSerializedDataIterator = [C.Sizeof_SerializedDataIterator]byte
CppMetricsIterator = [C.Sizeof_MetricsIterator]byte
)

var (
Expand Down Expand Up @@ -3447,49 +3448,35 @@ func prometheusRemapStaleNansState(staleNansState, lsIdsMapping uintptr) {
)
}

func prometheusMetricsIteratorCtor() uintptr {
var res struct {
iterator uintptr
}
func prometheusMetricsIteratorCtor() CppMetricsIterator {
var iterator CppMetricsIterator

testGC()
fastcgo.UnsafeCall1(
C.prompp_metrics_iterator_ctor,
uintptr(unsafe.Pointer(&res)),
uintptr(unsafe.Pointer(&iterator)),
)

return res.iterator
return iterator
}

func prometheusMetricsIteratorDtor(iterator uintptr) {
func prometheusMetricsIteratorNext(iterator *CppMetricsIterator) *CppMetric {
args := struct {
iterator uintptr
}{iterator}

testGC()
fastcgo.UnsafeCall1(
C.prompp_metrics_iterator_dtor,
uintptr(unsafe.Pointer(&args)),
)
}

func prometheusMetricsIteratorSerialize(iterator uintptr) []byte {
args := struct {
iterator uintptr
}{iterator}
}{uintptr(unsafe.Pointer(iterator))}

var res struct {
data []byte
metric *CppMetric
}

testGC()
fastcgo.UnsafeCall2(
C.prompp_metrics_iterator_serialize,
C.prompp_metrics_iterator_next,
uintptr(unsafe.Pointer(&args)),
uintptr(unsafe.Pointer(&res)),
)

return res.data
return res.metric
}

func prometheusMetricsPageForTestCtor(labels Labels, counterName string, counterValue uint64) uintptr {
Expand Down
25 changes: 8 additions & 17 deletions pp/go/cppbridge/entrypoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ void prompp_dump_memory_profile(void* args, void* res);
#define Sizeof_InnerSeries (Sizeof_SizeT + Sizeof_BareBonesVector + Sizeof_RoaringBitset)

#define Sizeof_SerializedDataIterator 192

#define Sizeof_MetricsIterator 24
#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -481,35 +483,24 @@ extern "C" {
#endif

/**
* @brief Create metrics iterator
* @brief Initialize metrics iterator
*
* @param res {
* iterator uintptr // Pointer to constructed iterator
* }
*/
void prompp_metrics_iterator_ctor(void* res);

/**
* @brief Destroy metrics iterator
*
* @param args {
* iterator uintptr // Pointer to constructed iterator
* }
* @param args *MetricIterator
*/
void prompp_metrics_iterator_dtor(void* args);
void prompp_metrics_iterator_ctor(void* args);

/**
* @brief Serialize metric into protobuf and advance iterator to next metric
*
* @param args {
* iterator uintptr // Pointer to constructed iterator
* iterator *MetricIterator // Pointer to constructed iterator
* }
*
* @param res {
* buffer []bytes // serialized data
* metric *cppbridge.CppMetric // Pointer to go metric
* }
*/
void prompp_metrics_iterator_serialize(void* args, void* res);
void prompp_metrics_iterator_next(void* args, void* res);

/**
* @brief Create metrics page for test
Expand Down
Loading
Loading