param: network
This param shows the throughput in kb/s for each interface or for user specified interfacespull/1290/head
parent
66d5ce43d2
commit
a4862f5461
@ -0,0 +1,58 @@
|
||||
#include "net.h"
|
||||
#include "hud_elements.h"
|
||||
|
||||
Net::Net() {
|
||||
should_reset = false;
|
||||
fs::path net_dir(NETDIR);
|
||||
if (fs::exists(net_dir) && fs::is_directory(net_dir)) {
|
||||
for (const auto& entry : fs::directory_iterator(net_dir)) {
|
||||
if (fs::is_directory(entry.status())) {
|
||||
auto val = entry.path().filename().string();
|
||||
if (val == "lo")
|
||||
continue;
|
||||
|
||||
if (!HUDElements.params->network.empty() && HUDElements.params->network.front() == "1") {
|
||||
interfaces.push_back({entry.path().filename().string(), 0, 0});
|
||||
} else if (!HUDElements.params->network.empty()){
|
||||
auto it = std::find(HUDElements.params->network.begin(), HUDElements.params->network.end(), val);
|
||||
if (it != HUDElements.params->network.end())
|
||||
interfaces.push_back({entry.path().filename().string(), 0, 0});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (interfaces.empty())
|
||||
SPDLOG_ERROR("Network: couldn't find any interfaces");
|
||||
}
|
||||
|
||||
void Net::update() {
|
||||
if (!interfaces.empty()) {
|
||||
for (auto& iface : interfaces) {
|
||||
// path to tx_bytes and rx_bytes
|
||||
std::string txfile = (NETDIR + iface.name + TXFILE);
|
||||
std::string rxfile = (NETDIR + iface.name + RXFILE);
|
||||
|
||||
// amount of bytes at previous update
|
||||
uint64_t prevTx = iface.txBytes;
|
||||
uint64_t prevRx = iface.rxBytes;
|
||||
|
||||
// current amount of bytes
|
||||
iface.txBytes = std::stoll(read_line(txfile));
|
||||
iface.rxBytes = std::stoll(read_line(rxfile));
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
// calculate the bytes per second since last update
|
||||
iface.txBps = calculateThroughput(iface.txBytes, prevTx, iface.previousTime, now);
|
||||
iface.rxBps = calculateThroughput(iface.rxBytes, prevRx, iface.previousTime, now);
|
||||
iface.previousTime = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t Net::calculateThroughput(long long currentBytes, long long previousBytes,
|
||||
std::chrono::steady_clock::time_point previousTime,
|
||||
std::chrono::steady_clock::time_point currentTime) {
|
||||
std::chrono::duration<double> elapsed = (currentTime - previousTime);
|
||||
return static_cast<long long>((currentBytes - previousBytes) / elapsed.count());
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "filesystem.h"
|
||||
#include "file_utils.h"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace fs = ghc::filesystem;
|
||||
|
||||
#ifndef NETDIR
|
||||
#define NETDIR "/sys/class/net/"
|
||||
#endif
|
||||
|
||||
#ifndef TXFILE
|
||||
#define TXFILE "/statistics/tx_bytes"
|
||||
#endif
|
||||
|
||||
#ifndef RXFILE
|
||||
#define RXFILE "/statistics/rx_bytes"
|
||||
#endif
|
||||
|
||||
class Net {
|
||||
public:
|
||||
bool should_reset = false;
|
||||
struct interface {
|
||||
std::string name;
|
||||
uint64_t txBytes;
|
||||
uint64_t rxBytes;
|
||||
uint64_t txBps;
|
||||
uint64_t rxBps;
|
||||
std::chrono::steady_clock::time_point previousTime;
|
||||
};
|
||||
|
||||
Net();
|
||||
void update();
|
||||
std::vector<interface> interfaces = {};
|
||||
|
||||
private:
|
||||
uint64_t calculateThroughput(long long currentBytes, long long previousBytes,
|
||||
std::chrono::steady_clock::time_point previousTime,
|
||||
std::chrono::steady_clock::time_point currentTime);
|
||||
};
|
||||
|
||||
extern std::unique_ptr<Net> net;
|
Loading…
Reference in New Issue