1 /****************************************************************************
2 * RRDtool 1.4.5 Copyright by Tobi Oetiker, 1997-2010
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 free(rpnp); \
335 return NULL; \
336 } else expr+=length; \
337 } \
338 }
340 match_op(OP_ADD, +)
341 match_op(OP_SUB, -)
342 match_op(OP_MUL, *)
343 match_op(OP_DIV, /)
344 match_op(OP_MOD, %)
345 match_op(OP_SIN, SIN)
346 match_op(OP_COS, COS)
347 match_op(OP_LOG, LOG)
348 match_op(OP_FLOOR, FLOOR)
349 match_op(OP_CEIL, CEIL)
350 match_op(OP_EXP, EXP)
351 match_op(OP_DUP, DUP)
352 match_op(OP_EXC, EXC)
353 match_op(OP_POP, POP)
354 match_op(OP_LTIME, LTIME)
355 match_op(OP_LT, LT)
356 match_op(OP_LE, LE)
357 match_op(OP_GT, GT)
358 match_op(OP_GE, GE)
359 match_op(OP_EQ, EQ)
360 match_op(OP_IF, IF)
361 match_op(OP_MIN, MIN)
362 match_op(OP_MAX, MAX)
363 match_op(OP_LIMIT, LIMIT)
364 /* order is important here ! .. match longest first */
365 match_op(OP_UNKN, UNKN)
366 match_op(OP_UN, UN)
367 match_op(OP_NEGINF, NEGINF)
368 match_op(OP_NE, NE)
369 match_op(OP_COUNT, COUNT)
370 match_op_param(OP_PREV_OTHER, PREV)
371 match_op(OP_PREV, PREV)
372 match_op(OP_INF, INF)
373 match_op(OP_ISINF, ISINF)
374 match_op(OP_NOW, NOW)
375 match_op(OP_TIME, TIME)
376 match_op(OP_ATAN2, ATAN2)
377 match_op(OP_ATAN, ATAN)
378 match_op(OP_SQRT, SQRT)
379 match_op(OP_SORT, SORT)
380 match_op(OP_REV, REV)
381 match_op(OP_TREND, TREND)
382 match_op(OP_TRENDNAN, TRENDNAN)
383 match_op(OP_PREDICT, PREDICT)
384 match_op(OP_PREDICTSIGMA, PREDICTSIGMA)
385 match_op(OP_RAD2DEG, RAD2DEG)
386 match_op(OP_DEG2RAD, DEG2RAD)
387 match_op(OP_AVG, AVG)
388 match_op(OP_ABS, ABS)
389 match_op(OP_ADDNAN, ADDNAN)
390 #undef match_op
391 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
392 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
393 -1)) {
394 rpnp[steps].op = OP_VARIABLE;
395 expr += pos;
396 }
398 else {
399 setlocale(LC_NUMERIC, old_locale);
400 free(rpnp);
401 return NULL;
402 }
404 if (*expr == 0)
405 break;
406 if (*expr == ',')
407 expr++;
408 else {
409 setlocale(LC_NUMERIC, old_locale);
410 free(rpnp);
411 return NULL;
412 }
413 }
414 rpnp[steps + 1].op = OP_END;
415 setlocale(LC_NUMERIC, old_locale);
416 return rpnp;
417 }
419 void rpnstack_init(
420 rpnstack_t *rpnstack)
421 {
422 rpnstack->s = NULL;
423 rpnstack->dc_stacksize = 0;
424 rpnstack->dc_stackblock = 100;
425 }
427 void rpnstack_free(
428 rpnstack_t *rpnstack)
429 {
430 if (rpnstack->s != NULL)
431 free(rpnstack->s);
432 rpnstack->dc_stacksize = 0;
433 }
435 static int rpn_compare_double(
436 const void *x,
437 const void *y)
438 {
439 double diff = *((const double *) x) - *((const double *) y);
441 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
442 }
444 /* rpn_calc: run the RPN calculator; also performs variable substitution;
445 * moved and modified from data_calc() originally included in rrd_graph.c
446 * arguments:
447 * rpnp : an array of RPN operators (including variable references)
448 * rpnstack : the initialized stack
449 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
450 * is advanced by rpnp.ds_cnt; used only for variable substitution
451 * output : an array of output values; OP_PREV assumes this array contains
452 * the "previous" value at index position output_idx-1; the definition of
453 * "previous" depends on the calling environment
454 * output_idx : an index into the output array in which to store the output
455 * of the RPN calculator
456 * returns: -1 if the computation failed (also calls rrd_set_error)
457 * 0 on success
458 */
459 short rpn_calc(
460 rpnp_t *rpnp,
461 rpnstack_t *rpnstack,
462 long data_idx,
463 rrd_value_t *output,
464 int output_idx)
465 {
466 int rpi;
467 long stptr = -1;
469 /* process each op from the rpn in turn */
470 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
471 /* allocate or grow the stack */
472 if (stptr + 5 > rpnstack->dc_stacksize) {
473 /* could move this to a separate function */
474 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
475 rpnstack->s = (double*)rrd_realloc(rpnstack->s,
476 (rpnstack->dc_stacksize) *
477 sizeof(*(rpnstack->s)));
478 if (rpnstack->s == NULL) {
479 rrd_set_error("RPN stack overflow");
480 return -1;
481 }
482 }
483 #define stackunderflow(MINSIZE) \
484 if(stptr<MINSIZE){ \
485 rrd_set_error("RPN stack underflow"); \
486 return -1; \
487 }
489 switch (rpnp[rpi].op) {
490 case OP_NUMBER:
491 rpnstack->s[++stptr] = rpnp[rpi].val;
492 break;
493 case OP_VARIABLE:
494 case OP_PREV_OTHER:
495 /* Sanity check: VDEFs shouldn't make it here */
496 if (rpnp[rpi].ds_cnt == 0) {
497 rrd_set_error("VDEF made it into rpn_calc... aborting");
498 return -1;
499 } else {
500 /* make sure we pull the correct value from
501 * the *.data array. Adjust the pointer into
502 * the array acordingly. Advance the ptr one
503 * row in the rra (skip over non-relevant
504 * data sources)
505 */
506 if (rpnp[rpi].op == OP_VARIABLE) {
507 rpnstack->s[++stptr] = *(rpnp[rpi].data);
508 } else {
509 if ((output_idx) <= 0) {
510 rpnstack->s[++stptr] = DNAN;
511 } else {
512 rpnstack->s[++stptr] =
513 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
514 }
516 }
517 if (data_idx % rpnp[rpi].step == 0) {
518 rpnp[rpi].data += rpnp[rpi].ds_cnt;
519 }
520 }
521 break;
522 case OP_COUNT:
523 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
524 break;
525 case OP_PREV:
526 if ((output_idx) <= 0) {
527 rpnstack->s[++stptr] = DNAN;
528 } else {
529 rpnstack->s[++stptr] = output[output_idx - 1];
530 }
531 break;
532 case OP_UNKN:
533 rpnstack->s[++stptr] = DNAN;
534 break;
535 case OP_INF:
536 rpnstack->s[++stptr] = DINF;
537 break;
538 case OP_NEGINF:
539 rpnstack->s[++stptr] = -DINF;
540 break;
541 case OP_NOW:
542 rpnstack->s[++stptr] = (double) time(NULL);
543 break;
544 case OP_TIME:
545 /* HACK: this relies on the data_idx being the time,
546 ** which the within-function scope is unaware of */
547 rpnstack->s[++stptr] = (double) data_idx;
548 break;
549 case OP_LTIME:
550 rpnstack->s[++stptr] =
551 (double) tzoffset(data_idx) + (double) data_idx;
552 break;
553 case OP_ADD:
554 stackunderflow(1);
555 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
556 + rpnstack->s[stptr];
557 stptr--;
558 break;
559 case OP_ADDNAN:
560 stackunderflow(1);
561 if (isnan(rpnstack->s[stptr - 1])) {
562 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
563 } else if (isnan(rpnstack->s[stptr])) {
564 /* NOOP */
565 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
566 } else {
567 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
568 + rpnstack->s[stptr];
569 }
571 stptr--;
572 break;
573 case OP_SUB:
574 stackunderflow(1);
575 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
576 - rpnstack->s[stptr];
577 stptr--;
578 break;
579 case OP_MUL:
580 stackunderflow(1);
581 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
582 * (rpnstack->s[stptr]);
583 stptr--;
584 break;
585 case OP_DIV:
586 stackunderflow(1);
587 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
588 / rpnstack->s[stptr];
589 stptr--;
590 break;
591 case OP_MOD:
592 stackunderflow(1);
593 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
594 , rpnstack->s[stptr]);
595 stptr--;
596 break;
597 case OP_SIN:
598 stackunderflow(0);
599 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
600 break;
601 case OP_ATAN:
602 stackunderflow(0);
603 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
604 break;
605 case OP_RAD2DEG:
606 stackunderflow(0);
607 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
608 break;
609 case OP_DEG2RAD:
610 stackunderflow(0);
611 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
612 break;
613 case OP_ATAN2:
614 stackunderflow(1);
615 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
616 rpnstack->s[stptr]);
617 stptr--;
618 break;
619 case OP_COS:
620 stackunderflow(0);
621 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
622 break;
623 case OP_CEIL:
624 stackunderflow(0);
625 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
626 break;
627 case OP_FLOOR:
628 stackunderflow(0);
629 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
630 break;
631 case OP_LOG:
632 stackunderflow(0);
633 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
634 break;
635 case OP_DUP:
636 stackunderflow(0);
637 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
638 stptr++;
639 break;
640 case OP_POP:
641 stackunderflow(0);
642 stptr--;
643 break;
644 case OP_EXC:
645 stackunderflow(1);
646 {
647 double dummy;
649 dummy = rpnstack->s[stptr];
650 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
651 rpnstack->s[stptr - 1] = dummy;
652 }
653 break;
654 case OP_EXP:
655 stackunderflow(0);
656 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
657 break;
658 case OP_LT:
659 stackunderflow(1);
660 if (isnan(rpnstack->s[stptr - 1]));
661 else if (isnan(rpnstack->s[stptr]))
662 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
663 else
664 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
665 rpnstack->s[stptr] ? 1.0 : 0.0;
666 stptr--;
667 break;
668 case OP_LE:
669 stackunderflow(1);
670 if (isnan(rpnstack->s[stptr - 1]));
671 else if (isnan(rpnstack->s[stptr]))
672 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
673 else
674 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
675 rpnstack->s[stptr] ? 1.0 : 0.0;
676 stptr--;
677 break;
678 case OP_GT:
679 stackunderflow(1);
680 if (isnan(rpnstack->s[stptr - 1]));
681 else if (isnan(rpnstack->s[stptr]))
682 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
683 else
684 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
685 rpnstack->s[stptr] ? 1.0 : 0.0;
686 stptr--;
687 break;
688 case OP_GE:
689 stackunderflow(1);
690 if (isnan(rpnstack->s[stptr - 1]));
691 else if (isnan(rpnstack->s[stptr]))
692 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
693 else
694 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
695 rpnstack->s[stptr] ? 1.0 : 0.0;
696 stptr--;
697 break;
698 case OP_NE:
699 stackunderflow(1);
700 if (isnan(rpnstack->s[stptr - 1]));
701 else if (isnan(rpnstack->s[stptr]))
702 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
703 else
704 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
705 rpnstack->s[stptr] ? 0.0 : 1.0;
706 stptr--;
707 break;
708 case OP_EQ:
709 stackunderflow(1);
710 if (isnan(rpnstack->s[stptr - 1]));
711 else if (isnan(rpnstack->s[stptr]))
712 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
713 else
714 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
715 rpnstack->s[stptr] ? 1.0 : 0.0;
716 stptr--;
717 break;
718 case OP_IF:
719 stackunderflow(2);
720 rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
721 || rpnstack->s[stptr - 2] ==
722 0.0) ? rpnstack->s[stptr] : rpnstack->
723 s[stptr - 1];
724 stptr--;
725 stptr--;
726 break;
727 case OP_MIN:
728 stackunderflow(1);
729 if (isnan(rpnstack->s[stptr - 1]));
730 else if (isnan(rpnstack->s[stptr]))
731 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
732 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
733 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
734 stptr--;
735 break;
736 case OP_MAX:
737 stackunderflow(1);
738 if (isnan(rpnstack->s[stptr - 1]));
739 else if (isnan(rpnstack->s[stptr]))
740 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
741 else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
742 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
743 stptr--;
744 break;
745 case OP_LIMIT:
746 stackunderflow(2);
747 if (isnan(rpnstack->s[stptr - 2]));
748 else if (isnan(rpnstack->s[stptr - 1]))
749 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
750 else if (isnan(rpnstack->s[stptr]))
751 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
752 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
753 rpnstack->s[stptr - 2] = DNAN;
754 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
755 rpnstack->s[stptr - 2] = DNAN;
756 stptr -= 2;
757 break;
758 case OP_UN:
759 stackunderflow(0);
760 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
761 break;
762 case OP_ISINF:
763 stackunderflow(0);
764 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
765 break;
766 case OP_SQRT:
767 stackunderflow(0);
768 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
769 break;
770 case OP_SORT:
771 stackunderflow(0);
772 {
773 int spn = (int) rpnstack->s[stptr--];
775 stackunderflow(spn - 1);
776 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
777 rpn_compare_double);
778 }
779 break;
780 case OP_REV:
781 stackunderflow(0);
782 {
783 int spn = (int) rpnstack->s[stptr--];
784 double *p, *q;
786 stackunderflow(spn - 1);
788 p = rpnstack->s + stptr - spn + 1;
789 q = rpnstack->s + stptr;
790 while (p < q) {
791 double x = *q;
793 *q-- = *p;
794 *p++ = x;
795 }
796 }
797 break;
798 case OP_PREDICT:
799 case OP_PREDICTSIGMA:
800 stackunderflow(2);
801 {
802 /* the local averaging window (similar to trend, but better here, as we get better statistics thru numbers)*/
803 int locstepsize = rpnstack->s[--stptr];
804 /* the number of shifts and range-checking*/
805 int shifts = rpnstack->s[--stptr];
806 stackunderflow(shifts);
807 // handle negative shifts special
808 if (shifts<0) {
809 stptr--;
810 } else {
811 stptr-=shifts;
812 }
813 /* the real calculation */
814 double val=DNAN;
815 /* the info on the datasource */
816 time_t dsstep = (time_t) rpnp[rpi - 1].step;
817 int dscount = rpnp[rpi - 1].ds_cnt;
818 int locstep = (int)ceil((float)locstepsize/(float)dsstep);
820 /* the sums */
821 double sum = 0;
822 double sum2 = 0;
823 int count = 0;
824 /* now loop for each position */
825 int doshifts=shifts;
826 if (shifts<0) { doshifts=-shifts; }
827 for(int loop=0;loop<doshifts;loop++) {
828 /* calculate shift step */
829 int shiftstep=1;
830 if (shifts<0) {
831 shiftstep = loop*rpnstack->s[stptr];
832 } else {
833 shiftstep = rpnstack->s[stptr+loop];
834 }
835 if(shiftstep <0) {
836 rrd_set_error("negative shift step not allowed: %i",shiftstep);
837 return -1;
838 }
839 shiftstep=(int)ceil((float)shiftstep/(float)dsstep);
840 /* loop all local shifts */
841 for(int i=0;i<=locstep;i++) {
842 /* now calculate offset into data-array - relative to output_idx*/
843 int offset=shiftstep+i;
844 /* and process if we have index 0 of above */
845 if ((offset>=0)&&(offset<output_idx)) {
846 /* get the value */
847 val =rpnp[rpi - 1].data[-dscount * offset];
848 /* and handle the non NAN case only*/
849 if (! isnan(val)) {
850 sum+=val;
851 sum2+=val*val;
852 count++;
853 }
854 }
855 }
856 }
857 /* do the final calculations */
858 val=DNAN;
859 if (rpnp[rpi].op == OP_PREDICT) { /* the average */
860 if (count>0) {
861 val = sum/(double)count;
862 }
863 } else {
864 if (count>1) { /* the sigma case */
865 val=count*sum2-sum*sum;
866 if (val<0) {
867 val=DNAN;
868 } else {
869 val=sqrt(val/((float)count*((float)count-1.0)));
870 }
871 }
872 }
873 rpnstack->s[stptr] = val;
874 }
875 break;
876 case OP_TREND:
877 case OP_TRENDNAN:
878 stackunderflow(1);
879 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
880 rrd_set_error("malformed trend arguments");
881 return -1;
882 } else {
883 time_t dur = (time_t) rpnstack->s[stptr];
884 time_t step = (time_t) rpnp[rpi - 2].step;
886 if (output_idx + 1 >= (int) ceil((float) dur / (float) step)) {
887 int ignorenan = (rpnp[rpi].op == OP_TREND);
888 double accum = 0.0;
889 int i = 0;
890 int count = 0;
892 do {
893 double val =
894 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
895 if (ignorenan || !isnan(val)) {
896 accum += val;
897 ++count;
898 }
900 dur -= step;
901 } while (dur > 0);
903 rpnstack->s[--stptr] =
904 (count == 0) ? DNAN : (accum / count);
905 } else
906 rpnstack->s[--stptr] = DNAN;
907 }
908 break;
909 case OP_AVG:
910 stackunderflow(0);
911 {
912 int i = (int) rpnstack->s[stptr--];
913 double sum = 0;
914 int count = 0;
916 stackunderflow(i - 1);
917 while (i > 0) {
918 double val = rpnstack->s[stptr--];
920 i--;
921 if (isnan(val)) {
922 continue;
923 }
924 count++;
925 sum += val;
926 }
927 /* now push the result back on stack */
928 if (count > 0) {
929 rpnstack->s[++stptr] = sum / count;
930 } else {
931 rpnstack->s[++stptr] = DNAN;
932 }
933 }
934 break;
935 case OP_ABS:
936 stackunderflow(0);
937 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
938 break;
939 case OP_END:
940 break;
941 }
942 #undef stackunderflow
943 }
944 if (stptr != 0) {
945 rrd_set_error("RPN final stack size != 1");
946 return -1;
947 }
949 output[output_idx] = rpnstack->s[0];
950 return 0;
951 }
953 /* figure out what the local timezone offset for any point in
954 time was. Return it in seconds */
955 int tzoffset(
956 time_t now)
957 {
958 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
959 l_sec, l_min, l_hour, l_yday, l_year;
960 struct tm t;
961 int off;
963 gmtime_r(&now, &t);
964 gm_sec = t.tm_sec;
965 gm_min = t.tm_min;
966 gm_hour = t.tm_hour;
967 gm_yday = t.tm_yday;
968 gm_year = t.tm_year;
969 localtime_r(&now, &t);
970 l_sec = t.tm_sec;
971 l_min = t.tm_min;
972 l_hour = t.tm_hour;
973 l_yday = t.tm_yday;
974 l_year = t.tm_year;
975 off =
976 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
977 if (l_yday > gm_yday || l_year > gm_year) {
978 off += 24 * 3600;
979 } else if (l_yday < gm_yday || l_year < gm_year) {
980 off -= 24 * 3600;
981 }
982 return off;
983 }