1 /****************************************************************************
2 * RRDtool 1.3.9 Copyright by Tobi Oetiker, 1997-2009
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 // #include "rrd_graph.h"
10 #include <limits.h>
11 #include <locale.h>
13 #ifdef WIN32
14 #include <stdlib.h>
15 #endif
17 short addop2str(
18 enum op_en op,
19 enum op_en op_type,
20 char *op_str,
21 char **result_str,
22 unsigned short *offset);
23 int tzoffset(
24 time_t); /* used to implement LTIME */
26 short rpn_compact(
27 rpnp_t *rpnp,
28 rpn_cdefds_t **rpnc,
29 short *count)
30 {
31 short i;
33 *count = 0;
34 /* count the number of rpn nodes */
35 while (rpnp[*count].op != OP_END)
36 (*count)++;
37 if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
38 rrd_set_error("Maximum %d RPN nodes permitted",
39 DS_CDEF_MAX_RPN_NODES);
40 return -1;
41 }
43 /* allocate memory */
44 *rpnc = (rpn_cdefds_t *) calloc(*count, sizeof(rpn_cdefds_t));
45 for (i = 0; rpnp[i].op != OP_END; i++) {
46 (*rpnc)[i].op = (char) rpnp[i].op;
47 if (rpnp[i].op == OP_NUMBER) {
48 /* rpnp.val is a double, rpnc.val is a short */
49 double temp = floor(rpnp[i].val);
51 if (temp < SHRT_MIN || temp > SHRT_MAX) {
52 rrd_set_error
53 ("constants must be integers in the interval (%d, %d)",
54 SHRT_MIN, SHRT_MAX);
55 free(*rpnc);
56 return -1;
57 }
58 (*rpnc)[i].val = (short) temp;
59 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
60 (*rpnc)[i].val = (short) rpnp[i].ptr;
61 }
62 }
63 /* terminate the sequence */
64 (*rpnc)[(*count) - 1].op = OP_END;
65 return 0;
66 }
68 rpnp_t *rpn_expand(
69 rpn_cdefds_t *rpnc)
70 {
71 short i;
72 rpnp_t *rpnp;
74 /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
75 * memory we avoid any reallocs */
76 rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES, sizeof(rpnp_t));
77 if (rpnp == NULL)
78 return NULL;
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_RAD2DEG, RAD2DEG)
182 add_op(OP_DEG2RAD, DEG2RAD)
183 add_op(OP_AVG, AVG)
184 add_op(OP_ABS, ABS)
185 add_op(OP_ADDNAN, ADDNAN)
186 #undef add_op
187 }
188 (*str)[offset] = '\0';
190 }
192 short addop2str(
193 enum op_en op,
194 enum op_en op_type,
195 char *op_str,
196 char **result_str,
197 unsigned short *offset)
198 {
199 if (op == op_type) {
200 short op_len;
202 op_len = strlen(op_str);
203 *result_str = (char *) rrd_realloc(*result_str,
204 (op_len + 1 +
205 *offset) * sizeof(char));
206 if (*result_str == NULL) {
207 rrd_set_error("failed to alloc memory in addop2str");
208 return -1;
209 }
210 strncpy(&((*result_str)[*offset]), op_str, op_len);
211 *offset += op_len;
212 return 1;
213 }
214 return 0;
215 }
217 void parseCDEF_DS(
218 const char *def,
219 rrd_t *rrd,
220 int ds_idx)
221 {
222 rpnp_t *rpnp = NULL;
223 rpn_cdefds_t *rpnc = NULL;
224 short count, i;
226 rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
227 if (rpnp == NULL) {
228 rrd_set_error("failed to parse computed data source");
229 return;
230 }
231 /* Check for OP nodes not permitted in COMPUTE DS.
232 * Moved this check from within rpn_compact() because it really is
233 * COMPUTE DS specific. This is less efficient, but creation doesn't
234 * occur too often. */
235 for (i = 0; rpnp[i].op != OP_END; i++) {
236 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
237 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT) {
238 rrd_set_error
239 ("operators time, ltime, prev and count not supported with DS COMPUTE");
240 free(rpnp);
241 return;
242 }
243 }
244 if (rpn_compact(rpnp, &rpnc, &count) == -1) {
245 free(rpnp);
246 return;
247 }
248 /* copy the compact rpn representation over the ds_def par array */
249 memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
250 (void *) rpnc, count * sizeof(rpn_cdefds_t));
251 free(rpnp);
252 free(rpnc);
253 }
255 /* lookup a data source name in the rrd struct and return the index,
256 * should use ds_match() here except:
257 * (1) need a void * pointer to the rrd
258 * (2) error handling is left to the caller
259 */
260 long lookup_DS(
261 void *rrd_vptr,
262 char *ds_name)
263 {
264 unsigned int i;
265 rrd_t *rrd;
267 rrd = (rrd_t *) rrd_vptr;
269 for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
270 if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
271 return i;
272 }
273 /* the caller handles a bad data source name in the rpn string */
274 return -1;
275 }
277 /* rpn_parse : parse a string and generate a rpnp array; modified
278 * str2rpn() originally included in rrd_graph.c
279 * arguments:
280 * key_hash: a transparent argument passed to lookup(); conceptually this
281 * is a hash object for lookup of a numeric key given a variable name
282 * expr: the string RPN expression, including variable names
283 * lookup(): a function that retrieves a numeric key given a variable name
284 */
285 rpnp_t *rpn_parse(
286 void *key_hash,
287 const char *const expr_const,
288 long (*lookup) (void *,
289 char *))
290 {
291 int pos = 0;
292 char *expr;
293 long steps = -1;
294 rpnp_t *rpnp;
295 char vname[MAX_VNAME_LEN + 10];
296 char *old_locale;
298 old_locale = setlocale(LC_NUMERIC, "C");
300 rpnp = NULL;
301 expr = (char *) expr_const;
303 while (*expr) {
304 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
305 sizeof(rpnp_t))) == NULL) {
306 setlocale(LC_NUMERIC, old_locale);
307 return NULL;
308 }
310 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
311 && (expr[pos] == ',')) {
312 rpnp[steps].op = OP_NUMBER;
313 expr += pos;
314 }
315 #define match_op(VV,VVV) \
316 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
317 rpnp[steps].op = VV; \
318 expr+=strlen(#VVV); \
319 }
321 #define match_op_param(VV,VVV) \
322 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
323 int length = 0; \
324 if ((length = strlen(#VVV)+strlen(vname)+2, \
325 expr[length] == ',' || expr[length] == '\0') ) { \
326 rpnp[steps].op = VV; \
327 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
328 if (rpnp[steps].ptr < 0) { \
329 free(rpnp); \
330 return NULL; \
331 } else expr+=length; \
332 } \
333 }
335 match_op(OP_ADD, +)
336 match_op(OP_SUB, -)
337 match_op(OP_MUL, *)
338 match_op(OP_DIV, /)
339 match_op(OP_MOD, %)
340 match_op(OP_SIN, SIN)
341 match_op(OP_COS, COS)
342 match_op(OP_LOG, LOG)
343 match_op(OP_FLOOR, FLOOR)
344 match_op(OP_CEIL, CEIL)
345 match_op(OP_EXP, EXP)
346 match_op(OP_DUP, DUP)
347 match_op(OP_EXC, EXC)
348 match_op(OP_POP, POP)
349 match_op(OP_LTIME, LTIME)
350 match_op(OP_LT, LT)
351 match_op(OP_LE, LE)
352 match_op(OP_GT, GT)
353 match_op(OP_GE, GE)
354 match_op(OP_EQ, EQ)
355 match_op(OP_IF, IF)
356 match_op(OP_MIN, MIN)
357 match_op(OP_MAX, MAX)
358 match_op(OP_LIMIT, LIMIT)
359 /* order is important here ! .. match longest first */
360 match_op(OP_UNKN, UNKN)
361 match_op(OP_UN, UN)
362 match_op(OP_NEGINF, NEGINF)
363 match_op(OP_NE, NE)
364 match_op(OP_COUNT, COUNT)
365 match_op_param(OP_PREV_OTHER, PREV)
366 match_op(OP_PREV, PREV)
367 match_op(OP_INF, INF)
368 match_op(OP_ISINF, ISINF)
369 match_op(OP_NOW, NOW)
370 match_op(OP_TIME, TIME)
371 match_op(OP_ATAN2, ATAN2)
372 match_op(OP_ATAN, ATAN)
373 match_op(OP_SQRT, SQRT)
374 match_op(OP_SORT, SORT)
375 match_op(OP_REV, REV)
376 match_op(OP_TREND, TREND)
377 match_op(OP_TRENDNAN, TRENDNAN)
378 match_op(OP_RAD2DEG, RAD2DEG)
379 match_op(OP_DEG2RAD, DEG2RAD)
380 match_op(OP_AVG, AVG)
381 match_op(OP_ABS, ABS)
382 match_op(OP_ADDNAN, ADDNAN)
383 #undef match_op
384 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
385 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
386 -1)) {
387 rpnp[steps].op = OP_VARIABLE;
388 expr += pos;
389 }
391 else {
392 setlocale(LC_NUMERIC, old_locale);
393 free(rpnp);
394 return NULL;
395 }
397 if (*expr == 0)
398 break;
399 if (*expr == ',')
400 expr++;
401 else {
402 setlocale(LC_NUMERIC, old_locale);
403 free(rpnp);
404 return NULL;
405 }
406 }
407 rpnp[steps + 1].op = OP_END;
408 setlocale(LC_NUMERIC, old_locale);
409 return rpnp;
410 }
412 void rpnstack_init(
413 rpnstack_t *rpnstack)
414 {
415 rpnstack->s = NULL;
416 rpnstack->dc_stacksize = 0;
417 rpnstack->dc_stackblock = 100;
418 }
420 void rpnstack_free(
421 rpnstack_t *rpnstack)
422 {
423 if (rpnstack->s != NULL)
424 free(rpnstack->s);
425 rpnstack->dc_stacksize = 0;
426 }
428 static int rpn_compare_double(
429 const void *x,
430 const void *y)
431 {
432 double diff = *((const double *) x) - *((const double *) y);
434 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
435 }
437 /* rpn_calc: run the RPN calculator; also performs variable substitution;
438 * moved and modified from data_calc() originally included in rrd_graph.c
439 * arguments:
440 * rpnp : an array of RPN operators (including variable references)
441 * rpnstack : the initialized stack
442 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
443 * is advanced by rpnp.ds_cnt; used only for variable substitution
444 * output : an array of output values; OP_PREV assumes this array contains
445 * the "previous" value at index position output_idx-1; the definition of
446 * "previous" depends on the calling environment
447 * output_idx : an index into the output array in which to store the output
448 * of the RPN calculator
449 * returns: -1 if the computation failed (also calls rrd_set_error)
450 * 0 on success
451 */
452 short rpn_calc(
453 rpnp_t *rpnp,
454 rpnstack_t *rpnstack,
455 long data_idx,
456 rrd_value_t *output,
457 int output_idx)
458 {
459 int rpi;
460 long stptr = -1;
462 /* process each op from the rpn in turn */
463 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
464 /* allocate or grow the stack */
465 if (stptr + 5 > rpnstack->dc_stacksize) {
466 /* could move this to a separate function */
467 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
468 rpnstack->s = (double*)(rrd_realloc(rpnstack->s,
469 (rpnstack->dc_stacksize) *
470 sizeof(*(rpnstack->s))));
471 if (rpnstack->s == NULL) {
472 rrd_set_error("RPN stack overflow");
473 return -1;
474 }
475 }
476 #define stackunderflow(MINSIZE) \
477 if(stptr<MINSIZE){ \
478 rrd_set_error("RPN stack underflow"); \
479 return -1; \
480 }
482 switch (rpnp[rpi].op) {
483 case OP_NUMBER:
484 rpnstack->s[++stptr] = rpnp[rpi].val;
485 break;
486 case OP_VARIABLE:
487 case OP_PREV_OTHER:
488 /* Sanity check: VDEFs shouldn't make it here */
489 if (rpnp[rpi].ds_cnt == 0) {
490 rrd_set_error("VDEF made it into rpn_calc... aborting");
491 return -1;
492 } else {
493 /* make sure we pull the correct value from
494 * the *.data array. Adjust the pointer into
495 * the array acordingly. Advance the ptr one
496 * row in the rra (skip over non-relevant
497 * data sources)
498 */
499 if (rpnp[rpi].op == OP_VARIABLE) {
500 rpnstack->s[++stptr] = *(rpnp[rpi].data);
501 } else {
502 if ((output_idx) <= 0) {
503 rpnstack->s[++stptr] = DNAN;
504 } else {
505 rpnstack->s[++stptr] =
506 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
507 }
509 }
510 if (data_idx % rpnp[rpi].step == 0) {
511 rpnp[rpi].data += rpnp[rpi].ds_cnt;
512 }
513 }
514 break;
515 case OP_COUNT:
516 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
517 break;
518 case OP_PREV:
519 if ((output_idx) <= 0) {
520 rpnstack->s[++stptr] = DNAN;
521 } else {
522 rpnstack->s[++stptr] = output[output_idx - 1];
523 }
524 break;
525 case OP_UNKN:
526 rpnstack->s[++stptr] = DNAN;
527 break;
528 case OP_INF:
529 rpnstack->s[++stptr] = DINF;
530 break;
531 case OP_NEGINF:
532 rpnstack->s[++stptr] = -DINF;
533 break;
534 case OP_NOW:
535 rpnstack->s[++stptr] = (double) time(NULL);
536 break;
537 case OP_TIME:
538 /* HACK: this relies on the data_idx being the time,
539 ** which the within-function scope is unaware of */
540 rpnstack->s[++stptr] = (double) data_idx;
541 break;
542 case OP_LTIME:
543 rpnstack->s[++stptr] =
544 (double) tzoffset(data_idx) + (double) data_idx;
545 break;
546 case OP_ADD:
547 stackunderflow(1);
548 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
549 + rpnstack->s[stptr];
550 stptr--;
551 break;
552 case OP_ADDNAN:
553 stackunderflow(1);
554 if (isnan(rpnstack->s[stptr - 1])) {
555 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
556 } else if (isnan(rpnstack->s[stptr])) {
557 /* NOOP */
558 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
559 } else {
560 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
561 + rpnstack->s[stptr];
562 }
564 stptr--;
565 break;
566 case OP_SUB:
567 stackunderflow(1);
568 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
569 - rpnstack->s[stptr];
570 stptr--;
571 break;
572 case OP_MUL:
573 stackunderflow(1);
574 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
575 * (rpnstack->s[stptr]);
576 stptr--;
577 break;
578 case OP_DIV:
579 stackunderflow(1);
580 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
581 / rpnstack->s[stptr];
582 stptr--;
583 break;
584 case OP_MOD:
585 stackunderflow(1);
586 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
587 , rpnstack->s[stptr]);
588 stptr--;
589 break;
590 case OP_SIN:
591 stackunderflow(0);
592 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
593 break;
594 case OP_ATAN:
595 stackunderflow(0);
596 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
597 break;
598 case OP_RAD2DEG:
599 stackunderflow(0);
600 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
601 break;
602 case OP_DEG2RAD:
603 stackunderflow(0);
604 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
605 break;
606 case OP_ATAN2:
607 stackunderflow(1);
608 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
609 rpnstack->s[stptr]);
610 stptr--;
611 break;
612 case OP_COS:
613 stackunderflow(0);
614 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
615 break;
616 case OP_CEIL:
617 stackunderflow(0);
618 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
619 break;
620 case OP_FLOOR:
621 stackunderflow(0);
622 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
623 break;
624 case OP_LOG:
625 stackunderflow(0);
626 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
627 break;
628 case OP_DUP:
629 stackunderflow(0);
630 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
631 stptr++;
632 break;
633 case OP_POP:
634 stackunderflow(0);
635 stptr--;
636 break;
637 case OP_EXC:
638 stackunderflow(1);
639 {
640 double dummy;
642 dummy = rpnstack->s[stptr];
643 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
644 rpnstack->s[stptr - 1] = dummy;
645 }
646 break;
647 case OP_EXP:
648 stackunderflow(0);
649 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
650 break;
651 case OP_LT:
652 stackunderflow(1);
653 if (isnan(rpnstack->s[stptr - 1]));
654 else if (isnan(rpnstack->s[stptr]))
655 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
656 else
657 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
658 rpnstack->s[stptr] ? 1.0 : 0.0;
659 stptr--;
660 break;
661 case OP_LE:
662 stackunderflow(1);
663 if (isnan(rpnstack->s[stptr - 1]));
664 else if (isnan(rpnstack->s[stptr]))
665 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
666 else
667 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
668 rpnstack->s[stptr] ? 1.0 : 0.0;
669 stptr--;
670 break;
671 case OP_GT:
672 stackunderflow(1);
673 if (isnan(rpnstack->s[stptr - 1]));
674 else if (isnan(rpnstack->s[stptr]))
675 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
676 else
677 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
678 rpnstack->s[stptr] ? 1.0 : 0.0;
679 stptr--;
680 break;
681 case OP_GE:
682 stackunderflow(1);
683 if (isnan(rpnstack->s[stptr - 1]));
684 else if (isnan(rpnstack->s[stptr]))
685 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
686 else
687 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
688 rpnstack->s[stptr] ? 1.0 : 0.0;
689 stptr--;
690 break;
691 case OP_NE:
692 stackunderflow(1);
693 if (isnan(rpnstack->s[stptr - 1]));
694 else if (isnan(rpnstack->s[stptr]))
695 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
696 else
697 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
698 rpnstack->s[stptr] ? 0.0 : 1.0;
699 stptr--;
700 break;
701 case OP_EQ:
702 stackunderflow(1);
703 if (isnan(rpnstack->s[stptr - 1]));
704 else if (isnan(rpnstack->s[stptr]))
705 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
706 else
707 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
708 rpnstack->s[stptr] ? 1.0 : 0.0;
709 stptr--;
710 break;
711 case OP_IF:
712 stackunderflow(2);
713 rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
714 || rpnstack->s[stptr - 2] ==
715 0.0) ? rpnstack->s[stptr] : rpnstack->
716 s[stptr - 1];
717 stptr--;
718 stptr--;
719 break;
720 case OP_MIN:
721 stackunderflow(1);
722 if (isnan(rpnstack->s[stptr - 1]));
723 else if (isnan(rpnstack->s[stptr]))
724 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
725 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
726 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
727 stptr--;
728 break;
729 case OP_MAX:
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_LIMIT:
739 stackunderflow(2);
740 if (isnan(rpnstack->s[stptr - 2]));
741 else if (isnan(rpnstack->s[stptr - 1]))
742 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
743 else if (isnan(rpnstack->s[stptr]))
744 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
745 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
746 rpnstack->s[stptr - 2] = DNAN;
747 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
748 rpnstack->s[stptr - 2] = DNAN;
749 stptr -= 2;
750 break;
751 case OP_UN:
752 stackunderflow(0);
753 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
754 break;
755 case OP_ISINF:
756 stackunderflow(0);
757 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
758 break;
759 case OP_SQRT:
760 stackunderflow(0);
761 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
762 break;
763 case OP_SORT:
764 stackunderflow(0);
765 {
766 int spn = (int) rpnstack->s[stptr--];
768 stackunderflow(spn - 1);
769 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
770 rpn_compare_double);
771 }
772 break;
773 case OP_REV:
774 stackunderflow(0);
775 {
776 int spn = (int) rpnstack->s[stptr--];
777 double *p, *q;
779 stackunderflow(spn - 1);
781 p = rpnstack->s + stptr - spn + 1;
782 q = rpnstack->s + stptr;
783 while (p < q) {
784 double x = *q;
786 *q-- = *p;
787 *p++ = x;
788 }
789 }
790 break;
791 case OP_TREND:
792 case OP_TRENDNAN:
793 stackunderflow(1);
794 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
795 rrd_set_error("malformed trend arguments");
796 return -1;
797 } else {
798 time_t dur = (time_t) rpnstack->s[stptr];
799 time_t step = (time_t) rpnp[rpi - 2].step;
801 if (output_idx > (int) ceil((float) dur / (float) step)) {
802 int ignorenan = (rpnp[rpi].op == OP_TREND);
803 double accum = 0.0;
804 int i = 0;
805 int count = 0;
807 do {
808 double val =
809 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
810 if (ignorenan || !isnan(val)) {
811 accum += val;
812 ++count;
813 }
815 dur -= step;
816 } while (dur > 0);
818 rpnstack->s[--stptr] =
819 (count == 0) ? DNAN : (accum / count);
820 } else
821 rpnstack->s[--stptr] = DNAN;
822 }
823 break;
824 case OP_AVG:
825 stackunderflow(0);
826 {
827 int i = (int) rpnstack->s[stptr--];
828 double sum = 0;
829 int count = 0;
831 stackunderflow(i - 1);
832 while (i > 0) {
833 double val = rpnstack->s[stptr--];
835 i--;
836 if (isnan(val)) {
837 continue;
838 }
839 count++;
840 sum += val;
841 }
842 /* now push the result back on stack */
843 if (count > 0) {
844 rpnstack->s[++stptr] = sum / count;
845 } else {
846 rpnstack->s[++stptr] = DNAN;
847 }
848 }
849 break;
850 case OP_ABS:
851 stackunderflow(0);
852 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
853 break;
854 case OP_END:
855 break;
856 }
857 #undef stackunderflow
858 }
859 if (stptr != 0) {
860 rrd_set_error("RPN final stack size != 1");
861 return -1;
862 }
864 output[output_idx] = rpnstack->s[0];
865 return 0;
866 }
868 /* figure out what the local timezone offset for any point in
869 time was. Return it in seconds */
870 int tzoffset(
871 time_t now)
872 {
873 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
874 l_sec, l_min, l_hour, l_yday, l_year;
875 struct tm t;
876 int off;
878 gmtime_r(&now, &t);
879 gm_sec = t.tm_sec;
880 gm_min = t.tm_min;
881 gm_hour = t.tm_hour;
882 gm_yday = t.tm_yday;
883 gm_year = t.tm_year;
884 localtime_r(&now, &t);
885 l_sec = t.tm_sec;
886 l_min = t.tm_min;
887 l_hour = t.tm_hour;
888 l_yday = t.tm_yday;
889 l_year = t.tm_year;
890 off =
891 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
892 if (l_yday > gm_yday || l_year > gm_year) {
893 off += 24 * 3600;
894 } else if (l_yday < gm_yday || l_year < gm_year) {
895 off -= 24 * 3600;
896 }
897 return off;
898 }