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"
17 unsigned short *offset);
19 time_t); /* used to implement LTIME */
29 /* count the number of rpn nodes */
30 while (rpnp[*count].op != OP_END)
32 if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
33 rrd_set_error("Maximum %d RPN nodes permitted",
34 DS_CDEF_MAX_RPN_NODES);
39 *rpnc = (rpn_cdefds_t *) calloc(*count, sizeof(rpn_cdefds_t));
40 for (i = 0; rpnp[i].op != OP_END; i++) {
41 (*rpnc)[i].op = (char) rpnp[i].op;
42 if (rpnp[i].op == OP_NUMBER) {
43 /* rpnp.val is a double, rpnc.val is a short */
44 double temp = floor(rpnp[i].val);
46 if (temp < SHRT_MIN || temp > SHRT_MAX) {
48 ("constants must be integers in the interval (%d, %d)",
53 (*rpnc)[i].val = (short) temp;
54 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
55 (*rpnc)[i].val = (short) rpnp[i].ptr;
58 /* terminate the sequence */
59 (*rpnc)[(*count) - 1].op = OP_END;
69 /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
70 * memory we avoid any reallocs */
71 rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES, sizeof(rpnp_t));
74 for (i = 0; rpnc[i].op != OP_END; ++i) {
75 rpnp[i].op = (long) rpnc[i].op;
76 if (rpnp[i].op == OP_NUMBER) {
77 rpnp[i].val = (double) rpnc[i].val;
78 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
79 rpnp[i].ptr = (long) rpnc[i].val;
82 /* terminate the sequence */
87 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
88 * into a CDEF string. This function is used by rrd_dump.
90 * rpnc: an array of compact RPN operator nodes
91 * ds_def: a pointer to the data source definition section of an RRD header
92 * for lookup of data source names by index
93 * str: out string, memory is allocated by the function, must be freed by the
100 unsigned short i, offset = 0;
101 char buffer[7]; /* short as a string */
103 for (i = 0; rpnc[i].op != OP_END; i++) {
105 (*str)[offset++] = ',';
107 #define add_op(VV,VVV) \
108 if (addop2str(rpnc[i].op, VV, VVV, str, &offset) == 1) continue;
110 if (rpnc[i].op == OP_NUMBER) {
111 /* convert a short into a string */
112 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
113 _itoa(rpnc[i].val, buffer, 10);
115 sprintf(buffer, "%d", rpnc[i].val);
117 add_op(OP_NUMBER, buffer)
120 if (rpnc[i].op == OP_VARIABLE) {
121 char *ds_name = ds_def[rpnc[i].val].ds_nam;
123 add_op(OP_VARIABLE, ds_name)
126 if (rpnc[i].op == OP_PREV_OTHER) {
127 char *ds_name = ds_def[rpnc[i].val].ds_nam;
129 add_op(OP_VARIABLE, ds_name)
133 #define add_op(VV,VVV) \
134 if (addop2str(rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
144 add_op(OP_FLOOR, FLOOR)
145 add_op(OP_CEIL, CEIL)
158 add_op(OP_LIMIT, LIMIT)
159 add_op(OP_UNKN, UNKN)
161 add_op(OP_NEGINF, NEGINF)
163 add_op(OP_PREV, PREV)
165 add_op(OP_ISINF, ISINF)
167 add_op(OP_LTIME, LTIME)
168 add_op(OP_TIME, TIME)
169 add_op(OP_ATAN2, ATAN2)
170 add_op(OP_ATAN, ATAN)
171 add_op(OP_SQRT, SQRT)
172 add_op(OP_SORT, SORT)
174 add_op(OP_TREND, TREND)
175 add_op(OP_RAD2DEG, RAD2DEG)
176 add_op(OP_DEG2RAD, DEG2RAD)
181 (*str)[offset] = '\0';
190 unsigned short *offset)
195 op_len = strlen(op_str);
196 *result_str = (char *) rrd_realloc(*result_str,
198 *offset) * sizeof(char));
199 if (*result_str == NULL) {
200 rrd_set_error("failed to alloc memory in addop2str");
203 strncpy(&((*result_str)[*offset]), op_str, op_len);
216 rpn_cdefds_t *rpnc = NULL;
219 rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
221 rrd_set_error("failed to parse computed data source");
224 /* Check for OP nodes not permitted in COMPUTE DS.
225 * Moved this check from within rpn_compact() because it really is
226 * COMPUTE DS specific. This is less efficient, but creation doesn't
227 * occur too often. */
228 for (i = 0; rpnp[i].op != OP_END; i++) {
229 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
230 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT) {
232 ("operators time, ltime, prev and count not supported with DS COMPUTE");
237 if (rpn_compact(rpnp, &rpnc, &count) == -1) {
241 /* copy the compact rpn representation over the ds_def par array */
242 memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
243 (void *) rpnc, count * sizeof(rpn_cdefds_t));
248 /* lookup a data source name in the rrd struct and return the index,
249 * should use ds_match() here except:
250 * (1) need a void * pointer to the rrd
251 * (2) error handling is left to the caller
260 rrd = (rrd_t *) rrd_vptr;
262 for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
263 if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
266 /* the caller handles a bad data source name in the rpn string */
270 /* rpn_parse : parse a string and generate a rpnp array; modified
271 * str2rpn() originally included in rrd_graph.c
273 * key_hash: a transparent argument passed to lookup(); conceptually this
274 * is a hash object for lookup of a numeric key given a variable name
275 * expr: the string RPN expression, including variable names
276 * lookup(): a function that retrieves a numeric key given a variable name
280 const char *const expr_const,
281 long (*lookup) (void *,
288 char vname[MAX_VNAME_LEN + 10];
291 expr = (char *) expr_const;
294 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
295 sizeof(rpnp_t))) == NULL) {
299 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
300 && (expr[pos] == ',')) {
301 rpnp[steps].op = OP_NUMBER;
304 #define match_op(VV,VVV) \
305 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
306 rpnp[steps].op = VV; \
307 expr+=strlen(#VVV); \
311 #define match_op_param(VV,VVV) \
312 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
314 if ((length = strlen(#VVV)+strlen(vname)+2, \
315 expr[length] == ',' || expr[length] == '\0') ) { \
316 rpnp[steps].op = VV; \
317 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
318 if (rpnp[steps].ptr < 0) { \
321 } else expr+=length; \
330 match_op(OP_SIN, SIN)
331 match_op(OP_COS, COS)
332 match_op(OP_LOG, LOG)
333 match_op(OP_FLOOR, FLOOR)
334 match_op(OP_CEIL, CEIL)
335 match_op(OP_EXP, EXP)
336 match_op(OP_DUP, DUP)
337 match_op(OP_EXC, EXC)
338 match_op(OP_POP, POP)
339 match_op(OP_LTIME, LTIME)
346 match_op(OP_MIN, MIN)
347 match_op(OP_MAX, MAX)
348 match_op(OP_LIMIT, LIMIT)
349 /* order is important here ! .. match longest first */
350 match_op(OP_UNKN, UNKN)
352 match_op(OP_NEGINF, NEGINF)
354 match_op(OP_COUNT, COUNT)
355 match_op_param(OP_PREV_OTHER, PREV)
356 match_op(OP_PREV, PREV)
357 match_op(OP_INF, INF)
358 match_op(OP_ISINF, ISINF)
359 match_op(OP_NOW, NOW)
360 match_op(OP_TIME, TIME)
361 match_op(OP_ATAN2, ATAN2)
362 match_op(OP_ATAN, ATAN)
363 match_op(OP_SQRT, SQRT)
364 match_op(OP_SORT, SORT)
365 match_op(OP_REV, REV)
366 match_op(OP_TREND, TREND)
367 match_op(OP_RAD2DEG, RAD2DEG)
368 match_op(OP_DEG2RAD, DEG2RAD)
369 match_op(OP_AVG, AVG)
370 match_op(OP_ABS, ABS)
372 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
373 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
375 rpnp[steps].op = OP_VARIABLE;
392 rpnp[steps + 1].op = OP_END;
397 rpnstack_t *rpnstack)
400 rpnstack->dc_stacksize = 0;
401 rpnstack->dc_stackblock = 100;
405 rpnstack_t *rpnstack)
407 if (rpnstack->s != NULL)
409 rpnstack->dc_stacksize = 0;
412 static int rpn_compare_double(
416 double diff = *((const double *) x) - *((const double *) y);
418 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
421 /* rpn_calc: run the RPN calculator; also performs variable substitution;
422 * moved and modified from data_calc() originally included in rrd_graph.c
424 * rpnp : an array of RPN operators (including variable references)
425 * rpnstack : the initialized stack
426 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
427 * is advanced by rpnp.ds_cnt; used only for variable substitution
428 * output : an array of output values; OP_PREV assumes this array contains
429 * the "previous" value at index position output_idx-1; the definition of
430 * "previous" depends on the calling environment
431 * output_idx : an index into the output array in which to store the output
432 * of the RPN calculator
433 * returns: -1 if the computation failed (also calls rrd_set_error)
438 rpnstack_t *rpnstack,
446 /* process each op from the rpn in turn */
447 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
448 /* allocate or grow the stack */
449 if (stptr + 5 > rpnstack->dc_stacksize) {
450 /* could move this to a separate function */
451 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
452 rpnstack->s = rrd_realloc(rpnstack->s,
453 (rpnstack->dc_stacksize) *
454 sizeof(*(rpnstack->s)));
455 if (rpnstack->s == NULL) {
456 rrd_set_error("RPN stack overflow");
460 #define stackunderflow(MINSIZE) \
462 rrd_set_error("RPN stack underflow"); \
466 switch (rpnp[rpi].op) {
468 rpnstack->s[++stptr] = rpnp[rpi].val;
472 /* Sanity check: VDEFs shouldn't make it here */
473 if (rpnp[rpi].ds_cnt == 0) {
474 rrd_set_error("VDEF made it into rpn_calc... aborting");
477 /* make sure we pull the correct value from
478 * the *.data array. Adjust the pointer into
479 * the array acordingly. Advance the ptr one
480 * row in the rra (skip over non-relevant
483 if (rpnp[rpi].op == OP_VARIABLE) {
484 rpnstack->s[++stptr] = *(rpnp[rpi].data);
486 if ((output_idx) <= 0) {
487 rpnstack->s[++stptr] = DNAN;
489 rpnstack->s[++stptr] =
490 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
494 if (data_idx % rpnp[rpi].step == 0) {
495 rpnp[rpi].data += rpnp[rpi].ds_cnt;
500 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
503 if ((output_idx) <= 0) {
504 rpnstack->s[++stptr] = DNAN;
506 rpnstack->s[++stptr] = output[output_idx - 1];
510 rpnstack->s[++stptr] = DNAN;
513 rpnstack->s[++stptr] = DINF;
516 rpnstack->s[++stptr] = -DINF;
519 rpnstack->s[++stptr] = (double) time(NULL);
522 /* HACK: this relies on the data_idx being the time,
523 ** which the within-function scope is unaware of */
524 rpnstack->s[++stptr] = (double) data_idx;
527 rpnstack->s[++stptr] =
528 (double) tzoffset(data_idx) + (double) data_idx;
532 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
533 + rpnstack->s[stptr];
538 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
539 - rpnstack->s[stptr];
544 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
545 * (rpnstack->s[stptr]);
550 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
551 / rpnstack->s[stptr];
556 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
557 , rpnstack->s[stptr]);
562 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
566 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
570 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
574 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
578 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
584 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
588 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
592 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
596 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
600 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
612 dummy = rpnstack->s[stptr];
613 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
614 rpnstack->s[stptr - 1] = dummy;
619 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
623 if (isnan(rpnstack->s[stptr - 1]));
624 else if (isnan(rpnstack->s[stptr]))
625 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
627 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
628 rpnstack->s[stptr] ? 1.0 : 0.0;
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] ? 0.0 : 1.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] ? 1.0 : 0.0;
683 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 2] != 0.0 ?
684 rpnstack->s[stptr - 1] : rpnstack->s[stptr];
690 if (isnan(rpnstack->s[stptr - 1]));
691 else if (isnan(rpnstack->s[stptr]))
692 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
693 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
694 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 - 2]));
709 else if (isnan(rpnstack->s[stptr - 1]))
710 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
711 else if (isnan(rpnstack->s[stptr]))
712 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
713 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
714 rpnstack->s[stptr - 2] = DNAN;
715 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
716 rpnstack->s[stptr - 2] = DNAN;
721 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
725 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
729 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
734 int spn = (int) rpnstack->s[stptr--];
736 stackunderflow(spn - 1);
737 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
744 int spn = (int) rpnstack->s[stptr--];
747 stackunderflow(spn - 1);
749 p = rpnstack->s + stptr - spn + 1;
750 q = rpnstack->s + stptr;
761 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
762 rrd_set_error("malformed trend arguments");
765 time_t dur = (time_t) rpnstack->s[stptr];
766 time_t step = (time_t) rpnp[rpi - 2].step;
768 if (output_idx > (int) ceil((float) dur / (float) step)) {
774 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
778 rpnstack->s[--stptr] = (accum / -i);
780 rpnstack->s[--stptr] = DNAN;
786 int i = (int) rpnstack->s[stptr--];
790 stackunderflow(i - 1);
792 double val = rpnstack->s[stptr--];
801 /* now push the result back on stack */
803 rpnstack->s[++stptr] = sum / count;
805 rpnstack->s[++stptr] = DNAN;
811 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
816 #undef stackunderflow
819 rrd_set_error("RPN final stack size != 1");
823 output[output_idx] = rpnstack->s[0];
827 /* figure out what the local timezone offset for any point in
828 time was. Return it in seconds */
832 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
833 l_sec, l_min, l_hour, l_yday, l_year;
843 localtime_r(&now, &t);
850 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
851 if (l_yday > gm_yday || l_year > gm_year) {
853 } else if (l_yday < gm_yday || l_year < gm_year) {