Skip to content

Commit

Permalink
Don't shift/push the samples array
Browse files Browse the repository at this point in the history
- just index % 5 into it since we have a fixed size array
- rename LAST_UTILS_NUM to NUM_SAMPLES for more readability
  • Loading branch information
lizmat committed Oct 27, 2017
1 parent 6bf5892 commit 6de66df
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/core/ThreadPoolScheduler.pm
Expand Up @@ -441,7 +441,7 @@ my class ThreadPoolScheduler does Scheduler {
# it takes stock of the current situation and decides whether or not to
# add threads.
my constant SUPERVISION_INTERVAL = 0.01;
my constant LAST_UTILS_NUM = 5;
my constant NUM_SAMPLES = 5;
method !maybe-start-supervisor(--> Nil) {
unless $!supervisor.DEFINITE {
$!supervisor = Thread.start(:app_lifetime, {
Expand Down Expand Up @@ -474,7 +474,8 @@ my class ThreadPoolScheduler does Scheduler {
scheduler-debug "Supervisor started";
my num $last-rusage-time = nqp::time_n;
my int $last-usage = self!getrusage-total();
my num @last-utils = 0e0 xx LAST_UTILS_NUM;
my num @last-utils = 0e0 xx NUM_SAMPLES;
my int $sampled;
my int $cpu-cores = nqp::cpucores();
scheduler-debug "Supervisor thinks there are $cpu-cores CPU cores";
loop {
Expand All @@ -496,12 +497,11 @@ my class ThreadPoolScheduler does Scheduler {
my num $normalized-delta = $usage-delta / $rusage-period;
my num $per-core = $normalized-delta / $cpu-cores;
my num $per-core-util =
100 * ($per-core / (1000000 * LAST_UTILS_NUM));
100 * ($per-core / (1000000 * NUM_SAMPLES));

# Since those values are noisy, average the last
# LAST_UTILS_NUM values to get a smoothed value.
@last-utils.shift;
@last-utils.push: $per-core-util;
# NUM_SAMPLES values to get a smoothed value.
@last-utils[++$sampled % NUM_SAMPLES] = $per-core-util;
my $smooth-per-core-util = @last-utils.sum;
scheduler-debug-status "Per-core utilization (approx): $smooth-per-core-util%";

Expand Down

0 comments on commit 6de66df

Please sign in to comment.