1 /****************************************************************************
2 * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
8 #include "rrd_rpncalc.h"
18 unsigned short *offset);
20 time_t); /* used to implement LTIME */
30 /* count the number of rpn nodes */
31 while (rpnp[*count].op != OP_END)
33 if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
34 rrd_set_error("Maximum %d RPN nodes permitted",
35 DS_CDEF_MAX_RPN_NODES);
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) {
49 ("constants must be integers in the interval (%d, %d)",
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;
59 /* terminate the sequence */
60 (*rpnc)[(*count) - 1].op = OP_END;
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));
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;
83 /* terminate the sequence */
88 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
89 * into a CDEF string. This function is used by rrd_dump.
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
101 unsigned short i, offset = 0;
102 char buffer[7]; /* short as a string */
104 for (i = 0; rpnc[i].op != OP_END; i++) {
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);
116 sprintf(buffer, "%d", rpnc[i].val);
118 add_op(OP_NUMBER, buffer)
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)
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)
134 #define add_op(VV,VVV) \
135 if (addop2str(rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
145 add_op(OP_FLOOR, FLOOR)
146 add_op(OP_CEIL, CEIL)
159 add_op(OP_LIMIT, LIMIT)
160 add_op(OP_UNKN, UNKN)
162 add_op(OP_NEGINF, NEGINF)
164 add_op(OP_PREV, PREV)
166 add_op(OP_ISINF, ISINF)
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)
175 add_op(OP_TREND, TREND)
176 add_op(OP_TRENDNAN, TRENDNAN)
177 add_op(OP_RAD2DEG, RAD2DEG)
178 add_op(OP_DEG2RAD, DEG2RAD)
183 (*str)[offset] = '\0';
192 unsigned short *offset)
197 op_len = strlen(op_str);
198 *result_str = (char *) rrd_realloc(*result_str,
200 *offset) * sizeof(char));
201 if (*result_str == NULL) {
202 rrd_set_error("failed to alloc memory in addop2str");
205 strncpy(&((*result_str)[*offset]), op_str, op_len);
218 rpn_cdefds_t *rpnc = NULL;
221 rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
223 rrd_set_error("failed to parse computed data source");
226 /* Check for OP nodes not permitted in COMPUTE DS.
227 * Moved this check from within rpn_compact() because it really is
228 * COMPUTE DS specific. This is less efficient, but creation doesn't
229 * occur too often. */
230 for (i = 0; rpnp[i].op != OP_END; i++) {
231 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
232 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT) {
234 ("operators time, ltime, prev and count not supported with DS COMPUTE");
239 if (rpn_compact(rpnp, &rpnc, &count) == -1) {
243 /* copy the compact rpn representation over the ds_def par array */
244 memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
245 (void *) rpnc, count * sizeof(rpn_cdefds_t));
250 /* lookup a data source name in the rrd struct and return the index,
251 * should use ds_match() here except:
252 * (1) need a void * pointer to the rrd
253 * (2) error handling is left to the caller
262 rrd = (rrd_t *) rrd_vptr;
264 for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
265 if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
268 /* the caller handles a bad data source name in the rpn string */
272 /* rpn_parse : parse a string and generate a rpnp array; modified
273 * str2rpn() originally included in rrd_graph.c
275 * key_hash: a transparent argument passed to lookup(); conceptually this
276 * is a hash object for lookup of a numeric key given a variable name
277 * expr: the string RPN expression, including variable names
278 * lookup(): a function that retrieves a numeric key given a variable name
282 const char *const expr_const,
283 long (*lookup) (void *,
290 char vname[MAX_VNAME_LEN + 10];
293 old_locale = setlocale(LC_NUMERIC, "C");
296 expr = (char *) expr_const;
299 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
300 sizeof(rpnp_t))) == NULL) {
301 setlocale(LC_NUMERIC, old_locale);
305 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
306 && (expr[pos] == ',')) {
307 rpnp[steps].op = OP_NUMBER;
310 #define match_op(VV,VVV) \
311 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
312 rpnp[steps].op = VV; \
313 expr+=strlen(#VVV); \
316 #define match_op_param(VV,VVV) \
317 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
319 if ((length = strlen(#VVV)+strlen(vname)+2, \
320 expr[length] == ',' || expr[length] == '\0') ) { \
321 rpnp[steps].op = VV; \
322 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
323 if (rpnp[steps].ptr < 0) { \
326 } else expr+=length; \
335 match_op(OP_SIN, SIN)
336 match_op(OP_COS, COS)
337 match_op(OP_LOG, LOG)
338 match_op(OP_FLOOR, FLOOR)
339 match_op(OP_CEIL, CEIL)
340 match_op(OP_EXP, EXP)
341 match_op(OP_DUP, DUP)
342 match_op(OP_EXC, EXC)
343 match_op(OP_POP, POP)
344 match_op(OP_LTIME, LTIME)
351 match_op(OP_MIN, MIN)
352 match_op(OP_MAX, MAX)
353 match_op(OP_LIMIT, LIMIT)
354 /* order is important here ! .. match longest first */
355 match_op(OP_UNKN, UNKN)
357 match_op(OP_NEGINF, NEGINF)
359 match_op(OP_COUNT, COUNT)
360 match_op_param(OP_PREV_OTHER, PREV)
361 match_op(OP_PREV, PREV)
362 match_op(OP_INF, INF)
363 match_op(OP_ISINF, ISINF)
364 match_op(OP_NOW, NOW)
365 match_op(OP_TIME, TIME)
366 match_op(OP_ATAN2, ATAN2)
367 match_op(OP_ATAN, ATAN)
368 match_op(OP_SQRT, SQRT)
369 match_op(OP_SORT, SORT)
370 match_op(OP_REV, REV)
371 match_op(OP_TREND, TREND)
372 match_op(OP_TRENDNAN, TRENDNAN)
373 match_op(OP_RAD2DEG, RAD2DEG)
374 match_op(OP_DEG2RAD, DEG2RAD)
375 match_op(OP_AVG, AVG)
376 match_op(OP_ABS, ABS)
378 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
379 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
381 rpnp[steps].op = OP_VARIABLE;
386 setlocale(LC_NUMERIC, old_locale);
396 setlocale(LC_NUMERIC, old_locale);
401 rpnp[steps + 1].op = OP_END;
402 setlocale(LC_NUMERIC, old_locale);
407 rpnstack_t *rpnstack)
410 rpnstack->dc_stacksize = 0;
411 rpnstack->dc_stackblock = 100;
415 rpnstack_t *rpnstack)
417 if (rpnstack->s != NULL)
419 rpnstack->dc_stacksize = 0;
422 static int rpn_compare_double(
426 double diff = *((const double *) x) - *((const double *) y);
428 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
431 /* rpn_calc: run the RPN calculator; also performs variable substitution;
432 * moved and modified from data_calc() originally included in rrd_graph.c
434 * rpnp : an array of RPN operators (including variable references)
435 * rpnstack : the initialized stack
436 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
437 * is advanced by rpnp.ds_cnt; used only for variable substitution
438 * output : an array of output values; OP_PREV assumes this array contains
439 * the "previous" value at index position output_idx-1; the definition of
440 * "previous" depends on the calling environment
441 * output_idx : an index into the output array in which to store the output
442 * of the RPN calculator
443 * returns: -1 if the computation failed (also calls rrd_set_error)
448 rpnstack_t *rpnstack,
456 /* process each op from the rpn in turn */
457 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
458 /* allocate or grow the stack */
459 if (stptr + 5 > rpnstack->dc_stacksize) {
460 /* could move this to a separate function */
461 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
462 rpnstack->s = rrd_realloc(rpnstack->s,
463 (rpnstack->dc_stacksize) *
464 sizeof(*(rpnstack->s)));
465 if (rpnstack->s == NULL) {
466 rrd_set_error("RPN stack overflow");
470 #define stackunderflow(MINSIZE) \
472 rrd_set_error("RPN stack underflow"); \
476 switch (rpnp[rpi].op) {
478 rpnstack->s[++stptr] = rpnp[rpi].val;
482 /* Sanity check: VDEFs shouldn't make it here */
483 if (rpnp[rpi].ds_cnt == 0) {
484 rrd_set_error("VDEF made it into rpn_calc... aborting");
487 /* make sure we pull the correct value from
488 * the *.data array. Adjust the pointer into
489 * the array acordingly. Advance the ptr one
490 * row in the rra (skip over non-relevant
493 if (rpnp[rpi].op == OP_VARIABLE) {
494 rpnstack->s[++stptr] = *(rpnp[rpi].data);
496 if ((output_idx) <= 0) {
497 rpnstack->s[++stptr] = DNAN;
499 rpnstack->s[++stptr] =
500 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
504 if (data_idx % rpnp[rpi].step == 0) {
505 rpnp[rpi].data += rpnp[rpi].ds_cnt;
510 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
513 if ((output_idx) <= 0) {
514 rpnstack->s[++stptr] = DNAN;
516 rpnstack->s[++stptr] = output[output_idx - 1];
520 rpnstack->s[++stptr] = DNAN;
523 rpnstack->s[++stptr] = DINF;
526 rpnstack->s[++stptr] = -DINF;
529 rpnstack->s[++stptr] = (double) time(NULL);
532 /* HACK: this relies on the data_idx being the time,
533 ** which the within-function scope is unaware of */
534 rpnstack->s[++stptr] = (double) data_idx;
537 rpnstack->s[++stptr] =
538 (double) tzoffset(data_idx) + (double) data_idx;
542 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
543 + rpnstack->s[stptr];
548 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
549 - rpnstack->s[stptr];
554 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
555 * (rpnstack->s[stptr]);
560 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
561 / rpnstack->s[stptr];
566 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
567 , rpnstack->s[stptr]);
572 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
576 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
580 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
584 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
588 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
594 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
598 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
602 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
606 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
610 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
622 dummy = rpnstack->s[stptr];
623 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
624 rpnstack->s[stptr - 1] = dummy;
629 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
633 if (isnan(rpnstack->s[stptr - 1]));
634 else if (isnan(rpnstack->s[stptr]))
635 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
637 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
638 rpnstack->s[stptr] ? 1.0 : 0.0;
643 if (isnan(rpnstack->s[stptr - 1]));
644 else if (isnan(rpnstack->s[stptr]))
645 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
647 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
648 rpnstack->s[stptr] ? 1.0 : 0.0;
653 if (isnan(rpnstack->s[stptr - 1]));
654 else if (isnan(rpnstack->s[stptr]))
655 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
657 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
658 rpnstack->s[stptr] ? 1.0 : 0.0;
663 if (isnan(rpnstack->s[stptr - 1]));
664 else if (isnan(rpnstack->s[stptr]))
665 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
667 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
668 rpnstack->s[stptr] ? 1.0 : 0.0;
673 if (isnan(rpnstack->s[stptr - 1]));
674 else if (isnan(rpnstack->s[stptr]))
675 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
677 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
678 rpnstack->s[stptr] ? 0.0 : 1.0;
683 if (isnan(rpnstack->s[stptr - 1]));
684 else if (isnan(rpnstack->s[stptr]))
685 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
687 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
688 rpnstack->s[stptr] ? 1.0 : 0.0;
693 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 2] != 0.0 ?
694 rpnstack->s[stptr - 1] : rpnstack->s[stptr];
700 if (isnan(rpnstack->s[stptr - 1]));
701 else if (isnan(rpnstack->s[stptr]))
702 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
703 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
704 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
709 if (isnan(rpnstack->s[stptr - 1]));
710 else if (isnan(rpnstack->s[stptr]))
711 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
712 else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
713 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
718 if (isnan(rpnstack->s[stptr - 2]));
719 else if (isnan(rpnstack->s[stptr - 1]))
720 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
721 else if (isnan(rpnstack->s[stptr]))
722 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
723 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
724 rpnstack->s[stptr - 2] = DNAN;
725 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
726 rpnstack->s[stptr - 2] = DNAN;
731 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
735 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
739 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
744 int spn = (int) rpnstack->s[stptr--];
746 stackunderflow(spn - 1);
747 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
754 int spn = (int) rpnstack->s[stptr--];
757 stackunderflow(spn - 1);
759 p = rpnstack->s + stptr - spn + 1;
760 q = rpnstack->s + stptr;
772 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
773 rrd_set_error("malformed trend arguments");
776 time_t dur = (time_t) rpnstack->s[stptr];
777 time_t step = (time_t) rpnp[rpi - 2].step;
779 if (output_idx > (int) ceil((float) dur / (float) step)) {
780 int ignorenan = (rpnp[rpi].op == OP_TREND);
787 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
788 if (ignorenan || !isnan(val)) {
796 rpnstack->s[--stptr] =
797 (count == 0) ? DNAN : (accum / count);
799 rpnstack->s[--stptr] = DNAN;
805 int i = (int) rpnstack->s[stptr--];
809 stackunderflow(i - 1);
811 double val = rpnstack->s[stptr--];
820 /* now push the result back on stack */
822 rpnstack->s[++stptr] = sum / count;
824 rpnstack->s[++stptr] = DNAN;
830 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
835 #undef stackunderflow
838 rrd_set_error("RPN final stack size != 1");
842 output[output_idx] = rpnstack->s[0];
846 /* figure out what the local timezone offset for any point in
847 time was. Return it in seconds */
851 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
852 l_sec, l_min, l_hour, l_yday, l_year;
862 localtime_r(&now, &t);
869 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
870 if (l_yday > gm_yday || l_year > gm_year) {
872 } else if (l_yday < gm_yday || l_year < gm_year) {