1 /****************************************************************************
2 * RRDtool 1.2.11 Copyright by Tobi Oetiker, 1997-2005
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
8 #include "rrd_rpncalc.h"
12 short addop2str(enum op_en op, enum op_en op_type, char *op_str,
13 char **result_str, unsigned short *offset);
14 int tzoffset(time_t); /* used to implement LTIME */
16 short rpn_compact(rpnp_t *rpnp, rpn_cdefds_t **rpnc, short *count)
20 /* count the number of rpn nodes */
21 while(rpnp[*count].op != OP_END) (*count)++;
22 if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
23 rrd_set_error("Maximum %d RPN nodes permitted",
24 DS_CDEF_MAX_RPN_NODES);
29 *rpnc = (rpn_cdefds_t *) calloc(*count,sizeof(rpn_cdefds_t));
30 for (i = 0; rpnp[i].op != OP_END; i++)
32 (*rpnc)[i].op = (char) rpnp[i].op;
33 if (rpnp[i].op == OP_NUMBER) {
34 /* rpnp.val is a double, rpnc.val is a short */
35 double temp = floor(rpnp[i].val);
36 if (temp < SHRT_MIN || temp > SHRT_MAX) {
38 "constants must be integers in the interval (%d, %d)",
43 (*rpnc)[i].val = (short) temp;
44 } else if (rpnp[i].op == OP_VARIABLE ||
45 rpnp[i].op == OP_PREV_OTHER) {
46 (*rpnc)[i].val = (short) rpnp[i].ptr;
49 /* terminate the sequence */
50 (*rpnc)[(*count) - 1].op = OP_END;
54 rpnp_t * rpn_expand(rpn_cdefds_t *rpnc)
59 /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
60 * memory we avoid any reallocs */
61 rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES,sizeof(rpnp_t));
62 if (rpnp == NULL) return NULL;
63 for (i = 0; rpnc[i].op != OP_END; ++i)
65 rpnp[i].op = (long) rpnc[i].op;
66 if (rpnp[i].op == OP_NUMBER) {
67 rpnp[i].val = (double) rpnc[i].val;
68 } else if (rpnp[i].op == OP_VARIABLE ||
69 rpnp[i].op == OP_PREV_OTHER) {
70 rpnp[i].ptr = (long) rpnc[i].val;
73 /* terminate the sequence */
78 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
79 * into a CDEF string. This function is used by rrd_dump.
81 * rpnc: an array of compact RPN operator nodes
82 * ds_def: a pointer to the data source definition section of an RRD header
83 * for lookup of data source names by index
84 * str: out string, memory is allocated by the function, must be freed by the
86 void rpn_compact2str(rpn_cdefds_t *rpnc,ds_def_t *ds_def,char **str)
88 unsigned short i,offset = 0;
89 char buffer[7]; /* short as a string */
91 for (i = 0; rpnc[i].op != OP_END; i++)
93 if (i > 0) (*str)[offset++] = ',';
95 #define add_op(VV,VVV) \
96 if (addop2str(rpnc[i].op, VV, VVV, str, &offset) == 1) continue;
98 if (rpnc[i].op == OP_NUMBER) {
99 /* convert a short into a string */
100 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
101 _itoa(rpnc[i].val,buffer,10);
103 sprintf(buffer,"%d",rpnc[i].val);
105 add_op(OP_NUMBER,buffer)
108 if (rpnc[i].op == OP_VARIABLE) {
109 char *ds_name = ds_def[rpnc[i].val].ds_nam;
110 add_op(OP_VARIABLE, ds_name)
113 if (rpnc[i].op == OP_PREV_OTHER) {
114 char *ds_name = ds_def[rpnc[i].val].ds_nam;
115 add_op(OP_VARIABLE, ds_name)
120 #define add_op(VV,VVV) \
121 if (addop2str(rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
131 add_op(OP_FLOOR,FLOOR)
145 add_op(OP_LIMIT,LIMIT)
148 add_op(OP_NEGINF,NEGINF)
152 add_op(OP_ISINF,ISINF)
154 add_op(OP_LTIME,LTIME)
156 add_op(OP_ATAN2,ATAN2)
161 add_op(OP_TREND,TREND)
162 add_op(OP_RAD2DEG,RAD2DEG)
163 add_op(OP_DEG2RAD,DEG2RAD)
166 (*str)[offset] = '\0';
170 short addop2str(enum op_en op, enum op_en op_type, char *op_str,
171 char **result_str, unsigned short *offset)
175 op_len = strlen(op_str);
176 *result_str = (char *) rrd_realloc(*result_str,
177 (op_len + 1 + *offset)*sizeof(char));
178 if (*result_str == NULL) {
179 rrd_set_error("failed to alloc memory in addop2str");
182 strncpy(&((*result_str)[*offset]),op_str,op_len);
189 void parseCDEF_DS(char *def,rrd_t *rrd, int ds_idx)
192 rpn_cdefds_t *rpnc = NULL;
195 rpnp = rpn_parse((void*) rrd, def, &lookup_DS);
197 rrd_set_error("failed to parse computed data source %s", def);
200 /* Check for OP nodes not permitted in COMPUTE DS.
201 * Moved this check from within rpn_compact() because it really is
202 * COMPUTE DS specific. This is less efficient, but creation doesn't
203 * occur too often. */
204 for (i = 0; rpnp[i].op != OP_END; i++) {
205 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
206 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT)
209 "operators time, ltime, prev and count not supported with DS COMPUTE");
214 if (rpn_compact(rpnp,&rpnc,&count) == -1) {
218 /* copy the compact rpn representation over the ds_def par array */
219 memcpy((void*) &(rrd -> ds_def[ds_idx].par[DS_cdef]),
220 (void*) rpnc, count*sizeof(rpn_cdefds_t));
225 /* lookup a data source name in the rrd struct and return the index,
226 * should use ds_match() here except:
227 * (1) need a void * pointer to the rrd
228 * (2) error handling is left to the caller
230 long lookup_DS(void *rrd_vptr,char *ds_name)
235 rrd = (rrd_t *) rrd_vptr;
237 for (i = 0; i < rrd -> stat_head -> ds_cnt; ++i)
239 if(strcmp(ds_name,rrd -> ds_def[i].ds_nam) == 0)
242 /* the caller handles a bad data source name in the rpn string */
246 /* rpn_parse : parse a string and generate a rpnp array; modified
247 * str2rpn() originally included in rrd_graph.c
249 * key_hash: a transparent argument passed to lookup(); conceptually this
250 * is a hash object for lookup of a numeric key given a variable name
251 * expr: the string RPN expression, including variable names
252 * lookup(): a function that retrieves a numeric key given a variable name
255 rpn_parse(void *key_hash,char *expr,long (*lookup)(void *,char*)){
264 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2)*
265 sizeof(rpnp_t)))==NULL){
269 else if((sscanf(expr,"%lf%n",&rpnp[steps].val,&pos) == 1) && (expr[pos] == ',')){
270 rpnp[steps].op = OP_NUMBER;
274 #define match_op(VV,VVV) \
275 else if (strncmp(expr, #VVV, strlen(#VVV))==0){ \
276 rpnp[steps].op = VV; \
277 expr+=strlen(#VVV); \
281 #define match_op_param(VV,VVV) \
282 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
284 if ((length = strlen(#VVV)+strlen(vname)+2, \
285 expr[length] == ',' || expr[length] == '\0') ) { \
286 rpnp[steps].op = VV; \
287 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
288 if (rpnp[steps].ptr < 0) { \
291 } else expr+=length; \
303 match_op(OP_FLOOR,FLOOR)
304 match_op(OP_CEIL,CEIL)
309 match_op(OP_LTIME,LTIME)
318 match_op(OP_LIMIT,LIMIT)
319 /* order is important here ! .. match longest first */
320 match_op(OP_UNKN,UNKN)
322 match_op(OP_NEGINF,NEGINF)
324 match_op(OP_COUNT,COUNT)
325 match_op_param(OP_PREV_OTHER,PREV)
326 match_op(OP_PREV,PREV)
328 match_op(OP_ISINF,ISINF)
330 match_op(OP_TIME,TIME)
331 match_op(OP_ATAN2,ATAN2)
332 match_op(OP_ATAN,ATAN)
333 match_op(OP_SQRT,SQRT)
334 match_op(OP_SORT,SORT)
336 match_op(OP_TREND,TREND)
337 match_op(OP_RAD2DEG,RAD2DEG)
338 match_op(OP_DEG2RAD,DEG2RAD)
342 else if ((sscanf(expr, DEF_NAM_FMT "%n",
344 && ((rpnp[steps].ptr = (*lookup)(key_hash,vname)) != -1)){
345 rpnp[steps].op = OP_VARIABLE;
362 rpnp[steps+1].op = OP_END;
367 rpnstack_init(rpnstack_t *rpnstack)
369 rpnstack -> s = NULL;
370 rpnstack -> dc_stacksize = 0;
371 rpnstack -> dc_stackblock = 100;
375 rpnstack_free(rpnstack_t *rpnstack)
377 if (rpnstack -> s != NULL)
379 rpnstack -> dc_stacksize = 0;
383 rpn_compare_double(const void *x, const void *y)
385 double diff = *((const double *)x) - *((const double *)y);
387 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
390 /* rpn_calc: run the RPN calculator; also performs variable substitution;
391 * moved and modified from data_calc() originally included in rrd_graph.c
393 * rpnp : an array of RPN operators (including variable references)
394 * rpnstack : the initialized stack
395 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
396 * is advanced by rpnp.ds_cnt; used only for variable substitution
397 * output : an array of output values; OP_PREV assumes this array contains
398 * the "previous" value at index position output_idx-1; the definition of
399 * "previous" depends on the calling environment
400 * output_idx : an index into the output array in which to store the output
401 * of the RPN calculator
402 * returns: -1 if the computation failed (also calls rrd_set_error)
406 rpn_calc(rpnp_t *rpnp, rpnstack_t *rpnstack, long data_idx,
407 rrd_value_t *output, int output_idx)
412 /* process each op from the rpn in turn */
413 for (rpi=0; rpnp[rpi].op != OP_END; rpi++){
414 /* allocate or grow the stack */
415 if (stptr + 5 > rpnstack -> dc_stacksize){
416 /* could move this to a separate function */
417 rpnstack -> dc_stacksize += rpnstack -> dc_stackblock;
418 rpnstack -> s = rrd_realloc(rpnstack -> s,
419 (rpnstack -> dc_stacksize)*sizeof(*(rpnstack -> s)));
420 if (rpnstack -> s == NULL){
421 rrd_set_error("RPN stack overflow");
426 #define stackunderflow(MINSIZE) \
428 rrd_set_error("RPN stack underflow"); \
432 switch (rpnp[rpi].op){
434 rpnstack -> s[++stptr] = rpnp[rpi].val;
438 /* Sanity check: VDEFs shouldn't make it here */
439 if (rpnp[rpi].ds_cnt == 0) {
440 rrd_set_error("VDEF made it into rpn_calc... aborting");
443 /* make sure we pull the correct value from
444 * the *.data array. Adjust the pointer into
445 * the array acordingly. Advance the ptr one
446 * row in the rra (skip over non-relevant
449 if (rpnp[rpi].op == OP_VARIABLE) {
450 rpnstack -> s[++stptr] = *(rpnp[rpi].data);
452 if ((output_idx) <= 0) {
453 rpnstack -> s[++stptr] = DNAN;
455 rpnstack -> s[++stptr] = *(rpnp[rpi].data-rpnp[rpi].ds_cnt);
459 if (data_idx % rpnp[rpi].step == 0){
460 rpnp[rpi].data += rpnp[rpi].ds_cnt;
465 rpnstack -> s[++stptr] = (output_idx+1); /* Note: Counter starts at 1 */
468 if ((output_idx) <= 0) {
469 rpnstack -> s[++stptr] = DNAN;
471 rpnstack -> s[++stptr] = output[output_idx-1];
475 rpnstack -> s[++stptr] = DNAN;
478 rpnstack -> s[++stptr] = DINF;
481 rpnstack -> s[++stptr] = -DINF;
484 rpnstack -> s[++stptr] = (double)time(NULL);
487 /* HACK: this relies on the data_idx being the time,
488 ** which the within-function scope is unaware of */
489 rpnstack -> s[++stptr] = (double) data_idx;
492 rpnstack -> s[++stptr] =
493 (double) tzoffset(data_idx) + (double)data_idx;
497 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1]
498 + rpnstack -> s[stptr];
503 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1]
504 - rpnstack -> s[stptr];
509 rpnstack -> s[stptr-1] = (rpnstack -> s[stptr-1])
510 * (rpnstack -> s[stptr]);
515 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1]
516 / rpnstack -> s[stptr];
521 rpnstack -> s[stptr-1]= fmod( rpnstack -> s[stptr-1]
522 ,rpnstack -> s[stptr]);
527 rpnstack -> s[stptr] = sin(rpnstack -> s[stptr]);
531 rpnstack -> s[stptr] = atan(rpnstack -> s[stptr]);
535 rpnstack -> s[stptr] = 57.29577951 * rpnstack -> s[stptr];
539 rpnstack -> s[stptr] = 0.0174532952 * rpnstack -> s[stptr];
543 rpnstack -> s[stptr-1]= atan2(
544 rpnstack -> s[stptr-1],
545 rpnstack -> s[stptr]);
550 rpnstack -> s[stptr] = cos(rpnstack -> s[stptr]);
554 rpnstack -> s[stptr] = ceil(rpnstack -> s[stptr]);
558 rpnstack -> s[stptr] = floor(rpnstack -> s[stptr]);
562 rpnstack -> s[stptr] = log(rpnstack -> s[stptr]);
566 rpnstack -> s[stptr+1] = rpnstack -> s[stptr];
577 dummy = rpnstack -> s[stptr] ;
578 rpnstack -> s[stptr] = rpnstack -> s[stptr-1];
579 rpnstack -> s[stptr-1] = dummy;
584 rpnstack -> s[stptr] = exp(rpnstack -> s[stptr]);
588 if (isnan(rpnstack -> s[stptr-1]))
590 else if (isnan(rpnstack -> s[stptr]))
591 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
593 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] <
594 rpnstack -> s[stptr] ? 1.0 : 0.0;
599 if (isnan(rpnstack -> s[stptr-1]))
601 else if (isnan(rpnstack -> s[stptr]))
602 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
604 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] <=
605 rpnstack -> s[stptr] ? 1.0 : 0.0;
610 if (isnan(rpnstack -> s[stptr-1]))
612 else if (isnan(rpnstack -> s[stptr]))
613 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
615 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] >
616 rpnstack -> s[stptr] ? 1.0 : 0.0;
621 if (isnan(rpnstack -> s[stptr-1]))
623 else if (isnan(rpnstack -> s[stptr]))
624 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
626 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] >=
627 rpnstack -> s[stptr] ? 1.0 : 0.0;
632 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] ? 0.0 : 1.0;
643 if (isnan(rpnstack -> s[stptr-1]))
645 else if (isnan(rpnstack -> s[stptr]))
646 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
648 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] ==
649 rpnstack -> s[stptr] ? 1.0 : 0.0;
654 rpnstack->s[stptr-2] = rpnstack->s[stptr-2] != 0.0 ?
655 rpnstack->s[stptr-1] : rpnstack->s[stptr];
661 if (isnan(rpnstack->s[stptr-1]))
663 else if (isnan(rpnstack->s[stptr]))
664 rpnstack->s[stptr-1] = rpnstack->s[stptr];
665 else if (rpnstack->s[stptr-1] > rpnstack->s[stptr])
666 rpnstack->s[stptr-1] = rpnstack->s[stptr];
671 if (isnan(rpnstack->s[stptr-1]))
673 else if (isnan(rpnstack->s[stptr]))
674 rpnstack->s[stptr-1] = rpnstack->s[stptr];
675 else if (rpnstack->s[stptr-1] < rpnstack->s[stptr])
676 rpnstack->s[stptr-1] = rpnstack->s[stptr];
681 if (isnan(rpnstack->s[stptr-2]))
683 else if (isnan(rpnstack->s[stptr-1]))
684 rpnstack->s[stptr-2] = rpnstack->s[stptr-1];
685 else if (isnan(rpnstack->s[stptr]))
686 rpnstack->s[stptr-2] = rpnstack->s[stptr];
687 else if (rpnstack->s[stptr-2] < rpnstack->s[stptr-1])
688 rpnstack->s[stptr-2] = DNAN;
689 else if (rpnstack->s[stptr-2] > rpnstack->s[stptr])
690 rpnstack->s[stptr-2] = DNAN;
695 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
699 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
703 rpnstack -> s[stptr] = sqrt(rpnstack -> s[stptr]);
708 int spn = (int)rpnstack -> s[stptr--];
710 stackunderflow(spn-1);
711 qsort(rpnstack -> s + stptr-spn+1, spn, sizeof(double),
718 int spn = (int)rpnstack -> s[stptr--];
721 stackunderflow(spn-1);
723 p = rpnstack -> s + stptr-spn+1;
724 q = rpnstack -> s + stptr;
735 if ((rpi < 2) || (rpnp[rpi-2].op != OP_VARIABLE)) {
736 rrd_set_error("malformed trend arguments");
739 time_t dur = (time_t)rpnstack -> s[stptr];
740 time_t step = (time_t)rpnp[rpi-2].step;
742 if (output_idx > (int)ceil((float)dur / (float)step)) {
747 accum += rpnp[rpi-2].data[rpnp[rpi-2].ds_cnt * i--];
751 rpnstack -> s[--stptr] = (accum / -i);
753 rpnstack -> s[--stptr] = DNAN;
759 #undef stackunderflow
762 rrd_set_error("RPN final stack size != 1");
766 output[output_idx] = rpnstack->s[0];
770 /* figure out what the local timezone offset for any point in
771 time was. Return it in seconds */
773 tzoffset( time_t now ){
774 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
775 l_sec, l_min, l_hour, l_yday, l_year;
784 localtime_r(&now, &t);
790 off = (l_sec-gm_sec)+(l_min-gm_min)*60+(l_hour-gm_hour)*3600;
791 if ( l_yday > gm_yday || l_year > gm_year){
793 } else if ( l_yday < gm_yday || l_year < gm_year){