1 /****************************************************************************
2 * RRDtool 1.2.23 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];
292 old_locale = setlocale(LC_NUMERIC,"C");
295 expr = (char *) expr_const;
298 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
299 sizeof(rpnp_t))) == NULL) {
300 setlocale(LC_NUMERIC,old_locale);
304 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
305 && (expr[pos] == ',')) {
306 rpnp[steps].op = OP_NUMBER;
309 #define match_op(VV,VVV) \
310 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
311 rpnp[steps].op = VV; \
312 expr+=strlen(#VVV); \
315 #define match_op_param(VV,VVV) \
316 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
318 if ((length = strlen(#VVV)+strlen(vname)+2, \
319 expr[length] == ',' || expr[length] == '\0') ) { \
320 rpnp[steps].op = VV; \
321 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
322 if (rpnp[steps].ptr < 0) { \
325 } else expr+=length; \
334 match_op(OP_SIN, SIN)
335 match_op(OP_COS, COS)
336 match_op(OP_LOG, LOG)
337 match_op(OP_FLOOR, FLOOR)
338 match_op(OP_CEIL, CEIL)
339 match_op(OP_EXP, EXP)
340 match_op(OP_DUP, DUP)
341 match_op(OP_EXC, EXC)
342 match_op(OP_POP, POP)
343 match_op(OP_LTIME, LTIME)
350 match_op(OP_MIN, MIN)
351 match_op(OP_MAX, MAX)
352 match_op(OP_LIMIT, LIMIT)
353 /* order is important here ! .. match longest first */
354 match_op(OP_UNKN, UNKN)
356 match_op(OP_NEGINF, NEGINF)
358 match_op(OP_COUNT, COUNT)
359 match_op_param(OP_PREV_OTHER, PREV)
360 match_op(OP_PREV, PREV)
361 match_op(OP_INF, INF)
362 match_op(OP_ISINF, ISINF)
363 match_op(OP_NOW, NOW)
364 match_op(OP_TIME, TIME)
365 match_op(OP_ATAN2, ATAN2)
366 match_op(OP_ATAN, ATAN)
367 match_op(OP_SQRT, SQRT)
368 match_op(OP_SORT, SORT)
369 match_op(OP_REV, REV)
370 match_op(OP_TREND, TREND)
371 match_op(OP_TRENDNAN, TRENDNAN)
372 match_op(OP_RAD2DEG, RAD2DEG)
373 match_op(OP_DEG2RAD, DEG2RAD)
374 match_op(OP_AVG, AVG)
375 match_op(OP_ABS, ABS)
377 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
378 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
380 rpnp[steps].op = OP_VARIABLE;
385 setlocale(LC_NUMERIC,old_locale);
395 setlocale(LC_NUMERIC,old_locale);
400 rpnp[steps + 1].op = OP_END;
401 setlocale(LC_NUMERIC,old_locale);
406 rpnstack_t *rpnstack)
409 rpnstack->dc_stacksize = 0;
410 rpnstack->dc_stackblock = 100;
414 rpnstack_t *rpnstack)
416 if (rpnstack->s != NULL)
418 rpnstack->dc_stacksize = 0;
421 static int rpn_compare_double(
425 double diff = *((const double *) x) - *((const double *) y);
427 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
430 /* rpn_calc: run the RPN calculator; also performs variable substitution;
431 * moved and modified from data_calc() originally included in rrd_graph.c
433 * rpnp : an array of RPN operators (including variable references)
434 * rpnstack : the initialized stack
435 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
436 * is advanced by rpnp.ds_cnt; used only for variable substitution
437 * output : an array of output values; OP_PREV assumes this array contains
438 * the "previous" value at index position output_idx-1; the definition of
439 * "previous" depends on the calling environment
440 * output_idx : an index into the output array in which to store the output
441 * of the RPN calculator
442 * returns: -1 if the computation failed (also calls rrd_set_error)
447 rpnstack_t *rpnstack,
455 /* process each op from the rpn in turn */
456 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
457 /* allocate or grow the stack */
458 if (stptr + 5 > rpnstack->dc_stacksize) {
459 /* could move this to a separate function */
460 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
461 rpnstack->s = rrd_realloc(rpnstack->s,
462 (rpnstack->dc_stacksize) *
463 sizeof(*(rpnstack->s)));
464 if (rpnstack->s == NULL) {
465 rrd_set_error("RPN stack overflow");
469 #define stackunderflow(MINSIZE) \
471 rrd_set_error("RPN stack underflow"); \
475 switch (rpnp[rpi].op) {
477 rpnstack->s[++stptr] = rpnp[rpi].val;
481 /* Sanity check: VDEFs shouldn't make it here */
482 if (rpnp[rpi].ds_cnt == 0) {
483 rrd_set_error("VDEF made it into rpn_calc... aborting");
486 /* make sure we pull the correct value from
487 * the *.data array. Adjust the pointer into
488 * the array acordingly. Advance the ptr one
489 * row in the rra (skip over non-relevant
492 if (rpnp[rpi].op == OP_VARIABLE) {
493 rpnstack->s[++stptr] = *(rpnp[rpi].data);
495 if ((output_idx) <= 0) {
496 rpnstack->s[++stptr] = DNAN;
498 rpnstack->s[++stptr] =
499 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
503 if (data_idx % rpnp[rpi].step == 0) {
504 rpnp[rpi].data += rpnp[rpi].ds_cnt;
509 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
512 if ((output_idx) <= 0) {
513 rpnstack->s[++stptr] = DNAN;
515 rpnstack->s[++stptr] = output[output_idx - 1];
519 rpnstack->s[++stptr] = DNAN;
522 rpnstack->s[++stptr] = DINF;
525 rpnstack->s[++stptr] = -DINF;
528 rpnstack->s[++stptr] = (double) time(NULL);
531 /* HACK: this relies on the data_idx being the time,
532 ** which the within-function scope is unaware of */
533 rpnstack->s[++stptr] = (double) data_idx;
536 rpnstack->s[++stptr] =
537 (double) tzoffset(data_idx) + (double) data_idx;
541 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
542 + rpnstack->s[stptr];
547 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
548 - rpnstack->s[stptr];
553 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
554 * (rpnstack->s[stptr]);
559 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
560 / rpnstack->s[stptr];
565 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
566 , rpnstack->s[stptr]);
571 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
575 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
579 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
583 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
587 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
593 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
597 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
601 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
605 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
609 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
621 dummy = rpnstack->s[stptr];
622 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
623 rpnstack->s[stptr - 1] = dummy;
628 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
632 if (isnan(rpnstack->s[stptr - 1]));
633 else if (isnan(rpnstack->s[stptr]))
634 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
636 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
637 rpnstack->s[stptr] ? 1.0 : 0.0;
642 if (isnan(rpnstack->s[stptr - 1]));
643 else if (isnan(rpnstack->s[stptr]))
644 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
646 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
647 rpnstack->s[stptr] ? 1.0 : 0.0;
652 if (isnan(rpnstack->s[stptr - 1]));
653 else if (isnan(rpnstack->s[stptr]))
654 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
656 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
657 rpnstack->s[stptr] ? 1.0 : 0.0;
662 if (isnan(rpnstack->s[stptr - 1]));
663 else if (isnan(rpnstack->s[stptr]))
664 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
666 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
667 rpnstack->s[stptr] ? 1.0 : 0.0;
672 if (isnan(rpnstack->s[stptr - 1]));
673 else if (isnan(rpnstack->s[stptr]))
674 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
676 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
677 rpnstack->s[stptr] ? 0.0 : 1.0;
682 if (isnan(rpnstack->s[stptr - 1]));
683 else if (isnan(rpnstack->s[stptr]))
684 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
686 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
687 rpnstack->s[stptr] ? 1.0 : 0.0;
692 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 2] != 0.0 ?
693 rpnstack->s[stptr - 1] : rpnstack->s[stptr];
699 if (isnan(rpnstack->s[stptr - 1]));
700 else if (isnan(rpnstack->s[stptr]))
701 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
702 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
703 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
708 if (isnan(rpnstack->s[stptr - 1]));
709 else if (isnan(rpnstack->s[stptr]))
710 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
711 else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
712 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
717 if (isnan(rpnstack->s[stptr - 2]));
718 else if (isnan(rpnstack->s[stptr - 1]))
719 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
720 else if (isnan(rpnstack->s[stptr]))
721 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
722 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
723 rpnstack->s[stptr - 2] = DNAN;
724 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
725 rpnstack->s[stptr - 2] = DNAN;
730 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
734 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
738 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
743 int spn = (int) rpnstack->s[stptr--];
745 stackunderflow(spn - 1);
746 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
753 int spn = (int) rpnstack->s[stptr--];
756 stackunderflow(spn - 1);
758 p = rpnstack->s + stptr - spn + 1;
759 q = rpnstack->s + stptr;
771 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
772 rrd_set_error("malformed trend arguments");
775 time_t dur = (time_t) rpnstack->s[stptr];
776 time_t step = (time_t) rpnp[rpi - 2].step;
778 if (output_idx > (int) ceil((float) dur / (float) step)) {
779 int ignorenan = (rpnp[rpi].op == OP_TREND);
786 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
787 if (ignorenan || !isnan(val)) {
795 rpnstack->s[--stptr] =
796 (count == 0) ? DNAN : (accum / count);
798 rpnstack->s[--stptr] = DNAN;
804 int i = (int) rpnstack->s[stptr--];
808 stackunderflow(i - 1);
810 double val = rpnstack->s[stptr--];
819 /* now push the result back on stack */
821 rpnstack->s[++stptr] = sum / count;
823 rpnstack->s[++stptr] = DNAN;
829 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
834 #undef stackunderflow
837 rrd_set_error("RPN final stack size != 1");
841 output[output_idx] = rpnstack->s[0];
845 /* figure out what the local timezone offset for any point in
846 time was. Return it in seconds */
850 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
851 l_sec, l_min, l_hour, l_yday, l_year;
861 localtime_r(&now, &t);
868 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
869 if (l_yday > gm_yday || l_year > gm_year) {
871 } else if (l_yday < gm_yday || l_year < gm_year) {