Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
alternate approach
  • Loading branch information
Sam Stenvall authored and perexg committed Sep 2, 2015
1 parent 80c4178 commit 23cc083
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/atomic.h
Expand Up @@ -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)
{
Expand Down
19 changes: 12 additions & 7 deletions src/subscriptions.c
Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions src/subscriptions.h
Expand Up @@ -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;

Expand Down

0 comments on commit 23cc083

Please sign in to comment.