1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

network: change type of BitRates= bus property

This commit is contained in:
Yu Watanabe 2019-06-19 22:18:54 +09:00
parent 9ff27e6413
commit 42a63431d3
2 changed files with 18 additions and 47 deletions

View File

@ -119,8 +119,8 @@ typedef struct LinkInfo {
struct rtnl_link_stats stats;
};
double tx_bitrate;
double rx_bitrate;
uint64_t tx_bitrate;
uint64_t rx_bitrate;
bool has_mac_address:1;
bool has_tx_queues:1;
@ -229,11 +229,11 @@ static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
r, "Failed to query link bit rates: %s", bus_error_message(&error, r));
}
r = sd_bus_message_enter_container(reply, 'v', "(dd)");
r = sd_bus_message_enter_container(reply, 'v', "(tt)");
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "(dd)", &link->tx_bitrate, &link->rx_bitrate);
r = sd_bus_message_read(reply, "(tt)", &link->tx_bitrate, &link->rx_bitrate);
if (r < 0)
return bus_log_parse_error(r);
@ -241,7 +241,7 @@ static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
if (r < 0)
return bus_log_parse_error(r);
link->has_bitrates = link->tx_bitrate >= 0 && link->rx_bitrate >= 0;
link->has_bitrates = true;
return 0;
}
@ -898,32 +898,6 @@ static int dump_statistics(Table *table, const LinkInfo *info) {
return 0;
}
static const struct {
double val;
const char *str;
} prefix_table[] = {
{ .val = 1e15, .str = "P" },
{ .val = 1e12, .str = "T" },
{ .val = 1e9, .str = "G" },
{ .val = 1e6, .str = "M" },
{ .val = 1e3, .str = "k" },
};
static void get_prefix(double val, double *ret_div, const char **ret_prefix) {
assert(ret_div);
assert(ret_prefix);
for (size_t i = 0; i < ELEMENTSOF(prefix_table); i++)
if (val > prefix_table[i].val) {
*ret_div = prefix_table[i].val;
*ret_prefix = prefix_table[i].str;
return;
}
*ret_div = 1;
*ret_prefix = NULL;
}
static int link_status_one(
sd_netlink *rtnl,
sd_hwdb *hwdb,
@ -1140,11 +1114,7 @@ static int link_status_one(
}
if (info->has_bitrates) {
const char *tx_prefix, *rx_prefix;
double tx_div, rx_div;
get_prefix(info->tx_bitrate, &tx_div, &tx_prefix);
get_prefix(info->rx_bitrate, &rx_div, &rx_prefix);
char tx[FORMAT_BYTES_MAX], rx[FORMAT_BYTES_MAX];
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
@ -1153,9 +1123,9 @@ static int link_status_one(
if (r < 0)
return r;
r = table_add_cell_stringf(table, NULL, "%.4g %sbps/%.4g %sbps",
info->tx_bitrate / tx_div, strempty(tx_prefix),
info->rx_bitrate / rx_div, strempty(rx_prefix));
r = table_add_cell_stringf(table, NULL, "%sbps/%sbps",
format_bytes_full(tx, sizeof tx, info->tx_bitrate, 0),
format_bytes_full(rx, sizeof rx, info->rx_bitrate, 0));
if (r < 0)
return r;
}

View File

@ -22,7 +22,8 @@ static int property_get_bit_rates(
Link *link = userdata;
Manager *manager;
double tx, rx, interval_sec;
double interval_sec;
uint64_t tx, rx;
assert(bus);
assert(reply);
@ -40,19 +41,19 @@ static int property_get_bit_rates(
return sd_bus_error_set(error, BUS_ERROR_SPEED_METER_INACTIVE, "Failed to measure bit-rates.");
assert(manager->speed_meter_usec_new > manager->speed_meter_usec_old);
interval_sec = (double) (manager->speed_meter_usec_new - manager->speed_meter_usec_old) / USEC_PER_SEC;
interval_sec = (manager->speed_meter_usec_new - manager->speed_meter_usec_old) / USEC_PER_SEC;
if (link->stats_new.tx_bytes > link->stats_old.tx_bytes)
tx = (link->stats_new.tx_bytes - link->stats_old.tx_bytes) / interval_sec;
tx = (uint64_t) ((link->stats_new.tx_bytes - link->stats_old.tx_bytes) / interval_sec);
else
tx = (UINT64_MAX - (link->stats_old.tx_bytes - link->stats_new.tx_bytes)) / interval_sec;
tx = (uint64_t) ((UINT64_MAX - (link->stats_old.tx_bytes - link->stats_new.tx_bytes)) / interval_sec);
if (link->stats_new.rx_bytes > link->stats_old.rx_bytes)
rx = (link->stats_new.rx_bytes - link->stats_old.rx_bytes) / interval_sec;
rx = (uint64_t) ((link->stats_new.rx_bytes - link->stats_old.rx_bytes) / interval_sec);
else
rx = (UINT64_MAX - (link->stats_old.rx_bytes - link->stats_new.rx_bytes)) / interval_sec;
rx = (uint64_t) ((UINT64_MAX - (link->stats_old.rx_bytes - link->stats_new.rx_bytes)) / interval_sec);
return sd_bus_message_append(reply, "(dd)", tx, rx);
return sd_bus_message_append(reply, "(tt)", tx, rx);
}
const sd_bus_vtable link_vtable[] = {
@ -60,7 +61,7 @@ const sd_bus_vtable link_vtable[] = {
SD_BUS_PROPERTY("OperationalState", "s", property_get_operational_state, offsetof(Link, operstate), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("AdministrativeState", "s", property_get_administrative_state, offsetof(Link, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("BitRates", "(dd)", property_get_bit_rates, 0, 0),
SD_BUS_PROPERTY("BitRates", "(tt)", property_get_bit_rates, 0, 0),
SD_BUS_VTABLE_END
};