Files
spark/src/lib/tracing/PerfLog.h
T

160 lines
4.6 KiB
C++

#pragma once
#include "lib/utils/TimeStamp.h"
#include <map>
#include <memory>
#include <tbb/concurrent_queue.h>
namespace bts {
namespace trace {
struct CorID {
const char *channel = "";
uint32_t sn = 0;
};
struct PerfLogEntry {
utils::TimeStamp ts;
const char *channel;
const char *entry_kind;
const char *txt_1;
uint64_t uint64_1;
const char* thread_name;
utils::TimeStamp cor_event;
uint32_t sn;
uint32_t uint32_1;
};
class PerfLog {
tbb::concurrent_queue<PerfLogEntry> _pending;
static thread_local const char* _thread_name_ptr;
bool _disabled;
struct PImpl;
std::unique_ptr<PImpl> me;
public:
PerfLog();
~PerfLog();
size_t store_log_entries();
void log(CorID cid, const char *entry_kind, uint64_t uint64_1 = 0,
const char *txt_1 = "", uint32_t uint32_1 = 0) {
_pending.push({utils::TimeStamp::now(), cid.channel, entry_kind, txt_1,
uint64_1, _thread_name_ptr, utils::TimeStamp(), cid.sn,
uint32_1});
}
void log(CorID cid, utils::TimeStamp ts, const char *entry_kind,
uint64_t uint64_1 = 0, const char *txt_1 = "",
uint32_t uint32_1 = 0) {
_pending.push({ts, cid.channel, entry_kind, txt_1, uint64_1,
_thread_name_ptr,utils::TimeStamp(), cid.sn, uint32_1});
}
void log(CorID cid, utils::TimeStamp ts, utils::TimeStamp ts2,
const char *entry_kind, uint64_t uint64_1 = 0,
const char *txt_1 = "", uint32_t uint32_1 = 0) {
_pending.push({ts, cid.channel, entry_kind, txt_1, uint64_1,
_thread_name_ptr,ts2, cid.sn, uint32_1});
}
void flush() {
_pending.push({utils::TimeStamp(), nullptr, nullptr, nullptr, 0,
_thread_name_ptr,utils::TimeStamp(), 0, 0});
}
void log(PerfLogEntry const &e) { _pending.push(e); }
// return the N last traces
std::vector<std::map<std::string, std::string>>
get_latest_requote_traces(size_t n = 0);
std::vector<std::map<std::string, std::string>>
get_latest_hedge_traces(size_t n = 0);
void print_latest_traces(std::ostream &, size_t n = 0);
private:
std::string init_storage_location();
size_t flush_current_batch_to_disk() ;
bool is_flush_signal(PerfLogEntry const&);
void init_db();
static const char*get_thread_name();
friend class ScopedPerfLogEntry;
};
class ScopedPerfLogEntry {
public:
ScopedPerfLogEntry() : _log(nullptr) { _begin.entry_kind = nullptr; }
ScopedPerfLogEntry(PerfLog &l, CorID cid, const char *entry_kind,
uint64_t uint64_1 = 0, const char *txt_1 = "",
uint32_t uint32_1 = 0)
: _log(&l), _min(), _begin{utils::TimeStamp::now(),
cid.channel,
entry_kind,
txt_1,
uint64_1,
l._thread_name_ptr,
utils::TimeStamp(),
cid.sn,
uint32_1} {
_log->log(_begin);
}
ScopedPerfLogEntry(PerfLog &l, CorID cid, const char *entry_kind,
utils::TimeStamp::Interval threshhold,
uint64_t uint64_1 = 0, const char *txt_1 = "",
uint32_t uint32_1 = 0)
: _log(&l), _min(threshhold), _begin{utils::TimeStamp::now(),
cid.channel,
entry_kind,
txt_1,
uint64_1,
l._thread_name_ptr,
utils::TimeStamp(),
cid.sn,
uint32_1} { }
ScopedPerfLogEntry(ScopedPerfLogEntry &&src)
: _log(src._log), _min(src._min), _begin(src._begin) {
src._begin.entry_kind = nullptr;
}
void finish() {
if (_begin.entry_kind) {
bool is_trivial = false;
auto ts = utils::TimeStamp::now();
if (!_min.zero()) {
if ((ts - _begin.ts) >= _min) {
_log->log(_begin);
}
else {
is_trivial = true;
}
}
if (!is_trivial) {
_begin.cor_event = _begin.ts;
_begin.ts = ts;
_log->log(_begin);
}
discard();
}
}
void discard() { _begin.entry_kind = nullptr; }
~ScopedPerfLogEntry() { finish(); }
private:
PerfLog *_log;
utils::TimeStamp::Interval _min;
PerfLogEntry _begin;
}; // ScopedPerfLogEntry
typedef ScopedPerfLogEntry log_scope;
} // namespace trace
} // namespace bts