]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
alternate approach
authorSam Stenvall <sam.stenvall@nordsoftware.com>
Wed, 2 Sep 2015 09:31:57 +0000 (12:31 +0300)
committerJaroslav Kysela <perex@perex.cz>
Wed, 2 Sep 2015 18:43:45 +0000 (20:43 +0200)
src/atomic.h
src/subscriptions.c
src/subscriptions.h

index d88d81215d1116288bfbed13abbd4b22ff840833..525b225e87f1b5427ad9109f769b8282170c7de6 100644 (file)
@@ -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)
 {
index 04906484ad2157eeced49442ea772661496814b6..4490361072f58c3fc250aa8cd0aeb6bba02348d2 100644 (file)
@@ -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);
 }
 
index f9648373ae12918ca7ed7e552d4402ac6750734b..7f5da43517e8f13754b0c5c107890b9a39e11c29 100644 (file)
@@ -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;