1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
12 #include "rrd_rpncalc.h"
13 // #include "rrd_graph.h"
20 unsigned short *offset);
22 time_t); /* used to implement LTIME */
32 /* count the number of rpn nodes */
33 while (rpnp[*count].op != OP_END)
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);
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) {
51 ("constants must be integers in the interval (%d, %d)",
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;
61 /* terminate the sequence */
62 (*rpnc)[(*count) - 1].op = OP_END;
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));
76 rrd_set_error("failed allocating rpnp array");
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;
87 /* terminate the sequence */
92 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
93 * into a CDEF string. This function is used by rrd_dump.
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
100 void rpn_compact2str(
105 unsigned short i, offset = 0;
106 char buffer[7]; /* short as a string */
108 for (i = 0; rpnc[i].op != OP_END; i++) {
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);
120 sprintf(buffer, "%d", rpnc[i].val);
122 add_op(OP_NUMBER, buffer)
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)
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)
138 #define add_op(VV,VVV) \
139 if (addop2str((enum op_en)rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
149 add_op(OP_FLOOR, FLOOR)
150 add_op(OP_CEIL, CEIL)
163 add_op(OP_LIMIT, LIMIT)
164 add_op(OP_UNKN, UNKN)
166 add_op(OP_NEGINF, NEGINF)
168 add_op(OP_PREV, PREV)
170 add_op(OP_ISINF, ISINF)
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)
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)
187 add_op(OP_ADDNAN, ADDNAN)
190 (*str)[offset] = '\0';
199 unsigned short *offset)
204 op_len = strlen(op_str);
205 *result_str = (char *) rrd_realloc(*result_str,
207 *offset) * sizeof(char));
208 if (*result_str == NULL) {
209 rrd_set_error("failed to alloc memory in addop2str");
212 strncpy(&((*result_str)[*offset]), op_str, op_len);
225 rpn_cdefds_t *rpnc = NULL;
228 rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
230 rrd_set_error("failed to parse computed data source");
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 ) {
243 ("operators TIME, LTIME, PREV COUNT TREND TRENDNAN PREDICT PREDICTSIGMA are not supported with DS COMPUTE");
248 if (rpn_compact(rpnp, &rpnc, &count) == -1) {
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));
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
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)
277 /* the caller handles a bad data source name in the rpn string */
281 /* rpn_parse : parse a string and generate a rpnp array; modified
282 * str2rpn() originally included in rrd_graph.c
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
291 const char *const expr_const,
292 long (*lookup) (void *,
299 char vname[MAX_VNAME_LEN + 10];
302 old_locale = setlocale(LC_NUMERIC, NULL);
303 setlocale(LC_NUMERIC, "C");
306 expr = (char *) expr_const;
309 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
310 sizeof(rpnp_t))) == NULL) {
311 setlocale(LC_NUMERIC, old_locale);
315 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
316 && (expr[pos] == ',')) {
317 rpnp[steps].op = OP_NUMBER;
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); \
326 #define match_op_param(VV,VVV) \
327 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
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) { \
336 } else expr+=length; \
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)
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)
367 match_op(OP_NEGINF, NEGINF)
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)
391 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
392 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
394 rpnp[steps].op = OP_VARIABLE;
399 setlocale(LC_NUMERIC, old_locale);
409 setlocale(LC_NUMERIC, old_locale);
414 rpnp[steps + 1].op = OP_END;
415 setlocale(LC_NUMERIC, old_locale);
420 rpnstack_t *rpnstack)
423 rpnstack->dc_stacksize = 0;
424 rpnstack->dc_stackblock = 100;
428 rpnstack_t *rpnstack)
430 if (rpnstack->s != NULL)
432 rpnstack->dc_stacksize = 0;
435 static int rpn_compare_double(
439 double diff = *((const double *) x) - *((const double *) y);
441 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
444 /* rpn_calc: run the RPN calculator; also performs variable substitution;
445 * moved and modified from data_calc() originally included in rrd_graph.c
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)
461 rpnstack_t *rpnstack,
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");
483 #define stackunderflow(MINSIZE) \
485 rrd_set_error("RPN stack underflow"); \
489 switch (rpnp[rpi].op) {
491 rpnstack->s[++stptr] = rpnp[rpi].val;
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");
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
506 if (rpnp[rpi].op == OP_VARIABLE) {
507 rpnstack->s[++stptr] = *(rpnp[rpi].data);
509 if ((output_idx) <= 0) {
510 rpnstack->s[++stptr] = DNAN;
512 rpnstack->s[++stptr] =
513 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
517 if (data_idx % rpnp[rpi].step == 0) {
518 rpnp[rpi].data += rpnp[rpi].ds_cnt;
523 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
526 if ((output_idx) <= 0) {
527 rpnstack->s[++stptr] = DNAN;
529 rpnstack->s[++stptr] = output[output_idx - 1];
533 rpnstack->s[++stptr] = DNAN;
536 rpnstack->s[++stptr] = DINF;
539 rpnstack->s[++stptr] = -DINF;
542 rpnstack->s[++stptr] = (double) time(NULL);
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;
550 rpnstack->s[++stptr] =
551 (double) tzoffset(data_idx) + (double) data_idx;
555 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
556 + rpnstack->s[stptr];
561 if (isnan(rpnstack->s[stptr - 1])) {
562 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
563 } else if (isnan(rpnstack->s[stptr])) {
565 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
567 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
568 + rpnstack->s[stptr];
575 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
576 - rpnstack->s[stptr];
581 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
582 * (rpnstack->s[stptr]);
587 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
588 / rpnstack->s[stptr];
593 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
594 , rpnstack->s[stptr]);
599 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
603 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
607 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
611 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
615 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
621 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
625 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
629 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
633 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
637 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
649 dummy = rpnstack->s[stptr];
650 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
651 rpnstack->s[stptr - 1] = dummy;
656 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
660 if (isnan(rpnstack->s[stptr - 1]));
661 else if (isnan(rpnstack->s[stptr]))
662 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
664 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
665 rpnstack->s[stptr] ? 1.0 : 0.0;
670 if (isnan(rpnstack->s[stptr - 1]));
671 else if (isnan(rpnstack->s[stptr]))
672 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
674 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
675 rpnstack->s[stptr] ? 1.0 : 0.0;
680 if (isnan(rpnstack->s[stptr - 1]));
681 else if (isnan(rpnstack->s[stptr]))
682 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
684 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
685 rpnstack->s[stptr] ? 1.0 : 0.0;
690 if (isnan(rpnstack->s[stptr - 1]));
691 else if (isnan(rpnstack->s[stptr]))
692 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
694 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
695 rpnstack->s[stptr] ? 1.0 : 0.0;
700 if (isnan(rpnstack->s[stptr - 1]));
701 else if (isnan(rpnstack->s[stptr]))
702 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
704 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
705 rpnstack->s[stptr] ? 0.0 : 1.0;
710 if (isnan(rpnstack->s[stptr - 1]));
711 else if (isnan(rpnstack->s[stptr]))
712 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
714 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
715 rpnstack->s[stptr] ? 1.0 : 0.0;
720 rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
721 || rpnstack->s[stptr - 2] ==
722 0.0) ? rpnstack->s[stptr] : rpnstack->
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];
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];
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;
760 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
764 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
768 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
773 int spn = (int) rpnstack->s[stptr--];
775 stackunderflow(spn - 1);
776 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
783 int spn = (int) rpnstack->s[stptr--];
786 stackunderflow(spn - 1);
788 p = rpnstack->s + stptr - spn + 1;
789 q = rpnstack->s + stptr;
799 case OP_PREDICTSIGMA:
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
813 /* the real calculation */
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);
824 /* now loop for each position */
826 if (shifts<0) { doshifts=-shifts; }
827 for(int loop=0;loop<doshifts;loop++) {
828 /* calculate shift step */
831 shiftstep = loop*rpnstack->s[stptr];
833 shiftstep = rpnstack->s[stptr+loop];
836 rrd_set_error("negative shift step not allowed: %i",shiftstep);
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)) {
847 val =rpnp[rpi - 1].data[-dscount * offset];
848 /* and handle the non NAN case only*/
857 /* do the final calculations */
859 if (rpnp[rpi].op == OP_PREDICT) { /* the average */
861 val = sum/(double)count;
864 if (count>1) { /* the sigma case */
865 val=count*sum2-sum*sum;
869 val=sqrt(val/((float)count*((float)count-1.0)));
873 rpnstack->s[stptr] = val;
879 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
880 rrd_set_error("malformed trend arguments");
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);
894 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
895 if (ignorenan || !isnan(val)) {
903 rpnstack->s[--stptr] =
904 (count == 0) ? DNAN : (accum / count);
906 rpnstack->s[--stptr] = DNAN;
912 int i = (int) rpnstack->s[stptr--];
916 stackunderflow(i - 1);
918 double val = rpnstack->s[stptr--];
927 /* now push the result back on stack */
929 rpnstack->s[++stptr] = sum / count;
931 rpnstack->s[++stptr] = DNAN;
937 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
942 #undef stackunderflow
945 rrd_set_error("RPN final stack size != 1");
949 output[output_idx] = rpnstack->s[0];
953 /* figure out what the local timezone offset for any point in
954 time was. Return it in seconds */
958 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
959 l_sec, l_min, l_hour, l_yday, l_year;
969 localtime_r(&now, &t);
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) {
979 } else if (l_yday < gm_yday || l_year < gm_year) {