Skip to content

Commit

Permalink
Move the ThreadPoolScheduler data collection logic
Browse files Browse the repository at this point in the history
- to make a similar API to Thread.usage
- allowing other modules access to the data without needing to know internals
- no change in functionality
  • Loading branch information
lizmat committed Nov 15, 2017
1 parent 15eb4f6 commit 2c84f77
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 87 deletions.
88 changes: 1 addition & 87 deletions lib/Telemetry.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,6 @@ class Telemetry::Instrument::ThreadPool does Telemetry::Instrument {
# actual snapping logic
class Snap does Telemetry::Instrument::Snap {

# Constants indexing into the data array
my constant SUPERVISOR = 0;
my constant GW = 1;
my constant GTQ = 2;
my constant GTC = 3;
my constant TW = 4;
my constant TTQ = 5;
my constant TTC = 6;
my constant AW = 7;
my constant ATQ = 8;
my constant ATC = 9;
my constant COLUMNS = 10;

# Initialize the dispatch hash using HLL features, as we only need to
# do this on module load time. First handle the usable names of
# attributes that are part of getrusage struct.
Expand All @@ -350,24 +337,6 @@ class Telemetry::Instrument::ThreadPool does Telemetry::Instrument {
# Allow for low-level dispatch hash access for speed
my $dispatch := nqp::getattr(%dispatch,Map,'$!storage');

# calculate number of tasks completed for a worker list
sub completed(\workers) is raw {
my int $elems = nqp::elems(workers);
my int $completed;
my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::stmts(
(my $w := nqp::atpos(workers,$i)),
($completed = nqp::add_i(
$completed,
nqp::getattr_i($w,$w.WHAT,'$!total')
))
)
);
$completed
}

method AT-KEY(Str:D $key) {
nqp::ifnull(
nqp::atkey($dispatch,$key),
Expand All @@ -378,62 +347,7 @@ class Telemetry::Instrument::ThreadPool does Telemetry::Instrument {
method EXISTS-KEY(Str:D $key) { nqp::existskey($dispatch,$key) }

method !snap() is raw {
my $data := nqp::setelems(nqp::list_i,COLUMNS);

if $*SCHEDULER -> \scheduler {
my $sched := nqp::decont(scheduler);

nqp::bindpos_i($data,SUPERVISOR,1)
if nqp::getattr($sched,ThreadPoolScheduler,'$!supervisor');

if nqp::getattr($sched,ThreadPoolScheduler,'$!general-workers')
-> \workers {
nqp::bindpos_i($data,GW,nqp::elems(workers));
if nqp::getattr($sched,ThreadPoolScheduler,'$!general-queue')
-> \queue {
nqp::bindpos_i($data,GTQ,nqp::elems(queue));
}
nqp::bindpos_i($data,GTC,completed(workers));
}

if nqp::getattr($sched,ThreadPoolScheduler,'$!timer-workers')
-> \workers {
nqp::bindpos_i($data,TW,nqp::elems(workers));
if nqp::getattr($sched,ThreadPoolScheduler,'$!timer-queue')
-> \queue {
nqp::bindpos_i($data,TTQ,nqp::elems(queue));
}
nqp::bindpos_i($data,TTC,completed(workers));
}

if nqp::getattr($sched,ThreadPoolScheduler,'$!affinity-workers')
-> \workers {
my int $elems =
nqp::bindpos_i($data,AW,nqp::elems(workers));
my int $completed;
my int $queued;
my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::stmts(
(my $w := nqp::atpos(workers,$i)),
($completed = nqp::add_i(
$completed,
nqp::getattr_i($w,$w.WHAT,'$!total')
)),
($queued = nqp::add_i(
$queued,
nqp::elems(nqp::getattr($w,$w.WHAT,'$!queue'))
))
)
);
nqp::bindpos_i($data,ATQ,$queued);
nqp::bindpos_i($data,ATC,$completed);
}
}

# the final thing
$data
$*SCHEDULER ?? $*SCHEDULER.usage !! ThreadPoolScheduler.usage
}
}

Expand Down
82 changes: 82 additions & 0 deletions src/core/ThreadPoolScheduler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,88 @@ my class ThreadPoolScheduler does Scheduler {

$loads
}

# Constants indexing into the data array
my constant SUPERVISOR = 0;
my constant GW = 1;
my constant GTQ = 2;
my constant GTC = 3;
my constant TW = 4;
my constant TTQ = 5;
my constant TTC = 6;
my constant AW = 7;
my constant ATQ = 8;
my constant ATC = 9;
my constant COLUMNS = 10;

# calculate number of tasks completed for a worker list
sub completed(\workers) is raw {
my int $elems = nqp::elems(workers);
my int $completed;
my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::stmts(
(my $w := nqp::atpos(workers,$i)),
($completed = nqp::add_i(
$completed,
nqp::getattr_i($w,$w.WHAT,'$!total')
))
)
);
$completed
}

proto method usage(|) {*}
multi method usage(ThreadPoolScheduler:U:) is raw {
nqp::setelems(nqp::list_i,COLUMNS)
}
multi method usage(ThreadPoolScheduler:D:) is raw {
my $data := nqp::setelems(nqp::list_i,COLUMNS);

nqp::bindpos_i($data,SUPERVISOR,1) if $!supervisor;

if $!general-workers -> \workers {
nqp::bindpos_i($data,GW,nqp::elems(workers));
nqp::bindpos_i($data,GTQ,nqp::elems($!general-queue))
if $!general-queue;
nqp::bindpos_i($data,GTC,completed(workers));
}

if $!timer-workers -> \workers {
nqp::bindpos_i($data,TW,nqp::elems(workers));
nqp::bindpos_i($data,TTQ,nqp::elems($!timer-queue))
if $!timer-queue;
nqp::bindpos_i($data,TTC,completed(workers));
}

if $!affinity-workers -> \workers {
my int $elems =
nqp::bindpos_i($data,AW,nqp::elems(workers));
my int $completed;
my int $queued;
my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::stmts(
(my $w := nqp::atpos(workers,$i)),
($completed = nqp::add_i(
$completed,
nqp::getattr_i($w,$w.WHAT,'$!total')
)),
($queued = nqp::add_i(
$queued,
nqp::elems(nqp::getattr($w,$w.WHAT,'$!queue'))
))
)
);
nqp::bindpos_i($data,ATQ,$queued);
nqp::bindpos_i($data,ATC,$completed);
}

# the final thing
$data
}
}

# vim: ft=perl6 expandtab sw=4

0 comments on commit 2c84f77

Please sign in to comment.