1 # Copyright (C) 2002 Stanislav Sinyagin
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 # $Id: SchedulerInfo.pm,v 1.1 2010-12-27 00:03:43 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
21 # Task scheduler runtime information. Quite basic statistics access.
23 package Torrus::SchedulerInfo;
37 %{$self->{'options'}} = %options;
39 die() if not defined( $options{'-Tree'} );
42 new Torrus::DB( 'scheduler_stats',
43 -Subdir => $self->{'options'}{'-Tree'},
45 -WriteAccess => $options{'-WriteAccess'} );
47 return( defined( $self->{'db_stats'} ) ? $self:undef );
54 delete $self->{'db_stats'};
64 my $cursor = $self->{'db_stats'}->cursor();
65 while( my ($key, $value) = $self->{'db_stats'}->next($cursor) )
67 my( $id, $variable ) = split( '#', $key );
68 if( defined( $id ) and defined( $variable ) )
70 $stats->{$id}{$variable} = $value;
86 $self->{'db_stats'}->put( join('#', $id, $variable), $value );
95 return $self->{'db_stats'}->get( join('#', $id, $variable) );
104 my $cursor = $self->{'db_stats'}->cursor( -Write => 1 );
105 while( my ($key, $value) = $self->{'db_stats'}->next($cursor) )
107 my( $db_id, $variable ) = split( '#', $key );
108 if( defined( $db_id ) and defined( $variable ) and
111 $self->{'db_stats'}->c_del( $cursor );
121 $self->{'db_stats'}->trunc();
129 my $variable = shift;
132 $self->setValue( $id, 'Last' . $variable, $value );
134 my $maxName = 'Max' . $variable;
135 my $maxVal = $self->getValue( $id, $maxName );
136 if( not defined( $maxVal ) or $value > $maxVal )
140 $self->setValue( $id, $maxName, $maxVal );
142 my $minName = 'Min' . $variable;
143 my $minVal = $self->getValue( $id, $minName );
144 if( not defined( $minVal ) or $value < $minVal )
148 $self->setValue( $id, $minName, $minVal );
150 my $timesName = 'NTimes' . $variable;
151 my $nTimes = $self->getValue( $id, $timesName );
153 my $avgName = 'Avg' . $variable;
154 my $average = $self->getValue( $id, $avgName );
156 if( not defined( $nTimes ) )
163 $average = ( $average * $nTimes + $value ) / ( $nTimes + 1 );
166 $self->setValue( $id, $timesName, $nTimes );
167 $self->setValue( $id, $avgName, $average );
169 my $expAvgName = 'ExpAvg' . $variable;
170 my $expAverage = $self->getValue( $id, $expAvgName );
171 if( not defined( $expAverage ) )
173 $expAverage = $value;
177 my $alpha = $Torrus::Scheduler::statsExpDecayAlpha;
178 $expAverage = $alpha * $value + ( 1 - $alpha ) * $expAverage;
180 $self->setValue( $id, $expAvgName, $expAverage );
188 my $variable = shift;
189 my $increment = shift;
191 if( not defined( $increment ) )
196 my $name = 'Count' . $variable;
197 my $previous = $self->getValue( $id, $name );
199 if( not defined( $previous ) )
204 $self->setValue( $id, $name, $previous + $increment );
214 # indent-tabs-mode: nil
215 # perl-indent-level: 4