From 133a2d1565cd37feab54e7e8b5a3b51b6256a038 Mon Sep 17 00:00:00 2001 From: Shinae Woo Date: Wed, 25 Jul 2018 14:23:38 -0700 Subject: [PATCH] Tread-safe track having per-worker statistics --- core/gate_hooks/track.cc | 17 ++++++++--------- core/gate_hooks/track.h | 34 ++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/core/gate_hooks/track.cc b/core/gate_hooks/track.cc index a8bd164e4..430fa2fb5 100644 --- a/core/gate_hooks/track.cc +++ b/core/gate_hooks/track.cc @@ -44,9 +44,7 @@ const GateHookCommands Track::cmds = {{"reset", "EmptyArg", Track::Track() : bess::GateHook(Track::kName, Track::kPriority), track_bytes_(), - cnt_(), - pkts_(), - bytes_() {} + worker_stats_() {} CommandResponse Track::Init(const bess::Gate *, const bess::pb::TrackArg &arg) { track_bytes_ = arg.bits(); @@ -54,23 +52,24 @@ CommandResponse Track::Init(const bess::Gate *, const bess::pb::TrackArg &arg) { } CommandResponse Track::CommandReset(const bess::pb::EmptyArg &) { - cnt_ = 0; - pkts_ = 0; - bytes_ = 0; + worker_stats_ = {}; return CommandSuccess(); } void Track::ProcessBatch(const bess::PacketBatch *batch) { + + TrackStats *stat = &worker_stats_[current_worker.wid()]; + size_t cnt = batch->cnt(); - cnt_ += 1; - pkts_ += cnt; + stat->cnt += 1; + stat->pkts += cnt; if (!track_bytes_) { return; } for (size_t i = 0; i < cnt; i++) { - bytes_ += batch->pkts()[i]->data_len() + kEthernetOverhead; + stat->bytes += batch->pkts()[i]->data_len() + kEthernetOverhead; } } diff --git a/core/gate_hooks/track.h b/core/gate_hooks/track.h index 16bab903e..5c772ed2d 100644 --- a/core/gate_hooks/track.h +++ b/core/gate_hooks/track.h @@ -43,11 +43,29 @@ class Track final : public bess::GateHook { CommandResponse Init(const bess::Gate *, const bess::pb::TrackArg &); - uint64_t cnt() const { return cnt_; } + uint64_t cnt() const { + uint64_t cnt = 0; + for (int i = 0; i < Worker::kMaxWorkers; i++) { + cnt += worker_stats_[i].cnt; + } + return cnt; + } - uint64_t pkts() const { return pkts_; } + uint64_t pkts() const { + uint64_t pkts = 0; + for (int i = 0; i < Worker::kMaxWorkers; i++) { + pkts += worker_stats_[i].pkts; + } + return pkts; + } - uint64_t bytes() const { return bytes_; } + uint64_t bytes() const { + uint64_t bytes = 0; + for (int i = 0; i < Worker::kMaxWorkers; i++) { + bytes += worker_stats_[i].bytes; + } + return bytes; + } void set_track_bytes(bool track) { track_bytes_ = track; } @@ -60,9 +78,13 @@ class Track final : public bess::GateHook { private: bool track_bytes_; - uint64_t cnt_; - uint64_t pkts_; - uint64_t bytes_; + struct alignas(64) TrackStats { + uint64_t cnt; + uint64_t pkts; + uint64_t bytes; + }; + + std::array worker_stats_; }; #endif // BESS_GATE_HOOKS_TRACK_