123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- # ex:ts=8 sw=4:
- # $OpenBSD: Clock.pm,v 1.8 2015/08/11 22:39:57 espie Exp $
- #
- # Copyright (c) 2011-2013 Marc Espie <espie@openbsd.org>
- #
- # Permission to use, copy, modify, and distribute this software for any
- # purpose with or without fee is hereby granted, provided that the above
- # copyright notice and this permission notice appear in all copies.
- #
- # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- use strict;
- use warnings;
- # everything needed to handle clock
- use Time::HiRes qw(time);
- # explicit stop/restart clock where needed
- package DPB::Clock;
- # users will register/unregister, they must have a
- # stopped_clock($gap) method to adjust.
- my $items = {};
- sub register
- {
- my ($class, $o) = @_;
- $items->{$o} = $o;
- }
- sub unregister
- {
- my ($class, $o) = @_;
- delete $items->{$o};
- }
- my $stopped_clock;
- sub stop
- {
- $stopped_clock = time();
- }
- sub restart
- {
- my $gap = time() - $stopped_clock;
- for my $o (values %$items) {
- $o->stopped_clock($gap, $stopped_clock);
- }
- }
- # tasks with a timer
- package DPB::Task::Clocked;
- our @ISA = qw(DPB::Task::Fork);
- sub fork
- {
- my ($self, $core) = @_;
- $self->{started} = time();
- DPB::Clock->register($self);
- return $self->SUPER::fork($core);
- }
- sub finalize
- {
- my ($self, $core) = @_;
- $self->{ended} = time();
- DPB::Clock->unregister($self);
- return $self->SUPER::finalize($core);
- }
- sub elapsed
- {
- my $self = shift;
- return $self->{ended} - $self->{started};
- }
- sub stopped_clock
- {
- my ($self, $gap) = @_;
- $self->{started} += $gap;
- }
- # how to know if we're stuck: we watch some file size.
- # if there's some expected value, then we can display a %
- package DPB::Watch;
- sub new
- {
- my ($class, $file, $expected, $offset, $time) = @_;
- my $o = bless {
- file => $file,
- expected => $expected,
- offset => $offset,
- time => $time,
- max => 0,
- }, $class;
- DPB::Clock->register($o);
- return $o;
- }
- sub check_change
- {
- my ($self, $current) = @_;
- $self->{time} //= $current;
- my $sz = ($self->{file}->stat)[7];
- if (defined $sz && defined $self->{offset}) {
- $sz -= $self->{offset};
- }
- if ((defined $sz &&
- (!defined $self->{sz} || $self->{sz} != $sz)) ||
- (!defined $sz && defined $self->{sz})) {
- $self->{sz} = $sz;
- $self->{time} = $current;
- }
- my $d = $current - $self->{time};
- if ($d > $self->{max}) {
- $self->{max} = $d;
- }
- return $d;
- }
- sub percent_message
- {
- my $self = shift;
- my $progress = '';
- if (defined $self->{sz}) {
- if (defined $self->{expected} &&
- $self->{sz} < 4 * $self->{expected}) {
- $progress = ' '.
- int($self->{sz}*100/$self->{expected}). '%';
- } else {
- $progress = ' '.$self->{sz};
- }
- }
- return $progress;
- }
- sub frozen_message
- {
- my ($self, $diff) = @_;
- my $unchanged = " frozen for ";
- if ($diff > 7200) {
- $unchanged .= int($diff/3600)." HOURS!";
- } elsif ($diff > 300) {
- $unchanged .= int($diff/60)."mn";
- } elsif ($diff > 10) {
- $unchanged .= int($diff)."s";
- } else {
- $unchanged = "";
- }
- return $unchanged;
- }
- sub reset_offset
- {
- my $self = shift;
- my $sz = ($self->{file}->stat)[7];
- if (defined $sz) {
- $self->{offset} = $sz;
- }
- }
- sub stopped_clock
- {
- my ($self, $gap) = @_;
- $self->{time} += $gap if defined $self->{time};
- }
- sub DESTROY
- {
- DPB::Clock->unregister(shift);
- }
- 1;
|