From f68a2fa8ea36c783bdd760371411fcb495aa3150 Mon Sep 17 00:00:00 2001 From: "Zachary H. Jones" Date: Mon, 19 Sep 2022 13:56:23 -0400 Subject: [PATCH] Minor improvements and cleaned up styling to keep code consistent --- include/pfs/net.hpp | 1 - include/pfs/types.hpp | 6 +++--- sample/format.hpp | 9 ++++++--- src/net.cpp | 19 ++++++++----------- src/parsers/net_device.cpp | 29 ++++++++++++++++------------- test/net_device.cpp | 26 +++++++++++++------------- 6 files changed, 46 insertions(+), 44 deletions(-) diff --git a/include/pfs/net.hpp b/include/pfs/net.hpp index b7eb631..5fcad59 100644 --- a/include/pfs/net.hpp +++ b/include/pfs/net.hpp @@ -63,7 +63,6 @@ class net final net(const std::string& procfs_root); private: - std::vector get_net_devices(const std::string& file) const; std::vector get_net_sockets(const std::string& file) const; static std::string build_net_root(const std::string& procfs_root); diff --git a/include/pfs/types.hpp b/include/pfs/types.hpp index dd5a12b..f76a8b2 100644 --- a/include/pfs/types.hpp +++ b/include/pfs/types.hpp @@ -456,10 +456,10 @@ 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; @@ -467,7 +467,7 @@ struct net_device 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; diff --git a/sample/format.hpp b/sample/format.hpp index f65d5ce..9481354 100644 --- a/sample/format.hpp +++ b/sample/format.hpp @@ -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; } diff --git a/src/net.cpp b/src/net.cpp index ca87aee..aa9e8e1 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -34,7 +34,14 @@ std::string net::build_net_root(const std::string& procfs_root) std::vector 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 output; + parsers::parse_lines(path, std::back_inserter(output), + parsers::parse_net_device_line, HEADER_LINES); + return output; } std::vector net::get_icmp() const @@ -135,14 +142,4 @@ std::vector net::get_net_sockets(const std::string& file) const return output; } -std::vector net::get_net_devices(const std::string& file) const -{ - auto path = _net_root + file; - - static const size_t HEADER_LINES = 2; - std::vector output; - parsers::parse_lines(path, std::back_inserter(output), parsers::parse_net_device_line, HEADER_LINES); - return output; -} - } // namespace pfs diff --git a/src/parsers/net_device.cpp b/src/parsers/net_device.cpp index 1fd1bd2..0309d26 100644 --- a/src/parsers/net_device.cpp +++ b/src/parsers/net_device.cpp @@ -25,17 +25,18 @@ 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, @@ -43,7 +44,7 @@ net_device parse_net_device_line(const std::string& line) RX_MULTICAST = 8, TX_BYTES = 9, TX_PACKETS = 10, - TX_ERRORS = 11, + TX_ERRS = 11, TX_DROP = 12, TX_FIFO = 13, TX_COLLS = 14, @@ -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); diff --git a/test/net_device.cpp b/test/net_device.cpp index 17f2c86..5640b45 100644 --- a/test/net_device.cpp +++ b/test/net_device.cpp @@ -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"; @@ -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; @@ -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); - }