Debian Codegraph @

utils/sample.c

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