Project

General

Profile

root / trunk / doc / manual / accounting.tex @ 705

1
In Chapter~\ref{chap:analysing} we saw that Haizea collects data while running through the use of \emph{probes}. While Haizea includes several probes, it is also possible for you to write your own probes by implementing a class that extends from the \texttt{AccountingProbe} class. A barebones probe would look like this:
2

    
3
\begin{wideshellverbatim}
4
from haizea.core.accounting import AccountingProbe
5

    
6
class MyProbe(AccountingProbe):
7
    
8
    def __init__(self, accounting):
9
        AccountingProbe.__init__(self, accounting)
10
        # Create counters, per-lease stats, per-run stats
11
    
12
    def finalize_accounting(self):
13
	# Collect information
14

    
15
    def at_timestep(self, lease_scheduler):
16
	# Collect information
17

    
18
    def at_lease_request(self, lease):
19
	# Collect information 
20

    
21
    def at_lease_done(self, lease):
22
	# Collect information 
23
\end{wideshellverbatim}
24

    
25
All the methods shown above are also present in \texttt{AccountingProbe}, but don't do anything. You have to override some or all of the methods to make sure that data gets collected. More specifically:
26

    
27
\begin{description}
28
\item[\texttt{at\_timestep}] Override this method to perform any actions every time the Haizea scheduler wakes up. The \texttt{lease\_scheduler} parameter contains Haizea's lease scheduler (an instance of the \texttt{LeaseScheduler} class), which you can use to gather scheduling data.
29
\item[\texttt{at\_lease\_request}] Override this method to collect data after a lease has been requested.
30
\item[\texttt{at\_lease\_done}] Override this method to collect data after a lease is done (this includes successful completion and rejected/cancelled/failed leases).
31
\item[\texttt{finalize\_accounting}] Override this method to perform any actions when data collection stops. This is usually where per-run data is computed.
32
\end{description}
33

    
34
Probes can collect three types of data:
35

    
36
\begin{description}
37
\item[Per-lease data:] Data attributable to individual leases or derived from how each lease was scheduled. 
38
\item[Per-run data:] Data from an entire run of Haizea
39
\item[Counters:] A counter is a time-ordered list showing how some metric varied throughout a single run of Haizea. 
40
\end{description}
41

    
42
The probe's constructor should create the counters and specify what per-lease data and per-run data will be collected by your probe (the methods to do this are described next). Notice how a probe's constructor receives a \texttt{accounting} parameter. When creating your probe, Haizea will pass an \texttt{AccountingDataCollection} object that you will be able to use in your probe's other methods to store data.
43

    
44
Once a probe has been implemented, it can be used in Haizea by specifying its full name in the \texttt{probes} option of the \texttt{accounting} section of the configuration file. For example, suppose you created a class called \texttt{MyProbe} in the {foobar.probes} module. To use the probe, the \texttt{probes} option would look like this:
45

    
46
\begin{wideshellverbatim}
47
probes: foobar.probes.MyProbe
48
\end{wideshellverbatim}
49

    
50
When running Haizea, you have to make sure that \texttt{foobar.probes.MyProbe} is in your \texttt{PYTHONPATH}. After running Haizea with your probe, you can access the data it collects using the \texttt{haizea-convert-data} command described in Section~\ref{sec:haizea-convert-data}
51

    
52
\section{Collecting per-lease data}
53

    
54
To collect per-lease data, you first have to specify the new type of data (or ``stat'') you will be collecting in your probe's constructor. This is done using the \verb+create_lease_stat+ method in \texttt{AccountingDataCollection} (which is stored in an \texttt{accounting} attribute in all probes). For example, let's assume you want to keep track of an admittedly silly statistic: whether the lease's identifier is odd or even. You could create a stat called \texttt{Odd or even?}:
55

    
56
\begin{wideshellverbatim}
57
from haizea.core.accounting import AccountingProbe
58

    
59
class MyProbe(AccountingProbe):
60
    
61
    def __init__(self, accounting):
62
        AccountingProbe.__init__(self, accounting)
63
        self.accounting.create_lease_stat("Odd or even?")	
64
\end{wideshellverbatim}
65

    
66
To set the value of this stat, you must use the \verb+set_lease_stat+ method in \texttt{AccountingDataCollection}. For this stat, it would make sense to call this method from the \texttt{at\_lease\_request} method in the probe:
67

    
68
\begin{wideshellverbatim}
69
def at_lease_request(self, lease):
70
    if lease.id \% 2 == 1:
71
        value = "odd"
72
    else:
73
        value = "even"
74
    self.accounting.set_lease_stat("Odd or even?", lease.id, value)	
75
\end{wideshellverbatim}
76

    
77
If you run Haizea with this probe, and then use \texttt{haizea-convert-data} to print the per-lease data collected by the probes, there will be an additional column titled \texttt{Odd or even?} in the generated CSV file.
78

    
79

    
80
\section{Collecting per-run data}
81

    
82
Collecting per-run data is similar to collecting per-lease data, and relies on two methods in \texttt{AccountingDataCollection}: \verb+create_stat+ to create the stat in the probe constructor and \verb+set_stat+ to set the value of the stat. Given that per-run data summarizes information from the entire run, \verb+set_stat+ will usually be called from the probe's \texttt{finalize\_accounting} method.
83

    
84

    
85
\section{Creating and updating counters}
86

    
87
To collect data using a counter you must first create the counter from the probe's constructor using the \verb+create_counter+ method in \texttt{AccountingDataCollection}:
88

    
89
\begin{wideshellverbatim}
90
create_counter(counter_id, avgtype)
91
\end{wideshellverbatim}
92

    
93
The first parameter is the name of the counter. The second parameter specifies the type of average to compute for the counter; Counters can store not just the value of the counter throughout time, but also a running average. There are three types of averages that can be specified through the \texttt{avgtype}
94
        
95
\begin{description}
96
\item[\texttt{AccountingDataCollection.AVERAGE\_NONE}:] Don't compute an average
97
\item[\texttt{AccountingDataCollection.AVERAGE\_NORMAL}:] For each entry, compute the average of all the values including and preceding that entry.
98
\item[\texttt{AccountingDataCollection.AVERAGE\_TIMEWEIGHTED}:]  For each entry, compute the average of all the values including and preceding that entry, weighing the average according to the time between each entry.
99
\end{description}
100

    
101
All counters are initialized to zero.
102

    
103
The counter can be updated using one of the following three methods:
104

    
105
\begin{wideshellverbatim}
106
incr_counter(counter_id, lease_id):
107
decr_counter(counter_id, lease_id):
108
append_to_counter(counter_id, value, lease_id):
109
\end{wideshellverbatim}
110

    
111
All three methods receive the name of the counter (\verb+counter_id+) and, optionally, the identifier of the lease that caused the update. \verb+incr_counter+ and \verb+decr_counter+ increment or decrement the counter, respectively, while \verb+append_to_counter+ changes the counter's value to the value specified by the \verb+value+ parameter.
112

    
113

    
114
\section{Examples}
115

    
116
See the \texttt{haizea.pluggable.accounting} module for the source code of the default probes included with Haizea.