On 2015-11-03 at 19:06:48 +0100, Vadim Kochan <[email protected]> wrote:
> Make rate calculation more carefully by
> checking previous & current bytes counter.
>
> Do calculation only if update time passed >= 1s.
>
> Signed-off-by: Vadim Kochan <[email protected]>
> ---
> flowtop.c | 22 +++++++++++++++++-----
> 1 file changed, 17 insertions(+), 5 deletions(-)
>
> diff --git a/flowtop.c b/flowtop.c
> index cf9ac6f..54a749a 100644
> --- a/flowtop.c
> +++ b/flowtop.c
> @@ -307,13 +307,25 @@ static void flow_entry_calc_rate(struct flow_entry *n,
> const struct nf_conntrack
> uint64_t pkts_dst = nfct_get_attr_u64(ct, ATTR_REPL_COUNTER_PACKETS);
> double sec = (double)time_after_us(&n->last_update) / USEC_PER_SEC;
>
> - if (sec <= 0)
> + if (sec < 1)
> return;
>
> - n->rate_bytes_src = (bytes_src - n->bytes_src) / sec;
> - n->rate_bytes_dst = (bytes_dst - n->bytes_dst) / sec;
> - n->rate_pkts_src = (pkts_src - n->pkts_src) / sec;
> - n->rate_pkts_dst = (pkts_dst - n->pkts_dst) / sec;
> + n->rate_bytes_src = 0;
> + n->rate_bytes_dst = 0;
> + n->rate_pkts_src = 0;
> + n->rate_pkts_dst = 0;
> + if (bytes_src > n->bytes_src)
> + n->rate_bytes_src = (bytes_src - n->bytes_src) / sec;
> +
> + if (bytes_dst > n->bytes_dst)
> + n->rate_bytes_dst = (bytes_dst - n->bytes_dst) / sec;
> +
> + if (pkts_dst > n->pkts_dst)
> + n->rate_pkts_src = (pkts_src - n->pkts_src) / sec;
> +
> + if (pkts_dst > n->pkts_dst)
> + n->rate_pkts_dst = (pkts_dst - n->pkts_dst) / sec;
How about a macro to assign these, e.g.:
#define CP_RATE(elem) \
do { n->rate_##elem() = (elem > (n->elem) ? (elem - n->elem) / sec :
0); } while (0)
(not actually tested, but something similar should work IMO)
--
You received this message because you are subscribed to the Google Groups
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.