My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
stat_msg_handler.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 */
20 #include "stat_msg_handler.h"
21 #include "timeseries.h"
22 #include "controller_stats.h"
23 #include "scheduling_decision.h"
24 #include "logging.h"
25 #include "scheduling.h"
26 #include "controller_dfg.h"
27 #include "controller_mysql.h"
28 
29 #define MAX_STAT_SAMPLES 128
30 #define MAX_SAMPLE_SIZE 64
32 
33 static int process_stat_sample(int runtime_id, struct stat_sample *sample) {
34  int rtn = db_insert_sample(sample->stats, &sample->hdr, runtime_id);
35  if (rtn < 0) {
36  log(LOG_MYSQL,"Error inserting stats %d.%u into DB",
37  sample->hdr.stat_id, sample->hdr.item_id);
38  }
39 
40  struct timed_rrdb *stat;
41  if (is_thread_stat(sample->hdr.stat_id)) {
42  stat = get_thread_stat(sample->hdr.stat_id, runtime_id, sample->hdr.item_id);
43  } else if (is_msu_stat(sample->hdr.stat_id)) {
44  stat = get_msu_stat(sample->hdr.stat_id, sample->hdr.item_id);
45  } else {
46  log_error("Cannot find statistic %d", sample->hdr.stat_id);
47  return -1;
48  }
49 
50  if (stat == NULL) {
51  log(LOG_PROCESS_STATS, "Error getting stat sample %d.%u",
52  sample->hdr.stat_id, sample->hdr.item_id);
53  return -1;
54  }
55  rtn = append_to_timeseries(sample->stats, sample->hdr.n_stats, stat);
56  if (rtn < 0) {
57  log_error("Error appending stats %d.%u to timeseries",
58  sample->hdr.stat_id, sample->hdr.item_id);
59  return -1;
60  }
61 
62 
63  return 0;
64 }
65 
66 int handle_serialized_stats_buffer(int runtime_id, void *buffer, size_t buffer_len) {
67  if (incoming_samples == NULL) {
68  log_error("Stat msg handler not initialized");
69  return -1;
70  }
71 
72  int n_samples = deserialize_stat_samples(buffer, buffer_len,
73  incoming_samples, MAX_STAT_SAMPLES);
74  if (n_samples < 0) {
75  log_error("Error deserializing stat samples");
76  return -1;
77  }
78 
79  for (int i=0; i<n_samples; i++) {
80  if (process_stat_sample(runtime_id, &incoming_samples[i]) != 0) {
81  //log_error("Error processing stat sample");
82  }
83  }
84 
85 #ifdef DYN_SCHED
88 #endif
89 
90  return 0;
91 }
92 
95  if (incoming_samples == NULL) {
96  log_error("Error initializing stat samples");
97  return -1;
98  }
99  return 0;
100 }
Round-robin database (circular buffer) for storing timeseries data.
Definition: timeseries.h:36
struct timed_stat * stats
The statistics in question.
Definition: stats.h:58
static int process_stat_sample(int runtime_id, struct stat_sample *sample)
int perform_cloning()
int handle_serialized_stats_buffer(int runtime_id, void *buffer, size_t buffer_len)
A single stat sample for a single item.
Definition: stats.h:53
struct stat_sample * init_stat_samples(int max_stats, int n_samples)
Initilizes n sets of samples of statistics, each of which contains max_stats points.
Definition: stats.c:80
int deserialize_stat_samples(void *buffer, size_t buff_len, struct stat_sample *samples, int n_samples)
Deserializes from the provided buffer into the samples structure.
Definition: stats.c:176
int init_stats_msg_handler()
Logging of status messages to the terminal.
#define MAX_STAT_SAMPLES
struct timed_rrdb * get_msu_stat(enum stat_id id, unsigned int msu_id)
int n_stats
The size of the sample (number of stats, not number of items)
Definition: stats.h:49
enum stat_id stat_id
The ID of the statistic being sampled.
Definition: stats.h:45
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 log_error(fmt,...)
Definition: logging.h:101
int fix_all_route_ranges(struct dedos_dfg *dfg)
Definition: scheduling.c:179
unsigned int item_id
The ID for the item being sampled.
Definition: stats.h:47
#define MAX_SAMPLE_SIZE
struct stat_sample * incoming_samples
static int runtime_id(int runtime_fd)
#define log(level, fmt,...)
Log at a custom level.
Definition: logging.h:147
struct dedos_dfg * get_dfg()
Definition: runtime_dfg.c:115
int is_msu_stat(enum stat_id id)
Definition: stats.c:49
struct timed_rrdb * get_thread_stat(enum stat_id id, unsigned int runtime_id, unsigned int thread_id)
struct stat_sample_hdr hdr
Definition: stats.h:54
int is_thread_stat(enum stat_id id)
Definition: stats.c:40
int db_insert_sample(struct timed_stat *input, struct stat_sample_hdr *input_hdr, int runtime_id)
Insert datapoint for a timseries in the DB.