misc fixed and TREND and reduce functionality by
[rrdtool.git] / doc / rrdgraph_rpn.src
1 =include name
2
3 =head1 SYNOPSIS
4
5 I<E<lt>RPN expressionE<gt>> := 
6 I<E<lt>vnameE<gt>>|I<E<lt>operatorE<gt>>|I<E<lt>valueE<gt>>
7 [ , I<E<lt>RPN expressionE<gt>>]
8
9 =head1 DESCRIPTION
10
11 If you have ever used a traditional HP calculator you already know
12 B<RPN>. The idea behind B<RPN> is that you have a stack and push
13 your data onto this stack. Whenever you execute an operation, it
14 takes as many elements from the stack as needed. Pushing is done
15 implicit so whenever you specify a number or a variable, it gets
16 pushed automatically.
17
18 At the end of the calculation there should be one and exactly one
19 value left on the stack.  This is the outcome of the function and
20 this is what is put into the I<vname>.  For B<CDEF> instructions,
21 the stack is processed for each data point on the graph. B<VDEF>
22 instructions work on an entire data set in one run.
23
24 Example: C<CDEF:mydatabits=mydata,8,*>
25
26 This means:  push variable I<mydata>, push the number 8, execute
27 the operator I<+>. The operator needs two elements and uses those
28 to return one value.  This value is then stored in I<mydatabits>.
29 As you may have guessed, this instruction means nothing more than
30 I<mydatabits = mydata * 8>.  The real power of B<RPN> lies in the
31 fact that it is always clear in which order to process the input.
32 For expressions like C<a = b + 3 * 5> you need to multiply 3 with
33 5 first before you add I<b> to get I<a>. However, with parentheses
34 you could change this order: C<a = (b + 3) * 5>. In B<RPN>, you
35 would do C<a = b, 3, +, 5, *> and need no parentheses.
36
37 =head1 OPERATORS
38
39 =over 4
40
41 =item Boolean operators
42
43 B<LT, LE, GT, GE, EQ, NE>
44
45 Pop two elements from the stack, compare them for the selected condition
46 and return 1 for true or 0 for false. Comparing an I<unknown> or an
47 I<infinite> value will always result in 0 (false).
48
49 B<UN, ISINF>
50
51 Pop one element from the stack, compare this to I<unknown> respectively
52 to I<positive or negative infinity>. Returns 1 for true or 0 for false.
53
54 B<IF>
55
56 Pops three elements from the stack.  If the last element is 0 (false),
57 the first value is pushed back onto the stack, otherwise the second
58 popped value is pushed back. This does, indeed, mean that any value
59 other than 0 is considered true.
60 I<Note: Should this change? It should IMHO as all the other functions
61 would return unknown if A,B or C were unknown>
62
63 Example: C<A,B,C,IF> should be read as C<if (A) then (B) else (C)>
64
65 Z<>
66
67 =item Comparing values
68
69 B<MIN, MAX> 
70
71 Pops two elements from the stack and returns the lesser or larger.
72 The two numbers shouldn't be I<infinite> or I<unknown>, if they are
73 that value is pushed back onto the stack as the result.
74
75 B<LIMIT>
76
77 Pops two elements from the stack and uses them to define a range.
78 Then it pops another element and if it falls inside the range, it
79 is pushed back. If not, an I<unknown> is pushed.
80
81 The range defined includes the two boundaries (so: a number equal
82 to one of the boundaries will be pushed back). If any of the three
83 numbers involved is either I<unknown> or I<infinite> this function
84 will always return an I<unknown>
85
86 Example: C<CDEF:a=alpha,0,100,LIMIT> will return I<unknown> if
87 alpha is lower than 0 or if it is higher than 100.
88
89 Z<>
90
91 =item Arithmetics
92
93 B<+, -, *, /, %>
94
95 Add, subtract, multiply, divide, modulo
96
97 B<SIN, COS, LOG, EXP, SQRT>
98
99 Sine, cosine (input in radians), log, exp (natural logarithm), square root
100
101 B<ATAN>
102
103 Arctangent. Output in radians.
104
105 B<FLOOR, CEIL>
106
107 Round down,up to the nearest integer
108
109 Z<>
110
111 =item Set Operations
112
113 B<SORT, REV>
114
115 Pop one element from the stack.  This is the I<count> of items to be sorted
116 (or reversed).  The top I<count> of the remaining elements are then sorted
117 (or reversed) in place on the stack.
118
119 Example: C<CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/> will
120 compute the average of the values v1..v6 after removing the smallest and
121 largest.
122
123 B<TREND>
124
125 Create a "sliding window" average of another data series.
126
127 Usage:
128 CDEF:smoothed=x,1800,TREND
129
130 This will create a half-hour (1800 second) sliding window average of x.  The
131 average is essentially computed as shown here:
132
133                  +---!---!---!---!---!---!---!---!--->
134                                                      now
135                        delay     t0
136                  <--------------->
137                          delay       t1
138                      <--------------->  
139                               delay      t2
140                          <--------------->
141
142
143      Value at sample (t0) will be the average between (t0-delay) and  (t0)
144      Value at sample (t1) will be the average between (t1-delay) and  (t1)
145      Value at sample (t2) will be the average between (t2-delay) and  (t2)
146
147 =item Special values
148
149 B<UNKN>
150
151 Pushes an unknown value on the stack
152
153 B<INF, NEGINF>
154
155 Pushes a positive or negative infinite value on the stack. When
156 such a value is graphed, it appears at the top or bottom of the
157 graph, no matter what the actual value on the y-axis is.
158
159 B<PREV>
160
161 Pushes an I<unknown> value if this is the first value of a data
162 set or otherwise the result of this B<CDEF> at the previous time
163 step. This allows you to do calculations across the data.  This
164 function cannot be used in B<VDEF> instructions.
165
166 B<PREV(vname)>
167
168 Pushes an I<unknown> value if this is the first value of a data
169 set or otherwise the result of vname variable at the previous time
170 step. This allows you to do calculations across the data.  This
171 function cannot be used in B<VDEF> instructions.
172
173 B<COUNT>
174
175 Pushes the number 1 if this is the first value of the data set, the 
176 number 2 if it is the second, and so on. This special value, allows 
177 you to make calculations based on the position of the value within 
178 the data set. This function cannot be used in B<VDEF> instructions.
179
180 Z<>
181
182 =item Time
183
184 Time inside RRDtool is measured in seconds since the epoch. This
185 epoch is defined to be S<C<Thu Jan  1 00:00:00 UTC 1970>>.
186
187 Z<>
188
189 =over 4
190
191 =item NOW
192
193 Pushes the current time on the stack.
194
195 Z<>
196
197 =item TIME
198
199 Pushes the time the currently processed value was taken onto the stack.
200
201 Z<>
202
203 =item LTIME
204
205 Takes the time as defined by B<TIME>, applies the time zone offset
206 valid at that time including daylight saving time if your OS supports
207 it, and pushes the result on the stack.  There is an elaborate example
208 in the examples section on how to use this.
209
210 =back
211
212 For B<VDEF> operations, B<TIME> and B<LTIME> have a different meaning
213 I<not yet implemented>.  As the B<VDEF> statement does not work per
214 value but rather on a complete time series, there is no such thing as
215 the currently processed value.  However, if you have used an operator
216 that returned a time component and would like to have this available
217 in the value component in stead (so you can use it as a number), you
218 can use B<TIME> or B<LTIME> for that.
219
220 Z<>
221
222 =item Processing the stack directly
223
224 B<DUP, POP, EXC>
225
226 Duplicate the top element, remove the top element, exchange the two
227 top elements.
228
229 Z<>
230
231 =item Selecting characteristics
232
233 These operators work only on B<VDEF> statements.
234 I<We can make most of them work at DEF and CDEF statements. If we do
235 so, we have a moving (not rolling!) average, max,min etcetera>
236
237 Z<>
238
239 =over 4
240
241 =item MAXIMUM, MINIMUM, AVERAGE
242
243 Return the corresponding value, MAXIMUM and MINIMUM also return
244 the first occurrence of that value in the time component.
245
246 Example: C<VDEF:avg=mydata,AVERAGE>
247
248 Z<>
249
250 =item LAST, FIRST
251
252 Return the last,first value including its time.  The time for
253 FIRST is actually the start of the corresponding interval, where
254 the LAST time component returns the end of the corresponding interval.
255
256 Example: C<VDEF:first=mydata,FIRST>
257
258 Z<>
259
260 =item TOTAL
261
262 Returns the rate from each defined time slot multiplied with the
263 step size.  This can for instance return total bytes transfered
264 when you have logged bytes per second. The time component returns
265 the amount of seconds 
266
267 Example: C<VDEF:total=mydata,TOTAL>
268
269 Z<>
270
271 =item PERCENT
272
273 Should follow a B<DEF> or B<CDEF> I<vname>. This I<vname> is popped,
274 another number is popped which is a certain percentage (0..100). The
275 data set is then sorted and the value returned is chosen such that
276 I<percentage> percent of the values is lower or equal than the result.
277 I<Unknown> values are considered lower than any finite number for this
278 purpose so if this operator returns an I<unknown> you have quite a lot
279 of them in your data.  B<Inf>inite numbers are lesser, or more, than the
280 finite numbers and are always more than the I<Unknown> numbers.
281
282 Example: C<VDEF:perc95=mydata,95,PERCENT>
283
284 =back
285
286 =back
287
288 =include see_also