Skip to content

Commit

Permalink
Minor improvements and cleaned up styling to keep code consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
bigzachattack authored and dtrugman committed Sep 19, 2022
1 parent b4a6aed commit f68a2fa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
1 change: 0 additions & 1 deletion include/pfs/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class net final
net(const std::string& procfs_root);

private:
std::vector<net_device> get_net_devices(const std::string& file) const;
std::vector<net_socket> get_net_sockets(const std::string& file) const;

static std::string build_net_root(const std::string& procfs_root);
Expand Down
6 changes: 3 additions & 3 deletions include/pfs/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,18 @@ struct ip

struct net_device
{
std::string name;
std::string interface;
uint64_t rx_bytes;
uint64_t rx_packets;
uint64_t rx_errors;
uint64_t rx_errs;
uint64_t rx_drop;
uint64_t rx_fifo;
uint64_t rx_frame;
uint64_t rx_compressed;
uint64_t rx_multicast;
uint64_t tx_bytes;
uint64_t tx_packets;
uint64_t tx_errors;
uint64_t tx_errs;
uint64_t tx_drop;
uint64_t tx_fifo;
uint64_t tx_colls;
Expand Down
9 changes: 6 additions & 3 deletions sample/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,26 @@ inline std::ostream& operator<<(std::ostream& out,
inline std::ostream& operator<<(std::ostream& out,
const pfs::net_device& device)
{
out << "name[" << device.name << "] ";
out << "interface[" << device.interface << "] ";

out << "rx_bytes[" << device.rx_bytes << "] ";
out << "rx_packets[" << device.rx_packets << "] ";
out << "rx_errors[" << device.rx_errors << "] ";
out << "rx_errs[" << device.rx_errs << "] ";
out << "rx_drop[" << device.rx_drop << "] ";
out << "rx_fifo[" << device.rx_fifo << "] ";
out << "rx_frame[" << device.rx_frame << "] ";
out << "rx_compressed[" << device.rx_compressed << "] ";
out << "rx_multicast[" << device.rx_multicast << "] ";

out << "tx_bytes[" << device.tx_bytes << "] ";
out << "tx_packets[" << device.tx_packets << "] ";
out << "tx_errors[" << device.tx_errors << "] ";
out << "tx_errs[" << device.tx_errs << "] ";
out << "tx_drop[" << device.tx_drop << "] ";
out << "tx_fifo[" << device.tx_fifo << "] ";
out << "tx_colls[" << device.tx_colls << "] ";
out << "tx_carrier[" << device.tx_carrier << "] ";
out << "tx_compressed[" << device.tx_compressed << "] ";

return out;
}

Expand Down
19 changes: 8 additions & 11 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ std::string net::build_net_root(const std::string& procfs_root)
std::vector<net_device> net::get_dev() const
{
static const std::string DEV_FILE("dev");
return get_net_devices(DEV_FILE);
auto path = _net_root + DEV_FILE;

static const size_t HEADER_LINES = 2;

std::vector<net_device> output;
parsers::parse_lines(path, std::back_inserter(output),
parsers::parse_net_device_line, HEADER_LINES);
return output;
}

std::vector<net_socket> net::get_icmp() const
Expand Down Expand Up @@ -135,14 +142,4 @@ std::vector<net_socket> net::get_net_sockets(const std::string& file) const
return output;
}

std::vector<net_device> net::get_net_devices(const std::string& file) const
{
auto path = _net_root + file;

static const size_t HEADER_LINES = 2;
std::vector<net_device> output;
parsers::parse_lines(path, std::back_inserter(output), parsers::parse_net_device_line, HEADER_LINES);
return output;
}

} // namespace pfs
29 changes: 16 additions & 13 deletions src/parsers/net_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@ net_device parse_net_device_line(const std::string& line)
{
// Example:
// clang-format off
// Inter-| Receive | Transmit
// face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
// lo: 27267010 48045 0 0 0 0 0 0 27267010 48045 0 0 0 0 0 0
// eth0: 335754274 58179 0 0 0 0 0 0 9805218 48519 0 0 0 0 0 0
// Inter-| Receive |Transmit
// face | bytes packets errs drop fifo frame compressed multicast| bytes packets errs drop fifo colls carrier compressed
// lo: 93144 1366 0 0 0 0 0 0 93144 1366 0 0 0 0 0 0
// eth0: 2893258 9219 0 0 0 0 0 556 1029533 7276 0 0 0 0 0 0
// clang-format on

enum token {
NAME = 0,
enum token
{
INTERFACE = 0,
RX_BYTES = 1,
RX_PACKETS = 2,
RX_ERRORS = 3,
RX_ERRS = 3,
RX_DROP = 4,
RX_FIFO = 5,
RX_FRAME = 6,
RX_COMPRESSED = 7,
RX_MULTICAST = 8,
TX_BYTES = 9,
TX_PACKETS = 10,
TX_ERRORS = 11,
TX_ERRS = 11,
TX_DROP = 12,
TX_FIFO = 13,
TX_COLLS = 14,
Expand All @@ -59,23 +60,25 @@ net_device parse_net_device_line(const std::string& line)
line);
}

try {
try
{
net_device dev;

dev.name = tokens[NAME];
dev.name.pop_back(); // Remove ':';
dev.interface = tokens[INTERFACE];
dev.interface.pop_back(); // Remove ':';

utils::stot(tokens[RX_BYTES], dev.rx_bytes, utils::base::decimal);
utils::stot(tokens[RX_PACKETS], dev.rx_packets, utils::base::decimal);
utils::stot(tokens[RX_ERRORS], dev.rx_errors, utils::base::decimal);
utils::stot(tokens[RX_ERRS], dev.rx_errs, utils::base::decimal);
utils::stot(tokens[RX_DROP], dev.rx_drop, utils::base::decimal);
utils::stot(tokens[RX_FIFO], dev.rx_fifo, utils::base::decimal);
utils::stot(tokens[RX_FRAME], dev.rx_frame, utils::base::decimal);
utils::stot(tokens[RX_COMPRESSED], dev.rx_compressed, utils::base::decimal);
utils::stot(tokens[RX_MULTICAST], dev.rx_multicast, utils::base::decimal);

utils::stot(tokens[TX_BYTES], dev.tx_bytes, utils::base::decimal);
utils::stot(tokens[TX_PACKETS], dev.tx_packets, utils::base::decimal);
utils::stot(tokens[TX_ERRORS], dev.tx_errors, utils::base::decimal);
utils::stot(tokens[TX_ERRS], dev.tx_errs, utils::base::decimal);
utils::stot(tokens[TX_DROP], dev.tx_drop, utils::base::decimal);
utils::stot(tokens[TX_FIFO], dev.tx_fifo, utils::base::decimal);
utils::stot(tokens[TX_COLLS], dev.tx_colls, utils::base::decimal);
Expand Down
26 changes: 13 additions & 13 deletions test/net_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ using namespace pfs::impl::parsers;

TEST_CASE("Parse corrupted net device", "[net][net_device]")
{

SECTION("Missing token")
{
std::string line =
std::string line =
"lo: 27267010 48045 0 0 0 0 0 0 "
"27267010 48045 0 0 0 0 0";

Expand All @@ -21,36 +20,36 @@ TEST_CASE("Parse corrupted net device", "[net][net_device]")

SECTION("Extra token")
{
std::string line =
std::string line =
"lo: 27267010 48045 0 0 0 0 0 0 "
"27267010 48045 0 0 0 0 0 0 0";

REQUIRE_THROWS_AS(parse_net_device_line(line), pfs::parser_error);
}

}

TEST_CASE("Parse net device", "[net][net_device]")
{

pfs::net_device expected;
pfs::net_device expected;

std::string line =
"eth0: 335754274 58179 1 2 3 4 5 "
"6 9805218 48519 11 12 13 14 15 16";

expected.name = "eth0";
expected.interface = "eth0";

expected.rx_bytes = 335754274;
expected.rx_packets = 58179;
expected.rx_errors = 1;
expected.rx_errs = 1;
expected.rx_drop = 2;
expected.rx_fifo = 3;
expected.rx_frame = 4;
expected.rx_compressed = 5;
expected.rx_multicast = 6;

expected.tx_bytes = 9805218;
expected.tx_packets = 48519;
expected.tx_errors = 11;
expected.tx_errs = 11;
expected.tx_drop = 12;
expected.tx_fifo = 13;
expected.tx_colls = 14;
Expand All @@ -59,21 +58,22 @@ TEST_CASE("Parse net device", "[net][net_device]")

auto device = parse_net_device_line(line);

REQUIRE(device.name == expected.name);
REQUIRE(device.interface == expected.interface);

REQUIRE(device.rx_bytes == expected.rx_bytes);
REQUIRE(device.rx_packets == expected.rx_packets);
REQUIRE(device.rx_errors == expected.rx_errors);
REQUIRE(device.rx_errs == expected.rx_errs);
REQUIRE(device.rx_drop == expected.rx_drop);
REQUIRE(device.rx_fifo == expected.rx_fifo);
REQUIRE(device.rx_compressed == expected.rx_compressed);
REQUIRE(device.rx_multicast == expected.rx_multicast);

REQUIRE(device.tx_bytes == expected.tx_bytes);
REQUIRE(device.tx_packets == expected.tx_packets);
REQUIRE(device.tx_errors == expected.tx_errors);
REQUIRE(device.tx_errs == expected.tx_errs);
REQUIRE(device.tx_drop == expected.tx_drop);
REQUIRE(device.tx_fifo == expected.tx_fifo);
REQUIRE(device.tx_colls == expected.tx_colls);
REQUIRE(device.tx_carrier == expected.tx_carrier);
REQUIRE(device.tx_compressed == expected.tx_compressed);

}

0 comments on commit f68a2fa

Please sign in to comment.