Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-safe track gatehook having per-worker statistics #822

Merged
merged 2 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions core/gate_hooks/track.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,35 @@ const GateHookCommands Track::cmds = {{"reset", "EmptyArg",
Track::Track()
: bess::GateHook(Track::kName, Track::kPriority),
track_bytes_(),
cnt_(),
pkts_(),
bytes_() {}
worker_stats_() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My C++ knowledge is not enough to understand why this is initialized to 0, but I tested it and it works well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something like int a{};, which will be initialized to zero. If a type doesn't have anything but default constructor, a() or a{} will trigger value initialization (i.e., zeroed), while simply a incurs default initialization (i.e., doing nothing for int or TrackStats).


CommandResponse Track::Init(const bess::Gate *, const bess::pb::TrackArg &arg) {
track_bytes_ = arg.bits();
return CommandSuccess();
}

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;
}

uint64_t bytes = 0;
for (size_t i = 0; i < cnt; i++) {
bytes_ += batch->pkts()[i]->data_len() + kEthernetOverhead;
bytes += batch->pkts()[i]->data_len() + kEthernetOverhead;
}
stat->bytes += bytes;
}

ADD_GATE_HOOK(Track, "count the packets and batches")
34 changes: 28 additions & 6 deletions core/gate_hooks/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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<TrackStats, Worker::kMaxWorkers> worker_stats_;
};

#endif // BESS_GATE_HOOKS_TRACK_