void heartbeat_finish(heartbeat_t* hb) { if (hb != NULL) { pthread_mutex_destroy(&hb->mutex); free(hb->window); if(hb->text_file != NULL) { hb_flush_buffer(hb); fclose(hb->text_file); } remove(hb->filename); /*TODO : need to deallocate log */ free(hb); } }
/** * Registers a heartbeat * @param hb pointer to heartbeat_t * @param tag integer */ int64_t heartbeat( heartbeat_t* hb, int tag ) { struct timespec time_info; int64_t time; int64_t old_last_time = hb->last_timestamp; //printf("Registering Heartbeat\n"); clock_gettime( CLOCK_REALTIME, &time_info ); time = ( (int64_t) time_info.tv_sec * 1000000000 + (int64_t) time_info.tv_nsec ); pthread_mutex_lock(&hb->mutex); hb->last_timestamp = time; if(hb->first_timestamp == -1) { //printf("In heartbeat - first time stamp\n"); hb->first_timestamp = time; hb->last_timestamp = time; hb->window[0] = 0; //printf(" - accessing state and log\n"); hb->log[0].beat = hb->state->counter; hb->log[0].tag = tag; hb->log[0].timestamp = time; hb->log[0].window_rate = 0; hb->log[0].instant_rate = 0; hb->log[0].global_rate = 0; hb->state->counter++; hb->state->buffer_index++; hb->state->valid = 1; } else { //printf("In heartbeat - NOT first time stamp - read index = %d\n",hb->state->read_index ); int index = hb->state->buffer_index; hb->last_timestamp = time; double window_heartrate = hb_window_average(hb, time-old_last_time); double global_heartrate = (((double) hb->state->counter+1) / ((double) (time - hb->first_timestamp)))*1000000000.0; double instant_heartrate = 1.0 /(((double) (time - old_last_time))) * 1000000000.0; hb->log[index].beat = hb->state->counter; hb->log[index].tag = tag; hb->log[index].timestamp = time; hb->log[index].window_rate = window_heartrate; hb->log[index].instant_rate = instant_heartrate; hb->log[index].global_rate = global_heartrate; hb->state->buffer_index++; hb->state->counter++; hb->state->read_index++; if(hb->state->buffer_index%hb->state->buffer_depth == 0) { if(hb->text_file != NULL) hb_flush_buffer(hb); hb->state->buffer_index = 0; } if(hb->state->read_index%hb->state->buffer_depth == 0) { hb->state->read_index = 0; } } pthread_mutex_unlock(&hb->mutex); return time; }