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