Skip to content
Closed
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
27 changes: 23 additions & 4 deletions fboss/agent/hw/test/HwPortFb303StatsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,14 @@ TEST(HwPortFb303StatsTest, ReInit) {
true /* inCongestionDiscardSeenSupported */);
stats.portNameChanged(kNewPortName);
for (const auto& sName : stats.kPortMonotonicCounterStatKeys()) {
EXPECT_TRUE(fbData->getStatMap()->contains(
HwPortFb303Stats::statName(sName, kNewPortName)));
EXPECT_FALSE(fbData->getStatMap()->contains(
HwPortFb303Stats::statName(sName, kPortName)));
const auto newStatName = HwPortFb303Stats::statName(sName, kNewPortName);
const auto oldStatName = HwPortFb303Stats::statName(sName, kPortName);
EXPECT_TRUE(fbData->getStatMap()->contains(newStatName));
EXPECT_FALSE(fbData->getStatMap()->contains(oldStatName));
EXPECT_TRUE(
fbData->getCounterIfExists(newStatName + ".rate.60").has_value());
EXPECT_FALSE(
fbData->getCounterIfExists(oldStatName + ".rate.60").has_value());
}
for (auto statKey : stats.kPortFb303CounterStatKeys()) {
EXPECT_FALSE(
Expand Down Expand Up @@ -832,6 +836,21 @@ TEST(HwPortFb303StatsTest, ReInit) {
// Hence, we don't check for their existence in this test.
}

TEST(HwPortFb303StatsTest, MonotonicCountersExportRate) {
const std::string portName = "eth1/1/7";
const auto statName = HwPortFb303Stats::statName(kInBytes(), portName);
const auto rateName = statName + ".rate.60";

{
HwPortFb303Stats stats(portName);
facebook::tcData().publishStats();
EXPECT_TRUE(fbData->getCounterIfExists(rateName).has_value());
}

facebook::tcData().publishStats();
EXPECT_FALSE(fbData->getCounterIfExists(rateName).has_value());
}

TEST(HwPortFb303Stats, UpdateStats) {
// Initialize with PFC priorities to ensure priority group counters are
// created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@
#include <fmt/color.h>
#include <re2/re2.h>

#include <stdexcept>

namespace {

constexpr auto kTrafficCounterRegex =
".*\\.(in_bytes|in_unicast_pkts|in_multicast_pkts|in_broadcast_pkts|"
"out_bytes|out_unicast_pkts|out_multicast_pkts|out_broadcast_pkts)"
"\\.rate\\.60$";

int64_t getTrafficCounter(
const std::map<std::string, int64_t>& counters,
const std::string& portName,
const std::string& suffix) {
const auto counterName = portName + suffix;
const auto counter = counters.find(counterName);
if (counter == counters.end()) {
throw std::runtime_error(
"Required HW-agent traffic counter is unavailable: " + counterName);
}
return counter->second;
}

} // namespace

namespace facebook::fboss {

CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient(
Expand All @@ -25,11 +49,8 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient(
auto client =
utils::createClient<apache::thrift::Client<FbossCtrl>>(hostInfo);

folly::IOThreadPoolExecutor executor(2);

// Gather port stats asynchrounously
auto portInfos =
client->semifuture_getAllPortInfo().via(executor.getEventBase());
std::map<int32_t, facebook::fboss::PortInfoThrift> portInfos;
client->sync_getAllPortInfo(portInfos);

std::map<std::string, int64_t> counters;
if (utils::isMultiSwitchEnabled(hostInfo)) {
Expand All @@ -40,34 +61,31 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient(
#ifndef IS_OSS
apache::thrift::Client<facebook::thrift::Monitor> monitoringClient{
client.getChannelShared()};
monitoringClient.sync_getCounters(hwAgentCounters);
monitoringClient.sync_getRegexCounters(
hwAgentCounters, kTrafficCounterRegex);
#else
// FbossHwCtrl does not extend FacebookService; the OSS HwAgent
// multiplex serves fb303 methods via a FacebookBase2 handler, so
// reuse the channel with an FbossCtrl client (which carries
// getCounters) to fetch HwAgent counters.
apache::thrift::Client<facebook::fboss::FbossCtrl> fb303Client{
client.getChannelShared()};
fb303Client.sync_getCounters(hwAgentCounters);
fb303Client.sync_getRegexCounters(
hwAgentCounters, kTrafficCounterRegex);
#endif
counters.merge(hwAgentCounters);
};
utils::runOnAllHwAgents(hostInfo, hwAgentQueryFn);
} else {
auto entries =
client->semifuture_getCounters().via(executor.getEventBase());
entries.wait();
counters = entries.value();
client->sync_getRegexCounters(counters, kTrafficCounterRegex);
}

portInfos.wait();

return createModel(portInfos.value(), counters, queriedIfs);
return createModel(portInfos, counters, queriedIfs);
}

CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel(
const std::map<int32_t, facebook::fboss::PortInfoThrift>& portCounters,
std::map<std::string, int64_t> intCounters,
const std::map<std::string, int64_t>& intCounters,
const std::vector<std::string>& queriedIfs) {
RetType ret;

Expand Down Expand Up @@ -107,48 +125,56 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel(
errorCounters.giantErrors() = 0;
errorCounters.txErrors() = outputErrors;

if (isNonZeroErrors(errorCounters)) {
ret.error_counters()->push_back(errorCounters);
}

// Down ports cannot have current traffic and may not have HwAgent rate
// counters. Do not turn an unsupported counter into a false zero.
if (operState != facebook::fboss::PortOperState::UP) {
continue;
}

cli::TrafficCounters trafficCounters;
// Getting various counters and converting to Mbps
int64_t portSpeed = folly::copy(portInfo.speedMbps().value());

long inSpeedBps = intCounters[pname + ".in_bytes.rate.60"] * 8;

double inSpeedMbps = (double)inSpeedBps / 1000000;
const double inSpeedMbps =
getTrafficCounter(intCounters, pname, ".in_bytes.rate.60") * 8.0 /
1000000.0;

// Unicast + multicast + broadcast = PPS
int64_t inPPS = intCounters[pname + ".in_unicast_pkts.rate.60"] +
intCounters[pname + ".in_multicast_pkts.rate.60"] +
intCounters[pname + ".in_broadcast_pkts.rate.60"];
const int64_t inPPS =
getTrafficCounter(intCounters, pname, ".in_unicast_pkts.rate.60") +
getTrafficCounter(intCounters, pname, ".in_multicast_pkts.rate.60") +
getTrafficCounter(intCounters, pname, ".in_broadcast_pkts.rate.60");

long outSpeedBps = intCounters[pname + ".out_bytes.rate.60"] * 8;

double outSpeedMbps = (double)outSpeedBps / 1000000;
const double outSpeedMbps =
getTrafficCounter(intCounters, pname, ".out_bytes.rate.60") * 8.0 /
1000000.0;

// Unicast + multicast + broadcast = PPS
int64_t outPPS = intCounters[pname + ".out_unicast_pkts.rate.60"] +
intCounters[pname + ".out_multicast_pkts.rate.60"] +
intCounters[pname + ".out_broadcast_pkts.rate.60"];
const int64_t outPPS =
getTrafficCounter(intCounters, pname, ".out_unicast_pkts.rate.60") +
getTrafficCounter(intCounters, pname, ".out_multicast_pkts.rate.60") +
getTrafficCounter(intCounters, pname, ".out_broadcast_pkts.rate.60");

trafficCounters.interfaceName() = pname;
trafficCounters.peerIf() =
extractExpectedPort(portInfo.description().value());
trafficCounters.inMbps() = inSpeedMbps;
trafficCounters.inPct() =
calculateUtilizationPercent(inSpeedMbps, portSpeed);
trafficCounters.inKpps() = inPPS / 1000;
trafficCounters.inKpps() = static_cast<double>(inPPS) / 1000.0;
trafficCounters.outMbps() = outSpeedMbps;
trafficCounters.outPct() =
calculateUtilizationPercent(outSpeedMbps, portSpeed);
trafficCounters.outKpps() = outPPS / 1000;
trafficCounters.outKpps() = static_cast<double>(outPPS) / 1000.0;
trafficCounters.portSpeed() = portSpeed;

// The original in fb_toolkit has an option for "show zero". Since we
// can't accept arbitrarily deep parameters right now we will default to
// show non-zero and revisit as an option down the road
if (isNonZeroErrors(errorCounters)) {
ret.error_counters()->push_back(errorCounters);
}

if (isInterestingTraffic(trafficCounters)) {
ret.traffic_counters()->push_back(trafficCounters);
}
Expand All @@ -173,6 +199,9 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel(
double CmdShowInterfaceTraffic::calculateUtilizationPercent(
double speedMbps,
int bandwidth) {
if (bandwidth <= 0) {
return 0.0;
}
return (speedMbps / bandwidth) * 100;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "fboss/cli/fboss2/commands/show/interface/traffic/gen-cpp2/model_types.h"
#include "fboss/cli/fboss2/utils/CmdUtils.h"
#include "fboss/cli/fboss2/utils/Table.h"
#include "folly/executors/IOThreadPoolExecutor.h"

namespace facebook::fboss {

Expand Down Expand Up @@ -53,7 +52,7 @@ class CmdShowInterfaceTraffic : public CmdHandler<
const std::vector<std::string>& queriedIfs);
RetType createModel(
const std::map<int32_t, facebook::fboss::PortInfoThrift>& portCounters,
std::map<std::string, int64_t> intCounters,
const std::map<std::string, int64_t>& intCounters,
const std::vector<std::string>& queriedIfs);
double calculateUtilizationPercent(double speedMbps, int bandwidth);
std::string extractExpectedPort(const std::string& portDescription);
Expand Down
69 changes: 69 additions & 0 deletions fboss/cli/fboss2/test/CmdShowInterfaceTrafficTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <cstdint>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

#include "fboss/agent/if/gen-cpp2/ctrl_types.h"
Expand Down Expand Up @@ -162,6 +164,69 @@ TEST_F(CmdShowInterfaceTrafficTestFixture, createModel) {
EXPECT_NEAR(trafficCounters[2].outPct().value(), 19.7531, 0.0001);
}

TEST_F(CmdShowInterfaceTrafficTestFixture, queryClient) {
setupMockedAgentServer();

EXPECT_CALL(getMockAgent(), getAllPortInfo(_))
.WillOnce(Invoke([&](auto& entries) { entries = portInfo; }));

MultiSwitchRunState runState;
runState.multiSwitchEnabled() = false;
EXPECT_CALL(getMockAgent(), getMultiSwitchRunState(_))
.WillOnce(Invoke([&](auto& response) { response = runState; }));

auto response = intCounters;
EXPECT_CALL(getMockAgent(), async_eb_getRegexCounters(_, _))
.WillOnce(Invoke([&response](auto callback, auto regex) {
EXPECT_THAT(*regex, HasSuffix("\\.rate\\.60$"));
callback->result(std::move(response));
}));

auto model = CmdShowInterfaceTraffic().queryClient(localhost(), queriedIfs);
EXPECT_EQ(model.traffic_counters()->size(), 3);
}

TEST_F(CmdShowInterfaceTrafficTestFixture, missingRateCounterFails) {
intCounters.erase("eth1/1/1.in_bytes.rate.60");

auto cmd = CmdShowInterfaceTraffic();
EXPECT_THAT(
[&]() { cmd.createModel(portInfo, intCounters, queriedIfs); },
ThrowsMessage<std::runtime_error>(HasSubstr(
"Required HW-agent traffic counter is unavailable: "
"eth1/1/1.in_bytes.rate.60")));
}

TEST_F(CmdShowInterfaceTrafficTestFixture, downPortDoesNotRequireRateCounters) {
portInfo.at(1).operState() = facebook::fboss::PortOperState::DOWN;
for (auto counter = intCounters.begin(); counter != intCounters.end();) {
if (counter->first.rfind("eth1/1/1.", 0) == 0) {
counter = intCounters.erase(counter);
} else {
++counter;
}
}

auto cmd = CmdShowInterfaceTraffic();
EXPECT_NO_THROW(cmd.createModel(portInfo, intCounters, queriedIfs));
}

TEST_F(CmdShowInterfaceTrafficTestFixture, preservesFractionalKpps) {
intCounters["eth1/1/1.in_unicast_pkts.rate.60"] = 100;
intCounters["eth1/1/1.in_multicast_pkts.rate.60"] = 200;
intCounters["eth1/1/1.in_broadcast_pkts.rate.60"] = 300;

auto cmd = CmdShowInterfaceTraffic();
auto model = cmd.createModel(portInfo, intCounters, queriedIfs);

EXPECT_DOUBLE_EQ(model.traffic_counters()->at(0).inKpps().value(), 0.6);
}

TEST_F(CmdShowInterfaceTrafficTestFixture, zeroBandwidthHasZeroUtilization) {
auto cmd = CmdShowInterfaceTraffic();
EXPECT_DOUBLE_EQ(cmd.calculateUtilizationPercent(100.0, 0), 0.0);
}

TEST_F(CmdShowInterfaceTrafficTestFixture, printOutput) {
auto cmd = CmdShowInterfaceTraffic();
auto model = cmd.createModel(portInfo, intCounters, queriedIfs);
Expand Down Expand Up @@ -234,6 +299,10 @@ TEST_F(CmdShowInterfaceTrafficTestFixture, createModelFreeFormDescription) {
// every rate counter, so isInterestingTraffic() filters out every row and the
// Total is computed over an empty set. Totals must read 0.00%.
TEST_F(CmdShowInterfaceTrafficTestFixture, printOutputNoInterestingTraffic) {
for (auto& [_, port] : portInfo) {
port.operState() = facebook::fboss::PortOperState::DOWN;
}

auto cmd = CmdShowInterfaceTraffic();
auto model = cmd.createModel(portInfo, {} /* intCounters */, queriedIfs);

Expand Down
Loading