new CDEF functions for predictions PREDICT and PREDICTSIGMA
[rrdtool.git] / src / rrd_rpncalc.c
1 /****************************************************************************
2  * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
3  ****************************************************************************
4  * rrd_rpncalc.c  RPN calculator functions
5  ****************************************************************************/
6
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 // #include "rrd_graph.h"
10 #include <limits.h>
11 #include <locale.h>
12
13 short     addop2str(
14     enum op_en op,
15     enum op_en op_type,
16     char *op_str,
17     char **result_str,
18     unsigned short *offset);
19 int       tzoffset(
20     time_t);            /* used to implement LTIME */
21
22 short rpn_compact(
23     rpnp_t *rpnp,
24     rpn_cdefds_t **rpnc,
25     short *count)
26 {
27     short     i;
28
29     *count = 0;
30     /* count the number of rpn nodes */
31     while (rpnp[*count].op != OP_END)
32         (*count)++;
33     if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
34         rrd_set_error("Maximum %d RPN nodes permitted",
35                       DS_CDEF_MAX_RPN_NODES);
36         return -1;
37     }
38
39     /* allocate memory */
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);
46
47             if (temp < SHRT_MIN || temp > SHRT_MAX) {
48                 rrd_set_error
49                     ("constants must be integers in the interval (%d, %d)",
50                      SHRT_MIN, SHRT_MAX);
51                 free(*rpnc);
52                 return -1;
53             }
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;
57         }
58     }
59     /* terminate the sequence */
60     (*rpnc)[(*count) - 1].op = OP_END;
61     return 0;
62 }
63
64 rpnp_t   *rpn_expand(
65     rpn_cdefds_t *rpnc)
66 {
67     short     i;
68     rpnp_t   *rpnp;
69
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));
73     if (rpnp == NULL)
74         return NULL;
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;
81         }
82     }
83     /* terminate the sequence */
84     rpnp[i].op = OP_END;
85     return rpnp;
86 }
87
88 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
89  * into a CDEF string. This function is used by rrd_dump.
90  * arguments:
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
95  *   the caller */
96 void rpn_compact2str(
97     rpn_cdefds_t *rpnc,
98     ds_def_t *ds_def,
99     char **str)
100 {
101     unsigned short i, offset = 0;
102     char      buffer[7];    /* short as a string */
103
104     for (i = 0; rpnc[i].op != OP_END; i++) {
105         if (i > 0)
106             (*str)[offset++] = ',';
107
108 #define add_op(VV,VVV) \
109           if (addop2str(rpnc[i].op, VV, VVV, str, &offset) == 1) continue;
110
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);
115 #else
116             sprintf(buffer, "%d", rpnc[i].val);
117 #endif
118             add_op(OP_NUMBER, buffer)
119         }
120
121         if (rpnc[i].op == OP_VARIABLE) {
122             char     *ds_name = ds_def[rpnc[i].val].ds_nam;
123
124             add_op(OP_VARIABLE, ds_name)
125         }
126
127         if (rpnc[i].op == OP_PREV_OTHER) {
128             char     *ds_name = ds_def[rpnc[i].val].ds_nam;
129
130             add_op(OP_VARIABLE, ds_name)
131         }
132 #undef add_op
133
134 #define add_op(VV,VVV) \
135           if (addop2str(rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
136
137         add_op(OP_ADD, +)
138             add_op(OP_SUB, -)
139             add_op(OP_MUL, *)
140             add_op(OP_DIV, /)
141             add_op(OP_MOD, %)
142             add_op(OP_SIN, SIN)
143             add_op(OP_COS, COS)
144             add_op(OP_LOG, LOG)
145             add_op(OP_FLOOR, FLOOR)
146             add_op(OP_CEIL, CEIL)
147             add_op(OP_EXP, EXP)
148             add_op(OP_DUP, DUP)
149             add_op(OP_EXC, EXC)
150             add_op(OP_POP, POP)
151             add_op(OP_LT, LT)
152             add_op(OP_LE, LE)
153             add_op(OP_GT, GT)
154             add_op(OP_GE, GE)
155             add_op(OP_EQ, EQ)
156             add_op(OP_IF, IF)
157             add_op(OP_MIN, MIN)
158             add_op(OP_MAX, MAX)
159             add_op(OP_LIMIT, LIMIT)
160             add_op(OP_UNKN, UNKN)
161             add_op(OP_UN, UN)
162             add_op(OP_NEGINF, NEGINF)
163             add_op(OP_NE, NE)
164             add_op(OP_PREV, PREV)
165             add_op(OP_INF, INF)
166             add_op(OP_ISINF, ISINF)
167             add_op(OP_NOW, NOW)
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)
174             add_op(OP_REV, REV)
175             add_op(OP_TREND, TREND)
176             add_op(OP_TRENDNAN, TRENDNAN)
177             add_op(OP_PREDICT, PREDICT)
178             add_op(OP_PREDICTSIGMA, PREDICTSIGMA)
179             add_op(OP_RAD2DEG, RAD2DEG)
180             add_op(OP_DEG2RAD, DEG2RAD)
181             add_op(OP_AVG, AVG)
182             add_op(OP_ABS, ABS)
183             add_op(OP_ADDNAN, ADDNAN)
184 #undef add_op
185     }
186     (*str)[offset] = '\0';
187
188 }
189
190 short addop2str(
191     enum op_en op,
192     enum op_en op_type,
193     char *op_str,
194     char **result_str,
195     unsigned short *offset)
196 {
197     if (op == op_type) {
198         short     op_len;
199
200         op_len = strlen(op_str);
201         *result_str = (char *) rrd_realloc(*result_str,
202                                            (op_len + 1 +
203                                             *offset) * sizeof(char));
204         if (*result_str == NULL) {
205             rrd_set_error("failed to alloc memory in addop2str");
206             return -1;
207         }
208         strncpy(&((*result_str)[*offset]), op_str, op_len);
209         *offset += op_len;
210         return 1;
211     }
212     return 0;
213 }
214
215 void parseCDEF_DS(
216     const char *def,
217     rrd_t *rrd,
218     int ds_idx)
219 {
220     rpnp_t   *rpnp = NULL;
221     rpn_cdefds_t *rpnc = NULL;
222     short     count, i;
223
224     rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
225     if (rpnp == NULL) {
226         rrd_set_error("failed to parse computed data source");
227         return;
228     }
229     /* Check for OP nodes not permitted in COMPUTE DS.
230      * Moved this check from within rpn_compact() because it really is
231      * COMPUTE DS specific. This is less efficient, but creation doesn't
232      * occur too often. */
233     for (i = 0; rpnp[i].op != OP_END; i++) {
234         if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
235             rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT) {
236             rrd_set_error
237                 ("operators time, ltime, prev and count not supported with DS COMPUTE");
238             free(rpnp);
239             return;
240         }
241     }
242     if (rpn_compact(rpnp, &rpnc, &count) == -1) {
243         free(rpnp);
244         return;
245     }
246     /* copy the compact rpn representation over the ds_def par array */
247     memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
248            (void *) rpnc, count * sizeof(rpn_cdefds_t));
249     free(rpnp);
250     free(rpnc);
251 }
252
253 /* lookup a data source name in the rrd struct and return the index,
254  * should use ds_match() here except:
255  * (1) need a void * pointer to the rrd
256  * (2) error handling is left to the caller
257  */
258 long lookup_DS(
259     void *rrd_vptr,
260     char *ds_name)
261 {
262     unsigned int i;
263     rrd_t    *rrd;
264
265     rrd = (rrd_t *) rrd_vptr;
266
267     for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
268         if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
269             return i;
270     }
271     /* the caller handles a bad data source name in the rpn string */
272     return -1;
273 }
274
275 /* rpn_parse : parse a string and generate a rpnp array; modified
276  * str2rpn() originally included in rrd_graph.c
277  * arguments:
278  * key_hash: a transparent argument passed to lookup(); conceptually this
279  *    is a hash object for lookup of a numeric key given a variable name
280  * expr: the string RPN expression, including variable names
281  * lookup(): a function that retrieves a numeric key given a variable name
282  */
283 rpnp_t   *rpn_parse(
284     void *key_hash,
285     const char *const expr_const,
286     long      (*lookup) (void *,
287                          char *))
288 {
289     int       pos = 0;
290     char     *expr;
291     long      steps = -1;
292     rpnp_t   *rpnp;
293     char      vname[MAX_VNAME_LEN + 10];
294     char     *old_locale;
295
296     old_locale = setlocale(LC_NUMERIC, "C");
297
298     rpnp = NULL;
299     expr = (char *) expr_const;
300
301     while (*expr) {
302         if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
303                                            sizeof(rpnp_t))) == NULL) {
304             setlocale(LC_NUMERIC, old_locale);
305             return NULL;
306         }
307
308         else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
309                  && (expr[pos] == ',')) {
310             rpnp[steps].op = OP_NUMBER;
311             expr += pos;
312         }
313 #define match_op(VV,VVV) \
314         else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
315             rpnp[steps].op = VV; \
316             expr+=strlen(#VVV); \
317         }
318
319 #define match_op_param(VV,VVV) \
320         else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
321           int length = 0; \
322           if ((length = strlen(#VVV)+strlen(vname)+2, \
323               expr[length] == ',' || expr[length] == '\0') ) { \
324              rpnp[steps].op = VV; \
325              rpnp[steps].ptr = (*lookup)(key_hash,vname); \
326              if (rpnp[steps].ptr < 0) { \
327                            free(rpnp); \
328                            return NULL; \
329                          } else expr+=length; \
330           } \
331         }
332
333         match_op(OP_ADD, +)
334             match_op(OP_SUB, -)
335             match_op(OP_MUL, *)
336             match_op(OP_DIV, /)
337             match_op(OP_MOD, %)
338             match_op(OP_SIN, SIN)
339             match_op(OP_COS, COS)
340             match_op(OP_LOG, LOG)
341             match_op(OP_FLOOR, FLOOR)
342             match_op(OP_CEIL, CEIL)
343             match_op(OP_EXP, EXP)
344             match_op(OP_DUP, DUP)
345             match_op(OP_EXC, EXC)
346             match_op(OP_POP, POP)
347             match_op(OP_LTIME, LTIME)
348             match_op(OP_LT, LT)
349             match_op(OP_LE, LE)
350             match_op(OP_GT, GT)
351             match_op(OP_GE, GE)
352             match_op(OP_EQ, EQ)
353             match_op(OP_IF, IF)
354             match_op(OP_MIN, MIN)
355             match_op(OP_MAX, MAX)
356             match_op(OP_LIMIT, LIMIT)
357             /* order is important here ! .. match longest first */
358             match_op(OP_UNKN, UNKN)
359             match_op(OP_UN, UN)
360             match_op(OP_NEGINF, NEGINF)
361             match_op(OP_NE, NE)
362             match_op(OP_COUNT, COUNT)
363             match_op_param(OP_PREV_OTHER, PREV)
364             match_op(OP_PREV, PREV)
365             match_op(OP_INF, INF)
366             match_op(OP_ISINF, ISINF)
367             match_op(OP_NOW, NOW)
368             match_op(OP_TIME, TIME)
369             match_op(OP_ATAN2, ATAN2)
370             match_op(OP_ATAN, ATAN)
371             match_op(OP_SQRT, SQRT)
372             match_op(OP_SORT, SORT)
373             match_op(OP_REV, REV)
374             match_op(OP_TREND, TREND)
375             match_op(OP_TRENDNAN, TRENDNAN)
376             match_op(OP_PREDICT, PREDICT)
377             match_op(OP_PREDICTSIGMA, PREDICTSIGMA)
378             match_op(OP_RAD2DEG, RAD2DEG)
379             match_op(OP_DEG2RAD, DEG2RAD)
380             match_op(OP_AVG, AVG)
381             match_op(OP_ABS, ABS)
382             match_op(OP_ADDNAN, ADDNAN)
383 #undef match_op
384             else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
385                      && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
386                          -1)) {
387             rpnp[steps].op = OP_VARIABLE;
388             expr += pos;
389         }
390
391         else {
392             setlocale(LC_NUMERIC, old_locale);
393             free(rpnp);
394             return NULL;
395         }
396
397         if (*expr == 0)
398             break;
399         if (*expr == ',')
400             expr++;
401         else {
402             setlocale(LC_NUMERIC, old_locale);
403             free(rpnp);
404             return NULL;
405         }
406     }
407     rpnp[steps + 1].op = OP_END;
408     setlocale(LC_NUMERIC, old_locale);
409     return rpnp;
410 }
411
412 void rpnstack_init(
413     rpnstack_t *rpnstack)
414 {
415     rpnstack->s = NULL;
416     rpnstack->dc_stacksize = 0;
417     rpnstack->dc_stackblock = 100;
418 }
419
420 void rpnstack_free(
421     rpnstack_t *rpnstack)
422 {
423     if (rpnstack->s != NULL)
424         free(rpnstack->s);
425     rpnstack->dc_stacksize = 0;
426 }
427
428 static int rpn_compare_double(
429     const void *x,
430     const void *y)
431 {
432     double    diff = *((const double *) x) - *((const double *) y);
433
434     return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
435 }
436
437 /* rpn_calc: run the RPN calculator; also performs variable substitution;
438  * moved and modified from data_calc() originally included in rrd_graph.c 
439  * arguments:
440  * rpnp : an array of RPN operators (including variable references)
441  * rpnstack : the initialized stack
442  * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
443  *   is advanced by rpnp.ds_cnt; used only for variable substitution
444  * output : an array of output values; OP_PREV assumes this array contains
445  *   the "previous" value at index position output_idx-1; the definition of
446  *   "previous" depends on the calling environment
447  * output_idx : an index into the output array in which to store the output
448  *   of the RPN calculator
449  * returns: -1 if the computation failed (also calls rrd_set_error)
450  *           0 on success
451  */
452 short rpn_calc(
453     rpnp_t *rpnp,
454     rpnstack_t *rpnstack,
455     long data_idx,
456     rrd_value_t *output,
457     int output_idx)
458 {
459     int       rpi;
460     long      stptr = -1;
461
462     /* process each op from the rpn in turn */
463     for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
464         /* allocate or grow the stack */
465         if (stptr + 5 > rpnstack->dc_stacksize) {
466             /* could move this to a separate function */
467             rpnstack->dc_stacksize += rpnstack->dc_stackblock;
468             rpnstack->s = rrd_realloc(rpnstack->s,
469                                       (rpnstack->dc_stacksize) *
470                                       sizeof(*(rpnstack->s)));
471             if (rpnstack->s == NULL) {
472                 rrd_set_error("RPN stack overflow");
473                 return -1;
474             }
475         }
476 #define stackunderflow(MINSIZE)                         \
477         if(stptr<MINSIZE){                              \
478             rrd_set_error("RPN stack underflow");       \
479             return -1;                                  \
480         }
481
482         switch (rpnp[rpi].op) {
483         case OP_NUMBER:
484             rpnstack->s[++stptr] = rpnp[rpi].val;
485             break;
486         case OP_VARIABLE:
487         case OP_PREV_OTHER:
488             /* Sanity check: VDEFs shouldn't make it here */
489             if (rpnp[rpi].ds_cnt == 0) {
490                 rrd_set_error("VDEF made it into rpn_calc... aborting");
491                 return -1;
492             } else {
493                 /* make sure we pull the correct value from
494                  * the *.data array. Adjust the pointer into
495                  * the array acordingly. Advance the ptr one
496                  * row in the rra (skip over non-relevant
497                  * data sources)
498                  */
499                 if (rpnp[rpi].op == OP_VARIABLE) {
500                     rpnstack->s[++stptr] = *(rpnp[rpi].data);
501                 } else {
502                     if ((output_idx) <= 0) {
503                         rpnstack->s[++stptr] = DNAN;
504                     } else {
505                         rpnstack->s[++stptr] =
506                             *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
507                     }
508
509                 }
510                 if (data_idx % rpnp[rpi].step == 0) {
511                     rpnp[rpi].data += rpnp[rpi].ds_cnt;
512                 }
513             }
514             break;
515         case OP_COUNT:
516             rpnstack->s[++stptr] = (output_idx + 1);    /* Note: Counter starts at 1 */
517             break;
518         case OP_PREV:
519             if ((output_idx) <= 0) {
520                 rpnstack->s[++stptr] = DNAN;
521             } else {
522                 rpnstack->s[++stptr] = output[output_idx - 1];
523             }
524             break;
525         case OP_UNKN:
526             rpnstack->s[++stptr] = DNAN;
527             break;
528         case OP_INF:
529             rpnstack->s[++stptr] = DINF;
530             break;
531         case OP_NEGINF:
532             rpnstack->s[++stptr] = -DINF;
533             break;
534         case OP_NOW:
535             rpnstack->s[++stptr] = (double) time(NULL);
536             break;
537         case OP_TIME:
538             /* HACK: this relies on the data_idx being the time,
539              ** which the within-function scope is unaware of */
540             rpnstack->s[++stptr] = (double) data_idx;
541             break;
542         case OP_LTIME:
543             rpnstack->s[++stptr] =
544                 (double) tzoffset(data_idx) + (double) data_idx;
545             break;
546         case OP_ADD:
547             stackunderflow(1);
548             rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
549                 + rpnstack->s[stptr];
550             stptr--;
551             break;
552         case OP_ADDNAN:
553             stackunderflow(1);
554             if (isnan(rpnstack->s[stptr - 1])) {
555                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
556             } else if (isnan(rpnstack->s[stptr])) {
557                 /* NOOP */
558                 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
559             } else {
560                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
561                     + rpnstack->s[stptr];
562             }
563
564             stptr--;
565             break;
566         case OP_SUB:
567             stackunderflow(1);
568             rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
569                 - rpnstack->s[stptr];
570             stptr--;
571             break;
572         case OP_MUL:
573             stackunderflow(1);
574             rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
575                 * (rpnstack->s[stptr]);
576             stptr--;
577             break;
578         case OP_DIV:
579             stackunderflow(1);
580             rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
581                 / rpnstack->s[stptr];
582             stptr--;
583             break;
584         case OP_MOD:
585             stackunderflow(1);
586             rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
587                                           , rpnstack->s[stptr]);
588             stptr--;
589             break;
590         case OP_SIN:
591             stackunderflow(0);
592             rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
593             break;
594         case OP_ATAN:
595             stackunderflow(0);
596             rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
597             break;
598         case OP_RAD2DEG:
599             stackunderflow(0);
600             rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
601             break;
602         case OP_DEG2RAD:
603             stackunderflow(0);
604             rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
605             break;
606         case OP_ATAN2:
607             stackunderflow(1);
608             rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
609                                            rpnstack->s[stptr]);
610             stptr--;
611             break;
612         case OP_COS:
613             stackunderflow(0);
614             rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
615             break;
616         case OP_CEIL:
617             stackunderflow(0);
618             rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
619             break;
620         case OP_FLOOR:
621             stackunderflow(0);
622             rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
623             break;
624         case OP_LOG:
625             stackunderflow(0);
626             rpnstack->s[stptr] = log(rpnstack->s[stptr]);
627             break;
628         case OP_DUP:
629             stackunderflow(0);
630             rpnstack->s[stptr + 1] = rpnstack->s[stptr];
631             stptr++;
632             break;
633         case OP_POP:
634             stackunderflow(0);
635             stptr--;
636             break;
637         case OP_EXC:
638             stackunderflow(1);
639             {
640                 double    dummy;
641
642                 dummy = rpnstack->s[stptr];
643                 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
644                 rpnstack->s[stptr - 1] = dummy;
645             }
646             break;
647         case OP_EXP:
648             stackunderflow(0);
649             rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
650             break;
651         case OP_LT:
652             stackunderflow(1);
653             if (isnan(rpnstack->s[stptr - 1]));
654             else if (isnan(rpnstack->s[stptr]))
655                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
656             else
657                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
658                     rpnstack->s[stptr] ? 1.0 : 0.0;
659             stptr--;
660             break;
661         case OP_LE:
662             stackunderflow(1);
663             if (isnan(rpnstack->s[stptr - 1]));
664             else if (isnan(rpnstack->s[stptr]))
665                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
666             else
667                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
668                     rpnstack->s[stptr] ? 1.0 : 0.0;
669             stptr--;
670             break;
671         case OP_GT:
672             stackunderflow(1);
673             if (isnan(rpnstack->s[stptr - 1]));
674             else if (isnan(rpnstack->s[stptr]))
675                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
676             else
677                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
678                     rpnstack->s[stptr] ? 1.0 : 0.0;
679             stptr--;
680             break;
681         case OP_GE:
682             stackunderflow(1);
683             if (isnan(rpnstack->s[stptr - 1]));
684             else if (isnan(rpnstack->s[stptr]))
685                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
686             else
687                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
688                     rpnstack->s[stptr] ? 1.0 : 0.0;
689             stptr--;
690             break;
691         case OP_NE:
692             stackunderflow(1);
693             if (isnan(rpnstack->s[stptr - 1]));
694             else if (isnan(rpnstack->s[stptr]))
695                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
696             else
697                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
698                     rpnstack->s[stptr] ? 0.0 : 1.0;
699             stptr--;
700             break;
701         case OP_EQ:
702             stackunderflow(1);
703             if (isnan(rpnstack->s[stptr - 1]));
704             else if (isnan(rpnstack->s[stptr]))
705                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
706             else
707                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
708                     rpnstack->s[stptr] ? 1.0 : 0.0;
709             stptr--;
710             break;
711         case OP_IF:
712             stackunderflow(2);
713             rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
714                                       || rpnstack->s[stptr - 2] ==
715                                       0.0) ? rpnstack->s[stptr] : rpnstack->
716                 s[stptr - 1];
717             stptr--;
718             stptr--;
719             break;
720         case OP_MIN:
721             stackunderflow(1);
722             if (isnan(rpnstack->s[stptr - 1]));
723             else if (isnan(rpnstack->s[stptr]))
724                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
725             else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
726                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
727             stptr--;
728             break;
729         case OP_MAX:
730             stackunderflow(1);
731             if (isnan(rpnstack->s[stptr - 1]));
732             else if (isnan(rpnstack->s[stptr]))
733                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
734             else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
735                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
736             stptr--;
737             break;
738         case OP_LIMIT:
739             stackunderflow(2);
740             if (isnan(rpnstack->s[stptr - 2]));
741             else if (isnan(rpnstack->s[stptr - 1]))
742                 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
743             else if (isnan(rpnstack->s[stptr]))
744                 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
745             else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
746                 rpnstack->s[stptr - 2] = DNAN;
747             else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
748                 rpnstack->s[stptr - 2] = DNAN;
749             stptr -= 2;
750             break;
751         case OP_UN:
752             stackunderflow(0);
753             rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
754             break;
755         case OP_ISINF:
756             stackunderflow(0);
757             rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
758             break;
759         case OP_SQRT:
760             stackunderflow(0);
761             rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
762             break;
763         case OP_SORT:
764             stackunderflow(0);
765             {
766                 int       spn = (int) rpnstack->s[stptr--];
767
768                 stackunderflow(spn - 1);
769                 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
770                       rpn_compare_double);
771             }
772             break;
773         case OP_REV:
774             stackunderflow(0);
775             {
776                 int       spn = (int) rpnstack->s[stptr--];
777                 double   *p, *q;
778
779                 stackunderflow(spn - 1);
780
781                 p = rpnstack->s + stptr - spn + 1;
782                 q = rpnstack->s + stptr;
783                 while (p < q) {
784                     double    x = *q;
785
786                     *q-- = *p;
787                     *p++ = x;
788                 }
789             }
790             break;
791         case OP_PREDICT:
792         case OP_PREDICTSIGMA:
793             stackunderflow(2);
794             {
795                 /* the local averaging window (similar to trend, but better here, as we get better statistics thru numbers)*/
796                 int   locstepsize = rpnstack->s[--stptr];
797                 /* the number of shifts and range-checking*/
798                 int     shifts = rpnstack->s[--stptr];
799                 stackunderflow(shifts);
800                 // handle negative shifts special
801                 if (shifts<0) {
802                     stptr--;
803                 } else {
804                     stptr-=shifts;
805                 }
806                 /* the real calculation */
807                 double val=DNAN;
808                 /* the info on the datasource */
809                 time_t  dsstep = (time_t) rpnp[rpi - 1].step;
810                 int    dscount = rpnp[rpi - 1].ds_cnt;
811                 int   locstep = (int)ceil((float)locstepsize/(float)dsstep);
812
813                 /* the sums */
814                 double    sum = 0;
815                 double    sum2 = 0;
816                 int       count = 0;
817                 /* now loop for each position */
818                 int doshifts=shifts;
819                 if (shifts<0) { doshifts=-shifts; }
820                 for(int loop=0;loop<doshifts;loop++) {
821                     /* calculate shift step */
822                     int shiftstep=1;
823                     if (shifts<0) {
824                         shiftstep = loop*rpnstack->s[stptr];
825                     } else { 
826                         shiftstep = rpnstack->s[stptr+loop]; 
827                     }
828                     if(shiftstep <0) {
829                         rrd_set_error("negative shift step not allowed: %i",shiftstep);
830                         return -1;
831                     }
832                     shiftstep=(int)ceil((float)shiftstep/(float)dsstep);
833                     /* loop all local shifts */
834                     for(int i=0;i<=locstep;i++) {
835                         /* now calculate offset into data-array - relative to output_idx*/
836                         int offset=shiftstep+i;
837                         /* and process if we have index 0 of above */
838                         if ((offset>=0)&&(offset<output_idx)) {
839                             /* get the value */
840                             val =rpnp[rpi - 1].data[-dscount * offset];
841                             /* and handle the non NAN case only*/
842                             if (! isnan(val)) {
843                                 sum+=val;
844                                 sum2+=val*val;
845                                 count++;
846                             }
847                         }
848                     }
849                 }
850                 /* do the final calculations */
851                 val=DNAN;
852                 if (rpnp[rpi].op == OP_PREDICT) {  /* the average */
853                     if (count>0) {
854                         val = sum/(double)count;
855                     } 
856                 } else {
857                     if (count>1) { /* the sigma case */
858                         val=count*sum2-sum*sum;
859                         if (val<0) {
860                             val=DNAN;
861                         } else {
862                             val=sqrt(val/((float)count*((float)count-1.0)));
863                         }
864                     }
865                 }
866                 rpnstack->s[stptr] = val;
867             }
868             break;
869         case OP_TREND:
870         case OP_TRENDNAN:
871             stackunderflow(1);
872             if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
873                 rrd_set_error("malformed trend arguments");
874                 return -1;
875             } else {
876                 time_t    dur = (time_t) rpnstack->s[stptr];
877                 time_t    step = (time_t) rpnp[rpi - 2].step;
878
879                 if (output_idx > (int) ceil((float) dur / (float) step)) {
880                     int       ignorenan = (rpnp[rpi].op == OP_TREND);
881                     double    accum = 0.0;
882                     int       i = 0;
883                     int       count = 0;
884
885                     do {
886                         double    val =
887                             rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
888                         if (ignorenan || !isnan(val)) {
889                             accum += val;
890                             ++count;
891                         }
892
893                         dur -= step;
894                     } while (dur > 0);
895
896                     rpnstack->s[--stptr] =
897                         (count == 0) ? DNAN : (accum / count);
898                 } else
899                     rpnstack->s[--stptr] = DNAN;
900             }
901             break;
902         case OP_AVG:
903             stackunderflow(0);
904             {
905                 int       i = (int) rpnstack->s[stptr--];
906                 double    sum = 0;
907                 int       count = 0;
908
909                 stackunderflow(i - 1);
910                 while (i > 0) {
911                     double    val = rpnstack->s[stptr--];
912
913                     i--;
914                     if (isnan(val)) {
915                         continue;
916                     }
917                     count++;
918                     sum += val;
919                 }
920                 /* now push the result back on stack */
921                 if (count > 0) {
922                     rpnstack->s[++stptr] = sum / count;
923                 } else {
924                     rpnstack->s[++stptr] = DNAN;
925                 }
926             }
927             break;
928         case OP_ABS:
929             stackunderflow(0);
930             rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
931             break;
932         case OP_END:
933             break;
934         }
935 #undef stackunderflow
936     }
937     if (stptr != 0) {
938         rrd_set_error("RPN final stack size != 1");
939         return -1;
940     }
941
942     output[output_idx] = rpnstack->s[0];
943     return 0;
944 }
945
946 /* figure out what the local timezone offset for any point in
947    time was. Return it in seconds */
948 int tzoffset(
949     time_t now)
950 {
951     int       gm_sec, gm_min, gm_hour, gm_yday, gm_year,
952         l_sec, l_min, l_hour, l_yday, l_year;
953     struct tm t;
954     int       off;
955
956     gmtime_r(&now, &t);
957     gm_sec = t.tm_sec;
958     gm_min = t.tm_min;
959     gm_hour = t.tm_hour;
960     gm_yday = t.tm_yday;
961     gm_year = t.tm_year;
962     localtime_r(&now, &t);
963     l_sec = t.tm_sec;
964     l_min = t.tm_min;
965     l_hour = t.tm_hour;
966     l_yday = t.tm_yday;
967     l_year = t.tm_year;
968     off =
969         (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
970     if (l_yday > gm_yday || l_year > gm_year) {
971         off += 24 * 3600;
972     } else if (l_yday < gm_yday || l_year < gm_year) {
973         off -= 24 * 3600;
974     }
975     return off;
976 }