a3faff22763adc407312858146c0864c74189179
1 /****************************************************************************
2 * RRDtool 1.2.28 Copyright by Tobi Oetiker, 1997-2008
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include "rrd_graph.h"
10 #include <limits.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)
17 {
18 short i;
19 *count = 0;
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);
25 return -1;
26 }
28 /* allocate memory */
29 *rpnc = (rpn_cdefds_t *) calloc(*count,sizeof(rpn_cdefds_t));
30 for (i = 0; rpnp[i].op != OP_END; i++)
31 {
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) {
37 rrd_set_error(
38 "constants must be integers in the interval (%d, %d)",
39 SHRT_MIN, SHRT_MAX);
40 free(*rpnc);
41 return -1;
42 }
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;
47 }
48 }
49 /* terminate the sequence */
50 (*rpnc)[(*count) - 1].op = OP_END;
51 return 0;
52 }
54 rpnp_t * rpn_expand(rpn_cdefds_t *rpnc)
55 {
56 short i;
57 rpnp_t *rpnp;
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)
64 {
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;
71 }
72 }
73 /* terminate the sequence */
74 rpnp[i].op = OP_END;
75 return rpnp;
76 }
78 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
79 * into a CDEF string. This function is used by rrd_dump.
80 * arguments:
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
85 * the caller */
86 void rpn_compact2str(rpn_cdefds_t *rpnc,ds_def_t *ds_def,char **str)
87 {
88 unsigned short i,offset = 0;
89 char buffer[7]; /* short as a string */
91 for (i = 0; rpnc[i].op != OP_END; i++)
92 {
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);
102 #else
103 sprintf(buffer,"%d",rpnc[i].val);
104 #endif
105 add_op(OP_NUMBER,buffer)
106 }
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)
111 }
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)
116 }
118 #undef add_op
120 #define add_op(VV,VVV) \
121 if (addop2str(rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
123 add_op(OP_ADD,+)
124 add_op(OP_SUB,-)
125 add_op(OP_MUL,*)
126 add_op(OP_DIV,/)
127 add_op(OP_MOD,%)
128 add_op(OP_SIN,SIN)
129 add_op(OP_COS,COS)
130 add_op(OP_LOG,LOG)
131 add_op(OP_FLOOR,FLOOR)
132 add_op(OP_CEIL,CEIL)
133 add_op(OP_EXP,EXP)
134 add_op(OP_DUP,DUP)
135 add_op(OP_EXC,EXC)
136 add_op(OP_POP,POP)
137 add_op(OP_LT,LT)
138 add_op(OP_LE,LE)
139 add_op(OP_GT,GT)
140 add_op(OP_GE,GE)
141 add_op(OP_EQ,EQ)
142 add_op(OP_IF,IF)
143 add_op(OP_MIN,MIN)
144 add_op(OP_MAX,MAX)
145 add_op(OP_LIMIT,LIMIT)
146 add_op(OP_UNKN,UNKN)
147 add_op(OP_UN,UN)
148 add_op(OP_NEGINF,NEGINF)
149 add_op(OP_NE,NE)
150 add_op(OP_PREV,PREV)
151 add_op(OP_INF,INF)
152 add_op(OP_ISINF,ISINF)
153 add_op(OP_NOW,NOW)
154 add_op(OP_LTIME,LTIME)
155 add_op(OP_TIME,TIME)
156 add_op(OP_ATAN2,ATAN2)
157 add_op(OP_ATAN,ATAN)
158 add_op(OP_SQRT,SQRT)
159 add_op(OP_SORT,SORT)
160 add_op(OP_REV,REV)
161 add_op(OP_TREND,TREND)
162 add_op(OP_RAD2DEG,RAD2DEG)
163 add_op(OP_DEG2RAD,DEG2RAD)
164 add_op(OP_AVG,AVG)
165 add_op(OP_ABS,ABS)
166 #undef add_op
167 }
168 (*str)[offset] = '\0';
170 }
172 short addop2str(enum op_en op, enum op_en op_type, char *op_str,
173 char **result_str, unsigned short *offset)
174 {
175 if (op == op_type) {
176 short op_len;
177 op_len = strlen(op_str);
178 *result_str = (char *) rrd_realloc(*result_str,
179 (op_len + 1 + *offset)*sizeof(char));
180 if (*result_str == NULL) {
181 rrd_set_error("failed to alloc memory in addop2str");
182 return -1;
183 }
184 strncpy(&((*result_str)[*offset]),op_str,op_len);
185 *offset += op_len;
186 return 1;
187 }
188 return 0;
189 }
191 void parseCDEF_DS(const char *def,rrd_t *rrd, int ds_idx)
192 {
193 rpnp_t *rpnp = NULL;
194 rpn_cdefds_t *rpnc = NULL;
195 short count, i;
197 rpnp = rpn_parse((void*) rrd, def, &lookup_DS);
198 if (rpnp == NULL) {
199 rrd_set_error("failed to parse computed data source");
200 return;
201 }
202 /* Check for OP nodes not permitted in COMPUTE DS.
203 * Moved this check from within rpn_compact() because it really is
204 * COMPUTE DS specific. This is less efficient, but creation doesn't
205 * occur too often. */
206 for (i = 0; rpnp[i].op != OP_END; i++) {
207 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
208 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT)
209 {
210 rrd_set_error(
211 "operators time, ltime, prev and count not supported with DS COMPUTE");
212 free(rpnp);
213 return;
214 }
215 }
216 if (rpn_compact(rpnp,&rpnc,&count) == -1) {
217 free(rpnp);
218 return;
219 }
220 /* copy the compact rpn representation over the ds_def par array */
221 memcpy((void*) &(rrd -> ds_def[ds_idx].par[DS_cdef]),
222 (void*) rpnc, count*sizeof(rpn_cdefds_t));
223 free(rpnp);
224 free(rpnc);
225 }
227 /* lookup a data source name in the rrd struct and return the index,
228 * should use ds_match() here except:
229 * (1) need a void * pointer to the rrd
230 * (2) error handling is left to the caller
231 */
232 long lookup_DS(void *rrd_vptr,char *ds_name)
233 {
234 unsigned int i;
235 rrd_t *rrd;
237 rrd = (rrd_t *) rrd_vptr;
239 for (i = 0; i < rrd -> stat_head -> ds_cnt; ++i)
240 {
241 if(strcmp(ds_name,rrd -> ds_def[i].ds_nam) == 0)
242 return i;
243 }
244 /* the caller handles a bad data source name in the rpn string */
245 return -1;
246 }
248 /* rpn_parse : parse a string and generate a rpnp array; modified
249 * str2rpn() originally included in rrd_graph.c
250 * arguments:
251 * key_hash: a transparent argument passed to lookup(); conceptually this
252 * is a hash object for lookup of a numeric key given a variable name
253 * expr: the string RPN expression, including variable names
254 * lookup(): a function that retrieves a numeric key given a variable name
255 */
256 rpnp_t *
257 rpn_parse(void *key_hash,const char *const expr_const,long (*lookup)(void *,char*)){
258 int pos=0;
259 char *expr;
260 long steps=-1;
261 rpnp_t *rpnp;
262 char vname[MAX_VNAME_LEN+10];
264 rpnp=NULL;
265 expr=(char *)expr_const;
267 while(*expr){
268 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2)*
269 sizeof(rpnp_t)))==NULL){
270 return NULL;
271 }
273 else if((sscanf(expr,"%lf%n",&rpnp[steps].val,&pos) == 1) && (expr[pos] == ',')){
274 rpnp[steps].op = OP_NUMBER;
275 expr+=pos;
276 }
278 #define match_op(VV,VVV) \
279 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
280 rpnp[steps].op = VV; \
281 expr+=strlen(#VVV); \
282 }
285 #define match_op_param(VV,VVV) \
286 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
287 int length = 0; \
288 if ((length = strlen(#VVV)+strlen(vname)+2, \
289 expr[length] == ',' || expr[length] == '\0') ) { \
290 rpnp[steps].op = VV; \
291 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
292 if (rpnp[steps].ptr < 0) { \
293 free(rpnp); \
294 return NULL; \
295 } else expr+=length; \
296 } \
297 }
299 match_op(OP_ADD,+)
300 match_op(OP_SUB,-)
301 match_op(OP_MUL,*)
302 match_op(OP_DIV,/)
303 match_op(OP_MOD,%)
304 match_op(OP_SIN,SIN)
305 match_op(OP_COS,COS)
306 match_op(OP_LOG,LOG)
307 match_op(OP_FLOOR,FLOOR)
308 match_op(OP_CEIL,CEIL)
309 match_op(OP_EXP,EXP)
310 match_op(OP_DUP,DUP)
311 match_op(OP_EXC,EXC)
312 match_op(OP_POP,POP)
313 match_op(OP_LTIME,LTIME)
314 match_op(OP_LT,LT)
315 match_op(OP_LE,LE)
316 match_op(OP_GT,GT)
317 match_op(OP_GE,GE)
318 match_op(OP_EQ,EQ)
319 match_op(OP_IF,IF)
320 match_op(OP_MIN,MIN)
321 match_op(OP_MAX,MAX)
322 match_op(OP_LIMIT,LIMIT)
323 /* order is important here ! .. match longest first */
324 match_op(OP_UNKN,UNKN)
325 match_op(OP_UN,UN)
326 match_op(OP_NEGINF,NEGINF)
327 match_op(OP_NE,NE)
328 match_op(OP_COUNT,COUNT)
329 match_op_param(OP_PREV_OTHER,PREV)
330 match_op(OP_PREV,PREV)
331 match_op(OP_INF,INF)
332 match_op(OP_ISINF,ISINF)
333 match_op(OP_NOW,NOW)
334 match_op(OP_TIME,TIME)
335 match_op(OP_ATAN2,ATAN2)
336 match_op(OP_ATAN,ATAN)
337 match_op(OP_SQRT,SQRT)
338 match_op(OP_SORT,SORT)
339 match_op(OP_REV,REV)
340 match_op(OP_TREND,TREND)
341 match_op(OP_RAD2DEG,RAD2DEG)
342 match_op(OP_DEG2RAD,DEG2RAD)
343 match_op(OP_AVG,AVG)
344 match_op(OP_ABS,ABS)
345 #undef match_op
348 else if ((sscanf(expr, DEF_NAM_FMT "%n",
349 vname,&pos) == 1)
350 && ((rpnp[steps].ptr = (*lookup)(key_hash,vname)) != -1)){
351 rpnp[steps].op = OP_VARIABLE;
352 expr+=pos;
353 }
355 else {
356 free(rpnp);
357 return NULL;
358 }
359 if (*expr == 0)
360 break;
361 if (*expr == ',')
362 expr++;
363 else {
364 free(rpnp);
365 return NULL;
366 }
367 }
368 rpnp[steps+1].op = OP_END;
369 return rpnp;
370 }
372 void
373 rpnstack_init(rpnstack_t *rpnstack)
374 {
375 rpnstack -> s = NULL;
376 rpnstack -> dc_stacksize = 0;
377 rpnstack -> dc_stackblock = 100;
378 }
380 void
381 rpnstack_free(rpnstack_t *rpnstack)
382 {
383 if (rpnstack -> s != NULL)
384 free(rpnstack -> s);
385 rpnstack -> dc_stacksize = 0;
386 }
388 static int
389 rpn_compare_double(const void *x, const void *y)
390 {
391 double diff = *((const double *)x) - *((const double *)y);
393 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
394 }
396 /* rpn_calc: run the RPN calculator; also performs variable substitution;
397 * moved and modified from data_calc() originally included in rrd_graph.c
398 * arguments:
399 * rpnp : an array of RPN operators (including variable references)
400 * rpnstack : the initialized stack
401 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
402 * is advanced by rpnp.ds_cnt; used only for variable substitution
403 * output : an array of output values; OP_PREV assumes this array contains
404 * the "previous" value at index position output_idx-1; the definition of
405 * "previous" depends on the calling environment
406 * output_idx : an index into the output array in which to store the output
407 * of the RPN calculator
408 * returns: -1 if the computation failed (also calls rrd_set_error)
409 * 0 on success
410 */
411 short
412 rpn_calc(rpnp_t *rpnp, rpnstack_t *rpnstack, long data_idx,
413 rrd_value_t *output, int output_idx)
414 {
415 int rpi;
416 long stptr = -1;
418 /* process each op from the rpn in turn */
419 for (rpi=0; rpnp[rpi].op != OP_END; rpi++){
420 /* allocate or grow the stack */
421 if (stptr + 5 > rpnstack -> dc_stacksize){
422 /* could move this to a separate function */
423 rpnstack -> dc_stacksize += rpnstack -> dc_stackblock;
424 rpnstack -> s = rrd_realloc(rpnstack -> s,
425 (rpnstack -> dc_stacksize)*sizeof(*(rpnstack -> s)));
426 if (rpnstack -> s == NULL){
427 rrd_set_error("RPN stack overflow");
428 return -1;
429 }
430 }
432 #define stackunderflow(MINSIZE) \
433 if(stptr<MINSIZE){ \
434 rrd_set_error("RPN stack underflow"); \
435 return -1; \
436 }
438 switch (rpnp[rpi].op){
439 case OP_NUMBER:
440 rpnstack -> s[++stptr] = rpnp[rpi].val;
441 break;
442 case OP_VARIABLE:
443 case OP_PREV_OTHER:
444 /* Sanity check: VDEFs shouldn't make it here */
445 if (rpnp[rpi].ds_cnt == 0) {
446 rrd_set_error("VDEF made it into rpn_calc... aborting");
447 return -1;
448 } else {
449 /* make sure we pull the correct value from
450 * the *.data array. Adjust the pointer into
451 * the array acordingly. Advance the ptr one
452 * row in the rra (skip over non-relevant
453 * data sources)
454 */
455 if (rpnp[rpi].op == OP_VARIABLE) {
456 rpnstack -> s[++stptr] = *(rpnp[rpi].data);
457 } else {
458 if ((output_idx) <= 0) {
459 rpnstack -> s[++stptr] = DNAN;
460 } else {
461 rpnstack -> s[++stptr] = *(rpnp[rpi].data-rpnp[rpi].ds_cnt);
462 }
464 }
465 if (data_idx % rpnp[rpi].step == 0){
466 rpnp[rpi].data += rpnp[rpi].ds_cnt;
467 }
468 }
469 break;
470 case OP_COUNT:
471 rpnstack -> s[++stptr] = (output_idx+1); /* Note: Counter starts at 1 */
472 break;
473 case OP_PREV:
474 if ((output_idx) <= 0) {
475 rpnstack -> s[++stptr] = DNAN;
476 } else {
477 rpnstack -> s[++stptr] = output[output_idx-1];
478 }
479 break;
480 case OP_UNKN:
481 rpnstack -> s[++stptr] = DNAN;
482 break;
483 case OP_INF:
484 rpnstack -> s[++stptr] = DINF;
485 break;
486 case OP_NEGINF:
487 rpnstack -> s[++stptr] = -DINF;
488 break;
489 case OP_NOW:
490 rpnstack -> s[++stptr] = (double)time(NULL);
491 break;
492 case OP_TIME:
493 /* HACK: this relies on the data_idx being the time,
494 ** which the within-function scope is unaware of */
495 rpnstack -> s[++stptr] = (double) data_idx;
496 break;
497 case OP_LTIME:
498 rpnstack -> s[++stptr] =
499 (double) tzoffset(data_idx) + (double)data_idx;
500 break;
501 case OP_ADD:
502 stackunderflow(1);
503 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1]
504 + rpnstack -> s[stptr];
505 stptr--;
506 break;
507 case OP_SUB:
508 stackunderflow(1);
509 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1]
510 - rpnstack -> s[stptr];
511 stptr--;
512 break;
513 case OP_MUL:
514 stackunderflow(1);
515 rpnstack -> s[stptr-1] = (rpnstack -> s[stptr-1])
516 * (rpnstack -> s[stptr]);
517 stptr--;
518 break;
519 case OP_DIV:
520 stackunderflow(1);
521 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1]
522 / rpnstack -> s[stptr];
523 stptr--;
524 break;
525 case OP_MOD:
526 stackunderflow(1);
527 rpnstack -> s[stptr-1]= fmod( rpnstack -> s[stptr-1]
528 ,rpnstack -> s[stptr]);
529 stptr--;
530 break;
531 case OP_SIN:
532 stackunderflow(0);
533 rpnstack -> s[stptr] = sin(rpnstack -> s[stptr]);
534 break;
535 case OP_ATAN:
536 stackunderflow(0);
537 rpnstack -> s[stptr] = atan(rpnstack -> s[stptr]);
538 break;
539 case OP_RAD2DEG:
540 stackunderflow(0);
541 rpnstack -> s[stptr] = 57.29577951 * rpnstack -> s[stptr];
542 break;
543 case OP_DEG2RAD:
544 stackunderflow(0);
545 rpnstack -> s[stptr] = 0.0174532952 * rpnstack -> s[stptr];
546 break;
547 case OP_ATAN2:
548 stackunderflow(1);
549 rpnstack -> s[stptr-1]= atan2(
550 rpnstack -> s[stptr-1],
551 rpnstack -> s[stptr]);
552 stptr--;
553 break;
554 case OP_COS:
555 stackunderflow(0);
556 rpnstack -> s[stptr] = cos(rpnstack -> s[stptr]);
557 break;
558 case OP_CEIL:
559 stackunderflow(0);
560 rpnstack -> s[stptr] = ceil(rpnstack -> s[stptr]);
561 break;
562 case OP_FLOOR:
563 stackunderflow(0);
564 rpnstack -> s[stptr] = floor(rpnstack -> s[stptr]);
565 break;
566 case OP_LOG:
567 stackunderflow(0);
568 rpnstack -> s[stptr] = log(rpnstack -> s[stptr]);
569 break;
570 case OP_DUP:
571 stackunderflow(0);
572 rpnstack -> s[stptr+1] = rpnstack -> s[stptr];
573 stptr++;
574 break;
575 case OP_POP:
576 stackunderflow(0);
577 stptr--;
578 break;
579 case OP_EXC:
580 stackunderflow(1);
581 {
582 double dummy;
583 dummy = rpnstack -> s[stptr] ;
584 rpnstack -> s[stptr] = rpnstack -> s[stptr-1];
585 rpnstack -> s[stptr-1] = dummy;
586 }
587 break;
588 case OP_EXP:
589 stackunderflow(0);
590 rpnstack -> s[stptr] = exp(rpnstack -> s[stptr]);
591 break;
592 case OP_LT:
593 stackunderflow(1);
594 if (isnan(rpnstack -> s[stptr-1]))
595 ;
596 else if (isnan(rpnstack -> s[stptr]))
597 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
598 else
599 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] <
600 rpnstack -> s[stptr] ? 1.0 : 0.0;
601 stptr--;
602 break;
603 case OP_LE:
604 stackunderflow(1);
605 if (isnan(rpnstack -> s[stptr-1]))
606 ;
607 else if (isnan(rpnstack -> s[stptr]))
608 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
609 else
610 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] <=
611 rpnstack -> s[stptr] ? 1.0 : 0.0;
612 stptr--;
613 break;
614 case OP_GT:
615 stackunderflow(1);
616 if (isnan(rpnstack -> s[stptr-1]))
617 ;
618 else if (isnan(rpnstack -> s[stptr]))
619 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
620 else
621 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] >
622 rpnstack -> s[stptr] ? 1.0 : 0.0;
623 stptr--;
624 break;
625 case OP_GE:
626 stackunderflow(1);
627 if (isnan(rpnstack -> s[stptr-1]))
628 ;
629 else if (isnan(rpnstack -> s[stptr]))
630 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
631 else
632 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] >=
633 rpnstack -> s[stptr] ? 1.0 : 0.0;
634 stptr--;
635 break;
636 case OP_NE:
637 stackunderflow(1);
638 if (isnan(rpnstack -> s[stptr-1]))
639 ;
640 else if (isnan(rpnstack -> s[stptr]))
641 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
642 else
643 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] ==
644 rpnstack -> s[stptr] ? 0.0 : 1.0;
645 stptr--;
646 break;
647 case OP_EQ:
648 stackunderflow(1);
649 if (isnan(rpnstack -> s[stptr-1]))
650 ;
651 else if (isnan(rpnstack -> s[stptr]))
652 rpnstack -> s[stptr-1] = rpnstack -> s[stptr];
653 else
654 rpnstack -> s[stptr-1] = rpnstack -> s[stptr-1] ==
655 rpnstack -> s[stptr] ? 1.0 : 0.0;
656 stptr--;
657 break;
658 case OP_IF:
659 stackunderflow(2);
660 rpnstack->s[stptr-2] = rpnstack->s[stptr-2] != 0.0 ?
661 rpnstack->s[stptr-1] : rpnstack->s[stptr];
662 stptr--;
663 stptr--;
664 break;
665 case OP_MIN:
666 stackunderflow(1);
667 if (isnan(rpnstack->s[stptr-1]))
668 ;
669 else if (isnan(rpnstack->s[stptr]))
670 rpnstack->s[stptr-1] = rpnstack->s[stptr];
671 else if (rpnstack->s[stptr-1] > rpnstack->s[stptr])
672 rpnstack->s[stptr-1] = rpnstack->s[stptr];
673 stptr--;
674 break;
675 case OP_MAX:
676 stackunderflow(1);
677 if (isnan(rpnstack->s[stptr-1]))
678 ;
679 else if (isnan(rpnstack->s[stptr]))
680 rpnstack->s[stptr-1] = rpnstack->s[stptr];
681 else if (rpnstack->s[stptr-1] < rpnstack->s[stptr])
682 rpnstack->s[stptr-1] = rpnstack->s[stptr];
683 stptr--;
684 break;
685 case OP_LIMIT:
686 stackunderflow(2);
687 if (isnan(rpnstack->s[stptr-2]))
688 ;
689 else if (isnan(rpnstack->s[stptr-1]))
690 rpnstack->s[stptr-2] = rpnstack->s[stptr-1];
691 else if (isnan(rpnstack->s[stptr]))
692 rpnstack->s[stptr-2] = rpnstack->s[stptr];
693 else if (rpnstack->s[stptr-2] < rpnstack->s[stptr-1])
694 rpnstack->s[stptr-2] = DNAN;
695 else if (rpnstack->s[stptr-2] > rpnstack->s[stptr])
696 rpnstack->s[stptr-2] = DNAN;
697 stptr-=2;
698 break;
699 case OP_UN:
700 stackunderflow(0);
701 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
702 break;
703 case OP_ISINF:
704 stackunderflow(0);
705 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
706 break;
707 case OP_SQRT:
708 stackunderflow(0);
709 rpnstack -> s[stptr] = sqrt(rpnstack -> s[stptr]);
710 break;
711 case OP_SORT:
712 stackunderflow(0);
713 {
714 int spn = (int)rpnstack -> s[stptr--];
716 stackunderflow(spn-1);
717 qsort(rpnstack -> s + stptr-spn+1, spn, sizeof(double),
718 rpn_compare_double);
719 }
720 break;
721 case OP_REV:
722 stackunderflow(0);
723 {
724 int spn = (int)rpnstack -> s[stptr--];
725 double *p, *q;
727 stackunderflow(spn-1);
729 p = rpnstack -> s + stptr-spn+1;
730 q = rpnstack -> s + stptr;
731 while (p < q) {
732 double x = *q;
734 *q-- = *p;
735 *p++ = x;
736 }
737 }
738 break;
739 case OP_TREND:
740 stackunderflow(1);
741 if ((rpi < 2) || (rpnp[rpi-2].op != OP_VARIABLE)) {
742 rrd_set_error("malformed trend arguments");
743 return -1;
744 } else {
745 time_t dur = (time_t)rpnstack -> s[stptr];
746 time_t step = (time_t)rpnp[rpi-2].step;
748 if (output_idx > (int)ceil((float)dur / (float)step)) {
749 double accum = 0.0;
750 int i = 0;
752 do {
753 accum += rpnp[rpi-2].data[rpnp[rpi-2].ds_cnt * i--];
754 dur -= step;
755 } while (dur > 0);
757 rpnstack -> s[--stptr] = (accum / -i);
758 } else
759 rpnstack -> s[--stptr] = DNAN;
760 }
761 break;
762 case OP_AVG:
763 stackunderflow(0);
764 {
765 int i=(int)rpnstack -> s[stptr--];
766 double sum=0;
767 int count=0;
768 stackunderflow(i-1);
769 while(i>0) {
770 double val=rpnstack -> s[stptr--];
771 i--;
772 if (isnan(val)) { continue; }
773 count++;
774 sum+=val;
775 }
776 /* now push the result back on stack */
777 if (count>0) {
778 rpnstack -> s[++stptr]=sum/count;
779 } else {
780 rpnstack -> s[++stptr]=DNAN;
781 }
782 }
783 break;
784 case OP_ABS:
785 stackunderflow(0);
786 rpnstack -> s[stptr] = fabs(rpnstack -> s[stptr]);
787 break;
788 case OP_END:
789 break;
790 }
791 #undef stackunderflow
792 }
793 if(stptr!=0){
794 rrd_set_error("RPN final stack size != 1");
795 return -1;
796 }
798 output[output_idx] = rpnstack->s[0];
799 return 0;
800 }
802 /* figure out what the local timezone offset for any point in
803 time was. Return it in seconds */
804 int
805 tzoffset( time_t now ){
806 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
807 l_sec, l_min, l_hour, l_yday, l_year;
808 struct tm t;
809 int off;
810 gmtime_r(&now, &t);
811 gm_sec = t.tm_sec;
812 gm_min = t.tm_min;
813 gm_hour = t.tm_hour;
814 gm_yday = t.tm_yday;
815 gm_year = t.tm_year;
816 localtime_r(&now, &t);
817 l_sec = t.tm_sec;
818 l_min = t.tm_min;
819 l_hour = t.tm_hour;
820 l_yday = t.tm_yday;
821 l_year = t.tm_year;
822 off = (l_sec-gm_sec)+(l_min-gm_min)*60+(l_hour-gm_hour)*3600;
823 if ( l_yday > gm_yday || l_year > gm_year){
824 off += 24*3600;
825 } else if ( l_yday < gm_yday || l_year < gm_year){
826 off -= 24*3600;
827 }
828 return off;
829 }