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