From: Sam Stenvall Date: Wed, 2 Sep 2015 09:31:57 +0000 (+0300) Subject: alternate approach X-Git-Tag: v4.2.1~2152 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23cc08388ea5f2bda49c3ebf0cca1a202c7f8a9c;p=thirdparty%2Ftvheadend.git alternate approach --- diff --git a/src/atomic.h b/src/atomic.h index d88d81215..525b225e8 100644 --- a/src/atomic.h +++ b/src/atomic.h @@ -40,6 +40,12 @@ atomic_exchange(volatile int *ptr, int new) return __sync_lock_test_and_set(ptr, new); } +static inline int +atomic_exchange_u64(volatile uint64_t *ptr, uint64_t new) +{ + return __sync_lock_test_and_set(ptr, new); +} + static inline uint64_t atomic_add_u64(volatile uint64_t *ptr, uint64_t incr) { diff --git a/src/subscriptions.c b/src/subscriptions.c index 04906484a..449036107 100644 --- a/src/subscriptions.c +++ b/src/subscriptions.c @@ -889,8 +889,8 @@ subscription_create_msg(th_subscription_t *s) else if(s->ths_dvrfile != NULL) htsmsg_add_str(m, "service", s->ths_dvrfile ?: ""); - htsmsg_add_u32(m, "in", s->ths_bytes_in_prev); - htsmsg_add_u32(m, "out", s->ths_bytes_out_prev); + htsmsg_add_u32(m, "in", s->ths_bytes_in_avg); + htsmsg_add_u32(m, "out", s->ths_bytes_out_avg); htsmsg_add_s64(m, "total_in", s->ths_total_bytes_in); htsmsg_add_s64(m, "total_out", s->ths_total_bytes_out); @@ -911,9 +911,16 @@ subscription_status_callback ( void *p ) subscription_status_callback, NULL, 1); LIST_FOREACH(s, &subscriptions, ths_global_link) { - /* Store the previous periods byte count */ - s->ths_bytes_in_prev = atomic_exchange(&s->ths_bytes_in, 0); - s->ths_bytes_out_prev = atomic_exchange(&s->ths_bytes_out, 0); + /* Store the difference between total bytes from the last round */ + uint64_t in_prev = s->ths_total_bytes_in_prev; + uint64_t in_curr = s->ths_total_bytes_in; + uint64_t out_prev = s->ths_total_bytes_out_prev; + uint64_t out_curr = s->ths_total_bytes_out; + + atomic_exchange(&s->ths_bytes_in_avg, (in_curr - in_prev)); + atomic_exchange_u64(&s->ths_total_bytes_in_prev, s->ths_total_bytes_in); + atomic_exchange(&s->ths_bytes_out_avg, (out_curr - out_prev)); + atomic_exchange_u64(&s->ths_total_bytes_out_prev, s->ths_total_bytes_out); htsmsg_t *m = subscription_create_msg(s); htsmsg_add_u32(m, "updateEntry", 1); @@ -958,7 +965,6 @@ subscription_done(void) */ void subscription_add_bytes_in(th_subscription_t *s, size_t in) { - atomic_add(&s->ths_bytes_in, in); atomic_add_u64(&s->ths_total_bytes_in, in); } @@ -967,7 +973,6 @@ void subscription_add_bytes_in(th_subscription_t *s, size_t in) */ void subscription_add_bytes_out(th_subscription_t *s, size_t out) { - atomic_add(&s->ths_bytes_out, out); atomic_add_u64(&s->ths_total_bytes_out, out); } diff --git a/src/subscriptions.h b/src/subscriptions.h index f9648373a..7f5da4351 100644 --- a/src/subscriptions.h +++ b/src/subscriptions.h @@ -88,10 +88,10 @@ typedef struct th_subscription { int ths_total_err; /* total errors during entire subscription */ uint64_t ths_total_bytes_in; /* total bytes since the subscription started */ uint64_t ths_total_bytes_out; /* total bytes since the subscription started */ - int ths_bytes_in; // Reset every second to get aprox. bandwidth (in) - int ths_bytes_out; // Reset every second to get approx bandwidth (out) - int ths_bytes_in_prev; /* Bytes received during the last second */ - int ths_bytes_out_prev; /* Bytes sent during the last second */ + uint64_t ths_total_bytes_in_prev; /* total bytes since the subscription started, minus 1 second */ + uint64_t ths_total_bytes_out_prev; /* total bytes since the subscription started, minus 1 second */ + int ths_bytes_in_avg; /* Average bytes in per second */ + int ths_bytes_out_avg; /* Average bytes out per second */ streaming_target_t ths_input;