Commit fb37f137 authored by Alexandre Bailon's avatar Alexandre Bailon Committed by Greg Kroah-Hartman

greybus: loopback: Fix averaging

Currently, we are adding 0.5 to the average to round the average.
But we are using the remainder to calculate the decimal, so we do not
need to round the average.
In addition, use a u64 type for the remainder to avoid overflow
that might happen when stats->sum value is too big,
usually for requests per seconds and the throughput.
Signed-off-by: default avatarAlexandre Bailon <abailon@baylibre.com>
Reviewed-by: default avatarJohan Hovold <johan@hovoldconsulting.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@google.com>
parent ab81bb9c
......@@ -157,14 +157,15 @@ static ssize_t name##_avg_show(struct device *dev, \
{ \
struct gb_loopback_stats *stats; \
struct gb_loopback *gb; \
u64 avg; \
u32 count, rem; \
u64 avg, rem; \
u32 count; \
gb = dev_get_drvdata(dev); \
stats = &gb->name; \
count = stats->count ? stats->count : 1; \
avg = stats->sum + count / 2; /* round closest */ \
avg = stats->sum; \
rem = do_div(avg, count); \
return sprintf(buf, "%llu.%06u\n", avg, 1000000 * rem / count); \
rem = 1000000 * rem / count; \
return sprintf(buf, "%llu.%06u\n", avg, (u32)rem); \
} \
static DEVICE_ATTR_RO(name##_avg)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment