aabd7383e43979ae6396d0fc210aa3c879a55563
[rrdtool.git] / doc / rrdgraph_rpn.pod
1 =head1 NAME
2
3 rrdgraph_rpn - About RPN Math in rrdtool graph
4
5 =head1 SYNOPSIS
6
7 I<RPN expression>:=I<vname>|I<operator>|I<value>[,I<RPN expression>]
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 implicitly, so whenever you specify a number or a variable, it gets
16 pushed onto the stack automatically.
17
18 At the end of the calculation there should be one and only one value left on
19 the stack.  This is the outcome of the function and this is what is put into
20 the I<vname>.  For B<CDEF> instructions, the stack is processed for each
21 data point on the graph. B<VDEF> instructions work on an entire data set in
22 one run. Note, that currently B<VDEF> instructions only support a limited
23 list of functions.
24
25 Example: C<VDEF:maximum=mydata,MAXIMUM>
26
27 This will set variable "maximum" which you now can use in the rest
28 of your RRD script.
29
30 Example: C<CDEF:mydatabits=mydata,8,*>
31
32 This means:  push variable I<mydata>, push the number 8, execute
33 the operator I<*>. The operator needs two elements and uses those
34 to return one value.  This value is then stored in I<mydatabits>.
35 As you may have guessed, this instruction means nothing more than
36 I<mydatabits = mydata * 8>.  The real power of B<RPN> lies in the
37 fact that it is always clear in which order to process the input.
38 For expressions like C<a = b + 3 * 5> you need to multiply 3 with
39 5 first before you add I<b> to get I<a>. However, with parentheses
40 you could change this order: C<a = (b + 3) * 5>. In B<RPN>, you
41 would do C<a = b, 3, +, 5, *> without the need for parentheses.
42
43 =head1 OPERATORS
44
45 =over 4
46
47 =item Boolean operators
48
49 B<LT, LE, GT, GE, EQ, NE>
50
51 Pop two elements from the stack, compare them for the selected condition
52 and return 1 for true or 0 for false. Comparing an I<unknown> or an
53 I<infinite> value will always result in 0 (false).
54
55 B<UN, ISINF>
56
57 Pop one element from the stack, compare this to I<unknown> respectively
58 to I<positive or negative infinity>. Returns 1 for true or 0 for false.
59
60 B<IF>
61
62 Pops three elements from the stack.  If the element popped last is 0
63 (false), the value popped first is pushed back onto the stack,
64 otherwise the value popped second is pushed back. This does, indeed,
65 mean that any value other than 0 is considered to be true.
66
67 Example: C<A,B,C,IF> should be read as C<if (A) then (B) else (C)>
68
69 Z<>
70
71 =item Comparing values
72
73 B<MIN, MAX>
74
75 Pops two elements from the stack and returns the smaller or larger,
76 respectively.  Note that I<infinite> is larger than anything else.
77 If one of the input numbers is I<unknown> then the result of the operation will be
78 I<unknown> too.
79
80 B<LIMIT>
81
82 Pops two elements from the stack and uses them to define a range.
83 Then it pops another element and if it falls inside the range, it
84 is pushed back. If not, an I<unknown> is pushed.
85
86 The range defined includes the two boundaries (so: a number equal
87 to one of the boundaries will be pushed back). If any of the three
88 numbers involved is either I<unknown> or I<infinite> this function
89 will always return an I<unknown>
90
91 Example: C<CDEF:a=alpha,0,100,LIMIT> will return I<unknown> if
92 alpha is lower than 0 or if it is higher than 100.
93
94 Z<>
95
96 =item Arithmetics
97
98 B<+, -, *, /, %>
99
100 Add, subtract, multiply, divide, modulo
101
102 B<ADDNAN>
103
104 NAN-safe addition. If one parameter is NAN/UNKNOWN it'll be treated as
105 zero. If both parameters are NAN/UNKNOWN, NAN/UNKNOWN will be returned.
106
107 B<SIN, COS, LOG, EXP, SQRT>
108
109 Sine and cosine (input in radians), log and exp (natural logarithm),
110 square root.
111
112 B<ATAN>
113
114 Arctangent (output in radians).
115
116 B<ATAN2>
117
118 Arctangent of y,x components (output in radians).
119 This pops one element from the stack, the x (cosine) component, and then
120 a second, which is the y (sine) component.
121 It then pushes the arctangent of their ratio, resolving the ambiguity between
122 quadrants.
123
124 Example: C<CDEF:angle=Y,X,ATAN2,RAD2DEG> will convert C<X,Y>
125 components into an angle in degrees.
126
127 B<FLOOR, CEIL>
128
129 Round down or up to the nearest integer.
130
131 B<DEG2RAD, RAD2DEG>
132
133 Convert angle in degrees to radians, or radians to degrees.
134
135 B<ABS>
136
137 Take the absolute value.
138
139 =item Set Operations
140
141 B<SORT, REV>
142
143 Pop one element from the stack.  This is the I<count> of items to be sorted
144 (or reversed).  The top I<count> of the remaining elements are then sorted
145 (or reversed) in place on the stack.
146
147 Example: C<CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/> will
148 compute the average of the values v1 to v6 after removing the smallest and
149 largest.
150
151 B<AVG>
152
153 Pop one element (I<count>) from the stack. Now pop I<count> elements and build the
154 average, ignoring all UNKNOWN values in the process.
155
156 Example: C<CDEF:x=a,b,c,d,4,AVG>
157
158 B<TREND, TRENDNAN>
159
160 Create a "sliding window" average of another data series.
161
162 Usage:
163 CDEF:smoothed=x,1800,TREND
164
165 This will create a half-hour (1800 second) sliding window average of x.  The
166 average is essentially computed as shown here:
167
168                  +---!---!---!---!---!---!---!---!--->
169                                                      now
170                        delay     t0
171                  <--------------->
172                          delay       t1
173                      <--------------->
174                               delay      t2
175                          <--------------->
176
177
178      Value at sample (t0) will be the average between (t0-delay) and (t0)
179      Value at sample (t1) will be the average between (t1-delay) and (t1)
180      Value at sample (t2) will be the average between (t2-delay) and (t2)
181
182 TRENDNAN is - in contrast to TREND - NAN-safe. If you use TREND and one 
183 source value is NAN the complete sliding window is affected. The TRENDNAN 
184 operation ignores all NAN-values in a sliding window and computes the 
185 average of the remaining values.
186
187
188 =item Special values
189
190 B<UNKN>
191
192 Pushes an unknown value on the stack
193
194 B<INF, NEGINF>
195
196 Pushes a positive or negative infinite value on the stack. When
197 such a value is graphed, it appears at the top or bottom of the
198 graph, no matter what the actual value on the y-axis is.
199
200 B<PREV>
201
202 Pushes an I<unknown> value if this is the first value of a data
203 set or otherwise the result of this B<CDEF> at the previous time
204 step. This allows you to do calculations across the data.  This
205 function cannot be used in B<VDEF> instructions.
206
207 B<PREV(vname)>
208
209 Pushes an I<unknown> value if this is the first value of a data
210 set or otherwise the result of the vname variable at the previous time
211 step. This allows you to do calculations across the data. This
212 function cannot be used in B<VDEF> instructions.
213
214 B<COUNT>
215
216 Pushes the number 1 if this is the first value of the data set, the
217 number 2 if it is the second, and so on. This special value allows
218 you to make calculations based on the position of the value within
219 the data set. This function cannot be used in B<VDEF> instructions.
220
221 =item Time
222
223 Time inside RRDtool is measured in seconds since the epoch. The
224 epoch is defined to be S<C<Thu Jan  1 00:00:00 UTC 1970>>.
225
226 B<NOW>
227
228 Pushes the current time on the stack.
229
230 B<TIME>
231
232 Pushes the time the currently processed value was taken at onto the stack.
233
234 B<LTIME>
235
236 Takes the time as defined by B<TIME>, applies the time zone offset
237 valid at that time including daylight saving time if your OS supports
238 it, and pushes the result on the stack.  There is an elaborate example
239 in the examples section below on how to use this.
240
241 =item Processing the stack directly
242
243 B<DUP, POP, EXC>
244
245 Duplicate the top element, remove the top element, exchange the two
246 top elements.
247
248 Z<>
249
250 =back
251
252 =head1 VARIABLES
253
254 These operators work only on B<VDEF> statements. Note that currently ONLY these work for B<VDEF>.
255
256 =over 4
257
258 =item MAXIMUM, MINIMUM, AVERAGE
259
260 Return the corresponding value, MAXIMUM and MINIMUM also return
261 the first occurrence of that value in the time component.
262
263 Example: C<VDEF:avg=mydata,AVERAGE>
264
265 =item STDEV
266
267 Returns the standard deviation of the values.
268
269 Example: C<VDEF:stdev=mydata,STDEV>
270
271 =item LAST, FIRST
272
273 Return the last/first value including its time.  The time for
274 FIRST is actually the start of the corresponding interval, whereas
275 LAST returns the end of the corresponding interval.
276
277 Example: C<VDEF:first=mydata,FIRST>
278
279 =item TOTAL
280
281 Returns the rate from each defined time slot multiplied with the
282 step size.  This can, for instance, return total bytes transfered
283 when you have logged bytes per second. The time component returns
284 the number of seconds.
285
286 Example: C<VDEF:total=mydata,TOTAL>
287
288 =item PERCENT
289
290 This should follow a B<DEF> or B<CDEF> I<vname>. The I<vname> is popped,
291 another number is popped which is a certain percentage (0..100). The
292 data set is then sorted and the value returned is chosen such that
293 I<percentage> percent of the values is lower or equal than the result.
294 I<Unknown> values are considered lower than any finite number for this
295 purpose so if this operator returns an I<unknown> you have quite a lot
296 of them in your data.  B<Inf>inite numbers are lesser, or more, than the
297 finite numbers and are always more than the I<Unknown> numbers.
298 (NaN E<lt> -INF E<lt> finite values E<lt> INF)
299
300 Example: C<VDEF:perc95=mydata,95,PERCENT>
301
302 =item LSLSLOPE, LSLINT, LSLCORREL
303
304 Return the parameters for a B<L>east B<S>quares B<L>ine I<(y = mx +b)> 
305 which approximate the provided dataset.  LSLSLOPE is the slope I<(m)> of
306 the line related to the COUNT position of the data.  LSLINT is the 
307 y-intercept I<(b)>, which happens also to be the first data point on the 
308 graph. LSLCORREL is the Correlation Coefficient (also know as Pearson's 
309 Product Moment Correlation Coefficient).  It will range from 0 to +/-1 
310 and represents the quality of fit for the approximation.   
311
312 Example: C<VDEF:slope=mydata,LSLSLOPE>
313
314 =back
315
316 =head1 SEE ALSO
317
318 L<rrdgraph> gives an overview of how B<rrdtool graph> works.
319 L<rrdgraph_data> describes B<DEF>,B<CDEF> and B<VDEF> in detail.
320 L<rrdgraph_rpn> describes the B<RPN> language used in the B<?DEF> statements.
321 L<rrdgraph_graph> page describes all of the graph and print functions.
322
323 Make sure to read L<rrdgraph_examples> for tipsE<amp>tricks.
324
325 =head1 AUTHOR
326
327 Program by Tobias Oetiker E<lt>tobi@oetiker.chE<gt>
328
329 This manual page by Alex van den Bogaerdt E<lt>alex@ergens.op.het.netE<gt>