2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* Trace.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_TRACE_H
|
|
|
|
#define FLOW_TRACE_H
|
|
|
|
#pragma once
|
|
|
|
|
2019-12-07 07:18:17 +08:00
|
|
|
#include <atomic>
|
2017-05-26 04:48:44 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2020-03-13 05:34:19 +08:00
|
|
|
#include <set>
|
2019-03-15 09:40:28 +08:00
|
|
|
#include <type_traits>
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "flow/IRandom.h"
|
|
|
|
#include "flow/Error.h"
|
2020-07-08 00:06:13 +08:00
|
|
|
#include "flow/ITrace.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
#define TRACE_DEFAULT_ROLL_SIZE (10 << 20)
|
|
|
|
#define TRACE_DEFAULT_MAX_LOGS_SIZE (10 * TRACE_DEFAULT_ROLL_SIZE)
|
|
|
|
|
|
|
|
inline int fastrand() {
|
|
|
|
static int g_seed = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
g_seed = 214013 * g_seed + 2531011;
|
|
|
|
return (g_seed >> 16) & 0x7fff;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// inline static bool TRACE_SAMPLE() { return fastrand()<16; }
|
|
|
|
inline static bool TRACE_SAMPLE() {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-03-17 04:59:31 +08:00
|
|
|
extern thread_local int g_allocation_tracing_disabled;
|
2019-05-07 08:41:32 +08:00
|
|
|
|
2020-11-02 14:13:24 +08:00
|
|
|
// Each major level of severity has 10 levels of minor levels, which are not all
|
|
|
|
// used. when the numbers of severity events in each level are counted, they are
|
|
|
|
// grouped by the major level.
|
2017-05-26 04:48:44 +08:00
|
|
|
enum Severity {
|
2019-11-06 01:42:17 +08:00
|
|
|
SevVerbose = 0,
|
2019-11-05 08:10:08 +08:00
|
|
|
SevSample = 1,
|
|
|
|
SevDebug = 5,
|
|
|
|
SevInfo = 10,
|
|
|
|
SevWarn = 20,
|
|
|
|
SevWarnAlways = 30,
|
|
|
|
SevError = 40,
|
|
|
|
SevMaxUsed = SevError,
|
|
|
|
SevMax = 1000000
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-05-03 23:09:52 +08:00
|
|
|
enum class ErrorKind : uint8_t {
|
|
|
|
Unset,
|
|
|
|
DiskIssue,
|
|
|
|
BugDetected,
|
|
|
|
};
|
|
|
|
|
2020-11-02 14:13:24 +08:00
|
|
|
const int NUM_MAJOR_LEVELS_OF_EVENTS = SevMaxUsed / 10 + 1;
|
|
|
|
|
2018-05-03 01:44:38 +08:00
|
|
|
class TraceEventFields {
|
|
|
|
public:
|
2019-01-31 05:53:23 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 11262274;
|
2018-05-03 01:44:38 +08:00
|
|
|
typedef std::pair<std::string, std::string> Field;
|
|
|
|
typedef std::vector<Field> FieldContainer;
|
|
|
|
typedef FieldContainer::const_iterator FieldIterator;
|
|
|
|
|
2018-06-01 04:27:35 +08:00
|
|
|
TraceEventFields();
|
|
|
|
|
2018-05-03 01:44:38 +08:00
|
|
|
size_t size() const;
|
2018-06-01 04:27:35 +08:00
|
|
|
size_t sizeBytes() const;
|
2018-05-03 01:44:38 +08:00
|
|
|
FieldIterator begin() const;
|
|
|
|
FieldIterator end() const;
|
2020-04-10 05:09:00 +08:00
|
|
|
bool isAnnotated() const;
|
|
|
|
void setAnnotated();
|
2018-05-03 01:44:38 +08:00
|
|
|
|
2018-06-27 05:37:21 +08:00
|
|
|
void addField(const std::string& key, const std::string& value);
|
|
|
|
void addField(std::string&& key, std::string&& value);
|
2018-05-03 01:44:38 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
const Field& operator[](int index) const;
|
|
|
|
bool tryGetValue(std::string key, std::string& outValue) const;
|
2018-05-03 01:44:38 +08:00
|
|
|
std::string getValue(std::string key) const;
|
2021-03-11 02:06:03 +08:00
|
|
|
int getInt(std::string key, bool permissive = false) const;
|
|
|
|
int64_t getInt64(std::string key, bool permissive = false) const;
|
|
|
|
double getDouble(std::string key, bool permissive = false) const;
|
2018-05-03 01:44:38 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
Field& mutate(int index);
|
2020-02-22 02:55:14 +08:00
|
|
|
|
2018-05-03 01:44:38 +08:00
|
|
|
std::string toString() const;
|
2018-06-09 04:24:30 +08:00
|
|
|
void validateFormat() const;
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class Archiver>
|
2019-01-29 11:38:13 +08:00
|
|
|
void serialize(Archiver& ar) {
|
|
|
|
static_assert(is_fb_function<Archiver>, "Streaming serializer has to use load/save");
|
|
|
|
serializer(ar, fields);
|
|
|
|
}
|
2018-05-03 01:44:38 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
FieldContainer fields;
|
2018-06-01 04:27:35 +08:00
|
|
|
size_t bytes;
|
2020-04-10 05:09:00 +08:00
|
|
|
bool annotated;
|
2018-05-03 01:44:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Archive>
|
2021-03-11 02:06:03 +08:00
|
|
|
inline void load(Archive& ar, TraceEventFields& value) {
|
2018-05-03 01:44:38 +08:00
|
|
|
uint32_t count;
|
|
|
|
ar >> count;
|
|
|
|
|
|
|
|
std::string k;
|
|
|
|
std::string v;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2018-05-03 01:44:38 +08:00
|
|
|
ar >> k >> v;
|
|
|
|
value.addField(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <class Archive>
|
2021-03-11 02:06:03 +08:00
|
|
|
inline void save(Archive& ar, const TraceEventFields& value) {
|
2018-05-03 01:44:38 +08:00
|
|
|
ar << (uint32_t)value.size();
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto itr : value) {
|
2018-05-03 01:44:38 +08:00
|
|
|
ar << itr.first << itr.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class TraceBatch {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
void addEvent(const char* name, uint64_t id, const char* location);
|
|
|
|
void addAttach(const char* name, uint64_t id, uint64_t to);
|
|
|
|
void addBuggify(int activated, int line, std::string file);
|
2017-05-26 04:48:44 +08:00
|
|
|
void dump();
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct EventInfo {
|
2018-05-03 01:44:38 +08:00
|
|
|
TraceEventFields fields;
|
2021-03-11 02:06:03 +08:00
|
|
|
EventInfo(double time, const char* name, uint64_t id, const char* location);
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AttachInfo {
|
2018-05-03 01:44:38 +08:00
|
|
|
TraceEventFields fields;
|
2021-03-11 02:06:03 +08:00
|
|
|
AttachInfo(double time, const char* name, uint64_t id, uint64_t to);
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BuggifyInfo {
|
2018-05-03 01:44:38 +08:00
|
|
|
TraceEventFields fields;
|
|
|
|
BuggifyInfo(double time, int activated, int line, std::string file);
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<EventInfo> eventBatch;
|
|
|
|
std::vector<AttachInfo> attachBatch;
|
|
|
|
std::vector<BuggifyInfo> buggifyBatch;
|
2020-04-02 11:38:48 +08:00
|
|
|
static bool dumpImmediately();
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DynamicEventMetric;
|
2019-03-15 09:40:28 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class IntType>
|
2019-03-16 06:32:39 +08:00
|
|
|
char base16Char(IntType c) {
|
2019-04-10 09:14:09 +08:00
|
|
|
switch ((c % 16 + 16) % 16) {
|
2021-03-11 02:06:03 +08:00
|
|
|
case 0:
|
|
|
|
return '0';
|
|
|
|
case 1:
|
|
|
|
return '1';
|
|
|
|
case 2:
|
|
|
|
return '2';
|
|
|
|
case 3:
|
|
|
|
return '3';
|
|
|
|
case 4:
|
|
|
|
return '4';
|
|
|
|
case 5:
|
|
|
|
return '5';
|
|
|
|
case 6:
|
|
|
|
return '6';
|
|
|
|
case 7:
|
|
|
|
return '7';
|
|
|
|
case 8:
|
|
|
|
return '8';
|
|
|
|
case 9:
|
|
|
|
return '9';
|
|
|
|
case 10:
|
|
|
|
return 'a';
|
|
|
|
case 11:
|
|
|
|
return 'b';
|
|
|
|
case 12:
|
|
|
|
return 'c';
|
|
|
|
case 13:
|
|
|
|
return 'd';
|
|
|
|
case 14:
|
|
|
|
return 'e';
|
|
|
|
case 15:
|
|
|
|
return 'f';
|
|
|
|
default:
|
|
|
|
UNSTOPPABLE_ASSERT(false);
|
2019-03-16 06:32:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 09:40:28 +08:00
|
|
|
// forward declare format from flow.h as we
|
|
|
|
// can't include flow.h here
|
|
|
|
std::string format(const char* form, ...);
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
2019-03-15 09:40:28 +08:00
|
|
|
struct Traceable : std::false_type {};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
#define FORMAT_TRACEABLE(type, fmt) \
|
|
|
|
template <> \
|
|
|
|
struct Traceable<type> : std::true_type { \
|
|
|
|
static std::string toString(type value) { return format(fmt, value); } \
|
2019-03-15 09:40:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FORMAT_TRACEABLE(bool, "%d");
|
|
|
|
FORMAT_TRACEABLE(signed char, "%d");
|
|
|
|
FORMAT_TRACEABLE(unsigned char, "%d");
|
|
|
|
FORMAT_TRACEABLE(short, "%d");
|
|
|
|
FORMAT_TRACEABLE(unsigned short, "%d");
|
|
|
|
FORMAT_TRACEABLE(int, "%d");
|
|
|
|
FORMAT_TRACEABLE(unsigned, "%u");
|
|
|
|
FORMAT_TRACEABLE(long int, "%ld");
|
|
|
|
FORMAT_TRACEABLE(unsigned long int, "%lu");
|
|
|
|
FORMAT_TRACEABLE(long long int, "%lld");
|
|
|
|
FORMAT_TRACEABLE(unsigned long long int, "%llu");
|
|
|
|
FORMAT_TRACEABLE(double, "%g");
|
2019-06-25 08:36:55 +08:00
|
|
|
FORMAT_TRACEABLE(void*, "%p");
|
2019-03-16 00:11:05 +08:00
|
|
|
FORMAT_TRACEABLE(volatile long, "%ld");
|
|
|
|
FORMAT_TRACEABLE(volatile unsigned long, "%lu");
|
|
|
|
FORMAT_TRACEABLE(volatile long long, "%lld");
|
|
|
|
FORMAT_TRACEABLE(volatile unsigned long long, "%llu");
|
|
|
|
FORMAT_TRACEABLE(volatile double, "%g");
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-03-15 09:40:28 +08:00
|
|
|
struct Traceable<UID> : std::true_type {
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(const UID& value) { return format("%016llx", value.first()); }
|
2019-03-15 09:40:28 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class Str>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct TraceableString {
|
2021-03-11 02:06:03 +08:00
|
|
|
static auto begin(const Str& value) -> decltype(value.begin()) { return value.begin(); }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static bool atEnd(const Str& value, decltype(value.begin()) iter) { return iter == value.end(); }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(const Str& value) { return value.toString(); }
|
2019-04-06 04:11:50 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct TraceableString<std::string> {
|
2021-03-11 02:06:03 +08:00
|
|
|
static auto begin(const std::string& value) -> decltype(value.begin()) { return value.begin(); }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static bool atEnd(const std::string& value, decltype(value.begin()) iter) { return iter == value.end(); }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class S>
|
2019-04-06 04:11:50 +08:00
|
|
|
static std::string toString(S&& value) {
|
|
|
|
return std::forward<S>(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct TraceableString<const char*> {
|
2021-03-11 02:06:03 +08:00
|
|
|
static const char* begin(const char* value) { return value; }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static bool atEnd(const char* value, const char* iter) { return *iter == '\0'; }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(const char* value) { return std::string(value); }
|
2019-03-16 06:32:39 +08:00
|
|
|
};
|
|
|
|
|
2019-04-16 00:43:26 +08:00
|
|
|
std::string traceableStringToString(const char* value, size_t S);
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <size_t S>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct TraceableString<char[S]> {
|
2019-04-16 06:20:09 +08:00
|
|
|
static_assert(S > 0, "Only string literals are supported.");
|
2021-03-11 02:06:03 +08:00
|
|
|
static const char* begin(const char* value) { return value; }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
|
|
|
static bool atEnd(const char* value, const char* iter) {
|
2019-04-12 01:39:48 +08:00
|
|
|
return iter - value == S - 1; // Exclude trailing \0 byte
|
2019-04-06 04:11:50 +08:00
|
|
|
}
|
|
|
|
|
2019-04-16 00:43:26 +08:00
|
|
|
static std::string toString(const char* value) { return traceableStringToString(value, S); }
|
2019-03-16 06:32:39 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct TraceableString<char*> {
|
2021-03-11 02:06:03 +08:00
|
|
|
static const char* begin(char* value) { return value; }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static bool atEnd(char* value, const char* iter) { return *iter == '\0'; }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(char* value) { return std::string(value); }
|
2019-03-16 06:32:39 +08:00
|
|
|
};
|
2019-03-15 09:40:28 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct TraceableStringImpl : std::true_type {
|
2019-04-12 01:01:06 +08:00
|
|
|
static constexpr bool isPrintable(char c) { return 32 <= c && c <= 126; }
|
2019-04-06 04:11:50 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class Str>
|
2019-03-16 06:32:39 +08:00
|
|
|
static std::string toString(Str&& value) {
|
|
|
|
// if all characters are printable ascii, we simply return the string
|
|
|
|
int nonPrintables = 0;
|
|
|
|
int numBackslashes = 0;
|
2019-04-06 04:11:50 +08:00
|
|
|
int size = 0;
|
|
|
|
for (auto iter = TraceableString<T>::begin(value); !TraceableString<T>::atEnd(value, iter); ++iter) {
|
|
|
|
++size;
|
|
|
|
if (!isPrintable(char(*iter))) {
|
2019-03-16 06:32:39 +08:00
|
|
|
++nonPrintables;
|
2019-04-06 04:11:50 +08:00
|
|
|
} else if (*iter == '\\') {
|
2019-03-16 06:32:39 +08:00
|
|
|
++numBackslashes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nonPrintables == 0 && numBackslashes == 0) {
|
2019-04-06 04:11:50 +08:00
|
|
|
return TraceableString<T>::toString(std::forward<Str>(value));
|
2019-03-16 06:32:39 +08:00
|
|
|
}
|
|
|
|
std::string result;
|
2019-04-06 04:11:50 +08:00
|
|
|
result.reserve(size - nonPrintables + (nonPrintables * 4) + numBackslashes);
|
|
|
|
for (auto iter = TraceableString<T>::begin(value); !TraceableString<T>::atEnd(value, iter); ++iter) {
|
|
|
|
if (isPrintable(*iter)) {
|
|
|
|
result.push_back(*iter);
|
|
|
|
} else if (*iter == '\\') {
|
2019-03-16 06:32:39 +08:00
|
|
|
result.push_back('\\');
|
|
|
|
result.push_back('\\');
|
|
|
|
} else {
|
2019-06-06 05:38:35 +08:00
|
|
|
const uint8_t byte = *iter;
|
2019-03-16 06:32:39 +08:00
|
|
|
result.push_back('\\');
|
|
|
|
result.push_back('x');
|
2019-06-06 05:38:35 +08:00
|
|
|
result.push_back(base16Char(byte / 16));
|
|
|
|
result.push_back(base16Char(byte));
|
2019-03-16 06:32:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
2019-03-15 09:40:28 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct Traceable<const char*> : TraceableStringImpl<const char*> {};
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct Traceable<char*> : TraceableStringImpl<char*> {};
|
2021-03-11 02:06:03 +08:00
|
|
|
template <size_t S>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct Traceable<char[S]> : TraceableStringImpl<char[S]> {};
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-04-06 04:11:50 +08:00
|
|
|
struct Traceable<std::string> : TraceableStringImpl<std::string> {};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
2019-03-15 09:40:28 +08:00
|
|
|
struct SpecialTraceMetricType
|
2021-03-11 02:06:03 +08:00
|
|
|
: std::conditional<std::is_integral<T>::value || std::is_enum<T>::value, std::true_type, std::false_type>::type {
|
|
|
|
static int64_t getValue(T v) { return v; }
|
2019-03-15 09:40:28 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
#define TRACE_METRIC_TYPE(from, to) \
|
|
|
|
template <> \
|
|
|
|
struct SpecialTraceMetricType<from> : std::true_type { \
|
|
|
|
static to getValue(from v) { return v; } \
|
2019-03-15 09:40:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE_METRIC_TYPE(double, double);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
struct TraceEvent {
|
2020-02-22 02:55:14 +08:00
|
|
|
TraceEvent();
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent(const char* type, UID id = UID()); // Assumes SevInfo severity
|
|
|
|
TraceEvent(Severity, const char* type, UID id = UID());
|
|
|
|
TraceEvent(struct TraceInterval&, UID id = UID());
|
|
|
|
TraceEvent(Severity severity, struct TraceInterval& interval, UID id = UID());
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent(TraceEvent&& ev);
|
|
|
|
TraceEvent& operator=(TraceEvent&& ev);
|
2020-02-22 02:55:14 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
static void setNetworkThread();
|
|
|
|
static bool isNetworkThread();
|
|
|
|
|
2019-12-07 02:15:49 +08:00
|
|
|
static double getCurrentTime();
|
2020-10-22 08:57:27 +08:00
|
|
|
static std::string printRealTime(double time);
|
2019-12-07 02:15:49 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Must be called directly after constructing the trace event
|
|
|
|
TraceEvent& error(const class Error& e, bool includeCancelled = false) {
|
2019-03-15 09:40:28 +08:00
|
|
|
if (enabled) {
|
|
|
|
return errorImpl(e, includeCancelled);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
|
|
|
typename std::enable_if<Traceable<T>::value, TraceEvent&>::type detail(std::string&& key, const T& value) {
|
2019-03-15 13:15:27 +08:00
|
|
|
if (enabled && init()) {
|
2019-03-15 09:40:28 +08:00
|
|
|
auto s = Traceable<T>::toString(value);
|
|
|
|
addMetric(key.c_str(), value, s);
|
2019-03-20 05:13:27 +08:00
|
|
|
return detailImpl(std::move(key), std::move(s), false);
|
2019-03-15 09:40:28 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
|
|
|
typename std::enable_if<Traceable<T>::value, TraceEvent&>::type detail(const char* key, const T& value) {
|
2019-03-15 13:15:27 +08:00
|
|
|
if (enabled && init()) {
|
2019-03-15 09:40:28 +08:00
|
|
|
auto s = Traceable<T>::toString(value);
|
|
|
|
addMetric(key, value, s);
|
|
|
|
return detailImpl(std::string(key), std::move(s), false);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
|
|
|
typename std::enable_if<std::is_enum<T>::value, TraceEvent&>::type detail(const char* key, T value) {
|
2019-03-15 13:15:27 +08:00
|
|
|
if (enabled && init()) {
|
2019-03-15 09:40:28 +08:00
|
|
|
setField(key, int64_t(value));
|
|
|
|
return detailImpl(std::string(key), format("%d", value), false);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent& detailf(std::string key, const char* valueFormat, ...);
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
|
|
|
typename std::enable_if<SpecialTraceMetricType<T>::value, void>::type addMetric(const char* key,
|
|
|
|
const T& value,
|
|
|
|
const std::string&) {
|
2019-03-15 09:40:28 +08:00
|
|
|
setField(key, SpecialTraceMetricType<T>::getValue(value));
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
|
|
|
typename std::enable_if<!SpecialTraceMetricType<T>::value, void>::type addMetric(const char* key,
|
|
|
|
const T&,
|
|
|
|
const std::string& value) {
|
2019-03-15 09:40:28 +08:00
|
|
|
setField(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setField(const char* key, int64_t value);
|
|
|
|
void setField(const char* key, double value);
|
|
|
|
void setField(const char* key, const std::string& value);
|
2021-07-01 03:45:50 +08:00
|
|
|
void setThreadId();
|
2019-03-15 09:40:28 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent& errorImpl(const class Error& e, bool includeCancelled = false);
|
2018-05-03 01:44:38 +08:00
|
|
|
// Private version of detailf that does NOT write to the eventMetric. This is to be used by other detail methods
|
2017-05-26 04:48:44 +08:00
|
|
|
// which can write field metrics of a more appropriate type than string but use detailf() to add to the TraceEvent.
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent& detailfNoMetric(std::string&& key, const char* valueFormat, ...);
|
|
|
|
TraceEvent& detailImpl(std::string&& key, std::string&& value, bool writeEventMetricField = true);
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2018-06-27 05:37:21 +08:00
|
|
|
TraceEvent& backtrace(const std::string& prefix = "");
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceEvent& trackLatest(const std::string& trackingKey);
|
|
|
|
TraceEvent& sample(double sampleRate, bool logSampleRate = true);
|
2018-08-02 05:30:57 +08:00
|
|
|
|
2019-07-13 07:12:35 +08:00
|
|
|
// Sets the maximum length a field can be before it gets truncated. A value of 0 uses the default, a negative value
|
|
|
|
// disables truncation. This should be called before the field whose length you want to change, and it can be
|
|
|
|
// changed multiple times in a single event.
|
|
|
|
TraceEvent& setMaxFieldLength(int maxFieldLength);
|
|
|
|
|
2021-01-29 17:31:26 +08:00
|
|
|
int getMaxFieldLength() const;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Sets the maximum event length before the event gets suppressed and a warning is logged. A value of 0 uses the
|
|
|
|
// default, a negative value disables length suppression. This should be called before adding details.
|
2019-07-13 07:12:35 +08:00
|
|
|
TraceEvent& setMaxEventLength(int maxEventLength);
|
|
|
|
|
2021-01-29 17:31:26 +08:00
|
|
|
int getMaxEventLength() const;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Cannot call other functions which could disable the trace event afterwords
|
|
|
|
TraceEvent& suppressFor(double duration, bool logSuppressedEventCount = true);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
TraceEvent& GetLastError();
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
bool isEnabled() const { return enabled; }
|
2019-12-20 10:26:29 +08:00
|
|
|
|
2021-06-12 05:20:38 +08:00
|
|
|
TraceEvent& setErrorKind(ErrorKind errorKind);
|
2021-05-03 23:09:52 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
explicit operator bool() const { return enabled; }
|
2020-05-14 05:28:04 +08:00
|
|
|
|
2020-01-04 04:15:38 +08:00
|
|
|
void log();
|
|
|
|
|
2021-06-12 05:20:38 +08:00
|
|
|
void disable() { enabled = false; } // Disables the trace event so it doesn't get
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
~TraceEvent(); // Actually logs the event
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-12-16 10:20:33 +08:00
|
|
|
// Return the number of invocations of TraceEvent() at the specified logging level.
|
|
|
|
static unsigned long CountEventsLoggedAt(Severity);
|
|
|
|
|
2020-11-27 15:24:47 +08:00
|
|
|
std::unique_ptr<DynamicEventMetric> tmpEventMetric; // This just just a place to store fields
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-06-12 05:20:38 +08:00
|
|
|
const TraceEventFields& getFields() const { return fields; }
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2018-08-02 05:30:57 +08:00
|
|
|
bool initialized;
|
2017-05-26 04:48:44 +08:00
|
|
|
bool enabled;
|
2020-01-04 04:15:38 +08:00
|
|
|
bool logged;
|
2017-05-26 04:48:44 +08:00
|
|
|
std::string trackingKey;
|
2018-05-03 01:44:38 +08:00
|
|
|
TraceEventFields fields;
|
2017-05-26 04:48:44 +08:00
|
|
|
Severity severity;
|
2021-05-06 05:18:06 +08:00
|
|
|
ErrorKind errorKind{ ErrorKind::Unset };
|
2021-03-11 02:06:03 +08:00
|
|
|
const char* type;
|
2017-05-26 04:48:44 +08:00
|
|
|
UID id;
|
2018-08-02 05:30:57 +08:00
|
|
|
Error err;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-07-13 07:12:35 +08:00
|
|
|
int maxFieldLength;
|
|
|
|
int maxEventLength;
|
2020-02-22 02:55:14 +08:00
|
|
|
int timeIndex;
|
2021-06-12 05:20:38 +08:00
|
|
|
int errorKindIndex{ -1 };
|
2019-07-13 07:12:35 +08:00
|
|
|
|
|
|
|
void setSizeLimits();
|
|
|
|
|
2020-11-02 14:13:24 +08:00
|
|
|
static unsigned long eventCounts[NUM_MAJOR_LEVELS_OF_EVENTS];
|
2017-05-26 04:48:44 +08:00
|
|
|
static thread_local bool networkThread;
|
|
|
|
|
2018-08-02 05:30:57 +08:00
|
|
|
bool init();
|
2021-03-11 02:06:03 +08:00
|
|
|
bool init(struct TraceInterval&);
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2020-07-08 00:06:13 +08:00
|
|
|
class StringRef;
|
2020-05-06 08:58:41 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
struct TraceInterval {
|
2021-03-11 02:06:03 +08:00
|
|
|
TraceInterval(const char* type) : count(-1), type(type), severity(SevInfo) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
TraceInterval& begin();
|
|
|
|
TraceInterval& end() { return *this; }
|
|
|
|
|
|
|
|
const char* type;
|
|
|
|
UID pairID;
|
|
|
|
int count;
|
2018-02-17 08:01:19 +08:00
|
|
|
Severity severity;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LatestEventCache {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
void set(std::string tag, const TraceEventFields& fields);
|
2021-06-10 02:37:14 +08:00
|
|
|
TraceEventFields get(std::string const& tag);
|
2018-05-03 01:44:38 +08:00
|
|
|
std::vector<TraceEventFields> getAll();
|
|
|
|
std::vector<TraceEventFields> getAllUnsafe();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-06-10 02:37:14 +08:00
|
|
|
void clear(std::string const& prefix);
|
2017-05-26 04:48:44 +08:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
// Latest error tracking only tracks errors when called from the main thread. Other errors are silently ignored.
|
2021-03-11 02:06:03 +08:00
|
|
|
void setLatestError(const TraceEventFields& contents);
|
2018-05-03 01:44:38 +08:00
|
|
|
TraceEventFields getLatestError();
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2019-03-15 09:40:28 +08:00
|
|
|
std::map<struct NetworkAddress, std::map<std::string, TraceEventFields>> latest;
|
|
|
|
std::map<struct NetworkAddress, TraceEventFields> latestErrors;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
extern LatestEventCache latestEventCache;
|
|
|
|
|
2020-03-06 10:17:06 +08:00
|
|
|
struct EventCacheHolder : public ReferenceCounted<EventCacheHolder> {
|
|
|
|
std::string trackingKey;
|
|
|
|
|
|
|
|
EventCacheHolder(const std::string& trackingKey) : trackingKey(trackingKey) {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
~EventCacheHolder() { latestEventCache.clear(trackingKey); }
|
2020-03-06 10:17:06 +08:00
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// Evil but potentially useful for verbose messages:
|
|
|
|
#if CENABLED(0, NOT_IN_CLEAN)
|
2021-03-11 02:06:03 +08:00
|
|
|
#define TRACE(t, m) \
|
|
|
|
if (TraceEvent::isEnabled(t)) \
|
|
|
|
TraceEvent(t, m)
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct NetworkAddress;
|
2021-03-11 02:06:03 +08:00
|
|
|
void openTraceFile(const NetworkAddress& na,
|
|
|
|
uint64_t rollsize,
|
|
|
|
uint64_t maxLogsSize,
|
|
|
|
std::string directory = ".",
|
|
|
|
std::string baseOfBase = "trace",
|
|
|
|
std::string logGroup = "default",
|
|
|
|
std::string identifier = "");
|
2017-05-26 04:48:44 +08:00
|
|
|
void initTraceEventMetrics();
|
|
|
|
void closeTraceFile();
|
|
|
|
bool traceFileIsOpen();
|
2019-06-15 02:35:38 +08:00
|
|
|
void flushTraceFileVoid();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2018-12-21 05:51:06 +08:00
|
|
|
// Changes the format of trace files. Returns false if the format is unrecognized. No longer safe to call after a call
|
|
|
|
// to openTraceFile.
|
|
|
|
bool selectTraceFormatter(std::string format);
|
2019-01-30 05:08:47 +08:00
|
|
|
// Returns true iff format is recognized.
|
|
|
|
bool validateTraceFormat(std::string format);
|
2018-12-21 05:51:06 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Select the clock source for trace files. Returns false if the format is unrecognized. No longer safe to call after a
|
|
|
|
// call to openTraceFile.
|
2019-11-07 06:31:13 +08:00
|
|
|
bool selectTraceClockSource(std::string source);
|
|
|
|
// Returns true iff source is recognized.
|
|
|
|
bool validateTraceClockSource(std::string source);
|
|
|
|
|
2021-06-10 02:37:14 +08:00
|
|
|
void addTraceRole(std::string const& role);
|
|
|
|
void removeTraceRole(std::string const& role);
|
2020-11-05 14:39:56 +08:00
|
|
|
void retrieveTraceLogIssues(std::set<std::string>& out);
|
2020-03-26 04:27:56 +08:00
|
|
|
void setTraceLogGroup(const std::string& role);
|
2020-03-13 05:34:19 +08:00
|
|
|
template <class T>
|
2020-03-17 04:36:55 +08:00
|
|
|
struct Future;
|
|
|
|
struct Void;
|
|
|
|
Future<Void> pingTraceLogWriterThread();
|
2018-09-06 06:06:14 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
enum trace_clock_t { TRACE_CLOCK_NOW, TRACE_CLOCK_REALTIME };
|
2019-12-07 02:15:49 +08:00
|
|
|
extern std::atomic<trace_clock_t> g_trace_clock;
|
2017-05-26 04:48:44 +08:00
|
|
|
extern TraceBatch g_traceBatch;
|
|
|
|
|
2019-09-04 06:50:21 +08:00
|
|
|
#define DUMPTOKEN(name) \
|
|
|
|
TraceEvent("DumpToken", recruited.id()).detail("Name", #name).detail("Token", name.getEndpoint().token)
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|