My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Classes | Macros | Functions | Variables
rt_stats.c File Reference

Collecting statistics within the runtime. More...

#include "rt_stats.h"
#include "stats.h"
#include "logging.h"
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  stat_item
 The internal statistics structure where stats are aggregated One per statistic-item. More...
 
struct  stat_type
 The structure holding all items of a type of stats. More...
 

Macros

#define CLOCK_ID   CLOCK_REALTIME_COARSE
 The clock used to mark the time at which events take place. More...
 
#define DURATION_CLOCK_ID   CLOCK_MONOTONIC
 
#define MAX_STATS   8192
 The maximum number of statistics that can be held in the rrdb. More...
 
#define GATHER_STATS   1
 If set to 0, will disable all gathering of statistics. More...
 
#define GATHER_MSU_STATS   1 & GATHER_STATS
 
#define GATHER_CUSTOM_STATS   1 & GATHER_STATS
 
#define GATHER_THREAD_STATS   1 & GATHER_STATS
 
#define GATHER_PROFILING   0
 
#define N_STAT_TYPES   (sizeof(stat_types) / sizeof(*stat_types))
 The number of types of statistics. More...
 
#define CHECK_INITIALIZATION
 
#define WARN_ROLLOVER(label, item_id)
 

Functions

static int write_item_to_log (FILE *out_file, struct stat_type *type, struct stat_item *item)
 Writes a single stat item to the log file. More...
 
static void get_elapsed_time (struct timespec *t)
 Gets the amount of time that has elapsed since logging started. More...
 
static int wrlock_type (struct stat_type *type)
 Locks a stat type for writing. More...
 
static int unlock_type (struct stat_type *type)
 Unlocks a stat type. More...
 
static int rlock_type (struct stat_type *type)
 Locks a stat type for reading. More...
 
static int lock_item (struct stat_item *item)
 Locks an item_stats structure, printing an error message on failure. More...
 
static int unlock_item (struct stat_item *item)
 Unlocks an item_stats structure, printing an error message on failure. More...
 
static void UNUSED flush_all_stats_to_log (FILE *file)
 Writes all statistics to the provided log file. More...
 
static void destroy_stat_item (struct stat_item *item)
 Frees the memory associated with an individual stat item. More...
 
static struct stat_itemget_item_stat (struct stat_type *type, unsigned int item_id)
 Gets the stat item associated with the given item_id in the given type. More...
 
static long double current_double_time ()
 
int record_start_time (enum stat_id stat_id, unsigned int item_id)
 Starts a measurement of elapsed time. More...
 
int record_end_time (enum stat_id stat_id, unsigned int item_id)
 Records the elapsed time since the previous call to record_start_time. More...
 
int increment_stat (enum stat_id stat_id, unsigned int item_id, double value)
 Increments the given statistic by the provided value. More...
 
int record_stat (enum stat_id stat_id, unsigned int item_id, double stat, bool relog)
 Records a statistic in the statlog. More...
 
double get_last_stat (enum stat_id stat_id, unsigned int item_id)
 Returns the last statistic recorded. More...
 
static int time_cmp (struct timespec *t1, struct timespec *t2)
 Returns 1 if t1 > t2, -1 if t2 > t1, 0 otherwise. More...
 
static int find_time_index (struct timed_stat *stats, struct timespec *time, int start_index, int stat_size)
 Finds the index at which the given time occurred in the stats. More...
 
static int sample_stat_item (struct stat_item *item, int stat_size, struct timespec *sample_end, struct timespec *interval, int sample_size, struct timed_stat *sample)
 Takes a sample of the provided stat item and stores it in sample More...
 
static int sample_stat (enum stat_id stat_id, struct timespec *end, struct timespec *interval, int sample_size, struct stat_sample *sample, int n_samples)
 Takes a sample of the provided stat type (stat_id) and places it in sample More...
 
static int UNUSED sample_recent_stats (enum stat_id stat_id, struct timespec *interval, int sample_size, struct stat_sample *sample, int n_samples)
 Samples sample_size most recent stats in interval intervals. More...
 
struct stat_sampleget_stat_samples (enum stat_id stat_id, struct timespec *sample_time, int *n_samples_out)
 Samples the statistic with the provided stat_id. More...
 
static void realloc_stat_samples (enum stat_id stat_id, int n_samples)
 Reallocates the existing stats structure to store the provided number of samples. More...
 
int remove_stat_item (enum stat_id stat_id, unsigned int item_id)
 Un-registers an item so it can no longer have statistics registered, and will not be reported to the global controller. More...
 
int init_stat_item (enum stat_id stat_id, unsigned int item_id)
 Initializes a stat item so that statistics can be logged to it. More...
 
int init_statistics ()
 Initializes the entire stats module. More...
 
void finalize_statistics (char *statlog)
 Writes the statistics to statlog if provided, and frees assocated structure. More...
 

Variables

static struct timespec stats_start_time
 The time at which the runtime started. More...
 
static bool stats_initialized = false
 Whether init_statistics has been called. More...
 
struct stat_type stat_types []
 The list of all statistics that can be gathered in the system. More...
 
static struct stat_samplestat_samples [N_REPORTED_STAT_TYPES]
 A statically allocated stat_sample structure which is pre-allocated to store samples from each stat type. More...
 
static struct timespec stat_sample_interval
 The interval at which stats should be sampled. More...
 

Detailed Description

Collecting statistics within the runtime.

Definition in file rt_stats.c.

Macro Definition Documentation

#define CHECK_INITIALIZATION
Value:
log_error("Statistics not initialized!"); \
return -1; \
}
#define log_error(fmt,...)
Definition: logging.h:101
static bool stats_initialized
Whether init_statistics has been called.
Definition: rt_stats.c:43

Definition at line 131 of file rt_stats.c.

#define CLOCK_ID   CLOCK_REALTIME_COARSE

The clock used to mark the time at which events take place.

Definition at line 37 of file rt_stats.c.

#define DURATION_CLOCK_ID   CLOCK_MONOTONIC

Definition at line 38 of file rt_stats.c.

#define GATHER_CUSTOM_STATS   1 & GATHER_STATS

Definition at line 86 of file rt_stats.c.

#define GATHER_MSU_STATS   1 & GATHER_STATS

Definition at line 85 of file rt_stats.c.

#define GATHER_PROFILING   0

Definition at line 92 of file rt_stats.c.

#define GATHER_STATS   1

If set to 0, will disable all gathering of statistics.

Definition at line 83 of file rt_stats.c.

#define GATHER_THREAD_STATS   1 & GATHER_STATS

Definition at line 87 of file rt_stats.c.

#define MAX_STATS   8192

The maximum number of statistics that can be held in the rrdb.

Definition at line 79 of file rt_stats.c.

#define N_STAT_TYPES   (sizeof(stat_types) / sizeof(*stat_types))

The number of types of statistics.

Definition at line 129 of file rt_stats.c.

#define WARN_ROLLOVER (   label,
  item_id 
)

Definition at line 351 of file rt_stats.c.

Function Documentation

static long double current_double_time ( )
inlinestatic

Definition at line 314 of file rt_stats.c.

static void destroy_stat_item ( struct stat_item item)
static

Frees the memory associated with an individual stat item.

Definition at line 294 of file rt_stats.c.

void finalize_statistics ( char *  statlog)

Writes the statistics to statlog if provided, and frees assocated structure.

Parameters
statlogLog file to dump statistics to or NULL if N/A

Definition at line 822 of file rt_stats.c.

static int find_time_index ( struct timed_stat stats,
struct timespec *  time,
int  start_index,
int  stat_size 
)
static

Finds the index at which the given time occurred in the stats.

Definition at line 505 of file rt_stats.c.

static void UNUSED flush_all_stats_to_log ( FILE *  file)
static

Writes all statistics to the provided log file.

Definition at line 278 of file rt_stats.c.

static void get_elapsed_time ( struct timespec *  t)
static

Gets the amount of time that has elapsed since logging started.

Parameters
*tthe elapsed time is output into this variable

Definition at line 221 of file rt_stats.c.

static struct stat_item* get_item_stat ( struct stat_type type,
unsigned int  item_id 
)
static

Gets the stat item associated with the given item_id in the given type.

Definition at line 300 of file rt_stats.c.

double get_last_stat ( enum stat_id stat_id  ,
unsigned int  item_id 
)

Returns the last statistic recorded.

Definition at line 466 of file rt_stats.c.

struct stat_sample* get_stat_samples ( enum stat_id  stat_id,
struct timespec *  time,
int *  n_sample_out 
)

Samples the statistic with the provided stat_id.

Parameters
stat_idthe ID of the stat_type to sample
timeThe time at which the samples should be taken
n_samples_outOutput argument, stores the number of samples acquired
Returns
The statically-allocated stat sample (does not need to be freed)

Definition at line 634 of file rt_stats.c.

int increment_stat ( enum stat_id  stat_id,
unsigned int  item_id,
double  value 
)

Increments the given statistic by the provided value.

Parameters
stat_idID for the stat being logged
item_idID for the item to which the stat refers (must be registered!)
valueThe amount to add to the given stat
Returns
0 on success, -1 on error

Definition at line 389 of file rt_stats.c.

int init_stat_item ( enum stat_id stat_id  ,
unsigned int  item_id 
)

Initializes a stat item so that statistics can be logged to it.

Initializes a new stat item so it can have stats registered and can be reported to the global controller.

(e.g. initializing MSU_QUEUE_LEN for item N, corresponding to msu # N)

Parameters
stat_idID of the statistic type to be logged
item_idID of the item to be logged

Definition at line 719 of file rt_stats.c.

int init_statistics ( )

Initializes the entire stats module.

MUST BE CALLED before runtime starts

Definition at line 783 of file rt_stats.c.

static int lock_item ( struct stat_item item)
inlinestatic

Locks an item_stats structure, printing an error message on failure.

Parameters
*itemthe item_stats structure to lock
Returns
0 on success, -1 on error

Definition at line 257 of file rt_stats.c.

static void realloc_stat_samples ( enum stat_id  stat_id,
int  n_samples 
)
static

Reallocates the existing stats structure to store the provided number of samples.

Definition at line 672 of file rt_stats.c.

int record_end_time ( enum stat_id  stat_id,
unsigned int  item_id 
)

Records the elapsed time since the previous call to record_start_time.

Parameters
stat_idID for stat being logged
item_idID for item within the statistic

Definition at line 355 of file rt_stats.c.

int record_start_time ( enum stat_id  stat_id,
unsigned int  item_id 
)

Starts a measurement of elapsed time.

Not added to the log until a call to record_end_time

Parameters
stat_idID for stat type being logged
item_idID for the item to which the stat refers
Returns
0 on success, -1 on error

Definition at line 320 of file rt_stats.c.

int record_stat ( enum stat_id  stat_id,
unsigned int  item_id,
double  stat,
bool  relog 
)

Records a statistic in the statlog.

Parameters
stat_idID for stat being logged
item_idID for item to which stat refers (must be registered!)
statStatistic to record
relogWhether to log statistic if it matches the previously logged stat
Returns
0 on success, -1 on error

Definition at line 426 of file rt_stats.c.

int remove_stat_item ( enum stat_id stat_id  ,
unsigned int  item_id 
)

Un-registers an item so it can no longer have statistics registered, and will not be reported to the global controller.

Definition at line 683 of file rt_stats.c.

static int rlock_type ( struct stat_type type)
inlinestatic

Locks a stat type for reading.

Definition at line 245 of file rt_stats.c.

static int UNUSED sample_recent_stats ( enum stat_id  stat_id,
struct timespec *  interval,
int  sample_size,
struct stat_sample sample,
int  n_samples 
)
static

Samples sample_size most recent stats in interval intervals.

Note: Not used at the moment, deferring to sample_stat instead

Definition at line 612 of file rt_stats.c.

static int sample_stat ( enum stat_id  stat_id,
struct timespec *  end,
struct timespec *  interval,
int  sample_size,
struct stat_sample sample,
int  n_samples 
)
static

Takes a sample of the provided stat type (stat_id) and places it in sample

Definition at line 565 of file rt_stats.c.

static int sample_stat_item ( struct stat_item item,
int  stat_size,
struct timespec *  sample_end,
struct timespec *  interval,
int  sample_size,
struct timed_stat sample 
)
static

Takes a sample of the provided stat item and stores it in sample

Definition at line 527 of file rt_stats.c.

static int time_cmp ( struct timespec *  t1,
struct timespec *  t2 
)
inlinestatic

Returns 1 if t1 > t2, -1 if t2 > t1, 0 otherwise.

Definition at line 497 of file rt_stats.c.

static int unlock_item ( struct stat_item item)
inlinestatic

Unlocks an item_stats structure, printing an error message on failure.

Parameters
*itemthe item_stats structure to unlock
Returns
0 on success, -1 on error

Definition at line 269 of file rt_stats.c.

static int unlock_type ( struct stat_type type)
inlinestatic

Unlocks a stat type.

Definition at line 236 of file rt_stats.c.

static int write_item_to_log ( FILE *  out_file,
struct stat_type type,
struct stat_item item 
)
static

Writes a single stat item to the log file.

Definition at line 187 of file rt_stats.c.

static int wrlock_type ( struct stat_type type)
inlinestatic

Locks a stat type for writing.

Definition at line 227 of file rt_stats.c.

Variable Documentation

struct timespec stat_sample_interval
static
Initial value:
= {
.tv_nsec = (int)(STAT_SAMPLE_PERIOD_MS * 1e6 / STAT_SAMPLE_SIZE) % (int)1e9
}
#define STAT_SAMPLE_PERIOD_MS
How often samples are sent from runtime to controller.
Definition: stats.h:132
#define STAT_SAMPLE_SIZE
Number of statistics sampled in each send from runtime to controller.
Definition: stats.h:129

The interval at which stats should be sampled.

Definition at line 629 of file rt_stats.c.

struct stat_sample* stat_samples[N_REPORTED_STAT_TYPES]
static

A statically allocated stat_sample structure which is pre-allocated to store samples from each stat type.

Definition at line 626 of file rt_stats.c.

struct stat_type stat_types[]

The list of all statistics that can be gathered in the system.

Definition at line 96 of file rt_stats.c.

bool stats_initialized = false
static

Whether init_statistics has been called.

Definition at line 43 of file rt_stats.c.

struct timespec stats_start_time
static

The time at which the runtime started.

Definition at line 41 of file rt_stats.c.