mirror of
https://github.com/apple/foundationdb.git
synced 2025-05-14 18:02:31 +08:00
205 lines
5.5 KiB
C++
205 lines
5.5 KiB
C++
/*
|
|
* ActorLineageProfiler.cpp
|
|
*
|
|
* This source file is part of the FoundationDB open source project
|
|
*
|
|
* Copyright 2013-2021 Apple Inc. and the FoundationDB project authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "flow/singleton.h"
|
|
#include "fdbclient/ActorLineageProfiler.h"
|
|
#include <msgpack.hpp>
|
|
#include <memory>
|
|
#include <boost/endian/conversion.hpp>
|
|
|
|
using namespace std::literals;
|
|
|
|
std::string_view to_string(WaitState w) {
|
|
switch (w) {
|
|
case WaitState::Running:
|
|
return "Running";
|
|
case WaitState::DiskIO:
|
|
return "DiskIO";
|
|
}
|
|
}
|
|
|
|
class Packer : public msgpack::packer<msgpack::sbuffer> {
|
|
struct visitor_t {
|
|
using VisitorMap = std::unordered_map<std::type_index, std::function<void(std::any const&, Packer& packer)>>;
|
|
VisitorMap visitorMap;
|
|
|
|
template <class T>
|
|
static void any_visitor(std::any const& val, Packer& packer) {
|
|
const T& v = std::any_cast<const T&>(val);
|
|
packer.pack(v);
|
|
}
|
|
|
|
template <class... Args>
|
|
struct populate_visitor_map;
|
|
template <class Head, class... Tail>
|
|
struct populate_visitor_map<Head, Tail...> {
|
|
static void populate(VisitorMap& map) {
|
|
map.emplace(std::type_index(typeid(Head)), any_visitor<Head>);
|
|
populate_visitor_map<Tail...>::populate(map);
|
|
}
|
|
};
|
|
template <>
|
|
struct populate_visitor_map<> {
|
|
static void populate(VisitorMap&) {}
|
|
};
|
|
|
|
visitor_t() {
|
|
populate_visitor_map<int64_t,
|
|
uint64_t,
|
|
bool,
|
|
float,
|
|
double,
|
|
std::string,
|
|
std::string_view,
|
|
std::vector<std::any>,
|
|
std::map<std::any, std::any>,
|
|
std::map<std::string_view, std::any>,
|
|
std::unordered_map<std::any, std::any>>::populate(visitorMap);
|
|
}
|
|
|
|
void visit(const std::any& val, Packer& packer) {
|
|
auto iter = visitorMap.find(val.type());
|
|
if (iter == visitorMap.end()) {
|
|
// TODO: trace error
|
|
} else {
|
|
iter->second(val, packer);
|
|
}
|
|
}
|
|
};
|
|
msgpack::sbuffer sbuffer;
|
|
// Initializing visitor_t involves building a type-map. As this is a relatively expensive operation, we don't want
|
|
// to do this each time we create a Packer object. So visitor_t is a stateless class and we only use it as a
|
|
// visitor.
|
|
crossbow::singleton<visitor_t> visitor;
|
|
|
|
public:
|
|
Packer() : msgpack::packer<msgpack::sbuffer>(sbuffer) {}
|
|
|
|
void pack(std::any const& val) { visitor->visit(val, *this); }
|
|
|
|
void pack(bool val) {
|
|
if (val) {
|
|
pack_true();
|
|
} else {
|
|
pack_false();
|
|
}
|
|
}
|
|
|
|
void pack(uint64_t val) {
|
|
if (val <= std::numeric_limits<uint8_t>::max()) {
|
|
pack_uint8(uint8_t(val));
|
|
} else if (val <= std::numeric_limits<uint16_t>::max()) {
|
|
pack_uint16(uint16_t(val));
|
|
} else if (val <= std::numeric_limits<uint32_t>::max()) {
|
|
pack_uint32(uint32_t(val));
|
|
} else {
|
|
pack_uint64(val);
|
|
}
|
|
}
|
|
|
|
void pack(int64_t val) {
|
|
if (val >= 0) {
|
|
this->pack(uint64_t(val));
|
|
} else if (val >= std::numeric_limits<uint8_t>::min()) {
|
|
pack_int8(int8_t(val));
|
|
} else if (val >= std::numeric_limits<int16_t>::min()) {
|
|
pack_int16(int16_t(val));
|
|
} else if (val >= std::numeric_limits<int32_t>::min()) {
|
|
pack_int32(int32_t(val));
|
|
} else if (val >= std::numeric_limits<int64_t>::min()) {
|
|
pack_int64(int64_t(val));
|
|
}
|
|
}
|
|
|
|
void pack(float val) { pack_float(val); }
|
|
void pack(double val) { pack_double(val); }
|
|
void pack(std::string const& str) {
|
|
pack_str(str.size());
|
|
pack_str_body(str.data(), str.size());
|
|
}
|
|
|
|
void pack(std::string_view val) {
|
|
pack_str(val.size());
|
|
pack_str_body(val.data(), val.size());
|
|
}
|
|
|
|
template <class K, class V>
|
|
void pack(std::map<K, V> const& map) {
|
|
pack_map(map.size());
|
|
for (const auto& p : map) {
|
|
pack(p.first);
|
|
pack(p.second);
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
void pack(std::vector<T> const& val) {
|
|
pack_array(val.size());
|
|
for (const auto& v : val) {
|
|
pack(v);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<Sample> done(double time) {
|
|
auto res = std::make_shared<Sample>();
|
|
res->time = time;
|
|
res->size = sbuffer.size();
|
|
res->data = sbuffer.release();
|
|
return res;
|
|
}
|
|
};
|
|
|
|
IALPCollectorBase::IALPCollectorBase() {
|
|
SampleCollector::instance().addCollector(this);
|
|
}
|
|
|
|
std::map<std::string_view, std::any> SampleCollectorT::collect(ActorLineage* lineage) {
|
|
std::map<std::string_view, std::any> out;
|
|
for (auto& collector : collectors) {
|
|
auto val = collector->collect(lineage);
|
|
if (val.has_value()) {
|
|
out[collector->name()] = val.value();
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::shared_ptr<Sample> SampleCollectorT::collect() {
|
|
Packer packer;
|
|
std::map<std::string_view, std::any> res;
|
|
double time = g_network->now();
|
|
res["time"sv] = time;
|
|
for (auto& p : getSamples) {
|
|
std::vector<std::map<std::string_view, std::any>> samples;
|
|
auto sampleVec = p.second();
|
|
for (auto& val : sampleVec) {
|
|
auto m = collect(val.getPtr());
|
|
if (!m.empty()) {
|
|
samples.emplace_back(std::move(m));
|
|
}
|
|
}
|
|
if (!samples.empty()) {
|
|
res[to_string(p.first)] = samples;
|
|
}
|
|
}
|
|
packer.pack(res);
|
|
return packer.done(time);
|
|
}
|