Code

a972313afc9ce6f654144cd929d415d9d6743b80
[pkg-rrdtool.git] / doc / rrdgraph_rpn.html
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>rrdgraph_rpn</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:root@localhost" />
8 </head>
10 <body style="background-color: white">
12 <p><a name="__index__"></a></p>
13 <!-- INDEX BEGIN -->
14 <!--
16 <ul>
18         <li><a href="#name">NAME</a></li>
19         <li><a href="#synopsis">SYNOPSIS</a></li>
20         <li><a href="#description">DESCRIPTION</a></li>
21         <li><a href="#operators">OPERATORS</a></li>
22         <li><a href="#variables">VARIABLES</a></li>
23         <li><a href="#see_also">SEE ALSO</a></li>
24         <li><a href="#author">AUTHOR</a></li>
25 </ul>
26 -->
27 <!-- INDEX END -->
29 <p>
30 </p>
31 <h1><a name="name">NAME</a></h1>
32 <p>rrdgraph_rpn - About RPN Math in rrdtool graph</p>
33 <p>
34 </p>
35 <hr />
36 <h1><a name="synopsis">SYNOPSIS</a></h1>
37 <p><em>RPN expression</em>:=<em>vname</em>|<em>operator</em>|<em>value</em>[,<em>RPN expression</em>]</p>
38 <p>
39 </p>
40 <hr />
41 <h1><a name="description">DESCRIPTION</a></h1>
42 <p>If you have ever used a traditional HP calculator you already know
43 <strong>RPN</strong>. The idea behind <strong>RPN</strong> is that you have a stack and push
44 your data onto this stack. Whenever you execute an operation, it
45 takes as many elements from the stack as needed. Pushing is done
46 implicitly, so whenever you specify a number or a variable, it gets
47 pushed onto the stack automatically.</p>
48 <p>At the end of the calculation there should be one and only one value left on
49 the stack.  This is the outcome of the function and this is what is put into
50 the <em>vname</em>.  For <strong>CDEF</strong> instructions, the stack is processed for each
51 data point on the graph. <strong>VDEF</strong> instructions work on an entire data set in
52 one run. Note, that currently <strong>VDEF</strong> instructions only support a limited
53 list of functions.</p>
54 <p>Example: <code>VDEF:maximum=mydata,MAXIMUM</code></p>
55 <p>This will set variable ``maximum'' which you now can use in the rest
56 of your RRD script.</p>
57 <p>Example: <code>CDEF:mydatabits=mydata,8,*</code></p>
58 <p>This means:  push variable <em>mydata</em>, push the number 8, execute
59 the operator <em>*</em>. The operator needs two elements and uses those
60 to return one value.  This value is then stored in <em>mydatabits</em>.
61 As you may have guessed, this instruction means nothing more than
62 <em>mydatabits = mydata * 8</em>.  The real power of <strong>RPN</strong> lies in the
63 fact that it is always clear in which order to process the input.
64 For expressions like <code>a = b + 3 * 5</code> you need to multiply 3 with
65 5 first before you add <em>b</em> to get <em>a</em>. However, with parentheses
66 you could change this order: <code>a = (b + 3) * 5</code>. In <strong>RPN</strong>, you
67 would do <code>a = b, 3, +, 5, *</code> without the need for parentheses.</p>
68 <p>
69 </p>
70 <hr />
71 <h1><a name="operators">OPERATORS</a></h1>
72 <dl>
73 <dt><strong><a name="item_boolean_operators">Boolean operators</a></strong>
75 <dd>
76 <p><strong>LT, LE, GT, GE, EQ, NE</strong></p>
77 </dd>
78 <dd>
79 <p>Pop two elements from the stack, compare them for the selected condition
80 and return 1 for true or 0 for false. Comparing an <em>unknown</em> or an
81 <em>infinite</em> value will always result in 0 (false).</p>
82 </dd>
83 <dd>
84 <p><strong>UN, ISINF</strong></p>
85 </dd>
86 <dd>
87 <p>Pop one element from the stack, compare this to <em>unknown</em> respectively
88 to <em>positive or negative infinity</em>. Returns 1 for true or 0 for false.</p>
89 </dd>
90 <dd>
91 <p><strong>IF</strong></p>
92 </dd>
93 <dd>
94 <p>Pops three elements from the stack.  If the element popped last is 0
95 (false), the value popped first is pushed back onto the stack,
96 otherwise the value popped second is pushed back. This does, indeed,
97 mean that any value other than 0 is considered to be true.</p>
98 </dd>
99 <dd>
100 <p>Example: <code>A,B,C,IF</code> should be read as <code>if (A) then (B) else (C)</code></p>
101 </dd>
102 </li>
103 <dt><strong><a name="item_comparing_values">Comparing values</a></strong>
105 <dd>
106 <p><strong>MIN, MAX</strong></p>
107 </dd>
108 <dd>
109 <p>Pops two elements from the stack and returns the smaller or larger,
110 respectively.  Note that <em>infinite</em> is larger than anything else.
111 If one of the input numbers is <em>unknown</em> then the result of the operation will be
112 <em>unknown</em> too.</p>
113 </dd>
114 <dd>
115 <p><strong>LIMIT</strong></p>
116 </dd>
117 <dd>
118 <p>Pops two elements from the stack and uses them to define a range.
119 Then it pops another element and if it falls inside the range, it
120 is pushed back. If not, an <em>unknown</em> is pushed.</p>
121 </dd>
122 <dd>
123 <p>The range defined includes the two boundaries (so: a number equal
124 to one of the boundaries will be pushed back). If any of the three
125 numbers involved is either <em>unknown</em> or <em>infinite</em> this function
126 will always return an <em>unknown</em></p>
127 </dd>
128 <dd>
129 <p>Example: <code>CDEF:a=alpha,0,100,LIMIT</code> will return <em>unknown</em> if
130 alpha is lower than 0 or if it is higher than 100.</p>
131 </dd>
132 </li>
133 <dt><strong><a name="item_arithmetics">Arithmetics</a></strong>
135 <dd>
136 <p><strong>+, -, *, /, %</strong></p>
137 </dd>
138 <dd>
139 <p>Add, subtract, multiply, divide, modulo</p>
140 </dd>
141 <dd>
142 <p><strong>SIN, COS, LOG, EXP, SQRT</strong></p>
143 </dd>
144 <dd>
145 <p>Sine and cosine (input in radians), log and exp (natural logarithm),
146 square root.</p>
147 </dd>
148 <dd>
149 <p><strong>ATAN</strong></p>
150 </dd>
151 <dd>
152 <p>Arctangent (output in radians).</p>
153 </dd>
154 <dd>
155 <p><strong>ATAN2</strong></p>
156 </dd>
157 <dd>
158 <p>Arctangent of y,x components (output in radians).
159 This pops one element from the stack, the x (cosine) component, and then
160 a second, which is the y (sine) component.
161 It then pushes the arctangent of their ratio, resolving the ambiguity between
162 quadrants.</p>
163 </dd>
164 <dd>
165 <p>Example: <code>CDEF:angle=Y,X,ATAN2,RAD2DEG</code> will convert <code>X,Y</code>
166 components into an angle in degrees.</p>
167 </dd>
168 <dd>
169 <p><strong>FLOOR, CEIL</strong></p>
170 </dd>
171 <dd>
172 <p>Round down or up to the nearest integer.</p>
173 </dd>
174 <dd>
175 <p><strong>DEG2RAD, RAD2DEG</strong></p>
176 </dd>
177 <dd>
178 <p>Convert angle in degrees to radians, or radians to degrees.</p>
179 </dd>
180 <dd>
181 <p><strong>ABS</strong></p>
182 </dd>
183 <dd>
184 <p>Take the absolute value.</p>
185 </dd>
186 </li>
187 <dt><strong><a name="item_set_operations">Set Operations</a></strong>
189 <dd>
190 <p><strong>SORT, REV</strong></p>
191 </dd>
192 <dd>
193 <p>Pop one element from the stack.  This is the <em>count</em> of items to be sorted
194 (or reversed).  The top <em>count</em> of the remaining elements are then sorted
195 (or reversed) in place on the stack.</p>
196 </dd>
197 <dd>
198 <p>Example: <code>CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/</code> will
199 compute the average of the values v1 to v6 after removing the smallest and
200 largest.</p>
201 </dd>
202 <dd>
203 <p><strong>AVG</strong></p>
204 </dd>
205 <dd>
206 <p>Pop one element (<em>count</em>) from the stack. Now pop <em>count</em> elements and build the
207 average, ignoring all UNKNOWN values in the process.</p>
208 </dd>
209 <dd>
210 <p>Example: <code>CDEF:x=a,b,c,d,4,AVG</code></p>
211 </dd>
212 <dd>
213 <p><strong>TREND</strong></p>
214 </dd>
215 <dd>
216 <p>Create a ``sliding window'' average of another data series.</p>
217 </dd>
218 <dd>
219 <p>Usage:
220 CDEF:smoothed=x,1800,TREND</p>
221 </dd>
222 <dd>
223 <p>This will create a half-hour (1800 second) sliding window average of x.  The
224 average is essentially computed as shown here:</p>
225 </dd>
226 <dd>
227 <pre>
228                  +---!---!---!---!---!---!---!---!---&gt;
229                                                      now
230                        delay     t0
231                  &lt;---------------&gt;
232                          delay       t1
233                      &lt;---------------&gt;
234                               delay      t2
235                          &lt;---------------&gt;</pre>
236 </dd>
237 <dd>
238 <pre>
239      Value at sample (t0) will be the average between (t0-delay) and (t0)
240      Value at sample (t1) will be the average between (t1-delay) and (t1)
241      Value at sample (t2) will be the average between (t2-delay) and (t2)</pre>
242 </dd>
243 </li>
244 <dt><strong><a name="item_special_values">Special values</a></strong>
246 <dd>
247 <p><strong>UNKN</strong></p>
248 </dd>
249 <dd>
250 <p>Pushes an unknown value on the stack</p>
251 </dd>
252 <dd>
253 <p><strong>INF, NEGINF</strong></p>
254 </dd>
255 <dd>
256 <p>Pushes a positive or negative infinite value on the stack. When
257 such a value is graphed, it appears at the top or bottom of the
258 graph, no matter what the actual value on the y-axis is.</p>
259 </dd>
260 <dd>
261 <p><strong>PREV</strong></p>
262 </dd>
263 <dd>
264 <p>Pushes an <em>unknown</em> value if this is the first value of a data
265 set or otherwise the result of this <strong>CDEF</strong> at the previous time
266 step. This allows you to do calculations across the data.  This
267 function cannot be used in <strong>VDEF</strong> instructions.</p>
268 </dd>
269 <dd>
270 <p><strong>PREV(vname)</strong></p>
271 </dd>
272 <dd>
273 <p>Pushes an <em>unknown</em> value if this is the first value of a data
274 set or otherwise the result of the vname variable at the previous time
275 step. This allows you to do calculations across the data. This
276 function cannot be used in <strong>VDEF</strong> instructions.</p>
277 </dd>
278 <dd>
279 <p><strong>COUNT</strong></p>
280 </dd>
281 <dd>
282 <p>Pushes the number 1 if this is the first value of the data set, the
283 number 2 if it is the second, and so on. This special value allows
284 you to make calculations based on the position of the value within
285 the data set. This function cannot be used in <strong>VDEF</strong> instructions.</p>
286 </dd>
287 </li>
288 <dt><strong><a name="item_time">Time</a></strong>
290 <dd>
291 <p>Time inside RRDtool is measured in seconds since the epoch. The
292 epoch is defined to be <code>Thu&nbsp;Jan&nbsp;&nbsp;1&nbsp;00:00:00&nbsp;UTC&nbsp;1970</code>.</p>
293 </dd>
294 <dd>
295 <p><strong>NOW</strong></p>
296 </dd>
297 <dd>
298 <p>Pushes the current time on the stack.</p>
299 </dd>
300 <dd>
301 <p><strong>TIME</strong></p>
302 </dd>
303 <dd>
304 <p>Pushes the time the currently processed value was taken at onto the stack.</p>
305 </dd>
306 <dd>
307 <p><strong>LTIME</strong></p>
308 </dd>
309 <dd>
310 <p>Takes the time as defined by <strong>TIME</strong>, applies the time zone offset
311 valid at that time including daylight saving time if your OS supports
312 it, and pushes the result on the stack.  There is an elaborate example
313 in the examples section below on how to use this.</p>
314 </dd>
315 </li>
316 <dt><strong><a name="item_processing_the_stack_directly">Processing the stack directly</a></strong>
318 <dd>
319 <p><strong>DUP, POP, EXC</strong></p>
320 </dd>
321 <dd>
322 <p>Duplicate the top element, remove the top element, exchange the two
323 top elements.</p>
324 </dd>
325 </li>
326 </dl>
327 <p>
328 </p>
329 <hr />
330 <h1><a name="variables">VARIABLES</a></h1>
331 <p>These operators work only on <strong>VDEF</strong> statements. Note that currently ONLY these work for <strong>VDEF</strong>.</p>
332 <dl>
333 <dt><strong><a name="item_maximum_2c_minimum_2c_average">MAXIMUM, MINIMUM, AVERAGE</a></strong>
335 <dd>
336 <p>Return the corresponding value, MAXIMUM and MINIMUM also return
337 the first occurrence of that value in the time component.</p>
338 </dd>
339 <dd>
340 <p>Example: <code>VDEF:avg=mydata,AVERAGE</code></p>
341 </dd>
342 </li>
343 <dt><strong><a name="item_last_2c_first">LAST, FIRST</a></strong>
345 <dd>
346 <p>Return the last/first value including its time.  The time for
347 FIRST is actually the start of the corresponding interval, whereas
348 LAST returns the end of the corresponding interval.</p>
349 </dd>
350 <dd>
351 <p>Example: <code>VDEF:first=mydata,FIRST</code></p>
352 </dd>
353 </li>
354 <dt><strong><a name="item_total">TOTAL</a></strong>
356 <dd>
357 <p>Returns the rate from each defined time slot multiplied with the
358 step size.  This can, for instance, return total bytes transfered
359 when you have logged bytes per second. The time component returns
360 the number of seconds.</p>
361 </dd>
362 <dd>
363 <p>Example: <code>VDEF:total=mydata,TOTAL</code></p>
364 </dd>
365 </li>
366 <dt><strong><a name="item_percent">PERCENT</a></strong>
368 <dd>
369 <p>This should follow a <strong>DEF</strong> or <strong>CDEF</strong> <em>vname</em>. The <em>vname</em> is popped,
370 another number is popped which is a certain percentage (0..100). The
371 data set is then sorted and the value returned is chosen such that
372 <em>percentage</em> percent of the values is lower or equal than the result.
373 <em>Unknown</em> values are considered lower than any finite number for this
374 purpose so if this operator returns an <em>unknown</em> you have quite a lot
375 of them in your data.  <strong>Inf</strong>inite numbers are lesser, or more, than the
376 finite numbers and are always more than the <em>Unknown</em> numbers.
377 (NaN &lt; -INF &lt; finite values &lt; INF)</p>
378 </dd>
379 <dd>
380 <p>Example: <code>VDEF:perc95=mydata,95,PERCENT</code></p>
381 </dd>
382 </li>
383 <dt><strong><a name="item_lslslope_2c_lslint_2c_lslcorrel">LSLSLOPE, LSLINT, LSLCORREL</a></strong>
385 <dd>
386 <p>Return the parameters for a <strong>L</strong>east <strong>S</strong>quares <strong>L</strong>ine <em>(y = mx +b)</em> 
387 which approximate the provided dataset.  LSLSLOPE is the slope <em>(m)</em> of
388 the line related to the COUNT position of the data.  LSLINT is the 
389 y-intercept <em>(b)</em>, which happens also to be the first data point on the 
390 graph. LSLCORREL is the Correlation Coefficient (also know as Pearson's 
391 Product Moment Correlation Coefficient).  It will range from 0 to +/-1 
392 and represents the quality of fit for the approximation.</p>
393 </dd>
394 <dd>
395 <p>Example: <code>VDEF:slope=mydata,LSLSLOPE</code></p>
396 </dd>
397 </li>
398 </dl>
399 <p>
400 </p>
401 <hr />
402 <h1><a name="see_also">SEE ALSO</a></h1>
403 <p><a href="././rrdgraph.html">the rrdgraph manpage</a> gives an overview of how <strong>rrdtool graph</strong> works.
404 <a href="././rrdgraph_data.html">the rrdgraph_data manpage</a> describes <strong>DEF</strong>,<strong>CDEF</strong> and <strong>VDEF</strong> in detail.
405 <a href="././rrdgraph_rpn.html">the rrdgraph_rpn manpage</a> describes the <strong>RPN</strong> language used in the <strong>?DEF</strong> statements.
406 <a href="././rrdgraph_graph.html">the rrdgraph_graph manpage</a> page describes all of the graph and print functions.</p>
407 <p>Make sure to read <a href="././rrdgraph_examples.html">the rrdgraph_examples manpage</a> for tips&amp;tricks.</p>
408 <p>
409 </p>
410 <hr />
411 <h1><a name="author">AUTHOR</a></h1>
412 <p>Program by Tobias Oetiker &lt;<a href="mailto:tobi@oetiker.ch">tobi@oetiker.ch</a>&gt;</p>
413 <p>This manual page by Alex van den Bogaerdt &lt;<a href="mailto:alex@ergens.op.het.net">alex@ergens.op.het.net</a>&gt;</p>
415 </body>
417 </html>