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

Commit 5c2a8bd

Browse files
committed
quic: further refinement to StatsDebug
1 parent 937bd04 commit 5c2a8bd

12 files changed

+119
-104
lines changed

src/quic/node_quic_buffer-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void QuicBufferChunk::Done(int status) {
5454
QuicBuffer::QuicBuffer(QuicBuffer&& src) noexcept
5555
: head_(src.head_),
5656
tail_(src.tail_),
57-
length_(src.length_),
58-
ended_(src.ended_) {
57+
ended_(src.ended_),
58+
length_(src.length_) {
5959
root_ = std::move(src.root_);
6060
src.head_ = nullptr;
6161
src.tail_ = nullptr;

src/quic/node_quic_http3_application.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ MaybeLocal<String> Http3Header::GetName(QuicApplication* app) const {
8282
}
8383

8484
MaybeLocal<String> Http3Header::GetValue(QuicApplication* app) const {
85-
Environment* env = app->env();
8685
return Http3RcBufferPointer::External::New(
8786
static_cast<Http3Application*>(app),
8887
value_);

src/quic/node_quic_session.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,9 +1347,6 @@ QuicSession::QuicSession(
13471347

13481348
QuicSession::~QuicSession() {
13491349
CHECK(!Ngtcp2CallbackScope::InNgtcp2CallbackScope(this));
1350-
StatsDebug stats_debug(this);
1351-
Debug(this, "Destroyed. %s", stats_debug.ToString().c_str());
1352-
13531350
crypto_context_->Cancel();
13541351
connection_.reset();
13551352

@@ -1359,18 +1356,11 @@ QuicSession::~QuicSession() {
13591356
RemoveListener(listener_);
13601357
}
13611358

1362-
std::string QuicSession::StatsDebug::ToString() {
1363-
#define V(_, name, label) \
1364-
" "## label + ": " + \
1365-
std::to_string(session_->GetStat(&QuicSessionStats::name)) + "\n"
1366-
1367-
std::string out = "Statistics:\n";
1368-
out += " Duration: " +
1369-
std::to_string(uv_hrtime() -
1370-
session_->GetStat(&QuicSessionStats::created_at)) + "\n" +
1371-
SESSION_STATS(V);
1372-
return out;
1373-
1359+
template <typename Fn>
1360+
void QuicSessionStatsTraits::ToString(const QuicSession& ptr, Fn&& add_field) {
1361+
#define V(_n, name, label) \
1362+
add_field(label, ptr.GetStat(&QuicSessionStats::name));
1363+
SESSION_STATS(V)
13741364
#undef V
13751365
}
13761366

src/quic/node_quic_session.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ struct QuicSessionStats {
234234
};
235235
#undef V
236236

237+
struct QuicSessionStatsTraits {
238+
using Stats = QuicSessionStats;
239+
using Base = QuicSession;
240+
241+
template <typename Fn>
242+
static void ToString(const QuicSession& ptr, Fn&& add_field);
243+
};
244+
237245
class QuicSessionListener {
238246
public:
239247
virtual ~QuicSessionListener();
@@ -613,7 +621,7 @@ class QuicApplication : public MemoryRetainer {
613621
// a QuicSession object.
614622
class QuicSession : public AsyncWrap,
615623
public mem::NgLibMemoryManager<QuicSession, ngtcp2_mem>,
616-
public StatsBase<QuicSessionStats> {
624+
public StatsBase<QuicSessionStatsTraits> {
617625
public:
618626
// The default preferred address strategy is to ignore it
619627
static void IgnorePreferredAddressStrategy(
@@ -1423,15 +1431,6 @@ class QuicSession : public AsyncWrap,
14231431

14241432
static const ngtcp2_conn_callbacks callbacks[2];
14251433

1426-
class StatsDebug {
1427-
public:
1428-
StatsDebug(QuicSession* session) : session_(session) {}
1429-
std::string ToString();
1430-
private:
1431-
QuicSession* session_;
1432-
};
1433-
1434-
14351434
friend class QuicCryptoContext;
14361435
friend class QuicSessionListener;
14371436
friend class JSQuicSessionListener;

src/quic/node_quic_socket.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -284,27 +284,17 @@ QuicSocket::QuicSocket(
284284
}
285285

286286
QuicSocket::~QuicSocket() {
287-
uint64_t now = uv_hrtime();
288-
StatsDebug stats_debug(this);
289-
Debug(this, "Destroyed. %s", stats_debug.ToString().c_str());
290287
QuicSocketListener* listener = listener_;
291288
listener_->OnDestroy();
292289
if (listener == listener_)
293290
RemoveListener(listener_);
294291
}
295292

296-
std::string QuicSocket::StatsDebug::ToString() {
297-
#define V(_, name, label) \
298-
" "## label + ": " + \
299-
std::to_string(socket_->GetStat(&QuicSocketStats::name)) + "\n"
300-
301-
std::string out = "Statistics:\n";
302-
out += " Duration: " +
303-
std::to_string(uv_hrtime() -
304-
socket_->GetStat(&QuicSocketStats::created_at)) + "\n" +
305-
SOCKET_STATS(V);
306-
return out;
307-
293+
template <typename Fn>
294+
void QuicSocketStatsTraits::ToString(const QuicSocket& ptr, Fn&& add_field) {
295+
#define V(_n, name, label) \
296+
add_field(label, ptr.GetStat(&QuicSocketStats::name));
297+
SOCKET_STATS(V)
308298
#undef V
309299
}
310300

src/quic/node_quic_socket.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ struct QuicSocketStats {
7070
};
7171
#undef V
7272

73+
struct QuicSocketStatsTraits {
74+
using Stats = QuicSocketStats;
75+
using Base = QuicSocket;
76+
77+
template <typename Fn>
78+
static void ToString(const QuicSocket& ptr, Fn&& add_field);
79+
};
80+
7381
class QuicSocket;
7482
class QuicEndpoint;
7583

@@ -244,7 +252,7 @@ class QuicEndpoint : public BaseObject,
244252
class QuicSocket : public AsyncWrap,
245253
public QuicEndpointListener,
246254
public mem::NgLibMemoryManager<QuicSocket, ngtcp2_mem>,
247-
public StatsBase<QuicSocketStats> {
255+
public StatsBase<QuicSocketStatsTraits> {
248256
public:
249257
static void Initialize(
250258
Environment* env,
@@ -531,14 +539,6 @@ class QuicSocket : public AsyncWrap,
531539

532540
SendWrap* last_created_send_wrap_ = nullptr;
533541

534-
class StatsDebug {
535-
public:
536-
StatsDebug(QuicSocket* socket) : socket_(socket) {}
537-
std::string ToString();
538-
private:
539-
QuicSocket* socket_;
540-
};
541-
542542
friend class QuicSocketListener;
543543
};
544544

src/quic/node_quic_stream-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void QuicStream::Unschedule() {
148148
stream_queue_.Remove();
149149
}
150150

151-
std::string QuicHeader::ToString() {
151+
std::string QuicHeader::ToString() const {
152152
return name() + " = " + value();
153153
}
154154

src/quic/node_quic_stream.cc

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,13 @@ QuicStream::QuicStream(
5555
IncrementStat(&QuicStreamStats::max_offset, params.initial_max_data);
5656
}
5757

58-
QuicStream::~QuicStream() {
59-
StatsDebug stats_debug(this);
60-
Debug(this, "Destroyed. %s", stats_debug.ToString().c_str());
61-
}
62-
63-
std::string QuicStream::StatsDebug::ToString() {
64-
#define V(_, name, label) \
65-
" "## label + ": " + \
66-
std::to_string(stream_->GetStat(&QuicStreamStats::name)) + "\n"
67-
68-
std::string out = "Statistics:\n";
69-
out += " Duration: " +
70-
std::to_string(uv_hrtime() -
71-
stream_->GetStat(&QuicStreamStats::created_at)) + "\n" +
72-
STREAM_STATS(V);
73-
return out;
58+
QuicStream::~QuicStream() {}
7459

60+
template <typename Fn>
61+
void QuicStreamStatsTraits::ToString(const QuicStream& ptr, Fn&& add_field) {
62+
#define V(_n, name, label) \
63+
add_field(label, ptr.GetStat(&QuicStreamStats::name));
64+
STREAM_STATS(V)
7565
#undef V
7666
}
7767

src/quic/node_quic_stream.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace node {
1818
namespace quic {
1919

2020
class QuicSession;
21+
class QuicStream;
2122
class QuicApplication;
2223

2324
enum QuicStreamHeaderFlags : uint32_t {
@@ -65,15 +66,22 @@ struct QuicStreamStats {
6566
};
6667
#undef V
6768

69+
struct QuicStreamStatsTraits {
70+
using Stats = QuicStreamStats;
71+
using Base = QuicStream;
72+
73+
template <typename Fn>
74+
static void ToString(const QuicStream& ptr, Fn&& add_field);
75+
};
76+
6877
// QuicHeader is a base class for implementing QUIC application
6978
// specific headers. Each type of QUIC application may have
7079
// different internal representations for a header name+value
7180
// pair. QuicApplication implementations that support headers
7281
// per stream must create a specialization of the Header class.
7382
class QuicHeader : public MemoryRetainer {
7483
public:
75-
QuicHeader() {}
76-
84+
QuicHeader() = default;
7785
virtual ~QuicHeader() {}
7886
virtual v8::MaybeLocal<v8::String> GetName(QuicApplication* app) const = 0;
7987
virtual v8::MaybeLocal<v8::String> GetValue(QuicApplication* app) const = 0;
@@ -84,7 +92,7 @@ class QuicHeader : public MemoryRetainer {
8492
// (including the name and value)
8593
virtual size_t length() const = 0;
8694

87-
inline std::string ToString();
95+
inline std::string ToString() const;
8896
};
8997

9098
enum QuicStreamStates : uint32_t {
@@ -198,7 +206,7 @@ enum QuicStreamOrigin {
198206
class QuicStream : public AsyncWrap,
199207
public bob::SourceImpl<ngtcp2_vec>,
200208
public StreamBase,
201-
public StatsBase<QuicStreamStats> {
209+
public StatsBase<QuicStreamStatsTraits> {
202210
public:
203211
static void Initialize(
204212
Environment* env,
@@ -380,15 +388,6 @@ class QuicStream : public AsyncWrap,
380388
size_t current_headers_length_ = 0;
381389

382390
ListNode<QuicStream> stream_queue_;
383-
384-
class StatsDebug {
385-
public:
386-
StatsDebug(QuicStream* stream) : stream_(stream) {}
387-
std::string ToString();
388-
private:
389-
QuicStream* stream_;
390-
};
391-
392391
public:
393392
// Linked List of QuicStream objects
394393
using Queue = ListHead<QuicStream, &QuicStream::stream_queue_>;

src/quic/node_quic_util-inl.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef SRC_QUIC_NODE_QUIC_UTIL_INL_H_
22
#define SRC_QUIC_NODE_QUIC_UTIL_INL_H_
33

4+
#include "debug_utils.h"
45
#include "node_internals.h"
6+
#include "node_quic_crypto.h"
57
#include "node_quic_util.h"
68
#include "memory_tracker-inl.h"
79
#include "env-inl.h"
@@ -324,7 +326,7 @@ StatsBase<T>::StatsBase(
324326
int options)
325327
: stats_buffer_(
326328
env->isolate(),
327-
sizeof(T) / sizeof(uint64_t),
329+
sizeof(typename T::Stats) / sizeof(uint64_t),
328330
reinterpret_cast<uint64_t*>(&stats_)) {
329331
static constexpr uint64_t kMax = std::numeric_limits<int64_t>::max();
330332
stats_.created_at = uv_hrtime();
@@ -368,30 +370,29 @@ StatsBase<T>::StatsBase(
368370
}
369371

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

376378
template <typename T>
377-
void StatsBase<T>::SetStat(uint64_t T::*member, uint64_t value) {
379+
void StatsBase<T>::SetStat(uint64_t T::Stats::*member, uint64_t value) {
378380
stats_.*member = value;
379381
}
380382

381383
template <typename T>
382-
void StatsBase<T>::RecordTimestamp(uint64_t T::*member) {
384+
void StatsBase<T>::RecordTimestamp(uint64_t T::Stats::*member) {
383385
stats_.*member = uv_hrtime();
384386
}
385387

386388
template <typename T>
387-
uint64_t StatsBase<T>::GetStat(uint64_t T::*member) const {
389+
uint64_t StatsBase<T>::GetStat(uint64_t T::Stats::*member) const {
388390
return stats_.*member;
389391
}
390392

391393
template <typename T>
392-
inline void StatsBase<T>::RecordRate(uint64_t T::*member) {
394+
inline void StatsBase<T>::RecordRate(uint64_t T::Stats::*member) {
393395
CHECK(rate_);
394-
395396
uint64_t received_at = GetStat(member);
396397
uint64_t now = uv_hrtime();
397398
if (received_at > 0)
@@ -406,7 +407,7 @@ inline void StatsBase<T>::RecordSize(uint64_t val) {
406407
}
407408

408409
template <typename T>
409-
inline void StatsBase<T>::RecordAck(uint64_t T::*member) {
410+
inline void StatsBase<T>::RecordAck(uint64_t T::Stats::*member) {
410411
CHECK(ack_);
411412
uint64_t acked_at = GetStat(member);
412413
uint64_t now = uv_hrtime();
@@ -423,6 +424,29 @@ void StatsBase<T>::StatsMemoryInfo(MemoryTracker* tracker) const {
423424
tracker->TrackField("ack_histogram", ack_);
424425
}
425426

427+
template <typename T>
428+
StatsBase<T>::~StatsBase() {
429+
StatsBase<T>::StatsDebug stats_debug(static_cast<typename T::Base*>(this));
430+
Debug(static_cast<typename T::Base*>(this),
431+
"Destroyed. %s",
432+
stats_debug.ToString().c_str());
433+
}
434+
435+
template <typename T>
436+
std::string StatsBase<T>::StatsDebug::ToString() const {
437+
std::string out = "Statistics:\n";
438+
auto add_field = [&out](const char* name, uint64_t val) {
439+
out += " ";
440+
out += std::string(name);
441+
out += ": ";
442+
out += std::to_string(val);
443+
out += "\n";
444+
};
445+
add_field("Duration", uv_hrtime() - ptr->GetStat(&T::Stats::created_at));
446+
T::ToString(*ptr, add_field);
447+
return out;
448+
}
449+
426450
template <typename T>
427451
size_t get_length(const T* vec, size_t count) {
428452
CHECK_NOT_NULL(vec);

0 commit comments

Comments
 (0)