Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
quic: further refinement to StatsDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 23, 2020
1 parent 937bd04 commit 9e495d4
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 96 deletions.
20 changes: 5 additions & 15 deletions src/quic/node_quic_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1347,9 +1347,6 @@ QuicSession::QuicSession(

QuicSession::~QuicSession() {
CHECK(!Ngtcp2CallbackScope::InNgtcp2CallbackScope(this));
StatsDebug stats_debug(this);
Debug(this, "Destroyed. %s", stats_debug.ToString().c_str());

crypto_context_->Cancel();
connection_.reset();

Expand All @@ -1359,18 +1356,11 @@ QuicSession::~QuicSession() {
RemoveListener(listener_);
}

std::string QuicSession::StatsDebug::ToString() {
#define V(_, name, label) \
" "## label + ": " + \
std::to_string(session_->GetStat(&QuicSessionStats::name)) + "\n"

std::string out = "Statistics:\n";
out += " Duration: " +
std::to_string(uv_hrtime() -
session_->GetStat(&QuicSessionStats::created_at)) + "\n" +
SESSION_STATS(V);
return out;

template <typename Fn>
void QuicSessionStatsTraits::ToString(const QuicSession& ptr, Fn&& add_field) {
#define V(_n, name, label) \
add_field(label, ptr.GetStat(&QuicSessionStats::name));
SESSION_STATS(V)
#undef V
}

Expand Down
19 changes: 9 additions & 10 deletions src/quic/node_quic_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ struct QuicSessionStats {
};
#undef V

struct QuicSessionStatsTraits {
using Stats = QuicSessionStats;
using Base = QuicSession;

template <typename Fn>
static void ToString(const QuicSession& ptr, Fn&& add_field);
};

class QuicSessionListener {
public:
virtual ~QuicSessionListener();
Expand Down Expand Up @@ -613,7 +621,7 @@ class QuicApplication : public MemoryRetainer {
// a QuicSession object.
class QuicSession : public AsyncWrap,
public mem::NgLibMemoryManager<QuicSession, ngtcp2_mem>,
public StatsBase<QuicSessionStats> {
public StatsBase<QuicSessionStatsTraits> {
public:
// The default preferred address strategy is to ignore it
static void IgnorePreferredAddressStrategy(
Expand Down Expand Up @@ -1423,15 +1431,6 @@ class QuicSession : public AsyncWrap,

static const ngtcp2_conn_callbacks callbacks[2];

class StatsDebug {
public:
StatsDebug(QuicSession* session) : session_(session) {}
std::string ToString();
private:
QuicSession* session_;
};


friend class QuicCryptoContext;
friend class QuicSessionListener;
friend class JSQuicSessionListener;
Expand Down
19 changes: 5 additions & 14 deletions src/quic/node_quic_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,26 +285,17 @@ QuicSocket::QuicSocket(

QuicSocket::~QuicSocket() {
uint64_t now = uv_hrtime();
StatsDebug stats_debug(this);
Debug(this, "Destroyed. %s", stats_debug.ToString().c_str());
QuicSocketListener* listener = listener_;
listener_->OnDestroy();
if (listener == listener_)
RemoveListener(listener_);
}

std::string QuicSocket::StatsDebug::ToString() {
#define V(_, name, label) \
" "## label + ": " + \
std::to_string(socket_->GetStat(&QuicSocketStats::name)) + "\n"

std::string out = "Statistics:\n";
out += " Duration: " +
std::to_string(uv_hrtime() -
socket_->GetStat(&QuicSocketStats::created_at)) + "\n" +
SOCKET_STATS(V);
return out;

template <typename Fn>
void QuicSocketStatsTraits::ToString(const QuicSocket& ptr, Fn&& add_field) {
#define V(_n, name, label) \
add_field(label, ptr.GetStat(&QuicSocketStats::name));
SOCKET_STATS(V)
#undef V
}

Expand Down
18 changes: 9 additions & 9 deletions src/quic/node_quic_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ struct QuicSocketStats {
};
#undef V

struct QuicSocketStatsTraits {
using Stats = QuicSocketStats;
using Base = QuicSocket;

template <typename Fn>
static void ToString(const QuicSocket& ptr, Fn&& add_field);
};

class QuicSocket;
class QuicEndpoint;

Expand Down Expand Up @@ -244,7 +252,7 @@ class QuicEndpoint : public BaseObject,
class QuicSocket : public AsyncWrap,
public QuicEndpointListener,
public mem::NgLibMemoryManager<QuicSocket, ngtcp2_mem>,
public StatsBase<QuicSocketStats> {
public StatsBase<QuicSocketStatsTraits> {
public:
static void Initialize(
Environment* env,
Expand Down Expand Up @@ -531,14 +539,6 @@ class QuicSocket : public AsyncWrap,

SendWrap* last_created_send_wrap_ = nullptr;

class StatsDebug {
public:
StatsDebug(QuicSocket* socket) : socket_(socket) {}
std::string ToString();
private:
QuicSocket* socket_;
};

friend class QuicSocketListener;
};

Expand Down
2 changes: 1 addition & 1 deletion src/quic/node_quic_stream-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void QuicStream::Unschedule() {
stream_queue_.Remove();
}

std::string QuicHeader::ToString() {
std::string QuicHeader::ToString() const {
return name() + " = " + value();
}

Expand Down
22 changes: 6 additions & 16 deletions src/quic/node_quic_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,13 @@ QuicStream::QuicStream(
IncrementStat(&QuicStreamStats::max_offset, params.initial_max_data);
}

QuicStream::~QuicStream() {
StatsDebug stats_debug(this);
Debug(this, "Destroyed. %s", stats_debug.ToString().c_str());
}

std::string QuicStream::StatsDebug::ToString() {
#define V(_, name, label) \
" "## label + ": " + \
std::to_string(stream_->GetStat(&QuicStreamStats::name)) + "\n"

std::string out = "Statistics:\n";
out += " Duration: " +
std::to_string(uv_hrtime() -
stream_->GetStat(&QuicStreamStats::created_at)) + "\n" +
STREAM_STATS(V);
return out;
QuicStream::~QuicStream() {}

template <typename Fn>
void QuicStreamStatsTraits::ToString(const QuicStream& ptr, Fn&& add_field) {
#define V(_n, name, label) \
add_field(label, ptr.GetStat(&QuicStreamStats::name));
STREAM_STATS(V)
#undef V
}

Expand Down
25 changes: 12 additions & 13 deletions src/quic/node_quic_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace node {
namespace quic {

class QuicSession;
class QuicStream;
class QuicApplication;

enum QuicStreamHeaderFlags : uint32_t {
Expand Down Expand Up @@ -65,15 +66,22 @@ struct QuicStreamStats {
};
#undef V

struct QuicStreamStatsTraits {
using Stats = QuicStreamStats;
using Base = QuicStream;

template <typename Fn>
static void ToString(const QuicStream& ptr, Fn&& add_field);
};

// QuicHeader is a base class for implementing QUIC application
// specific headers. Each type of QUIC application may have
// different internal representations for a header name+value
// pair. QuicApplication implementations that support headers
// per stream must create a specialization of the Header class.
class QuicHeader : public MemoryRetainer {
public:
QuicHeader() {}

QuicHeader() = default;
virtual ~QuicHeader() {}
virtual v8::MaybeLocal<v8::String> GetName(QuicApplication* app) const = 0;
virtual v8::MaybeLocal<v8::String> GetValue(QuicApplication* app) const = 0;
Expand All @@ -84,7 +92,7 @@ class QuicHeader : public MemoryRetainer {
// (including the name and value)
virtual size_t length() const = 0;

inline std::string ToString();
inline std::string ToString() const;
};

enum QuicStreamStates : uint32_t {
Expand Down Expand Up @@ -198,7 +206,7 @@ enum QuicStreamOrigin {
class QuicStream : public AsyncWrap,
public bob::SourceImpl<ngtcp2_vec>,
public StreamBase,
public StatsBase<QuicStreamStats> {
public StatsBase<QuicStreamStatsTraits> {
public:
static void Initialize(
Environment* env,
Expand Down Expand Up @@ -380,15 +388,6 @@ class QuicStream : public AsyncWrap,
size_t current_headers_length_ = 0;

ListNode<QuicStream> stream_queue_;

class StatsDebug {
public:
StatsDebug(QuicStream* stream) : stream_(stream) {}
std::string ToString();
private:
QuicStream* stream_;
};

public:
// Linked List of QuicStream objects
using Queue = ListHead<QuicStream, &QuicStream::stream_queue_>;
Expand Down
39 changes: 31 additions & 8 deletions src/quic/node_quic_util-inl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SRC_QUIC_NODE_QUIC_UTIL_INL_H_
#define SRC_QUIC_NODE_QUIC_UTIL_INL_H_

#include "debug_utils.h"
#include "node_internals.h"
#include "node_quic_util.h"
#include "memory_tracker-inl.h"
Expand Down Expand Up @@ -324,7 +325,7 @@ StatsBase<T>::StatsBase(
int options)
: stats_buffer_(
env->isolate(),
sizeof(T) / sizeof(uint64_t),
sizeof(typename T::Stats) / sizeof(uint64_t),
reinterpret_cast<uint64_t*>(&stats_)) {
static constexpr uint64_t kMax = std::numeric_limits<int64_t>::max();
stats_.created_at = uv_hrtime();
Expand Down Expand Up @@ -368,30 +369,29 @@ StatsBase<T>::StatsBase(
}

template <typename T>
void StatsBase<T>::IncrementStat(uint64_t T::*member, uint64_t amount) {
void StatsBase<T>::IncrementStat(uint64_t T::Stats::*member, uint64_t amount) {
static constexpr uint64_t kMax = std::numeric_limits<uint64_t>::max();
stats_.*member += std::min(amount, kMax - stats_.*member);
}

template <typename T>
void StatsBase<T>::SetStat(uint64_t T::*member, uint64_t value) {
void StatsBase<T>::SetStat(uint64_t T::Stats::*member, uint64_t value) {
stats_.*member = value;
}

template <typename T>
void StatsBase<T>::RecordTimestamp(uint64_t T::*member) {
void StatsBase<T>::RecordTimestamp(uint64_t T::Stats::*member) {
stats_.*member = uv_hrtime();
}

template <typename T>
uint64_t StatsBase<T>::GetStat(uint64_t T::*member) const {
uint64_t StatsBase<T>::GetStat(uint64_t T::Stats::*member) const {
return stats_.*member;
}

template <typename T>
inline void StatsBase<T>::RecordRate(uint64_t T::*member) {
inline void StatsBase<T>::RecordRate(uint64_t T::Stats::*member) {
CHECK(rate_);

uint64_t received_at = GetStat(member);
uint64_t now = uv_hrtime();
if (received_at > 0)
Expand All @@ -406,7 +406,7 @@ inline void StatsBase<T>::RecordSize(uint64_t val) {
}

template <typename T>
inline void StatsBase<T>::RecordAck(uint64_t T::*member) {
inline void StatsBase<T>::RecordAck(uint64_t T::Stats::*member) {
CHECK(ack_);
uint64_t acked_at = GetStat(member);
uint64_t now = uv_hrtime();
Expand All @@ -423,6 +423,29 @@ void StatsBase<T>::StatsMemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("ack_histogram", ack_);
}

template <typename T>
StatsBase<T>::~StatsBase() {
StatsBase<T>::StatsDebug stats_debug(static_cast<typename T::Base*>(this));
Debug(static_cast<T::Base*>(this),
"Destroyed. %s",
stats_debug.ToString().c_str());
}

template <typename T>
std::string StatsBase<T>::StatsDebug::ToString() const {
std::string out = "Statistics:\n";
auto add_field = [&out](const char* name, uint64_t val) {
out += " ";
out += std::string(name);
out += ": ";
out += std::to_string(val);
out += "\n";
};
add_field("Duration", uv_hrtime() - ptr->GetStat(&T::Stats::created_at));
T::ToString(*ptr, add_field);
return out;
}

template <typename T>
size_t get_length(const T* vec, size_t count) {
CHECK_NOT_NULL(vec);
Expand Down
Loading

0 comments on commit 9e495d4

Please sign in to comment.