My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
timeseries.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
26 #include "timeseries.h"
27 #include "stats.h"
28 #include "dfg.h"
29 #include "logging.h"
30 
31 double average_n(struct timed_rrdb *timeseries, int n_samples) {
32  double sum = 0;
33  int gathered = 0;
34  int i;
35  int start_index = timeseries->write_index - 1;
36  for (i = 0; i < n_samples; i++) {
37  int index = start_index - i;
38  if ( index < 0 )
39  index = RRDB_ENTRIES + index;
40  if ( timeseries->time[index].tv_sec < 0 ){
41  continue;
42  }
43  gathered++;
44  sum += timeseries->data[index];
45  }
46 
47  if (gathered == 0)
48  return -1;
49  return sum / (double)gathered;
50 }
51 
58 int append_to_timeseries(struct timed_stat *input, int input_size,
59  struct timed_rrdb *timeseries) {
60  int write_index = timeseries->write_index;
61  for (int i=0; i<input_size; i++) {
62  write_index %= RRDB_ENTRIES;
63  if (input[i].time.tv_sec == 0 && input[i].time.tv_nsec == 0) {
64  // FIXME: I don't know why this happens sometimes
65  continue;
66  }
67  timeseries->data[write_index] = input[i].value;
68  timeseries->time[write_index] = input[i].time;
69  write_index++;
70  }
71  timeseries->write_index = write_index % RRDB_ENTRIES;
72  return 0;
73 }
74 
77 #define PRINT_LEN 6
78 
82 void print_timeseries(struct timed_rrdb *timeseries){
83 
84  char str[RRDB_ENTRIES * 10 * 2];
85  char *buff = str;
86 
87  buff += sprintf(buff, "TIME: ");
88 
89  for (int i=0; i<PRINT_LEN; i++) {
90  int index = (timeseries->write_index + i) % RRDB_ENTRIES;
91  double time = timeseries->time[index].tv_sec +
92  ((double)timeseries->time[index].tv_nsec * 1e-9);
93  buff += sprintf(buff, "| %9.4f ", time);
94  }
95 
96  buff += sprintf(buff, "| ... ");
97 
98  for (int i=RRDB_ENTRIES-PRINT_LEN; i<RRDB_ENTRIES; i++) {
99  int index = (timeseries->write_index + i) % RRDB_ENTRIES;
100  double time = timeseries->time[index].tv_sec +
101  ((double)timeseries->time[index].tv_nsec * 1e-9);
102  buff += sprintf(buff, "| %9.4f ", time);
103  }
104 
105  buff += sprintf(buff, "\nDATA: ");
106  for (int i=0; i<PRINT_LEN; i++){
107  int index = (timeseries->write_index + i) % RRDB_ENTRIES;
108  buff += sprintf(buff, "| %9d ", (int)timeseries->data[index]);
109  }
110  buff += sprintf(buff, "| ... ");
111 
112  for (int i=RRDB_ENTRIES-PRINT_LEN; i<RRDB_ENTRIES; i++){
113  int index = (timeseries->write_index + i) % RRDB_ENTRIES;
114  buff += sprintf(buff, "| %9d ", (int)timeseries->data[index]);
115  }
116  buff += sprintf(buff, "\n");
117  printf("%s", str);
118 }
double average_n(struct timed_rrdb *timeseries, int n_samples)
timeseries.c
Definition: timeseries.c:31
Round-robin database (circular buffer) for storing timeseries data.
Definition: timeseries.h:36
#define PRINT_LEN
The length of the begnning and end of the timeseries that's printed when print_timeseries() is called...
Definition: timeseries.c:77
double data[240]
The statistics.
Definition: timeseries.h:37
Logging of status messages to the terminal.
Functions for the sending and receiving of statistics between ctrl and runtime.
int write_index
Offset into the rrdb at which writing has occurred.
Definition: timeseries.h:39
struct timespec time
Definition: stats.h:38
int append_to_timeseries(struct timed_stat *input, int input_size, struct timed_rrdb *timeseries)
Appends a number of timed statistics to a timeseries.
Definition: timeseries.c:58
#define RRDB_ENTRIES
timeseries.h
Definition: timeseries.h:32
void print_timeseries(struct timed_rrdb *timeseries)
Prints the beginning and end of a timeseries.
Definition: timeseries.c:82
Interfaces for the creation and modification of the data-flow-graph and and general description of th...
Holds a single timestamped value.
Definition: stats.h:37
struct timespec time[240]
The time at which the stats were gathered.
Definition: timeseries.h:38
long double value
Definition: stats.h:39