src moved back to pod. this include thing was a neat idea but it just adds another...
[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 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<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, *> and need no 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 last element is 0 (false),
62 the first value is pushed back onto the stack, otherwise the second
63 popped value is pushed back. This does, indeed, mean that any value
64 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 lesser or larger.
75 The two numbers shouldn't be I<infinite> or I<unknown>, if they are
76 that value is pushed back onto the stack as the result.
77
78 B<LIMIT>
79
80 Pops two elements from the stack and uses them to define a range.
81 Then it pops another element and if it falls inside the range, it
82 is pushed back. If not, an I<unknown> is pushed.
83
84 The range defined includes the two boundaries (so: a number equal
85 to one of the boundaries will be pushed back). If any of the three
86 numbers involved is either I<unknown> or I<infinite> this function
87 will always return an I<unknown>
88
89 Example: C<CDEF:a=alpha,0,100,LIMIT> will return I<unknown> if
90 alpha is lower than 0 or if it is higher than 100.
91
92 Z<>
93
94 =item Arithmetics
95
96 B<+, -, *, /, %>
97
98 Add, subtract, multiply, divide, modulo
99
100 B<SIN, COS, LOG, EXP, SQRT>
101
102 Sine, cosine (input in radians), log, exp (natural logarithm), square root
103
104 B<ATAN>
105
106 Arctangent. Output in radians.
107
108 B<FLOOR, CEIL>
109
110 Round down,up to the nearest integer
111
112 Z<>
113
114 =item Set Operations
115
116 B<SORT, REV>
117
118 Pop one element from the stack.  This is the I<count> of items to be sorted
119 (or reversed).  The top I<count> of the remaining elements are then sorted
120 (or reversed) in place on the stack.
121
122 Example: C<CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/> will
123 compute the average of the values v1..v6 after removing the smallest and
124 largest.
125
126 B<TREND>
127
128 Create a "sliding window" average of another data series.
129
130 Usage:
131 CDEF:smoothed=x,1800,TREND
132
133 This will create a half-hour (1800 second) sliding window average of x.  The
134 average is essentially computed as shown here:
135
136                  +---!---!---!---!---!---!---!---!--->
137                                                      now
138                        delay     t0
139                  <--------------->
140                          delay       t1
141                      <--------------->  
142                               delay      t2
143                          <--------------->
144
145
146      Value at sample (t0) will be the average between (t0-delay) and  (t0)
147      Value at sample (t1) will be the average between (t1-delay) and  (t1)
148      Value at sample (t2) will be the average between (t2-delay) and  (t2)
149
150 =item Special values
151
152 B<UNKN>
153
154 Pushes an unknown value on the stack
155
156 B<INF, NEGINF>
157
158 Pushes a positive or negative infinite value on the stack. When
159 such a value is graphed, it appears at the top or bottom of the
160 graph, no matter what the actual value on the y-axis is.
161
162 B<PREV>
163
164 Pushes an I<unknown> value if this is the first value of a data
165 set or otherwise the result of this B<CDEF> at the previous time
166 step. This allows you to do calculations across the data.  This
167 function cannot be used in B<VDEF> instructions.
168
169 B<PREV(vname)>
170
171 Pushes an I<unknown> value if this is the first value of a data
172 set or otherwise the result of vname variable at the previous time
173 step. This allows you to do calculations across the data.  This
174 function cannot be used in B<VDEF> instructions.
175
176 B<COUNT>
177
178 Pushes the number 1 if this is the first value of the data set, the 
179 number 2 if it is the second, and so on. This special value, allows 
180 you to make calculations based on the position of the value within 
181 the data set. This function cannot be used in B<VDEF> instructions.
182
183 Z<>
184
185 =item Time
186
187 Time inside RRDtool is measured in seconds since the epoch. This
188 epoch is defined to be S<C<Thu Jan  1 00:00:00 UTC 1970>>.
189
190 B<NOW>
191
192 Pushes the current time on the stack.
193
194 B<TIME>
195
196 Pushes the time the currently processed value was taken onto the stack.
197
198 B<LTIME>
199
200 Takes the time as defined by B<TIME>, applies the time zone offset
201 valid at that time including daylight saving time if your OS supports
202 it, and pushes the result on the stack.  There is an elaborate example
203 in the examples section on how to use this.
204
205 =item Processing the stack directly
206
207 B<DUP, POP, EXC>
208
209 Duplicate the top element, remove the top element, exchange the two
210 top elements.
211
212 Z<>
213
214 =back
215
216 =head1 VARIABLES
217
218 These operators work only on B<VDEF> statements.
219
220 =over 4
221
222 =item MAXIMUM, MINIMUM, AVERAGE
223
224 Return the corresponding value, MAXIMUM and MINIMUM also return
225 the first occurrence of that value in the time component.
226
227 Example: C<VDEF:avg=mydata,AVERAGE>
228
229 =item LAST, FIRST
230
231 Return the last,first value including its time.  The time for
232 FIRST is actually the start of the corresponding interval, where
233 the LAST time component returns the end of the corresponding interval.
234
235 Example: C<VDEF:first=mydata,FIRST>
236
237 =item TOTAL
238
239 Returns the rate from each defined time slot multiplied with the
240 step size.  This can for instance return total bytes transfered
241 when you have logged bytes per second. The time component returns
242 the amount of seconds 
243
244 Example: C<VDEF:total=mydata,TOTAL>
245
246 =item PERCENT
247
248 Should follow a B<DEF> or B<CDEF> I<vname>. This I<vname> is popped,
249 another number is popped which is a certain percentage (0..100). The
250 data set is then sorted and the value returned is chosen such that
251 I<percentage> percent of the values is lower or equal than the result.
252 I<Unknown> values are considered lower than any finite number for this
253 purpose so if this operator returns an I<unknown> you have quite a lot
254 of them in your data.  B<Inf>inite numbers are lesser, or more, than the
255 finite numbers and are always more than the I<Unknown> numbers.
256 (NaN E<lt> -INF E<lt> finite values E<lt> INF)
257
258 Example: C<VDEF:perc95=mydata,95,PERCENT>
259
260 =back
261
262 =head1 SEE ALSO
263
264 L<rrdgraph> gives an overview of how B<rrdtool graph> works.
265 L<rrdgraph_data> describes B<DEF>,B<CDEF> and B<VDEF> in detail,
266 L<rrdgraph_rpn> describes the B<RPN> language used in the B<?DEF> statements,
267 L<rrdgraph_graph> page describes all of the graph and print functions.
268
269 Make sure to read L<rrdgraph_examples> for tipsE<amp>tricks.
270
271 =head1 AUTHOR
272
273 Program by Tobias Oetiker E<lt>oetiker@ee.ethz.chE<gt>
274
275 This manual page by Alex van den Bogaerdt E<lt>alex@ergens.op.het.netE<gt>