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