From d1e8d44990eb9e1bb7280ccff07b0d396fdcc0ae Mon Sep 17 00:00:00 2001 From: Robert Gustafsson Date: Sat, 14 Oct 2017 14:48:28 +0200 Subject: Add statistics after SIGINT --- broccoli/src/fifoqueue.c | 11 +++++++++++ broccoli/src/midbro.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) (limited to 'broccoli/src') diff --git a/broccoli/src/fifoqueue.c b/broccoli/src/fifoqueue.c index 9b4b75e..6a99740 100644 --- a/broccoli/src/fifoqueue.c +++ b/broccoli/src/fifoqueue.c @@ -13,6 +13,10 @@ init_queue(int size) q->tail = NULL; q->maxSize = size; q->currentSize = 0; + q->droppedValues = 0; + q->largestBufferSize = 0; + q->valuesReceived = 0; + q->valuesReleased = 0; /*Queue empty from the beginning (block)*/ sem_init(&q->bufferEmptyBlock, 0, 0); sem_init(&q->lock, 0, 1); @@ -50,6 +54,7 @@ add_to_queue(Fifo_q * q, Sensor_t * sensor) #ifdef DLR pop_from_queue(q); #endif + q->droppedValues++; return 0; } sem_wait(&q->lock); @@ -63,6 +68,9 @@ add_to_queue(Fifo_q * q, Sensor_t * sensor) q->tail->next = new_elem; q->tail = new_elem; q->currentSize++; + q->valuesReceived++; + if(q->currentSize > q->largestBufferSize) + q->largestBufferSize = q->currentSize; sem_post(&q->lock); return 1; } @@ -93,6 +101,9 @@ pop_from_queue(Fifo_q * q) } free(head); q->currentSize--; + #ifndef DLR + q->valuesReleased++; + #endif sem_post(&q->lock); return sensor; } diff --git a/broccoli/src/midbro.c b/broccoli/src/midbro.c index 6c83207..e2f9644 100644 --- a/broccoli/src/midbro.c +++ b/broccoli/src/midbro.c @@ -1,5 +1,6 @@ #include #include +#include #include "fifoqueue.h" #include "broevent.h" #include "midbro.h" @@ -8,7 +9,24 @@ #endif Fifo_q * queue; +pthread_t event_listener; +sigset_t signal_set; + void +sigint_handler(int signal) +{ + printf("\nStatistics:\n" + "Total values received: %d\n" + "Total values dropped: %d\n" + "Total values released: %d\n" + "Maximum buffer utilization: %d\n" + "Buffer fixed size: %d\n" + "Buffer size upon termination: %d\n", + queue->valuesReceived, queue->droppedValues, + queue->valuesReleased, queue->largestBufferSize, + queue->maxSize, queue->currentSize); + exit(0); +} void request_n_values(int number, int arrayOfValues[]) { @@ -37,12 +55,20 @@ request_value() start_data_capture() { int res; - queue = init_queue(500000); /* Initiate queue with fixed size */ - pthread_t event_listener; + queue = init_queue(500); /* Initiate queue with fixed size */ /* Create producer thread that listen for bro events */ + sigemptyset(&signal_set); + sigaddset(&signal_set, SIGINT); + res = pthread_sigmask(SIG_BLOCK, &signal_set, NULL); + if(res != 0) + perror("SIGINT block"); res = pthread_create(&event_listener, NULL, bro_event_listener, queue); if(res){ perror("Unable to create thread"); exit(-1); } + res = pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL); + if(res != 0) + perror("SIGINT unblock"); + signal(SIGINT, sigint_handler); } -- cgit v1.2.3