Code

rpn compare operators CAN return unknown ... (#293)
[rrdtool.git] / doc / rrdgraph_rpn.pod
1 =head1 NAME
3 rrdgraph_rpn - About RPN Math in rrdtool graph
5 =head1 SYNOPSIS
7 I<RPN expression>:=I<vname>|I<operator>|I<value>[,I<RPN expression>]
9 =head1 DESCRIPTION
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.
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.
25 Example: C<VDEF:maximum=mydata,MAXIMUM>
27 This will set variable "maximum" which you now can use in the rest
28 of your RRD script.
30 Example: C<CDEF:mydatabits=mydata,8,*>
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.
43 =head1 OPERATORS
45 =over 4
47 =item Boolean operators
49 B<LT, LE, GT, GE, EQ, NE>
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 result in I<unknown> returned ... which will also be
54 treated as false by the B<IF> call.
56 B<UN, ISINF>
58 Pop one element from the stack, compare this to I<unknown> respectively
59 to I<positive or negative infinity>. Returns 1 for true or 0 for false.
61 B<IF>
63 Pops three elements from the stack.  If the element popped last is 0
64 (false), the value popped first is pushed back onto the stack,
65 otherwise the value popped second is pushed back. This does, indeed,
66 mean that any value other than 0 is considered to be true.
68 Example: C<A,B,C,IF> should be read as C<if (A) then (B) else (C)>
70 =item Comparing values
72 B<MIN, MAX>
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.
79 B<LIMIT>
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.
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>
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.
93 =item Arithmetics
95 B<+, -, *, /, %>
97 Add, subtract, multiply, divide, modulo
99 B<SIN, COS, LOG, EXP, SQRT>
101 Sine and cosine (input in radians), log and exp (natural logarithm),
102 square root.
104 B<ATAN>
106 Arctangent (output in radians).
108 B<ATAN2>
110 Arctangent of y,x components (output in radians).
111 This pops one element from the stack, the x (cosine) component, and then
112 a second, which is the y (sine) component.
113 It then pushes the arctangent of their ratio, resolving the ambiguity between
114 quadrants.
116 Example: C<CDEF:angle=Y,X,ATAN2,RAD2DEG> will convert C<X,Y>
117 components into an angle in degrees.
119 B<FLOOR, CEIL>
121 Round down or up to the nearest integer.
123 B<DEG2RAD, RAD2DEG>
125 Convert angle in degrees to radians, or radians to degrees.
127 B<ABS>
129 Take the absolute value.
131 =item Set Operations
133 B<SORT, REV>
135 Pop one element from the stack.  This is the I<count> of items to be sorted
136 (or reversed).  The top I<count> of the remaining elements are then sorted
137 (or reversed) in place on the stack.
139 Example: C<CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/> will
140 compute the average of the values v1 to v6 after removing the smallest and
141 largest.
143 B<AVG>
145 Pop one element (I<count>) from the stack. Now pop I<count> elements and build the
146 average, ignoring all UNKNOWN values in the process.
148 Example: C<CDEF:x=a,b,c,d,4,AVG>
150 B<TREND>
152 Create a "sliding window" average of another data series.
154 Usage:
155 CDEF:smoothed=x,1800,TREND
157 This will create a half-hour (1800 second) sliding window average of x.  The
158 average is essentially computed as shown here:
160                  +---!---!---!---!---!---!---!---!--->
161                                                      now
162                        delay     t0
163                  <--------------->
164                          delay       t1
165                      <--------------->
166                               delay      t2
167                          <--------------->
170      Value at sample (t0) will be the average between (t0-delay) and (t0)
171      Value at sample (t1) will be the average between (t1-delay) and (t1)
172      Value at sample (t2) will be the average between (t2-delay) and (t2)
174 =item Special values
176 B<UNKN>
178 Pushes an unknown value on the stack
180 B<INF, NEGINF>
182 Pushes a positive or negative infinite value on the stack. When
183 such a value is graphed, it appears at the top or bottom of the
184 graph, no matter what the actual value on the y-axis is.
186 B<PREV>
188 Pushes an I<unknown> value if this is the first value of a data
189 set or otherwise the result of this B<CDEF> at the previous time
190 step. This allows you to do calculations across the data.  This
191 function cannot be used in B<VDEF> instructions.
193 B<PREV(vname)>
195 Pushes an I<unknown> value if this is the first value of a data
196 set or otherwise the result of the vname variable at the previous time
197 step. This allows you to do calculations across the data. This
198 function cannot be used in B<VDEF> instructions.
200 B<COUNT>
202 Pushes the number 1 if this is the first value of the data set, the
203 number 2 if it is the second, and so on. This special value allows
204 you to make calculations based on the position of the value within
205 the data set. This function cannot be used in B<VDEF> instructions.
207 =item Time
209 Time inside RRDtool is measured in seconds since the epoch. The
210 epoch is defined to be S<C<Thu Jan  1 00:00:00 UTC 1970>>.
212 B<NOW>
214 Pushes the current time on the stack.
216 B<TIME>
218 Pushes the time the currently processed value was taken at onto the stack.
220 B<LTIME>
222 Takes the time as defined by B<TIME>, applies the time zone offset
223 valid at that time including daylight saving time if your OS supports
224 it, and pushes the result on the stack.  There is an elaborate example
225 in the examples section below on how to use this.
227 =item Processing the stack directly
229 B<DUP, POP, EXC>
231 Duplicate the top element, remove the top element, exchange the two
232 top elements.
234 =back
236 =head1 VARIABLES
238 These operators work only on B<VDEF> statements. Note that currently ONLY these work for B<VDEF>.
240 =over 4
242 =item MAXIMUM, MINIMUM, AVERAGE
244 Return the corresponding value, MAXIMUM and MINIMUM also return
245 the first occurrence of that value in the time component.
247 Example: C<VDEF:avg=mydata,AVERAGE>
249 =item LAST, FIRST
251 Return the last/first value including its time.  The time for
252 FIRST is actually the start of the corresponding interval, whereas
253 LAST returns the end of the corresponding interval.
255 Example: C<VDEF:first=mydata,FIRST>
257 =item TOTAL
259 Returns the rate from each defined time slot multiplied with the
260 step size.  This can, for instance, return total bytes transfered
261 when you have logged bytes per second. The time component returns
262 the number of seconds.
264 Example: C<VDEF:total=mydata,TOTAL>
266 =item PERCENT
268 This should follow a B<DEF> or B<CDEF> I<vname>. The I<vname> is popped,
269 another number is popped which is a certain percentage (0..100). The
270 data set is then sorted and the value returned is chosen such that
271 I<percentage> percent of the values is lower or equal than the result.
272 I<Unknown> values are considered lower than any finite number for this
273 purpose so if this operator returns an I<unknown> you have quite a lot
274 of them in your data.  B<Inf>inite numbers are lesser, or more, than the
275 finite numbers and are always more than the I<Unknown> numbers.
276 (NaN E<lt> -INF E<lt> finite values E<lt> INF)
278 Example: C<VDEF:perc95=mydata,95,PERCENT>
280 =item LSLSLOPE, LSLINT, LSLCORREL
282 Return the parameters for a B<L>east B<S>quares B<L>ine I<(y = mx +b)> 
283 which approximate the provided dataset.  LSLSLOPE is the slope I<(m)> of
284 the line related to the COUNT position of the data.  LSLINT is the 
285 y-intercept I<(b)>, which happens also to be the first data point on the 
286 graph. LSLCORREL is the Correlation Coefficient (also know as Pearson's 
287 Product Moment Correlation Coefficient).  It will range from 0 to +/-1 
288 and represents the quality of fit for the approximation.   
290 Example: C<VDEF:slope=mydata,LSLSLOPE>
292 =back
294 =head1 SEE ALSO
296 L<rrdgraph> gives an overview of how B<rrdtool graph> works.
297 L<rrdgraph_data> describes B<DEF>,B<CDEF> and B<VDEF> in detail.
298 L<rrdgraph_rpn> describes the B<RPN> language used in the B<?DEF> statements.
299 L<rrdgraph_graph> page describes all of the graph and print functions.
301 Make sure to read L<rrdgraph_examples> for tipsE<amp>tricks.
303 =head1 AUTHOR
305 Program by Tobias Oetiker E<lt>tobi@oetiker.chE<gt>
307 This manual page by Alex van den Bogaerdt E<lt>alex@vandenbogaerdt.nlE<gt>
308 with corrections and/or additions by several people