forked from chaos/diod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.c
217 lines (192 loc) · 5.21 KB
/
sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*****************************************************************************
* Copyright (C) 2010-14 Lawrence Livermore National Security, LLC.
* Written by Jim Garlick <[email protected]> LLNL-CODE-423279
* All Rights Reserved.
*
* This file is part of the Distributed I/O Daemon (diod).
* For details, see http://code.google.com/p/diod.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the license, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* See also: http://www.gnu.org/licenses
*****************************************************************************/
/* sample.c - ADT for 2-point data values used in rate calculation */
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "diod_log.h"
#include "sample.h"
struct sample_struct {
double val[2];
time_t time[2];
int valid; /* count of valid samples [0,1,2] */
int stale_secs;
};
sample_t
sample_create (int stale_secs)
{
sample_t s = malloc (sizeof (*s));
if (!s)
msg_exit ("out of memory");
memset (s, 0, sizeof (*s));
s->stale_secs = stale_secs;
return s;
}
void
sample_destroy (sample_t s)
{
free (s);
}
sample_t
sample_copy (sample_t s1)
{
sample_t s = malloc (sizeof (*s));
if (!s)
msg_exit ("out of memory");
memcpy (s, s1, sizeof (*s));
return s;
}
/* Invalidate both data points.
*/
void
sample_invalidate (sample_t s)
{
s->valid = 0;
}
/* Update sample with val @ timestamp t.
*/
void
sample_update (sample_t s, double val, time_t t)
{
if (s->valid == 0) {
s->time[1] = t;
s->val[1] = val;
s->valid++;
} else if (s->time[1] < t) {
s->time[0] = s->time[1];
s->val[0] = s->val[1];
s->time[1] = t;
s->val[1] = val;
if (s->valid < 2)
s->valid++;
}
}
/* s1 += s2
* Only has an effect if samples were collected at the same times.
* (This is a somewhat contrived interface for ltop aggregation of ost data)
*/
void
sample_add (sample_t s1, sample_t s2)
{
if (s1->valid != s2->valid)
return;
if (s1->valid > 0 && s1->time[1] != s2->time[1])
return;
if (s1->valid > 1 && s1->time[0] != s2->time[0])
return;
if (s1->valid > 0)
s1->val[1] += s2->val[1];
if (s1->valid > 1)
s1->val[0] += s2->val[0];
}
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
/* s1 = MAX (s1, s2)
* Only has an effect if samples were collected at the same times.
* (This is a somewhat contrived interface for ltop aggregation of ost data)
*/
void
sample_max (sample_t s1, sample_t s2)
{
if (s1->valid != s2->valid)
return;
if (s1->valid > 0 && s1->time[1] != s2->time[1])
return;
if (s1->valid > 1 && s1->time[0] != s2->time[0])
return;
if (s1->valid > 0)
s1->val[1] = MAX (s1->val[1], s2->val[1]);
if (s1->valid > 1)
s1->val[0] = MAX (s1->val[0], s2->val[0]);
}
/* s1 = MAX (s1, s2)
* Only has an effect if samples were collected at the same times.
* (This is a somewhat contrived interface for ltop aggregation of ost data)
*/
void
sample_min (sample_t s1, sample_t s2)
{
if (s1->valid != s2->valid)
return;
if (s1->valid > 0 && s1->time[1] != s2->time[1])
return;
if (s1->valid > 1 && s1->time[0] != s2->time[0])
return;
if (s1->valid > 0)
s1->val[1] = MIN (s1->val[1], s2->val[1]);
if (s1->valid > 1)
s1->val[0] = MIN (s1->val[0], s2->val[0]);
}
/* Return delta(val) / delta(time),
* Returns 0 if expired, or < 2 valid data points.
*/
double
sample_rate (sample_t s, time_t tnow)
{
double val = 0;
if (s->valid == 2 && (tnow - s->time[1]) <= s->stale_secs)
val = (s->val[1] - s->val[0]) / (s->time[1] - s->time[0]);
if (val < 0)
val = 0;
return val;
}
/* Return newest data point.
* Returns 0 if expired or < 1 valid data point.
*/
double
sample_val (sample_t s, time_t tnow)
{
if (s->valid > 0 && (tnow - s->time[1]) <= s->stale_secs)
return s->val[1];
return 0;
}
/* Compare sample values for sorting.
*/
int
sample_val_cmp (sample_t s1, sample_t s2, time_t tnow)
{
double v1 = sample_val (s1, tnow);
double v2 = sample_val (s2, tnow);
return (v1 == v2 ? 0 : v1 < v2 ? -1 : 1);
}
/* Compare rate values for sorting.
*/
int
sample_rate_cmp (sample_t s1, sample_t s2, time_t tnow)
{
double v1 = sample_rate (s1, tnow);
double v2 = sample_rate (s2, tnow);
return (v1 == v2 ? 0 : v1 < v2 ? -1 : 1);
}
/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/