Skip to content

Commit

Permalink
Introducing Usage (name to be bikeshedded)
Browse files Browse the repository at this point in the history
- exposing nqp::getrusage information
- methods: cpu, cpu-user, cpu-sys providing microsecond accuracy
- method: interval, returning CPU microseconds since last call
- can all be called as class and as instance method
- also provides infix:<->(Usage,Usage) for convenience
  • Loading branch information
lizmat committed Oct 28, 2017
1 parent 61af87b commit cbd4f21
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/core/Usage.pm
@@ -0,0 +1,56 @@
# An attempt at providing an API to nqp::getrusage.

class Usage {
has int $!cpu-user;
has int $!cpu-sys;

submethod BUILD(--> Nil) {
my \rusage = nqp::getrusage;
$!cpu-user = nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC);
$!cpu-sys = nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC);
}

proto method cpu() { * }
multi method cpu(Usage:U:) is raw {
my \rusage = nqp::getrusage;
nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC)
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC)
}
multi method cpu(Usage:D:) is raw {
nqp::add_i($!cpu-user,$!cpu-sys)
}

proto method cpu-user() { * }
multi method cpu-user(Usage:U:) is raw {
my \rusage = nqp::getrusage;
nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC)
}
multi method cpu-user(Usage:D:) is raw { $!cpu-user }

proto method cpu-sys() { * }
multi method cpu-sys(Usage:U:) is raw {
my \rusage = nqp::getrusage;
nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC)
}
multi method cpu-sys(Usage:D:) is raw { $!cpu-sys }

proto method interval() { * }
multi method interval(Usage:U \SELF:) is raw { SELF = SELF.new; 0 }
multi method interval(Usage:D \SELF:) is raw {
my int $cpu = (my $new := Usage.new) - SELF;
SELF = $new;
$cpu;
}
}

multi sub infix:<->(Usage $a, Usage $b) {
$a.cpu - $b.cpu
}

# vim: ft=perl6 expandtab sw=4
1 change: 1 addition & 0 deletions tools/build/jvm_core_sources
Expand Up @@ -161,6 +161,7 @@ src/core/Awaitable.pm
src/core/Awaiter.pm
src/core/Scheduler.pm
src/core/Env.pm
src/core/Usage.pm
src/core/ThreadPoolScheduler.pm
src/core/CurrentThreadScheduler.pm
src/core/Promise.pm
Expand Down
1 change: 1 addition & 0 deletions tools/build/moar_core_sources
Expand Up @@ -164,6 +164,7 @@ src/core/Awaiter.pm
src/core/Scheduler.pm
src/core/Env.pm
src/core/atomicops.pm
src/core/Usage.pm
src/core/ThreadPoolScheduler.pm
src/core/CurrentThreadScheduler.pm
src/core/Promise.pm
Expand Down

0 comments on commit cbd4f21

Please sign in to comment.