1 /****************************************************************************
2 * RRDtool 1.4.8 Copyright by Tobi Oetiker, 1997-2013
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
7 #include <limits.h>
8 #include <locale.h>
9 #include <stdlib.h>
11 #include "rrd_tool.h"
12 #include "rrd_rpncalc.h"
13 // #include "rrd_graph.h"
15 short addop2str(
16 enum op_en op,
17 enum op_en op_type,
18 char *op_str,
19 char **result_str,
20 unsigned short *offset);
21 int tzoffset(
22 time_t); /* used to implement LTIME */
24 short rpn_compact(
25 rpnp_t *rpnp,
26 rpn_cdefds_t **rpnc,
27 short *count)
28 {
29 short i;
31 *count = 0;
32 /* count the number of rpn nodes */
33 while (rpnp[*count].op != OP_END)
34 (*count)++;
35 if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
36 rrd_set_error("Maximum %d RPN nodes permitted. Got %d RPN nodes at present.",
37 DS_CDEF_MAX_RPN_NODES-1,(*count)-1);
38 return -1;
39 }
41 /* allocate memory */
42 *rpnc = (rpn_cdefds_t *) calloc(*count, sizeof(rpn_cdefds_t));
43 for (i = 0; rpnp[i].op != OP_END; i++) {
44 (*rpnc)[i].op = (char) rpnp[i].op;
45 if (rpnp[i].op == OP_NUMBER) {
46 /* rpnp.val is a double, rpnc.val is a short */
47 double temp = floor(rpnp[i].val);
49 if (temp < SHRT_MIN || temp > SHRT_MAX) {
50 rrd_set_error
51 ("constants must be integers in the interval (%d, %d)",
52 SHRT_MIN, SHRT_MAX);
53 free(*rpnc);
54 return -1;
55 }
56 (*rpnc)[i].val = (short) temp;
57 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
58 (*rpnc)[i].val = (short) rpnp[i].ptr;
59 }
60 }
61 /* terminate the sequence */
62 (*rpnc)[(*count) - 1].op = OP_END;
63 return 0;
64 }
66 rpnp_t *rpn_expand(
67 rpn_cdefds_t *rpnc)
68 {
69 short i;
70 rpnp_t *rpnp;
72 /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
73 * memory we avoid any reallocs */
74 rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES, sizeof(rpnp_t));
75 if (rpnp == NULL) {
76 rrd_set_error("failed allocating rpnp array");
77 return NULL;
78 }
79 for (i = 0; rpnc[i].op != OP_END; ++i) {
80 rpnp[i].op = (enum op_en)rpnc[i].op;
81 if (rpnp[i].op == OP_NUMBER) {
82 rpnp[i].val = (double) rpnc[i].val;
83 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
84 rpnp[i].ptr = (long) rpnc[i].val;
85 }
86 }
87 /* terminate the sequence */
88 rpnp[i].op = OP_END;
89 return rpnp;
90 }
92 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
93 * into a CDEF string. This function is used by rrd_dump.
94 * arguments:
95 * rpnc: an array of compact RPN operator nodes
96 * ds_def: a pointer to the data source definition section of an RRD header
97 * for lookup of data source names by index
98 * str: out string, memory is allocated by the function, must be freed by the
99 * the caller */
100 void rpn_compact2str(
101 rpn_cdefds_t *rpnc,
102 ds_def_t *ds_def,
103 char **str)
104 {
105 unsigned short i, offset = 0;
106 char buffer[7]; /* short as a string */
108 for (i = 0; rpnc[i].op != OP_END; i++) {
109 if (i > 0)
110 (*str)[offset++] = ',';
112 #define add_op(VV,VVV) \
113 if (addop2str((enum op_en)(rpnc[i].op), VV, VVV, str, &offset) == 1) continue;
115 if (rpnc[i].op == OP_NUMBER) {
116 /* convert a short into a string */
117 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
118 _itoa(rpnc[i].val, buffer, 10);
119 #else
120 sprintf(buffer, "%d", rpnc[i].val);
121 #endif
122 add_op(OP_NUMBER, buffer)
123 }
125 if (rpnc[i].op == OP_VARIABLE) {
126 char *ds_name = ds_def[rpnc[i].val].ds_nam;
128 add_op(OP_VARIABLE, ds_name)
129 }
131 if (rpnc[i].op == OP_PREV_OTHER) {
132 char *ds_name = ds_def[rpnc[i].val].ds_nam;
134 add_op(OP_VARIABLE, ds_name)
135 }
136 #undef add_op
138 #define add_op(VV,VVV) \
139 if (addop2str((enum op_en)rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
141 add_op(OP_ADD, +)
142 add_op(OP_SUB, -)
143 add_op(OP_MUL, *)
144 add_op(OP_DIV, /)
145 add_op(OP_MOD, %)
146 add_op(OP_SIN, SIN)
147 add_op(OP_COS, COS)
148 add_op(OP_LOG, LOG)
149 add_op(OP_FLOOR, FLOOR)
150 add_op(OP_CEIL, CEIL)
151 add_op(OP_EXP, EXP)
152 add_op(OP_DUP, DUP)
153 add_op(OP_EXC, EXC)
154 add_op(OP_POP, POP)
155 add_op(OP_LT, LT)
156 add_op(OP_LE, LE)
157 add_op(OP_GT, GT)
158 add_op(OP_GE, GE)
159 add_op(OP_EQ, EQ)
160 add_op(OP_IF, IF)
161 add_op(OP_MIN, MIN)
162 add_op(OP_MAX, MAX)
163 add_op(OP_LIMIT, LIMIT)
164 add_op(OP_UNKN, UNKN)
165 add_op(OP_UN, UN)
166 add_op(OP_NEGINF, NEGINF)
167 add_op(OP_NE, NE)
168 add_op(OP_PREV, PREV)
169 add_op(OP_INF, INF)
170 add_op(OP_ISINF, ISINF)
171 add_op(OP_NOW, NOW)
172 add_op(OP_LTIME, LTIME)
173 add_op(OP_TIME, TIME)
174 add_op(OP_ATAN2, ATAN2)
175 add_op(OP_ATAN, ATAN)
176 add_op(OP_SQRT, SQRT)
177 add_op(OP_SORT, SORT)
178 add_op(OP_REV, REV)
179 add_op(OP_TREND, TREND)
180 add_op(OP_TRENDNAN, TRENDNAN)
181 add_op(OP_PREDICT, PREDICT)
182 add_op(OP_PREDICTSIGMA, PREDICTSIGMA)
183 add_op(OP_RAD2DEG, RAD2DEG)
184 add_op(OP_DEG2RAD, DEG2RAD)
185 add_op(OP_AVG, AVG)
186 add_op(OP_ABS, ABS)
187 add_op(OP_ADDNAN, ADDNAN)
188 #undef add_op
189 }
190 (*str)[offset] = '\0';
192 }
194 short addop2str(
195 enum op_en op,
196 enum op_en op_type,
197 char *op_str,
198 char **result_str,
199 unsigned short *offset)
200 {
201 if (op == op_type) {
202 short op_len;
204 op_len = strlen(op_str);
205 *result_str = (char *) rrd_realloc(*result_str,
206 (op_len + 1 +
207 *offset) * sizeof(char));
208 if (*result_str == NULL) {
209 rrd_set_error("failed to alloc memory in addop2str");
210 return -1;
211 }
212 strncpy(&((*result_str)[*offset]), op_str, op_len);
213 *offset += op_len;
214 return 1;
215 }
216 return 0;
217 }
219 void parseCDEF_DS(
220 const char *def,
221 rrd_t *rrd,
222 int ds_idx)
223 {
224 rpnp_t *rpnp = NULL;
225 rpn_cdefds_t *rpnc = NULL;
226 short count, i;
228 rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
229 if (rpnp == NULL) {
230 rrd_set_error("failed to parse computed data source");
231 return;
232 }
233 /* Check for OP nodes not permitted in COMPUTE DS.
234 * Moved this check from within rpn_compact() because it really is
235 * COMPUTE DS specific. This is less efficient, but creation doesn't
236 * occur too often. */
237 for (i = 0; rpnp[i].op != OP_END; i++) {
238 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
239 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT ||
240 rpnp[i].op == OP_TREND || rpnp[i].op == OP_TRENDNAN ||
241 rpnp[i].op == OP_PREDICT || rpnp[i].op == OP_PREDICTSIGMA ) {
242 rrd_set_error
243 ("operators TIME, LTIME, PREV COUNT TREND TRENDNAN PREDICT PREDICTSIGMA are not supported with DS COMPUTE");
244 free(rpnp);
245 return;
246 }
247 }
248 if (rpn_compact(rpnp, &rpnc, &count) == -1) {
249 free(rpnp);
250 return;
251 }
252 /* copy the compact rpn representation over the ds_def par array */
253 memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
254 (void *) rpnc, count * sizeof(rpn_cdefds_t));
255 free(rpnp);
256 free(rpnc);
257 }
259 /* lookup a data source name in the rrd struct and return the index,
260 * should use ds_match() here except:
261 * (1) need a void * pointer to the rrd
262 * (2) error handling is left to the caller
263 */
264 long lookup_DS(
265 void *rrd_vptr,
266 char *ds_name)
267 {
268 unsigned int i;
269 rrd_t *rrd;
271 rrd = (rrd_t *) rrd_vptr;
273 for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
274 if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
275 return i;
276 }
277 /* the caller handles a bad data source name in the rpn string */
278 return -1;
279 }
281 /* rpn_parse : parse a string and generate a rpnp array; modified
282 * str2rpn() originally included in rrd_graph.c
283 * arguments:
284 * key_hash: a transparent argument passed to lookup(); conceptually this
285 * is a hash object for lookup of a numeric key given a variable name
286 * expr: the string RPN expression, including variable names
287 * lookup(): a function that retrieves a numeric key given a variable name
288 */
289 rpnp_t *rpn_parse(
290 void *key_hash,
291 const char *const expr_const,
292 long (*lookup) (void *,
293 char *))
294 {
295 int pos = 0;
296 char *expr;
297 long steps = -1;
298 rpnp_t *rpnp;
299 char vname[MAX_VNAME_LEN + 10];
300 char *old_locale;
302 old_locale = setlocale(LC_NUMERIC, NULL);
303 setlocale(LC_NUMERIC, "C");
305 rpnp = NULL;
306 expr = (char *) expr_const;
308 while (*expr) {
309 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
310 sizeof(rpnp_t))) == NULL) {
311 setlocale(LC_NUMERIC, old_locale);
312 return NULL;
313 }
315 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
316 && (expr[pos] == ',')) {
317 rpnp[steps].op = OP_NUMBER;
318 expr += pos;
319 }
320 #define match_op(VV,VVV) \
321 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
322 rpnp[steps].op = VV; \
323 expr+=strlen(#VVV); \
324 }
326 #define match_op_param(VV,VVV) \
327 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
328 int length = 0; \
329 if ((length = strlen(#VVV)+strlen(vname)+2, \
330 expr[length] == ',' || expr[length] == '\0') ) { \
331 rpnp[steps].op = VV; \
332 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
333 if (rpnp[steps].ptr < 0) { \
334 rrd_set_error("variable '%s' not found",vname);\
335 free(rpnp); \
336 return NULL; \
337 } else expr+=length; \
338 } \
339 }
341 match_op(OP_ADD, +)
342 match_op(OP_SUB, -)
343 match_op(OP_MUL, *)
344 match_op(OP_DIV, /)
345 match_op(OP_MOD, %)
346 match_op(OP_SIN, SIN)
347 match_op(OP_COS, COS)
348 match_op(OP_LOG, LOG)
349 match_op(OP_FLOOR, FLOOR)
350 match_op(OP_CEIL, CEIL)
351 match_op(OP_EXP, EXP)
352 match_op(OP_DUP, DUP)
353 match_op(OP_EXC, EXC)
354 match_op(OP_POP, POP)
355 match_op(OP_LTIME, LTIME)
356 match_op(OP_LT, LT)
357 match_op(OP_LE, LE)
358 match_op(OP_GT, GT)
359 match_op(OP_GE, GE)
360 match_op(OP_EQ, EQ)
361 match_op(OP_IF, IF)
362 match_op(OP_MIN, MIN)
363 match_op(OP_MAX, MAX)
364 match_op(OP_LIMIT, LIMIT)
365 /* order is important here ! .. match longest first */
366 match_op(OP_UNKN, UNKN)
367 match_op(OP_UN, UN)
368 match_op(OP_NEGINF, NEGINF)
369 match_op(OP_NE, NE)
370 match_op(OP_COUNT, COUNT)
371 match_op_param(OP_PREV_OTHER, PREV)
372 match_op(OP_PREV, PREV)
373 match_op(OP_INF, INF)
374 match_op(OP_ISINF, ISINF)
375 match_op(OP_NOW, NOW)
376 match_op(OP_TIME, TIME)
377 match_op(OP_ATAN2, ATAN2)
378 match_op(OP_ATAN, ATAN)
379 match_op(OP_SQRT, SQRT)
380 match_op(OP_SORT, SORT)
381 match_op(OP_REV, REV)
382 match_op(OP_TREND, TREND)
383 match_op(OP_TRENDNAN, TRENDNAN)
384 match_op(OP_PREDICT, PREDICT)
385 match_op(OP_PREDICTSIGMA, PREDICTSIGMA)
386 match_op(OP_RAD2DEG, RAD2DEG)
387 match_op(OP_DEG2RAD, DEG2RAD)
388 match_op(OP_AVG, AVG)
389 match_op(OP_ABS, ABS)
390 match_op(OP_ADDNAN, ADDNAN)
391 #undef match_op
392 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
393 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
394 -1)) {
395 rpnp[steps].op = OP_VARIABLE;
396 expr += pos;
397 }
399 else {
400 rrd_set_error("don't undestand '%s'",expr);
401 setlocale(LC_NUMERIC, old_locale);
402 free(rpnp);
403 return NULL;
404 }
406 if (*expr == 0)
407 break;
408 if (*expr == ',')
409 expr++;
410 else {
411 setlocale(LC_NUMERIC, old_locale);
412 free(rpnp);
413 return NULL;
414 }
415 }
416 rpnp[steps + 1].op = OP_END;
417 setlocale(LC_NUMERIC, old_locale);
418 return rpnp;
419 }
421 void rpnstack_init(
422 rpnstack_t *rpnstack)
423 {
424 rpnstack->s = NULL;
425 rpnstack->dc_stacksize = 0;
426 rpnstack->dc_stackblock = 100;
427 }
429 void rpnstack_free(
430 rpnstack_t *rpnstack)
431 {
432 if (rpnstack->s != NULL)
433 free(rpnstack->s);
434 rpnstack->dc_stacksize = 0;
435 }
437 static int rpn_compare_double(
438 const void *x,
439 const void *y)
440 {
441 double diff = *((const double *) x) - *((const double *) y);
443 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
444 }
446 /* rpn_calc: run the RPN calculator; also performs variable substitution;
447 * moved and modified from data_calc() originally included in rrd_graph.c
448 * arguments:
449 * rpnp : an array of RPN operators (including variable references)
450 * rpnstack : the initialized stack
451 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
452 * is advanced by rpnp.ds_cnt; used only for variable substitution
453 * output : an array of output values; OP_PREV assumes this array contains
454 * the "previous" value at index position output_idx-1; the definition of
455 * "previous" depends on the calling environment
456 * output_idx : an index into the output array in which to store the output
457 * of the RPN calculator
458 * returns: -1 if the computation failed (also calls rrd_set_error)
459 * 0 on success
460 */
461 short rpn_calc(
462 rpnp_t *rpnp,
463 rpnstack_t *rpnstack,
464 long data_idx,
465 rrd_value_t *output,
466 int output_idx)
467 {
468 int rpi;
469 long stptr = -1;
471 /* process each op from the rpn in turn */
472 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
473 /* allocate or grow the stack */
474 if (stptr + 5 > rpnstack->dc_stacksize) {
475 /* could move this to a separate function */
476 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
477 rpnstack->s = (double*)rrd_realloc(rpnstack->s,
478 (rpnstack->dc_stacksize) *
479 sizeof(*(rpnstack->s)));
480 if (rpnstack->s == NULL) {
481 rrd_set_error("RPN stack overflow");
482 return -1;
483 }
484 }
485 #define stackunderflow(MINSIZE) \
486 if(stptr<MINSIZE){ \
487 rrd_set_error("RPN stack underflow"); \
488 return -1; \
489 }
491 switch (rpnp[rpi].op) {
492 case OP_NUMBER:
493 rpnstack->s[++stptr] = rpnp[rpi].val;
494 break;
495 case OP_VARIABLE:
496 case OP_PREV_OTHER:
497 /* Sanity check: VDEFs shouldn't make it here */
498 if (rpnp[rpi].ds_cnt == 0) {
499 rrd_set_error("VDEF made it into rpn_calc... aborting");
500 return -1;
501 } else {
502 /* make sure we pull the correct value from
503 * the *.data array. Adjust the pointer into
504 * the array acordingly. Advance the ptr one
505 * row in the rra (skip over non-relevant
506 * data sources)
507 */
508 if (rpnp[rpi].op == OP_VARIABLE) {
509 rpnstack->s[++stptr] = *(rpnp[rpi].data);
510 } else {
511 if ((output_idx) <= 0) {
512 rpnstack->s[++stptr] = DNAN;
513 } else {
514 rpnstack->s[++stptr] =
515 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
516 }
518 }
519 if (data_idx % rpnp[rpi].step == 0) {
520 rpnp[rpi].data += rpnp[rpi].ds_cnt;
521 }
522 }
523 break;
524 case OP_COUNT:
525 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
526 break;
527 case OP_PREV:
528 if ((output_idx) <= 0) {
529 rpnstack->s[++stptr] = DNAN;
530 } else {
531 rpnstack->s[++stptr] = output[output_idx - 1];
532 }
533 break;
534 case OP_UNKN:
535 rpnstack->s[++stptr] = DNAN;
536 break;
537 case OP_INF:
538 rpnstack->s[++stptr] = DINF;
539 break;
540 case OP_NEGINF:
541 rpnstack->s[++stptr] = -DINF;
542 break;
543 case OP_NOW:
544 rpnstack->s[++stptr] = (double) time(NULL);
545 break;
546 case OP_TIME:
547 /* HACK: this relies on the data_idx being the time,
548 ** which the within-function scope is unaware of */
549 rpnstack->s[++stptr] = (double) data_idx;
550 break;
551 case OP_LTIME:
552 rpnstack->s[++stptr] =
553 (double) tzoffset(data_idx) + (double) data_idx;
554 break;
555 case OP_ADD:
556 stackunderflow(1);
557 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
558 + rpnstack->s[stptr];
559 stptr--;
560 break;
561 case OP_ADDNAN:
562 stackunderflow(1);
563 if (isnan(rpnstack->s[stptr - 1])) {
564 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
565 } else if (isnan(rpnstack->s[stptr])) {
566 /* NOOP */
567 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
568 } else {
569 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
570 + rpnstack->s[stptr];
571 }
573 stptr--;
574 break;
575 case OP_SUB:
576 stackunderflow(1);
577 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
578 - rpnstack->s[stptr];
579 stptr--;
580 break;
581 case OP_MUL:
582 stackunderflow(1);
583 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
584 * (rpnstack->s[stptr]);
585 stptr--;
586 break;
587 case OP_DIV:
588 stackunderflow(1);
589 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
590 / rpnstack->s[stptr];
591 stptr--;
592 break;
593 case OP_MOD:
594 stackunderflow(1);
595 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
596 , rpnstack->s[stptr]);
597 stptr--;
598 break;
599 case OP_SIN:
600 stackunderflow(0);
601 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
602 break;
603 case OP_ATAN:
604 stackunderflow(0);
605 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
606 break;
607 case OP_RAD2DEG:
608 stackunderflow(0);
609 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
610 break;
611 case OP_DEG2RAD:
612 stackunderflow(0);
613 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
614 break;
615 case OP_ATAN2:
616 stackunderflow(1);
617 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
618 rpnstack->s[stptr]);
619 stptr--;
620 break;
621 case OP_COS:
622 stackunderflow(0);
623 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
624 break;
625 case OP_CEIL:
626 stackunderflow(0);
627 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
628 break;
629 case OP_FLOOR:
630 stackunderflow(0);
631 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
632 break;
633 case OP_LOG:
634 stackunderflow(0);
635 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
636 break;
637 case OP_DUP:
638 stackunderflow(0);
639 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
640 stptr++;
641 break;
642 case OP_POP:
643 stackunderflow(0);
644 stptr--;
645 break;
646 case OP_EXC:
647 stackunderflow(1);
648 {
649 double dummy;
651 dummy = rpnstack->s[stptr];
652 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
653 rpnstack->s[stptr - 1] = dummy;
654 }
655 break;
656 case OP_EXP:
657 stackunderflow(0);
658 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
659 break;
660 case OP_LT:
661 stackunderflow(1);
662 if (isnan(rpnstack->s[stptr - 1]));
663 else if (isnan(rpnstack->s[stptr]))
664 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
665 else
666 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
667 rpnstack->s[stptr] ? 1.0 : 0.0;
668 stptr--;
669 break;
670 case OP_LE:
671 stackunderflow(1);
672 if (isnan(rpnstack->s[stptr - 1]));
673 else if (isnan(rpnstack->s[stptr]))
674 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
675 else
676 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
677 rpnstack->s[stptr] ? 1.0 : 0.0;
678 stptr--;
679 break;
680 case OP_GT:
681 stackunderflow(1);
682 if (isnan(rpnstack->s[stptr - 1]));
683 else if (isnan(rpnstack->s[stptr]))
684 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
685 else
686 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
687 rpnstack->s[stptr] ? 1.0 : 0.0;
688 stptr--;
689 break;
690 case OP_GE:
691 stackunderflow(1);
692 if (isnan(rpnstack->s[stptr - 1]));
693 else if (isnan(rpnstack->s[stptr]))
694 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
695 else
696 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
697 rpnstack->s[stptr] ? 1.0 : 0.0;
698 stptr--;
699 break;
700 case OP_NE:
701 stackunderflow(1);
702 if (isnan(rpnstack->s[stptr - 1]));
703 else if (isnan(rpnstack->s[stptr]))
704 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
705 else
706 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
707 rpnstack->s[stptr] ? 0.0 : 1.0;
708 stptr--;
709 break;
710 case OP_EQ:
711 stackunderflow(1);
712 if (isnan(rpnstack->s[stptr - 1]));
713 else if (isnan(rpnstack->s[stptr]))
714 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
715 else
716 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
717 rpnstack->s[stptr] ? 1.0 : 0.0;
718 stptr--;
719 break;
720 case OP_IF:
721 stackunderflow(2);
722 rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
723 || rpnstack->s[stptr - 2] ==
724 0.0) ? rpnstack->s[stptr] : rpnstack->
725 s[stptr - 1];
726 stptr--;
727 stptr--;
728 break;
729 case OP_MIN:
730 stackunderflow(1);
731 if (isnan(rpnstack->s[stptr - 1]));
732 else if (isnan(rpnstack->s[stptr]))
733 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
734 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
735 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
736 stptr--;
737 break;
738 case OP_MAX:
739 stackunderflow(1);
740 if (isnan(rpnstack->s[stptr - 1]));
741 else if (isnan(rpnstack->s[stptr]))
742 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
743 else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
744 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
745 stptr--;
746 break;
747 case OP_LIMIT:
748 stackunderflow(2);
749 if (isnan(rpnstack->s[stptr - 2]));
750 else if (isnan(rpnstack->s[stptr - 1]))
751 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
752 else if (isnan(rpnstack->s[stptr]))
753 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
754 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
755 rpnstack->s[stptr - 2] = DNAN;
756 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
757 rpnstack->s[stptr - 2] = DNAN;
758 stptr -= 2;
759 break;
760 case OP_UN:
761 stackunderflow(0);
762 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
763 break;
764 case OP_ISINF:
765 stackunderflow(0);
766 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
767 break;
768 case OP_SQRT:
769 stackunderflow(0);
770 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
771 break;
772 case OP_SORT:
773 stackunderflow(0);
774 {
775 int spn = (int) rpnstack->s[stptr--];
777 stackunderflow(spn - 1);
778 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
779 rpn_compare_double);
780 }
781 break;
782 case OP_REV:
783 stackunderflow(0);
784 {
785 int spn = (int) rpnstack->s[stptr--];
786 double *p, *q;
788 stackunderflow(spn - 1);
790 p = rpnstack->s + stptr - spn + 1;
791 q = rpnstack->s + stptr;
792 while (p < q) {
793 double x = *q;
795 *q-- = *p;
796 *p++ = x;
797 }
798 }
799 break;
800 case OP_PREDICT:
801 case OP_PREDICTSIGMA:
802 stackunderflow(2);
803 {
804 /* the local averaging window (similar to trend, but better here, as we get better statistics thru numbers)*/
805 int locstepsize = rpnstack->s[--stptr];
806 /* the number of shifts and range-checking*/
807 int shifts = rpnstack->s[--stptr];
808 stackunderflow(shifts);
809 // handle negative shifts special
810 if (shifts<0) {
811 stptr--;
812 } else {
813 stptr-=shifts;
814 }
815 /* the real calculation */
816 double val=DNAN;
817 /* the info on the datasource */
818 time_t dsstep = (time_t) rpnp[rpi - 1].step;
819 int dscount = rpnp[rpi - 1].ds_cnt;
820 int locstep = (int)ceil((float)locstepsize/(float)dsstep);
822 /* the sums */
823 double sum = 0;
824 double sum2 = 0;
825 int count = 0;
826 /* now loop for each position */
827 int doshifts=shifts;
828 if (shifts<0) { doshifts=-shifts; }
829 for(int loop=0;loop<doshifts;loop++) {
830 /* calculate shift step */
831 int shiftstep=1;
832 if (shifts<0) {
833 shiftstep = loop*rpnstack->s[stptr];
834 } else {
835 shiftstep = rpnstack->s[stptr+loop];
836 }
837 if(shiftstep <0) {
838 rrd_set_error("negative shift step not allowed: %i",shiftstep);
839 return -1;
840 }
841 shiftstep=(int)ceil((float)shiftstep/(float)dsstep);
842 /* loop all local shifts */
843 for(int i=0;i<=locstep;i++) {
844 /* now calculate offset into data-array - relative to output_idx*/
845 int offset=shiftstep+i;
846 /* and process if we have index 0 of above */
847 if ((offset>=0)&&(offset<output_idx)) {
848 /* get the value */
849 val =rpnp[rpi - 1].data[-dscount * offset];
850 /* and handle the non NAN case only*/
851 if (! isnan(val)) {
852 sum+=val;
853 sum2+=val*val;
854 count++;
855 }
856 }
857 }
858 }
859 /* do the final calculations */
860 val=DNAN;
861 if (rpnp[rpi].op == OP_PREDICT) { /* the average */
862 if (count>0) {
863 val = sum/(double)count;
864 }
865 } else {
866 if (count>1) { /* the sigma case */
867 val=count*sum2-sum*sum;
868 if (val<0) {
869 val=DNAN;
870 } else {
871 val=sqrt(val/((float)count*((float)count-1.0)));
872 }
873 }
874 }
875 rpnstack->s[stptr] = val;
876 }
877 break;
878 case OP_TREND:
879 case OP_TRENDNAN:
880 stackunderflow(1);
881 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
882 rrd_set_error("malformed trend arguments");
883 return -1;
884 } else {
885 time_t dur = (time_t) rpnstack->s[stptr];
886 time_t step = (time_t) rpnp[rpi - 2].step;
888 if (output_idx + 1 >= (int) ceil((float) dur / (float) step)) {
889 int ignorenan = (rpnp[rpi].op == OP_TREND);
890 double accum = 0.0;
891 int i = -1; /* pick the current entries, not the next one
892 as the data pointer has already been forwarded
893 when the OP_VARIABLE was processed */
894 int count = 0;
896 do {
897 double val =
898 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
899 if (ignorenan || !isnan(val)) {
900 accum += val;
901 ++count;
902 }
904 dur -= step;
905 } while (dur > 0);
907 rpnstack->s[--stptr] =
908 (count == 0) ? DNAN : (accum / count);
909 } else
910 rpnstack->s[--stptr] = DNAN;
911 }
912 break;
913 case OP_AVG:
914 stackunderflow(0);
915 {
916 int i = (int) rpnstack->s[stptr--];
917 double sum = 0;
918 int count = 0;
920 stackunderflow(i - 1);
921 while (i > 0) {
922 double val = rpnstack->s[stptr--];
924 i--;
925 if (isnan(val)) {
926 continue;
927 }
928 count++;
929 sum += val;
930 }
931 /* now push the result back on stack */
932 if (count > 0) {
933 rpnstack->s[++stptr] = sum / count;
934 } else {
935 rpnstack->s[++stptr] = DNAN;
936 }
937 }
938 break;
939 case OP_ABS:
940 stackunderflow(0);
941 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
942 break;
943 case OP_END:
944 break;
945 }
946 #undef stackunderflow
947 }
948 if (stptr != 0) {
949 rrd_set_error("RPN final stack size != 1");
950 return -1;
951 }
953 output[output_idx] = rpnstack->s[0];
954 return 0;
955 }
957 /* figure out what the local timezone offset for any point in
958 time was. Return it in seconds */
959 int tzoffset(
960 time_t now)
961 {
962 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
963 l_sec, l_min, l_hour, l_yday, l_year;
964 struct tm t;
965 int off;
967 gmtime_r(&now, &t);
968 gm_sec = t.tm_sec;
969 gm_min = t.tm_min;
970 gm_hour = t.tm_hour;
971 gm_yday = t.tm_yday;
972 gm_year = t.tm_year;
973 localtime_r(&now, &t);
974 l_sec = t.tm_sec;
975 l_min = t.tm_min;
976 l_hour = t.tm_hour;
977 l_yday = t.tm_yday;
978 l_year = t.tm_year;
979 off =
980 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
981 if (l_yday > gm_yday || l_year > gm_year) {
982 off += 24 * 3600;
983 } else if (l_yday < gm_yday || l_year < gm_year) {
984 off -= 24 * 3600;
985 }
986 return off;
987 }