1bd335c64Sesolom#!/usr/perl5/bin/perl 2bd335c64Sesolom# 3bd335c64Sesolom# CDDL HEADER START 4bd335c64Sesolom# 5bd335c64Sesolom# The contents of this file are subject to the terms of the 6bd335c64Sesolom# Common Development and Distribution License, Version 1.0 only 7bd335c64Sesolom# (the "License"). You may not use this file except in compliance 8bd335c64Sesolom# with the License. 9bd335c64Sesolom# 10bd335c64Sesolom# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11bd335c64Sesolom# or http://www.opensolaris.org/os/licensing. 12bd335c64Sesolom# See the License for the specific language governing permissions 13bd335c64Sesolom# and limitations under the License. 14bd335c64Sesolom# 15bd335c64Sesolom# When distributing Covered Code, include this CDDL HEADER in each 16bd335c64Sesolom# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17bd335c64Sesolom# If applicable, add the following below this CDDL HEADER, with the 18bd335c64Sesolom# fields enclosed by brackets "[]" replaced with your own identifying 19bd335c64Sesolom# information: Portions Copyright [yyyy] [name of copyright owner] 20bd335c64Sesolom# 21bd335c64Sesolom# CDDL HEADER END 22bd335c64Sesolom# 23bd335c64Sesolom 24bd335c64Sesolom# 25bd335c64Sesolom# Copyright 2005 Sun Microsystems, Inc. All rights reserved. 26bd335c64Sesolom# Use is subject to license terms. 27bd335c64Sesolom# 28bd335c64Sesolom#ident "%Z%%M% %I% %E% SMI" 29bd335c64Sesolom# 30bd335c64Sesolom 31bd335c64Sesolomrequire 5.6.1; 32bd335c64Sesolomuse strict; 33bd335c64Sesolomuse warnings; 34bd335c64Sesolomuse POSIX; 35bd335c64Sesolomuse File::Basename("basename"); 36bd335c64Sesolom 37bd335c64Sesolommy $cmdname = basename($0); 38bd335c64Sesolom 39bd335c64Sesolommy $using_scengen = 0; # 1 if using scenario simulator 40bd335c64Sesolommy $debug = 0; 41bd335c64Sesolom 42bd335c64Sesolommy $min_sleeptime = 1; 43bd335c64Sesolommy $max_sleeptime = 15; 44bd335c64Sesolommy $onecpu_sleeptime = (60 * 15); # used if only 1 CPU on system 45bd335c64Sesolommy $sleeptime = $min_sleeptime; # time to sleep between kstat updates 46bd335c64Sesolom 47bd335c64Sesolom# For timerange_foo variables, see comments at tail of &getstat() 48bd335c64Sesolom 49bd335c64Sesolommy $timerange_toohi = .01; 50bd335c64Sesolommy $timerange_hithresh = .0003; 51bd335c64Sesolommy $timerange_lothresh = $timerange_hithresh / 2; 52bd335c64Sesolommy $unsafe_timerange = .02; 53bd335c64Sesolom 54bd335c64Sesolommy $statslen = 60; # time period (in secs) to keep in @deltas 55bd335c64Sesolom 56bd335c64Sesolom 57bd335c64Sesolom# Parse arguments. intrd does not accept any public arguments; the two 58bd335c64Sesolom# arguments below are meant for testing purposes. -D generates a significant 59bd335c64Sesolom# amount of syslog output. -S <filename> loads the filename as a perl 60bd335c64Sesolom# script. That file is expected to implement a kstat "simulator" which 61bd335c64Sesolom# can be used to feed information to intrd and verify intrd's responses. 62bd335c64Sesolom 63bd335c64Sesolomwhile ($_ = shift @ARGV) { 64bd335c64Sesolom if ($_ eq "-S" && $#ARGV != -1) { 65bd335c64Sesolom $using_scengen = 1; 66bd335c64Sesolom do $ARGV[0]; # load simulator 67bd335c64Sesolom shift @ARGV; 68bd335c64Sesolom } elsif ($_ eq "-D") { 69bd335c64Sesolom $debug = 1; 70bd335c64Sesolom } 71bd335c64Sesolom} 72bd335c64Sesolom 73bd335c64Sesolomif ($using_scengen == 0) { 74bd335c64Sesolom require Sun::Solaris::Kstat; 75bd335c64Sesolom require Sun::Solaris::Intrs; 76bd335c64Sesolom import Sun::Solaris::Intrs(qw(intrmove)); 77bd335c64Sesolom require Sys::Syslog; 78bd335c64Sesolom import Sys::Syslog; 79bd335c64Sesolom openlog($cmdname, 'pid', 'daemon'); 80bd335c64Sesolom setlogmask(Sys::Syslog::LOG_UPTO($debug > 0 ? &Sys::Syslog::LOG_DEBUG : 81bd335c64Sesolom &Sys::Syslog::LOG_INFO)); 82bd335c64Sesolom} 83bd335c64Sesolom 84bd335c64Sesolom 85bd335c64Sesolommy $asserted = 0; 86bd335c64Sesolommy $assert_level = 'debug'; # syslog level for assertion failures 87bd335c64Sesolomsub VERIFY($@) 88bd335c64Sesolom{ 89bd335c64Sesolom my $bad = (shift() == 0); # $_[0] == 0 means assert failed 90bd335c64Sesolom if ($bad) { 91bd335c64Sesolom my $msg = shift(); 92bd335c64Sesolom syslog($assert_level, "VERIFY: $msg", @_); 93bd335c64Sesolom $asserted++; 94bd335c64Sesolom } 95bd335c64Sesolom return ($bad); 96bd335c64Sesolom} 97bd335c64Sesolom 98bd335c64Sesolom 99bd335c64Sesolom 100bd335c64Sesolom 101bd335c64Sesolomsub getstat($); 102bd335c64Sesolomsub generate_delta($$); 103bd335c64Sesolomsub compress_deltas($); 104bd335c64Sesolomsub dumpdelta($); 105bd335c64Sesolom 106bd335c64Sesolomsub goodness($); 107bd335c64Sesolomsub imbalanced($$); 108bd335c64Sesolomsub do_reconfig($); 109bd335c64Sesolom 110bd335c64Sesolomsub goodness_cpu($$); # private function 111bd335c64Sesolomsub move_intr($$$$); # private function 112bd335c64Sesolomsub ivecs_to_string(@); # private function 113bd335c64Sesolomsub do_find_goal($$$$); # private function 114bd335c64Sesolomsub find_goal($$); # private function 115bd335c64Sesolomsub do_reconfig_cpu2cpu($$$$); # private function 116bd335c64Sesolomsub do_reconfig_cpu($$$); # private function 117bd335c64Sesolom 118bd335c64Sesolom 119bd335c64Sesolom# 120bd335c64Sesolom# What follow are the basic data structures routines of intrd. 121bd335c64Sesolom# 122bd335c64Sesolom# getstat() is responsible for reading the kstats and generating a "stat" hash. 123bd335c64Sesolom# 124bd335c64Sesolom# generate_delta() is responsible for taking two "stat" hashes and creating 125bd335c64Sesolom# a new "delta" hash that represents what has changed over time. 126bd335c64Sesolom# 127bd335c64Sesolom# compress_deltas() is responsible for taking a list of deltas and generating 128bd335c64Sesolom# a single delta hash that encompasses all the time periods described by the 129bd335c64Sesolom# deltas. 130bd335c64Sesolom 131bd335c64Sesolom 132bd335c64Sesolom# 133bd335c64Sesolom# getstat() is handed a reference to a kstat and generates a hash, returned 134bd335c64Sesolom# by reference, containing all the fields from the kstats which we need. 135bd335c64Sesolom# If it returns the scalar 0, it failed to gather the kstats, and the caller 136bd335c64Sesolom# should react accordingly. 137bd335c64Sesolom# 138bd335c64Sesolom# getstat() is also responsible for maintaining a reasonable $sleeptime. 139bd335c64Sesolom# 140bd335c64Sesolom# {"snaptime"} kstat's snaptime 141bd335c64Sesolom# {<cpuid>} one hash reference per online cpu 142bd335c64Sesolom# ->{"tot"} == cpu:<cpuid>:sys:cpu_nsec_{user + kernel + idle} 143bd335c64Sesolom# ->{"crtime"} == cpu:<cpuid>:sys:crtime 144bd335c64Sesolom# ->{"ivecs"} 145bd335c64Sesolom# ->{<cookie#>} iterates over pci_intrs::config:cookie 146bd335c64Sesolom# ->{"time"} == pci_intrs:<ivec#>:config:time (in nsec) 147bd335c64Sesolom# ->{"pil"} == pci_intrs:<ivec#>:config:pil 148bd335c64Sesolom# ->{"crtime"} == pci_intrs:<ivec#>:config:crtime 149bd335c64Sesolom# ->{"ino"} == pci_intrs:<ivec#>:config:ino 150bd335c64Sesolom# ->{"buspath"} == pci_intrs:<ivec#>:config:buspath 151bd335c64Sesolom# ->{"name"} == pci_intrs:<ivec#>:config:name 152bd335c64Sesolom# ->{"ihs"} == pci_intrs:<ivec#>:config:ihs 153bd335c64Sesolom# 154bd335c64Sesolom 155bd335c64Sesolomsub getstat($) 156bd335c64Sesolom{ 157bd335c64Sesolom my ($ks) = @_; 158bd335c64Sesolom 159bd335c64Sesolom my $cpucnt = 0; 160bd335c64Sesolom my %stat = (); 161bd335c64Sesolom my ($minsnap, $maxsnap); 162bd335c64Sesolom 163bd335c64Sesolom # kstats are not generated atomically. Each kstat hierarchy will 164bd335c64Sesolom # have been generated within the kernel at a different time. On a 165bd335c64Sesolom # thrashing system, we may not run quickly enough in order to get 166bd335c64Sesolom # coherent kstat timing information across all the kstats. To 167bd335c64Sesolom # determine if this is occurring, $minsnap/$maxsnap are used to 168bd335c64Sesolom # find the breadth between the first and last snaptime of all the 169bd335c64Sesolom # kstats we access. $maxsnap - $minsnap roughly represents the 170bd335c64Sesolom # total time taken up in getstat(). If this time approaches the 171bd335c64Sesolom # time between snapshots, our results may not be useful. 172bd335c64Sesolom 173bd335c64Sesolom $minsnap = -1; # snaptime is always a positive number 174bd335c64Sesolom $maxsnap = $minsnap; 175bd335c64Sesolom 176bd335c64Sesolom # Iterate over the cpus in cpu:<cpuid>::. Check 177bd335c64Sesolom # cpu_info:<cpuid>:cpu_info<cpuid>:state to make sure the 178bd335c64Sesolom # processor is "on-line". If not, it isn't accepting interrupts 179bd335c64Sesolom # and doesn't concern us. 180bd335c64Sesolom # 181bd335c64Sesolom # Record cpu:<cpuid>:sys:snaptime, and check $minsnap/$maxsnap. 182bd335c64Sesolom 183bd335c64Sesolom while (my ($cpu, $cpst) = each %{$ks->{cpu}}) { 184bd335c64Sesolom next if !exists($ks->{cpu_info}{$cpu}{"cpu_info$cpu"}{state}); 185bd335c64Sesolom my $state = $ks->{cpu_info}{$cpu}{"cpu_info$cpu"}{state}; 186bd335c64Sesolom next if ($state !~ /^on-line\0/); 187bd335c64Sesolom my $cpu_sys = $cpst->{sys}; 188bd335c64Sesolom 189bd335c64Sesolom $stat{$cpu}{tot} = ($cpu_sys->{cpu_nsec_idle} + 190bd335c64Sesolom $cpu_sys->{cpu_nsec_user} + 191bd335c64Sesolom $cpu_sys->{cpu_nsec_kernel}); 192bd335c64Sesolom $stat{$cpu}{crtime} = $cpu_sys->{crtime}; 193bd335c64Sesolom $stat{$cpu}{ivecs} = {}; 194bd335c64Sesolom 195bd335c64Sesolom if ($minsnap == -1 || $cpu_sys->{snaptime} < $minsnap) { 196bd335c64Sesolom $minsnap = $cpu_sys->{snaptime}; 197bd335c64Sesolom } 198bd335c64Sesolom if ($cpu_sys->{snaptime} > $maxsnap) { 199bd335c64Sesolom $maxsnap = $cpu_sys->{snaptime}; 200bd335c64Sesolom } 201bd335c64Sesolom $cpucnt++; 202bd335c64Sesolom } 203bd335c64Sesolom 204bd335c64Sesolom if ($cpucnt <= 1) { 205bd335c64Sesolom $sleeptime = $onecpu_sleeptime; 206bd335c64Sesolom return (0); # nothing to do with 1 CPU 207bd335c64Sesolom } 208bd335c64Sesolom 209bd335c64Sesolom # Iterate over the ivecs. If the cpu is not on-line, ignore the 210bd335c64Sesolom # ivecs mapped to it, if any. 211bd335c64Sesolom # 212bd335c64Sesolom # Record pci_intrs:{inum}:config:time, snaptime, crtime, pil, 213bd335c64Sesolom # ino, name, and buspath. Check $minsnap/$maxsnap. 214bd335c64Sesolom 215bd335c64Sesolom foreach my $inst (values(%{$ks->{pci_intrs}})) { 216bd335c64Sesolom my $intrcfg = $inst->{config}; 217bd335c64Sesolom my $cpu = $intrcfg->{cpu}; 218bd335c64Sesolom 219bd335c64Sesolom next unless exists $stat{$cpu}; 220*e1d9f4e6Sschwartz next if ($intrcfg->{type} =~ /^disabled\0/); 221bd335c64Sesolom 222bd335c64Sesolom if ($intrcfg->{snaptime} < $minsnap) { 223bd335c64Sesolom $minsnap = $intrcfg->{snaptime}; 224bd335c64Sesolom } elsif ($intrcfg->{snaptime} > $maxsnap) { 225bd335c64Sesolom $maxsnap = $intrcfg->{snaptime}; 226bd335c64Sesolom } 227bd335c64Sesolom 228bd335c64Sesolom my $cookie = "$intrcfg->{buspath} $intrcfg->{ino}"; 229bd335c64Sesolom if (exists $stat{$cpu}{ivecs}{$cookie}) { 230bd335c64Sesolom my $cookiestats = $stat{$cpu}{ivecs}{$cookie}; 231bd335c64Sesolom 232bd335c64Sesolom $cookiestats->{time} += $intrcfg->{time}; 233bd335c64Sesolom $cookiestats->{name} .= "/$intrcfg->{name}"; 234bd335c64Sesolom 235bd335c64Sesolom # If this new interrupt sharing $cookie represents a 236bd335c64Sesolom # change from an earlier getstat, make sure that 237bd335c64Sesolom # generate_delta will see the change by setting 238bd335c64Sesolom # crtime to the most recent crtime of its components. 239bd335c64Sesolom 240bd335c64Sesolom if ($intrcfg->{crtime} > $cookiestats->{crtime}) { 241bd335c64Sesolom $cookiestats->{crtime} = $intrcfg->{crtime}; 242bd335c64Sesolom } 243bd335c64Sesolom $cookiestats->{ihs}++; 244bd335c64Sesolom next; 245bd335c64Sesolom } 246bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{time} = $intrcfg->{time}; 247bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{crtime} = $intrcfg->{crtime}; 248bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{pil} = $intrcfg->{pil}; 249bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{ino} = $intrcfg->{ino}; 250bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{buspath} = $intrcfg->{buspath}; 251bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{name} = $intrcfg->{name}; 252bd335c64Sesolom $stat{$cpu}{ivecs}{$cookie}{ihs} = 1; 253bd335c64Sesolom } 254bd335c64Sesolom 255bd335c64Sesolom # We define the timerange as the amount of time spent gathering the 256bd335c64Sesolom # various kstats, divided by our sleeptime. If we take a lot of time 257bd335c64Sesolom # to access the kstats, and then we create a delta comparing these 258bd335c64Sesolom # kstats with a prior set of kstats, that delta will cover 259bd335c64Sesolom # substaintially different amount of time depending upon which 260bd335c64Sesolom # interrupt or CPU is being examined. 261bd335c64Sesolom # 262bd335c64Sesolom # By checking the timerange here, we guarantee that any deltas 263bd335c64Sesolom # created from these kstats will contain self-consistent data, 264bd335c64Sesolom # in that all CPUs and interrupts cover a similar span of time. 265bd335c64Sesolom # 266bd335c64Sesolom # We attempt to keep this timerange between $timerange_lothresh and 267bd335c64Sesolom # $timerange_hithresh. If the timerange gets too large, not only are 268bd335c64Sesolom # there the accuracy concerns above, but it means that intrd is using 269bd335c64Sesolom # a lot of CPU time. If the timerange gets too small, that means our 270bd335c64Sesolom # sleep time is large, and we could fail to react quickly enough to a 271bd335c64Sesolom # sudden change. 272bd335c64Sesolom # 273bd335c64Sesolom # Finally, $timerange_toohi is the upper bound. Any timerange above 274bd335c64Sesolom # this is thrown out as garbage. If the stat is safely within this 275bd335c64Sesolom # bound, we treat the stat as representing an instant in time, rather 276bd335c64Sesolom # than the time range it actually spans. We arbitrarily choose minsnap 277bd335c64Sesolom # as the snaptime of the stat. 278bd335c64Sesolom 279bd335c64Sesolom $stat{snaptime} = $minsnap; 280bd335c64Sesolom my $timerange = ($maxsnap - $minsnap) / $sleeptime; 281bd335c64Sesolom if ($sleeptime == $onecpu_sleeptime) { 282bd335c64Sesolom $sleeptime = $min_sleeptime; # time to come out of idling 283bd335c64Sesolom } elsif ($timerange > $timerange_hithresh && 284bd335c64Sesolom $sleeptime < $max_sleeptime) { 285bd335c64Sesolom $sleeptime++; 286bd335c64Sesolom } elsif ($timerange < $timerange_lothresh && 287bd335c64Sesolom $sleeptime > $min_sleeptime) { 288bd335c64Sesolom $sleeptime--; 289bd335c64Sesolom } 290bd335c64Sesolom return (0) if ($timerange > $timerange_toohi); # i.e. failure 291bd335c64Sesolom return (\%stat); 292bd335c64Sesolom} 293bd335c64Sesolom 294bd335c64Sesolom# 295bd335c64Sesolom# dumpdelta takes a reference to our "delta" structure: 296bd335c64Sesolom# {"missing"} "1" if the delta's component stats had inconsistencies 297bd335c64Sesolom# {"minsnap"} time of the first kstat snaptime used in this delta 298bd335c64Sesolom# {"maxsnap"} time of the last kstat snaptime used in this delta 299bd335c64Sesolom# {"goodness"} cost function applied to this delta 300bd335c64Sesolom# {"avgintrload"} avg of interrupt load across cpus, as a percentage 301bd335c64Sesolom# {"avgintrnsec"} avg number of nsec spent in interrupts, per cpu 302bd335c64Sesolom# {<cpuid>} iterates over on-line cpus 303bd335c64Sesolom# ->{"intrs"} cpu's movable intr time (sum of "time" for each ivec) 304bd335c64Sesolom# ->{"tot"} CPU load from all sources 305bd335c64Sesolom# ->{"bigintr"} largest value of {ivecs}{<ivec#>}{time} from below 306bd335c64Sesolom# ->{"intrload"} intrs / tot 307bd335c64Sesolom# ->{"ivecs"} 308bd335c64Sesolom# ->{<ivec#>} iterates over ivecs for this cpu 309bd335c64Sesolom# ->{"time"} time used by this interrupt (in nsec) 310bd335c64Sesolom# ->{"pil"} pil level of this interrupt 311bd335c64Sesolom# ->{"ino"} interrupt number 312bd335c64Sesolom# ->{"buspath"} filename of the directory of the device's bus 313bd335c64Sesolom# ->{"name"} device name 314bd335c64Sesolom# ->{"ihs"} number of different handlers sharing this ino 315bd335c64Sesolom# 316bd335c64Sesolom# It prints out the delta structure in a nice, human readable display. 317bd335c64Sesolom# 318bd335c64Sesolom 319bd335c64Sesolomsub dumpdelta($) 320bd335c64Sesolom{ 321bd335c64Sesolom my ($delta) = @_; 322bd335c64Sesolom 323bd335c64Sesolom # print global info 324bd335c64Sesolom 325bd335c64Sesolom syslog('debug', "dumpdelta:"); 326bd335c64Sesolom syslog('debug', " RECONFIGURATION IN DELTA") if $delta->{missing} > 0; 327bd335c64Sesolom syslog('debug', " avgintrload: %5.2f%% avgintrnsec: %d", 328bd335c64Sesolom $delta->{avgintrload} * 100, $delta->{avgintrnsec}); 329bd335c64Sesolom syslog('debug', " goodness: %5.2f%%", $delta->{goodness} * 100) 330bd335c64Sesolom if exists($delta->{goodness}); 331bd335c64Sesolom 332bd335c64Sesolom # iterate over cpus 333bd335c64Sesolom 334bd335c64Sesolom while (my ($cpu, $cpst) = each %$delta) { 335bd335c64Sesolom next if !ref($cpst); # skip non-cpuid entries 336bd335c64Sesolom my $tot = $cpst->{tot}; 337bd335c64Sesolom syslog('debug', " cpu %3d intr %7.3f%% (bigintr %7.3f%%)", 338bd335c64Sesolom $cpu, $cpst->{intrload}*100, $cpst->{bigintr}*100/$tot); 339bd335c64Sesolom syslog('debug', " intrs %d, bigintr %d", 340bd335c64Sesolom $cpst->{intrs}, $cpst->{bigintr}); 341bd335c64Sesolom 342bd335c64Sesolom # iterate over ivecs on this cpu 343bd335c64Sesolom 344bd335c64Sesolom while (my ($ivec, $ivst) = each %{$cpst->{ivecs}}) { 3455bb4956eSesolom syslog('debug', " %15s:\"%s\": %7.3f%% %d", 3465bb4956eSesolom ($ivst->{ihs} > 1 ? "$ivst->{name}($ivst->{ihs})" : 3475bb4956eSesolom $ivst->{name}), $ivec, 3485bb4956eSesolom $ivst->{time}*100 / $tot, $ivst->{time}); 349bd335c64Sesolom } 350bd335c64Sesolom } 351bd335c64Sesolom} 352bd335c64Sesolom 353bd335c64Sesolom# 354bd335c64Sesolom# generate_delta($stat, $newstat) takes two stat references, returned from 355bd335c64Sesolom# getstat(), and creates a %delta. %delta (not surprisingly) contains the 356bd335c64Sesolom# same basic info as stat and newstat, but with the timestamps as deltas 357bd335c64Sesolom# instead of absolute times. We return a reference to the delta. 358bd335c64Sesolom# 359bd335c64Sesolom 360bd335c64Sesolomsub generate_delta($$) 361bd335c64Sesolom{ 362bd335c64Sesolom my ($stat, $newstat) = @_; 363bd335c64Sesolom 364bd335c64Sesolom my %delta = (); 365bd335c64Sesolom my $intrload; 366bd335c64Sesolom my $intrnsec; 367bd335c64Sesolom my $cpus; 368bd335c64Sesolom 369bd335c64Sesolom # Take the worstcase timerange 370bd335c64Sesolom $delta{minsnap} = $stat->{snaptime}; 371bd335c64Sesolom $delta{maxsnap} = $newstat->{snaptime}; 372bd335c64Sesolom if (VERIFY($delta{maxsnap} > $delta{minsnap}, 373bd335c64Sesolom "generate_delta: stats aren't ascending")) { 374bd335c64Sesolom $delta{missing} = 1; 375bd335c64Sesolom return (\%delta); 376bd335c64Sesolom } 377bd335c64Sesolom 378bd335c64Sesolom # if there are a different number of cpus in the stats, set missing 379bd335c64Sesolom 380bd335c64Sesolom $delta{missing} = (keys(%$stat) != keys(%$newstat)); 381bd335c64Sesolom if (VERIFY($delta{missing} == 0, 382bd335c64Sesolom "generate_delta: number of CPUs changed")) { 383bd335c64Sesolom return (\%delta); 384bd335c64Sesolom } 385bd335c64Sesolom 386bd335c64Sesolom # scan through every cpu in %newstat and compare against %stat 387bd335c64Sesolom 388bd335c64Sesolom while (my ($cpu, $newcpst) = each %$newstat) { 389bd335c64Sesolom next if !ref($newcpst); # skip non-cpuid fields 390bd335c64Sesolom 391bd335c64Sesolom # If %stat is missing a cpu from %newstat, then it was just 392bd335c64Sesolom # onlined. Mark missing. 393bd335c64Sesolom 394bd335c64Sesolom if (VERIFY(exists $stat->{$cpu} && 395bd335c64Sesolom $stat->{$cpu}{crtime} == $newcpst->{crtime}, 396bd335c64Sesolom "generate_delta: cpu $cpu changed")) { 397bd335c64Sesolom $delta{missing} = 1; 398bd335c64Sesolom return (\%delta); 399bd335c64Sesolom } 400bd335c64Sesolom my $cpst = $stat->{$cpu}; 401bd335c64Sesolom $delta{$cpu}{tot} = $newcpst->{tot} - $cpst->{tot}; 402bd335c64Sesolom if (VERIFY($delta{$cpu}{tot} >= 0, 403bd335c64Sesolom "generate_delta: deltas are not ascending?")) { 404bd335c64Sesolom $delta{missing} = 1; 405bd335c64Sesolom delete($delta{$cpu}); 406bd335c64Sesolom return (\%delta); 407bd335c64Sesolom } 408bd335c64Sesolom # Avoid remote chance of division by zero 409bd335c64Sesolom $delta{$cpu}{tot} = 1 if $delta{$cpu}{tot} == 0; 410bd335c64Sesolom $delta{$cpu}{intrs} = 0; 411bd335c64Sesolom $delta{$cpu}{bigintr} = 0; 412bd335c64Sesolom 413bd335c64Sesolom my %ivecs = (); 414bd335c64Sesolom $delta{$cpu}{ivecs} = \%ivecs; 415bd335c64Sesolom 416bd335c64Sesolom # if the number of ivecs differs, set missing 417bd335c64Sesolom 418bd335c64Sesolom if (VERIFY(keys(%{$cpst->{ivecs}}) == 419bd335c64Sesolom keys(%{$newcpst->{ivecs}}), 420bd335c64Sesolom "generate_delta: cpu $cpu has more/less". 421bd335c64Sesolom " interrupts")) { 422bd335c64Sesolom $delta{missing} = 1; 423bd335c64Sesolom return (\%delta); 424bd335c64Sesolom } 425bd335c64Sesolom 426bd335c64Sesolom while (my ($inum, $newivec) = each %{$newcpst->{ivecs}}) { 427bd335c64Sesolom # If this ivec doesn't exist in $stat, or if $stat 428bd335c64Sesolom # shows a different crtime, set missing. 429bd335c64Sesolom 430bd335c64Sesolom if (VERIFY(exists $cpst->{ivecs}{$inum} && 431bd335c64Sesolom $cpst->{ivecs}{$inum}{crtime} == 432bd335c64Sesolom $newivec->{crtime}, 433bd335c64Sesolom "generate_delta: cpu $cpu inum $inum". 434bd335c64Sesolom " has changed")) { 435bd335c64Sesolom $delta{missing} = 1; 436bd335c64Sesolom return (\%delta); 437bd335c64Sesolom } 438bd335c64Sesolom my $ivec = $cpst->{ivecs}{$inum}; 439bd335c64Sesolom 440bd335c64Sesolom # Create $delta{$cpu}{ivecs}{$inum}. 441bd335c64Sesolom 442bd335c64Sesolom my %dltivec = (); 443bd335c64Sesolom $delta{$cpu}{ivecs}{$inum} = \%dltivec; 444bd335c64Sesolom 445bd335c64Sesolom # calculate time used by this interrupt 446bd335c64Sesolom 447bd335c64Sesolom my $time = $newivec->{time} - $ivec->{time}; 448bd335c64Sesolom if (VERIFY($time >= 0, 449bd335c64Sesolom "generate_delta: ivec went backwards?")) { 450bd335c64Sesolom $delta{missing} = 1; 451bd335c64Sesolom delete($delta{$cpu}{ivecs}{$inum}); 452bd335c64Sesolom return (\%delta); 453bd335c64Sesolom } 454bd335c64Sesolom $delta{$cpu}{intrs} += $time; 455bd335c64Sesolom $dltivec{time} = $time; 456bd335c64Sesolom if ($time > $delta{$cpu}{bigintr}) { 457bd335c64Sesolom $delta{$cpu}{bigintr} = $time; 458bd335c64Sesolom } 459bd335c64Sesolom 460bd335c64Sesolom # Transfer over basic info about the kstat. We 461bd335c64Sesolom # don't have to worry about discrepancies between 462bd335c64Sesolom # ivec and newivec because we verified that both 463bd335c64Sesolom # have the same crtime. 464bd335c64Sesolom 465bd335c64Sesolom $dltivec{pil} = $newivec->{pil}; 466bd335c64Sesolom $dltivec{ino} = $newivec->{ino}; 467bd335c64Sesolom $dltivec{buspath} = $newivec->{buspath}; 468bd335c64Sesolom $dltivec{name} = $newivec->{name}; 469bd335c64Sesolom $dltivec{ihs} = $newivec->{ihs}; 470bd335c64Sesolom } 471bd335c64Sesolom if ($delta{$cpu}{tot} < $delta{$cpu}{intrs}) { 472bd335c64Sesolom # Ewww! Hopefully just a rounding error. 473bd335c64Sesolom # Make something up. 474bd335c64Sesolom $delta{$cpu}{tot} = $delta{$cpu}{intrs}; 475bd335c64Sesolom } 476bd335c64Sesolom $delta{$cpu}{intrload} = 477bd335c64Sesolom $delta{$cpu}{intrs} / $delta{$cpu}{tot}; 478bd335c64Sesolom $intrload += $delta{$cpu}{intrload}; 479bd335c64Sesolom $intrnsec += $delta{$cpu}{intrs}; 480bd335c64Sesolom $cpus++; 481bd335c64Sesolom } 482bd335c64Sesolom if ($cpus > 0) { 483bd335c64Sesolom $delta{avgintrload} = $intrload / $cpus; 484bd335c64Sesolom $delta{avgintrnsec} = $intrnsec / $cpus; 485bd335c64Sesolom } else { 486bd335c64Sesolom $delta{avgintrload} = 0; 487bd335c64Sesolom $delta{avgintrnsec} = 0; 488bd335c64Sesolom } 489bd335c64Sesolom return (\%delta); 490bd335c64Sesolom} 491bd335c64Sesolom 492bd335c64Sesolom 493bd335c64Sesolom# compress_delta takes a list of deltas, and returns a single new delta 494bd335c64Sesolom# which represents the combined information from all the deltas. The deltas 495bd335c64Sesolom# provided are assumed to be sequential in time. The resulting compressed 496bd335c64Sesolom# delta looks just like any other delta. This new delta is also more accurate 497bd335c64Sesolom# since its statistics are averaged over a longer period than any of the 498bd335c64Sesolom# original deltas. 499bd335c64Sesolom 500bd335c64Sesolomsub compress_deltas ($) 501bd335c64Sesolom{ 502bd335c64Sesolom my ($deltas) = @_; 503bd335c64Sesolom 504bd335c64Sesolom my %newdelta = (); 505bd335c64Sesolom my ($intrs, $tot); 506bd335c64Sesolom my $cpus = 0; 507bd335c64Sesolom 508bd335c64Sesolom if (VERIFY($#$deltas != -1, 509bd335c64Sesolom "compress_deltas: list of delta is empty?")) { 510bd335c64Sesolom return (0); 511bd335c64Sesolom } 512bd335c64Sesolom $newdelta{minsnap} = $deltas->[0]{minsnap}; 513bd335c64Sesolom $newdelta{maxsnap} = $deltas->[$#$deltas]{maxsnap}; 514bd335c64Sesolom $newdelta{missing} = 0; 515bd335c64Sesolom 516bd335c64Sesolom foreach my $delta (@$deltas) { 517bd335c64Sesolom if (VERIFY($delta->{missing} == 0, 518bd335c64Sesolom "compressing bad deltas?")) { 519bd335c64Sesolom return (0); 520bd335c64Sesolom } 521bd335c64Sesolom while (my ($cpuid, $cpu) = each %$delta) { 522bd335c64Sesolom next if !ref($cpu); 523bd335c64Sesolom 524bd335c64Sesolom $intrs += $cpu->{intrs}; 525bd335c64Sesolom $tot += $cpu->{tot}; 526bd335c64Sesolom $newdelta{$cpuid}{intrs} += $cpu->{intrs}; 527bd335c64Sesolom $newdelta{$cpuid}{tot} += $cpu->{tot}; 528bd335c64Sesolom if (!exists $newdelta{$cpuid}{ivecs}) { 529bd335c64Sesolom my %ivecs = (); 530bd335c64Sesolom $newdelta{$cpuid}{ivecs} = \%ivecs; 531bd335c64Sesolom } 532bd335c64Sesolom while (my ($inum, $ivec) = each %{$cpu->{ivecs}}) { 533bd335c64Sesolom my $newivecs = $newdelta{$cpuid}{ivecs}; 534bd335c64Sesolom $newivecs->{$inum}{time} += $ivec->{time}; 535bd335c64Sesolom $newivecs->{$inum}{pil} = $ivec->{pil}; 536bd335c64Sesolom $newivecs->{$inum}{ino} = $ivec->{ino}; 537bd335c64Sesolom $newivecs->{$inum}{buspath} = $ivec->{buspath}; 538bd335c64Sesolom $newivecs->{$inum}{name} = $ivec->{name}; 539bd335c64Sesolom $newivecs->{$inum}{ihs} = $ivec->{ihs}; 540bd335c64Sesolom } 541bd335c64Sesolom } 542bd335c64Sesolom } 543bd335c64Sesolom foreach my $cpu (values(%newdelta)) { 544bd335c64Sesolom next if !ref($cpu); # ignore non-cpu fields 545bd335c64Sesolom $cpus++; 546bd335c64Sesolom 547bd335c64Sesolom my $bigintr = 0; 548bd335c64Sesolom foreach my $ivec (values(%{$cpu->{ivecs}})) { 549bd335c64Sesolom if ($ivec->{time} > $bigintr) { 550bd335c64Sesolom $bigintr = $ivec->{time}; 551bd335c64Sesolom } 552bd335c64Sesolom } 553bd335c64Sesolom $cpu->{bigintr} = $bigintr; 554bd335c64Sesolom $cpu->{intrload} = $cpu->{intrs} / $cpu->{tot}; 555bd335c64Sesolom $cpu->{tot} = 1 if $cpu->{tot} <= 0; 556bd335c64Sesolom } 557bd335c64Sesolom if ($cpus == 0) { 558bd335c64Sesolom $newdelta{avgintrnsec} = 0; 559bd335c64Sesolom $newdelta{avgintrload} = 0; 560bd335c64Sesolom } else { 561bd335c64Sesolom $newdelta{avgintrnsec} = $intrs / $cpus; 562bd335c64Sesolom $newdelta{avgintrload} = $intrs / $tot; 563bd335c64Sesolom } 564bd335c64Sesolom return (\%newdelta); 565bd335c64Sesolom} 566bd335c64Sesolom 567bd335c64Sesolom 568bd335c64Sesolom 569bd335c64Sesolom 570bd335c64Sesolom 571bd335c64Sesolom# What follow are the core functions responsible for examining the deltas 572bd335c64Sesolom# generated above and deciding what to do about them. 573bd335c64Sesolom# 574bd335c64Sesolom# goodness() and its helper goodness_cpu() return a heuristic which describe 575bd335c64Sesolom# how good (or bad) the current interrupt balance is. The value returned will 576bd335c64Sesolom# be between 0 and 1, with 0 representing maximum goodness, and 1 representing 577bd335c64Sesolom# maximum badness. 578bd335c64Sesolom# 579bd335c64Sesolom# imbalanced() compares a current and historical value of goodness, and 580bd335c64Sesolom# determines if there has been enough change to warrant evaluating a 581bd335c64Sesolom# reconfiguration of the interrupts 582bd335c64Sesolom# 583bd335c64Sesolom# do_reconfig(), and its helpers, do_reconfig_cpu(), do_reconfig_cpu2cpu(), 584bd335c64Sesolom# find_goal(), do_find_goal(), and move_intr(), are responsible for examining 585bd335c64Sesolom# a delta and determining the best possible assignment of interrupts to CPUs. 586bd335c64Sesolom# 587bd335c64Sesolom# It is important that do_reconfig() be in alignment with goodness(). If 588bd335c64Sesolom# do_reconfig were to generate a new interrupt distribution that worsened 589bd335c64Sesolom# goodness, we could get into a pathological loop with intrd fighting itself, 590bd335c64Sesolom# constantly deciding that things are imbalanced, and then changing things 591bd335c64Sesolom# only to make them worse. 592bd335c64Sesolom 593bd335c64Sesolom 594bd335c64Sesolom 595bd335c64Sesolom# any goodness over $goodness_unsafe_load is considered really bad 596bd335c64Sesolom# goodness must drop by at least $goodness_mindelta for a reconfig 597bd335c64Sesolom 598bd335c64Sesolommy $goodness_unsafe_load = .9; 599bd335c64Sesolommy $goodness_mindelta = .1; 600bd335c64Sesolom 601bd335c64Sesolom# goodness(%delta) examines a delta and return its "goodness". goodness will 602bd335c64Sesolom# be between 0 (best) and 1 (major bad). goodness is determined by evaluating 603bd335c64Sesolom# the goodness of each individual cpu, and returning the worst case. This 604bd335c64Sesolom# helps on systems with many CPUs, where otherwise a single pathological CPU 605bd335c64Sesolom# might otherwise be ignored because the average was OK. 606bd335c64Sesolom# 607bd335c64Sesolom# To calculate the goodness of an individual CPU, we start by looking at its 608bd335c64Sesolom# load due to interrupts. If the load is above a certain high threshold and 609bd335c64Sesolom# there is more than one interrupt assigned to this CPU, we set goodness 610bd335c64Sesolom# to worst-case. If the load is below the average interrupt load of all CPUs, 611bd335c64Sesolom# then we return best-case, since what's to complain about? 612bd335c64Sesolom# 613bd335c64Sesolom# Otherwise we look at how much the load is above the average, and return 614bd335c64Sesolom# that as the goodness, with one caveat: we never return more than the CPU's 615bd335c64Sesolom# interrupt load ignoring its largest single interrupt source. This is 616bd335c64Sesolom# because a CPU with one high-load interrupt, and no other interrupts, is 617bd335c64Sesolom# perfectly balanced. Nothing can be done to improve the situation, and thus 618bd335c64Sesolom# it is perfectly balanced even if the interrupt's load is 100%. 619bd335c64Sesolom 620bd335c64Sesolomsub goodness($) 621bd335c64Sesolom{ 622bd335c64Sesolom my ($delta) = @_; 623bd335c64Sesolom 624bd335c64Sesolom return (1) if $delta->{missing} > 0; 625bd335c64Sesolom 626bd335c64Sesolom my $high_goodness = 0; 627bd335c64Sesolom my $goodness; 628bd335c64Sesolom 629bd335c64Sesolom foreach my $cpu (values(%$delta)) { 630bd335c64Sesolom next if !ref($cpu); # skip non-cpuid fields 631bd335c64Sesolom 632bd335c64Sesolom $goodness = goodness_cpu($cpu, $delta->{avgintrload}); 633bd335c64Sesolom if (VERIFY($goodness >= 0 && $goodness <= 1, 634bd335c64Sesolom "goodness: cpu goodness out of range?")) { 635bd335c64Sesolom dumpdelta($delta); 636bd335c64Sesolom return (1); 637bd335c64Sesolom } 638bd335c64Sesolom if ($goodness == 1) { 639bd335c64Sesolom return (1); # worst case, no need to continue 640bd335c64Sesolom } 641bd335c64Sesolom if ($goodness > $high_goodness) { 642bd335c64Sesolom $high_goodness = $goodness; 643bd335c64Sesolom } 644bd335c64Sesolom } 645bd335c64Sesolom return ($high_goodness); 646bd335c64Sesolom} 647bd335c64Sesolom 648bd335c64Sesolomsub goodness_cpu($$) # private function 649bd335c64Sesolom{ 650bd335c64Sesolom my ($cpu, $avgintrload) = @_; 651bd335c64Sesolom 652bd335c64Sesolom my $goodness; 653bd335c64Sesolom my $load = $cpu->{intrs} / $cpu->{tot}; 654bd335c64Sesolom 655bd335c64Sesolom return (0) if ($load < $avgintrload); # low loads are perfectly good 656bd335c64Sesolom 657bd335c64Sesolom # Calculate $load_no_bigintr, which represents the load 658bd335c64Sesolom # due to interrupts, excluding the one biggest interrupt. 659bd335c64Sesolom # This is the most gain we can get on this CPU from 660bd335c64Sesolom # offloading interrupts. 661bd335c64Sesolom 662bd335c64Sesolom my $load_no_bigintr = ($cpu->{intrs} - $cpu->{bigintr}) / $cpu->{tot}; 663bd335c64Sesolom 664bd335c64Sesolom # A major imbalance is indicated if a CPU is saturated 665bd335c64Sesolom # with interrupt handling, and it has more than one 666bd335c64Sesolom # source of interrupts. Those other interrupts could be 667bd335c64Sesolom # starved if of a lower pil. Return a goodness of 1, 668bd335c64Sesolom # which is the worst possible return value, 669bd335c64Sesolom # which will effectively contaminate this entire delta. 670bd335c64Sesolom 671bd335c64Sesolom my $cnt = keys(%{$cpu->{ivecs}}); 672bd335c64Sesolom 673bd335c64Sesolom if ($load > $goodness_unsafe_load && $cnt > 1) { 674bd335c64Sesolom return (1); 675bd335c64Sesolom } 676bd335c64Sesolom $goodness = $load - $avgintrload; 677bd335c64Sesolom if ($goodness > $load_no_bigintr) { 678bd335c64Sesolom $goodness = $load_no_bigintr; 679bd335c64Sesolom } 680bd335c64Sesolom return ($goodness); 681bd335c64Sesolom} 682bd335c64Sesolom 683bd335c64Sesolom 684bd335c64Sesolom# imbalanced() is used by the main routine to determine if the goodness 685bd335c64Sesolom# has shifted far enough from our last baseline to warrant a reassignment 686bd335c64Sesolom# of interrupts. A very high goodness indicates that a CPU is way out of 687bd335c64Sesolom# whack. If the goodness has varied too much since the baseline, then 688bd335c64Sesolom# perhaps a reconfiguration is worth considering. 689bd335c64Sesolom 690bd335c64Sesolomsub imbalanced ($$) 691bd335c64Sesolom{ 692bd335c64Sesolom my ($goodness, $baseline) = @_; 693bd335c64Sesolom 694bd335c64Sesolom # Return 1 if we are pathological, or creeping away from the baseline 695bd335c64Sesolom 696bd335c64Sesolom return (1) if $goodness > .50; 697bd335c64Sesolom return (1) if abs($goodness - $baseline) > $goodness_mindelta; 698bd335c64Sesolom return (0); 699bd335c64Sesolom} 700bd335c64Sesolom 701bd335c64Sesolom# do_reconfig(), do_reconfig_cpu(), and do_reconfig_cpu2cpu(), are the 702bd335c64Sesolom# decision-making functions responsible for generating a new interrupt 703bd335c64Sesolom# distribution. They are designed with the definition of goodness() in 704bd335c64Sesolom# mind, i.e. they use the same definition of "good distribution" as does 705bd335c64Sesolom# goodness(). 706bd335c64Sesolom# 707bd335c64Sesolom# do_reconfig() is responsible for deciding whether a redistribution is 708bd335c64Sesolom# actually warranted. If the goodness is already pretty good, it doesn't 709bd335c64Sesolom# waste the CPU time to generate a new distribution. If it 710bd335c64Sesolom# calculates a new distribution and finds that it is not sufficiently 711bd335c64Sesolom# improved from the prior distirbution, it will not do the redistribution, 712bd335c64Sesolom# mainly to avoid the disruption to system performance caused by 713bd335c64Sesolom# rejuggling interrupts. 714bd335c64Sesolom# 715bd335c64Sesolom# Its main loop works by going through a list of cpus sorted from 716bd335c64Sesolom# highest to lowest interrupt load. It removes the highest-load cpus 717bd335c64Sesolom# one at a time and hands them off to do_reconfig_cpu(). This function 718bd335c64Sesolom# then re-sorts the remaining CPUs from lowest to highest interrupt load, 719bd335c64Sesolom# and one at a time attempts to rejuggle interrupts between the original 720bd335c64Sesolom# high-load CPU and the low-load CPU. Rejuggling on a high-load CPU is 721bd335c64Sesolom# considered finished as soon as its interrupt load is within 722bd335c64Sesolom# $goodness_mindelta of the average interrupt load. Such a CPU will have 723bd335c64Sesolom# a goodness of below the $goodness_mindelta threshold. 724bd335c64Sesolom 725bd335c64Sesolom# 726bd335c64Sesolom# move_intr(\%delta, $inum, $oldcpu, $newcpu) 727bd335c64Sesolom# used by reconfiguration code to move an interrupt between cpus within 728bd335c64Sesolom# a delta. This manipulates data structures, and does not actually move 729bd335c64Sesolom# the interrupt on the running system. 730bd335c64Sesolom# 731bd335c64Sesolomsub move_intr($$$$) # private function 732bd335c64Sesolom{ 733bd335c64Sesolom my ($delta, $inum, $oldcpuid, $newcpuid) = @_; 734bd335c64Sesolom 735bd335c64Sesolom my $ivec = $delta->{$oldcpuid}{ivecs}{$inum}; 736bd335c64Sesolom 737bd335c64Sesolom # Remove ivec from old cpu 738bd335c64Sesolom 739bd335c64Sesolom my $oldcpu = $delta->{$oldcpuid}; 740bd335c64Sesolom $oldcpu->{intrs} -= $ivec->{time}; 741bd335c64Sesolom $oldcpu->{intrload} = $oldcpu->{intrs} / $oldcpu->{tot}; 742bd335c64Sesolom delete($oldcpu->{ivecs}{$inum}); 743bd335c64Sesolom 744bd335c64Sesolom VERIFY($oldcpu->{intrs} >= 0, "move_intr: intr's time > total time?"); 745bd335c64Sesolom VERIFY($ivec->{time} <= $oldcpu->{bigintr}, 746bd335c64Sesolom "move_intr: intr's time > bigintr?"); 747bd335c64Sesolom 748bd335c64Sesolom if ($ivec->{time} >= $oldcpu->{bigintr}) { 749bd335c64Sesolom my $bigtime = 0; 750bd335c64Sesolom 751bd335c64Sesolom foreach my $ivec (values(%{$oldcpu->{ivecs}})) { 752bd335c64Sesolom $bigtime = $ivec->{time} if $ivec->{time} > $bigtime; 753bd335c64Sesolom } 754bd335c64Sesolom $oldcpu->{bigintr} = $bigtime; 755bd335c64Sesolom } 756bd335c64Sesolom 757bd335c64Sesolom # Add ivec onto new cpu 758bd335c64Sesolom 759bd335c64Sesolom my $newcpu = $delta->{$newcpuid}; 760bd335c64Sesolom 761bd335c64Sesolom $ivec->{nowcpu} = $newcpuid; 762bd335c64Sesolom $newcpu->{intrs} += $ivec->{time}; 763bd335c64Sesolom $newcpu->{intrload} = $newcpu->{intrs} / $newcpu->{tot}; 764bd335c64Sesolom $newcpu->{ivecs}{$inum} = $ivec; 765bd335c64Sesolom 766bd335c64Sesolom $newcpu->{bigintr} = $ivec->{time} 767bd335c64Sesolom if $ivec->{time} > $newcpu->{bigintr}; 768bd335c64Sesolom} 769bd335c64Sesolom 770bd335c64Sesolomsub move_intr_check($$$) # private function 771bd335c64Sesolom{ 772bd335c64Sesolom my ($delta, $oldcpuid, $newcpuid) = @_; 773bd335c64Sesolom 774bd335c64Sesolom VERIFY($delta->{$oldcpuid}{tot} >= $delta->{$oldcpuid}{intrs}, 775bd335c64Sesolom "Moved interrupts left 100+%% load on src cpu"); 776bd335c64Sesolom VERIFY($delta->{$newcpuid}{tot} >= $delta->{$newcpuid}{intrs}, 777bd335c64Sesolom "Moved interrupts left 100+%% load on tgt cpu"); 778bd335c64Sesolom} 779bd335c64Sesolom 780bd335c64Sesolomsub ivecs_to_string(@) # private function 781bd335c64Sesolom{ 782bd335c64Sesolom my $str = ""; 783bd335c64Sesolom foreach my $ivec (@_) { 784bd335c64Sesolom $str = "$str $ivec->{inum}"; 785bd335c64Sesolom } 786bd335c64Sesolom return ($str); 787bd335c64Sesolom} 788bd335c64Sesolom 789bd335c64Sesolom 790bd335c64Sesolomsub do_reconfig($) 791bd335c64Sesolom{ 792bd335c64Sesolom my ($delta) = @_; 793bd335c64Sesolom 794bd335c64Sesolom my $goodness = $delta->{goodness}; 795bd335c64Sesolom 796bd335c64Sesolom # We can't improve goodness to better than 0. We should stop here 797bd335c64Sesolom # if, even if we achieve a goodness of 0, the improvement is still 798bd335c64Sesolom # too small to merit the action. 799bd335c64Sesolom 800bd335c64Sesolom if ($goodness - 0 < $goodness_mindelta) { 801bd335c64Sesolom syslog('debug', "goodness good enough, don't reconfig"); 802bd335c64Sesolom return (0); 803bd335c64Sesolom } 804bd335c64Sesolom 805bd335c64Sesolom syslog('notice', "Optimizing interrupt assignments"); 806bd335c64Sesolom 807bd335c64Sesolom if (VERIFY ($delta->{missing} == 0, "RECONFIG Aborted: should not ". 808bd335c64Sesolom "have a delta with missing")) { 809bd335c64Sesolom return (-1); 810bd335c64Sesolom } 811bd335c64Sesolom 812bd335c64Sesolom # Make a list of all cpuids, and also add some extra information 813bd335c64Sesolom # to the ivec structures. 814bd335c64Sesolom 815bd335c64Sesolom my @cpusortlist = (); 816bd335c64Sesolom 817bd335c64Sesolom while (my ($cpuid, $cpu) = each %$delta) { 818bd335c64Sesolom next if !ref($cpu); # skip non-cpu entries 819bd335c64Sesolom 820bd335c64Sesolom push(@cpusortlist, $cpuid); 821bd335c64Sesolom while (my ($inum, $ivec) = each %{$cpu->{ivecs}}) { 822bd335c64Sesolom $ivec->{origcpu} = $cpuid; 823bd335c64Sesolom $ivec->{nowcpu} = $cpuid; 824bd335c64Sesolom $ivec->{inum} = $inum; 825bd335c64Sesolom } 826bd335c64Sesolom } 827bd335c64Sesolom 828bd335c64Sesolom # Sort the list of CPUs from highest to lowest interrupt load. 829bd335c64Sesolom # Remove the top CPU from that list and attempt to redistribute 830bd335c64Sesolom # its interrupts. If the CPU has a goodness below a threshold, 831bd335c64Sesolom # just ignore the CPU and move to the next one. If the CPU's 832bd335c64Sesolom # load falls below the average load plus that same threshold, 833bd335c64Sesolom # then there are no CPUs left worth reconfiguring, and we're done. 834bd335c64Sesolom 835bd335c64Sesolom while (@cpusortlist) { 836bd335c64Sesolom # Re-sort cpusortlist each time, since do_reconfig_cpu can 837bd335c64Sesolom # move interrupts around. 838bd335c64Sesolom 839bd335c64Sesolom @cpusortlist = 840bd335c64Sesolom sort({$delta->{$b}{intrload} <=> $delta->{$a}{intrload}} 841bd335c64Sesolom @cpusortlist); 842bd335c64Sesolom 843bd335c64Sesolom my $cpu = shift(@cpusortlist); 844bd335c64Sesolom if (($delta->{$cpu}{intrload} <= $goodness_unsafe_load) && 845bd335c64Sesolom ($delta->{$cpu}{intrload} <= 846bd335c64Sesolom $delta->{avgintrload} + $goodness_mindelta)) { 847bd335c64Sesolom syslog('debug', "finished reconfig: cpu $cpu load ". 848bd335c64Sesolom "$delta->{$cpu}{intrload} avgload ". 849bd335c64Sesolom "$delta->{avgintrload}"); 850bd335c64Sesolom last; 851bd335c64Sesolom } 852bd335c64Sesolom if (goodness_cpu($delta->{$cpu}, $delta->{avgintrload}) < 853bd335c64Sesolom $goodness_mindelta) { 854bd335c64Sesolom next; 855bd335c64Sesolom } 856bd335c64Sesolom do_reconfig_cpu($delta, \@cpusortlist, $cpu); 857bd335c64Sesolom } 858bd335c64Sesolom 859bd335c64Sesolom # How good a job did we do? If the improvement was minimal, and 860bd335c64Sesolom # our goodness wasn't pathological (and thus needing any help it 861bd335c64Sesolom # can get), then don't bother moving the interrupts. 862bd335c64Sesolom 863bd335c64Sesolom my $newgoodness = goodness($delta); 864bd335c64Sesolom VERIFY($newgoodness <= $goodness, 865bd335c64Sesolom "reconfig: result has worse goodness?"); 866bd335c64Sesolom 867bd335c64Sesolom if (($goodness != 1 || $newgoodness == 1) && 868bd335c64Sesolom $goodness - $newgoodness < $goodness_mindelta) { 869bd335c64Sesolom syslog('debug', "goodness already near optimum, ". 870bd335c64Sesolom "don't reconfig"); 871bd335c64Sesolom return (0); 872bd335c64Sesolom } 873bd335c64Sesolom syslog('debug', "goodness %5.2f%% --> %5.2f%%", $goodness*100, 874bd335c64Sesolom $newgoodness*100); 875bd335c64Sesolom 876bd335c64Sesolom # Time to move those interrupts! 877bd335c64Sesolom 878bd335c64Sesolom my $ret = 1; 879bd335c64Sesolom my $warned = 0; 880bd335c64Sesolom while (my ($cpuid, $cpu) = each %$delta) { 881bd335c64Sesolom next if $cpuid =~ /\D/; 882bd335c64Sesolom while (my ($inum, $ivec) = each %{$cpu->{ivecs}}) { 883bd335c64Sesolom next if ($ivec->{origcpu} == $cpuid); 884bd335c64Sesolom 885bd335c64Sesolom if (!intrmove($ivec->{buspath}, $ivec->{ino}, 886bd335c64Sesolom $cpuid)) { 887bd335c64Sesolom syslog('warning', "Unable to move interrupts") 888bd335c64Sesolom if $warned++ == 0; 889bd335c64Sesolom syslog('debug', "Unable to move buspath ". 890bd335c64Sesolom "$ivec->{buspath} ino $ivec->{ino} to ". 891bd335c64Sesolom "cpu $cpuid"); 892bd335c64Sesolom $ret = -1; 893bd335c64Sesolom } 894bd335c64Sesolom } 895bd335c64Sesolom } 896bd335c64Sesolom 897bd335c64Sesolom syslog('notice', "Interrupt assignments optimized"); 898bd335c64Sesolom return ($ret); 899bd335c64Sesolom} 900bd335c64Sesolom 901bd335c64Sesolomsub do_reconfig_cpu($$$) # private function 902bd335c64Sesolom{ 903bd335c64Sesolom my ($delta, $cpusortlist, $oldcpuid) = @_; 904bd335c64Sesolom 905bd335c64Sesolom # We have been asked to rejuggle interrupts between $oldcpuid and 906bd335c64Sesolom # other CPUs found on $cpusortlist so as to improve the load on 907bd335c64Sesolom # $oldcpuid. We reverse $cpusortlist to get our own copy of the 908bd335c64Sesolom # list, sorted from lowest to highest interrupt load. One at a 909bd335c64Sesolom # time, shift a CPU off of this list of CPUs, and attempt to 910bd335c64Sesolom # rejuggle interrupts between the two CPUs. Don't do this if the 911bd335c64Sesolom # other CPU has a higher load than oldcpuid. We're done rejuggling 912bd335c64Sesolom # once $oldcpuid's goodness falls below a threshold. 913bd335c64Sesolom 914bd335c64Sesolom syslog('debug', "reconfiguring $oldcpuid"); 915bd335c64Sesolom 916bd335c64Sesolom my $cpu = $delta->{$oldcpuid}; 917bd335c64Sesolom my $avgintrload = $delta->{avgintrload}; 918bd335c64Sesolom 919bd335c64Sesolom my @cputargetlist = reverse(@$cpusortlist); # make a copy of the list 920bd335c64Sesolom while ($#cputargetlist != -1) { 921bd335c64Sesolom last if goodness_cpu($cpu, $avgintrload) < $goodness_mindelta; 922bd335c64Sesolom 923bd335c64Sesolom my $tgtcpuid = shift(@cputargetlist); 924bd335c64Sesolom my $tgt = $delta->{$tgtcpuid}; 925bd335c64Sesolom my $load = $cpu->{intrload}; 926bd335c64Sesolom my $tgtload = $tgt->{intrload}; 927bd335c64Sesolom last if $tgtload > $load; 928bd335c64Sesolom do_reconfig_cpu2cpu($delta, $oldcpuid, $tgtcpuid, $load); 929bd335c64Sesolom } 930bd335c64Sesolom} 931bd335c64Sesolom 932bd335c64Sesolomsub do_reconfig_cpu2cpu($$$$) # private function 933bd335c64Sesolom{ 934bd335c64Sesolom my ($delta, $srccpuid, $tgtcpuid, $srcload) = @_; 935bd335c64Sesolom 936bd335c64Sesolom # We've been asked to consider interrupt juggling between srccpuid 937bd335c64Sesolom # (with a high interrupt load) and tgtcpuid (with a lower interrupt 938bd335c64Sesolom # load). First, make a single list with all of the ivecs from both 939bd335c64Sesolom # CPUs, and sort the list from highest to lowest load. 940bd335c64Sesolom 941bd335c64Sesolom syslog('debug', "exchanging intrs between $srccpuid and $tgtcpuid"); 942bd335c64Sesolom 943bd335c64Sesolom # Gather together all the ivecs and sort by load 944bd335c64Sesolom 945bd335c64Sesolom my @ivecs = (values(%{$delta->{$srccpuid}{ivecs}}), 946bd335c64Sesolom values(%{$delta->{$tgtcpuid}{ivecs}})); 947bd335c64Sesolom return if $#ivecs == -1; 948bd335c64Sesolom 949bd335c64Sesolom @ivecs = sort({$b->{time} <=> $a->{time}} @ivecs); 950bd335c64Sesolom 951bd335c64Sesolom # Our "goal" load for srccpuid is the average load across all CPUs. 952bd335c64Sesolom # find_goal() will find determine the optimum selection of the 953bd335c64Sesolom # available interrupts which comes closest to this goal without 954bd335c64Sesolom # falling below the goal. 955bd335c64Sesolom 956bd335c64Sesolom my $goal = $delta->{avgintrnsec}; 957bd335c64Sesolom 958bd335c64Sesolom # We know that the interrupt load on tgtcpuid is less than that on 959bd335c64Sesolom # srccpuid, but its load could still be above avgintrnsec. Don't 960bd335c64Sesolom # choose a goal which would bring srccpuid below the load on tgtcpuid. 961bd335c64Sesolom 962bd335c64Sesolom my $avgnsec = 963bd335c64Sesolom ($delta->{$srccpuid}{intrs} + $delta->{$tgtcpuid}{intrs}) / 2; 964bd335c64Sesolom if ($goal < $avgnsec) { 965bd335c64Sesolom $goal = $avgnsec; 966bd335c64Sesolom } 967bd335c64Sesolom 968bd335c64Sesolom # If the largest of the interrupts is on srccpuid, leave it there. 969bd335c64Sesolom # This can help minimize the disruption caused by moving interrupts. 970bd335c64Sesolom 971bd335c64Sesolom if ($ivecs[0]->{origcpu} == $srccpuid) { 972bd335c64Sesolom syslog('debug', "Keeping $ivecs[0]->{inum} on $srccpuid"); 973bd335c64Sesolom $goal -= $ivecs[0]->{time}; 974bd335c64Sesolom shift(@ivecs); 975bd335c64Sesolom } 976bd335c64Sesolom 977bd335c64Sesolom syslog('debug', "GOAL: inums should total $goal"); 978bd335c64Sesolom find_goal(\@ivecs, $goal); 979bd335c64Sesolom 980bd335c64Sesolom # find_goal() returned its results to us by setting $ivec->{goal} if 981bd335c64Sesolom # the ivec should be on srccpuid, or clearing it for tgtcpuid. 982bd335c64Sesolom # Call move_intr() to update our $delta with the new results. 983bd335c64Sesolom 984bd335c64Sesolom foreach my $ivec (@ivecs) { 985bd335c64Sesolom syslog('debug', "ivec $ivec->{inum} goal $ivec->{goal}"); 986bd335c64Sesolom VERIFY($ivec->{nowcpu} == $srccpuid || 987bd335c64Sesolom $ivec->{nowcpu} == $tgtcpuid, "cpu2cpu found an ". 988bd335c64Sesolom "interrupt not currently on src or tgt cpu"); 989bd335c64Sesolom 990bd335c64Sesolom if ($ivec->{goal} && $ivec->{nowcpu} != $srccpuid) { 991bd335c64Sesolom move_intr($delta, $ivec->{inum}, $ivec->{nowcpu}, 992bd335c64Sesolom $srccpuid); 993bd335c64Sesolom } elsif ($ivec->{goal} == 0 && $ivec->{nowcpu} != $tgtcpuid) { 994bd335c64Sesolom move_intr($delta, $ivec->{inum}, $ivec->{nowcpu}, 995bd335c64Sesolom $tgtcpuid); 996bd335c64Sesolom } 997bd335c64Sesolom } 998bd335c64Sesolom move_intr_check($delta, $srccpuid, $tgtcpuid); # asserts 999bd335c64Sesolom 1000bd335c64Sesolom my $newload = $delta->{$srccpuid}{intrs} / $delta->{$srccpuid}{tot}; 1001bd335c64Sesolom VERIFY($newload <= $srcload && $newload > $delta->{avgintrload}, 1002bd335c64Sesolom "cpu2cpu: new load didn't end up in expected range"); 1003bd335c64Sesolom} 1004bd335c64Sesolom 1005bd335c64Sesolom 1006bd335c64Sesolom# find_goal() and its helper do_find_goal() are used to find the best 1007bd335c64Sesolom# combination of interrupts in order to generate a load that is as close 1008bd335c64Sesolom# as possible to a goal load without falling below that goal. Before returning 1009bd335c64Sesolom# to its caller, find_goal() sets a new value in the hash of each interrupt, 1010bd335c64Sesolom# {goal}, which if set signifies that this interrupt is one of the interrupts 1011bd335c64Sesolom# identified as part of the set of interrupts which best meet the goal. 1012bd335c64Sesolom# 1013bd335c64Sesolom# The arguments to find_goal are a list of ivecs (hash references), sorted 1014bd335c64Sesolom# by descending {time}, and the goal load. The goal is relative to {time}. 1015bd335c64Sesolom# The best fit is determined by performing a depth-first search. do_find_goal 1016bd335c64Sesolom# is the recursive subroutine which carries out the search. 1017bd335c64Sesolom# 1018bd335c64Sesolom# It is passed an index as an argument, originally 0. On a given invocation, 1019bd335c64Sesolom# it is only to consider interrupts in the ivecs array starting at that index. 1020bd335c64Sesolom# It then considers two possibilities: 1021bd335c64Sesolom# 1) What is the best goal-fit if I include ivecs[index]? 1022bd335c64Sesolom# 2) What is the best goal-fit if I exclude ivecs[index]? 1023bd335c64Sesolom# To determine case 1, it subtracts the load of ivecs[index] from the goal, 1024bd335c64Sesolom# and calls itself recursively with that new goal and index++. 1025bd335c64Sesolom# To determine case 2, it calls itself recursively with the same goal and 1026bd335c64Sesolom# index++. 1027bd335c64Sesolom# 1028bd335c64Sesolom# It then compares the two results, decide which one best meets the goals, 1029bd335c64Sesolom# and returns the result. The return value is the best-fit's interrupt load, 1030bd335c64Sesolom# followed by a list of all the interrupts which make up that best-fit. 1031bd335c64Sesolom# 1032bd335c64Sesolom# As an optimization, a second array loads[] is created which mirrors ivecs[]. 1033bd335c64Sesolom# loads[i] will equal the total loads of all ivecs[i..$#ivecs]. This is used 1034bd335c64Sesolom# by do_find_goal to avoid recursing all the way to the end of the ivecs 1035bd335c64Sesolom# array if including all remaining interrupts will still leave the best-fit 1036bd335c64Sesolom# at below goal load. If so, it then includes all remaining interrupts on 1037bd335c64Sesolom# the goal list and returns. 1038bd335c64Sesolom# 1039bd335c64Sesolomsub find_goal($$) # private function 1040bd335c64Sesolom{ 1041bd335c64Sesolom my ($ivecs, $goal) = @_; 1042bd335c64Sesolom 1043bd335c64Sesolom my @goals; 1044bd335c64Sesolom my $load; 1045bd335c64Sesolom my $ivec; 1046bd335c64Sesolom 1047bd335c64Sesolom if ($goal <= 0) { 1048bd335c64Sesolom @goals = (); # the empty set will best meet the goal 1049bd335c64Sesolom } else { 1050bd335c64Sesolom syslog('debug', "finding goal from intrs %s", 1051bd335c64Sesolom ivecs_to_string(@$ivecs)); 1052bd335c64Sesolom 1053bd335c64Sesolom # Generate @loads array 1054bd335c64Sesolom 1055bd335c64Sesolom my $tot = 0; 1056bd335c64Sesolom foreach $ivec (@$ivecs) { 1057bd335c64Sesolom $tot += $ivec->{time}; 1058bd335c64Sesolom } 1059bd335c64Sesolom my @loads = (); 1060bd335c64Sesolom foreach $ivec (@$ivecs) { 1061bd335c64Sesolom push(@loads, $tot); 1062bd335c64Sesolom $tot -= $ivec->{time}; 1063bd335c64Sesolom } 1064bd335c64Sesolom ($load, @goals) = do_find_goal($ivecs, \@loads, $goal, 0); 1065bd335c64Sesolom VERIFY($load >= $goal, "find_goal didn't meet goals"); 10665bb4956eSesolom } 1067bd335c64Sesolom syslog('debug', "goals found: %s", ivecs_to_string(@goals)); 1068bd335c64Sesolom 1069bd335c64Sesolom # Set or clear $ivec->{goal} for each ivec, based on returned @goals 1070bd335c64Sesolom 1071bd335c64Sesolom foreach $ivec (@$ivecs) { 1072bd335c64Sesolom if ($#goals > -1 && $ivec == $goals[0]) { 1073bd335c64Sesolom syslog('debug', "inum $ivec->{inum} on source cpu"); 1074bd335c64Sesolom $ivec->{goal} = 1; 1075bd335c64Sesolom shift(@goals); 1076bd335c64Sesolom } else { 1077bd335c64Sesolom syslog('debug', "inum $ivec->{inum} on target cpu"); 1078bd335c64Sesolom $ivec->{goal} = 0; 1079bd335c64Sesolom } 1080bd335c64Sesolom } 1081bd335c64Sesolom} 1082bd335c64Sesolom 1083bd335c64Sesolom 1084bd335c64Sesolomsub do_find_goal($$$$) # private function 1085bd335c64Sesolom{ 1086bd335c64Sesolom my ($ivecs, $loads, $goal, $idx) = @_; 1087bd335c64Sesolom 1088bd335c64Sesolom if ($idx > $#{$ivecs}) { 1089bd335c64Sesolom return (0); 1090bd335c64Sesolom } 1091bd335c64Sesolom syslog('debug', "$idx: finding goal $goal inum $ivecs->[$idx]{inum}"); 1092bd335c64Sesolom 1093bd335c64Sesolom my $load = $ivecs->[$idx]{time}; 1094bd335c64Sesolom my @goals_with = (); 1095bd335c64Sesolom my @goals_without = (); 1096bd335c64Sesolom my ($with, $without); 1097bd335c64Sesolom 1098bd335c64Sesolom # If we include all remaining items and we're still below goal, 1099bd335c64Sesolom # stop here. We can just return a result that includes $idx and all 1100bd335c64Sesolom # subsequent ivecs. Since this will still be below goal, there's 1101bd335c64Sesolom # nothing better to be done. 1102bd335c64Sesolom 1103bd335c64Sesolom if ($loads->[$idx] <= $goal) { 1104bd335c64Sesolom syslog('debug', 1105bd335c64Sesolom "$idx: including all remaining intrs %s with load %d", 1106bd335c64Sesolom ivecs_to_string(@$ivecs[$idx .. $#{$ivecs}]), 1107bd335c64Sesolom $loads->[$idx]); 1108bd335c64Sesolom return ($loads->[$idx], @$ivecs[$idx .. $#{$ivecs}]); 1109bd335c64Sesolom } 1110bd335c64Sesolom 1111bd335c64Sesolom # Evaluate the "with" option, i.e. the best matching goal which 1112bd335c64Sesolom # includes $ivecs->[$idx]. If idx's load is more than our goal load, 1113bd335c64Sesolom # stop here. Once we're above the goal, there is no need to consider 1114bd335c64Sesolom # further interrupts since they'll only take us further from the goal. 1115bd335c64Sesolom 1116bd335c64Sesolom if ($goal <= $load) { 1117bd335c64Sesolom $with = $load; # stop here 1118bd335c64Sesolom } else { 1119bd335c64Sesolom ($with, @goals_with) = 1120bd335c64Sesolom do_find_goal($ivecs, $loads, $goal - $load, $idx + 1); 1121bd335c64Sesolom $with += $load; 1122bd335c64Sesolom } 1123bd335c64Sesolom syslog('debug', "$idx: with-load $with intrs %s", 1124bd335c64Sesolom ivecs_to_string($ivecs->[$idx], @goals_with)); 1125bd335c64Sesolom 1126bd335c64Sesolom # Evaluate the "without" option, i.e. the best matching goal which 1127bd335c64Sesolom # excludes $ivecs->[$idx]. 1128bd335c64Sesolom 1129bd335c64Sesolom ($without, @goals_without) = 1130bd335c64Sesolom &do_find_goal($ivecs, $loads, $goal, $idx + 1); 1131bd335c64Sesolom syslog('debug', "$idx: without-load $without intrs %s", 1132bd335c64Sesolom ivecs_to_string(@goals_without)); 1133bd335c64Sesolom 1134bd335c64Sesolom # We now have our "with" and "without" options, and we choose which 1135bd335c64Sesolom # best fits the goal. If one is greater than goal and the other is 1136bd335c64Sesolom # below goal, we choose the one that is greater. If they are both 1137bd335c64Sesolom # below goal, then we choose the one that is greater. If they are 1138bd335c64Sesolom # both above goal, then we choose the smaller. 1139bd335c64Sesolom 1140bd335c64Sesolom my $which; # 0 == with, 1 == without 1141bd335c64Sesolom if ($with >= $goal && $without < $goal) { 1142bd335c64Sesolom $which = 0; 1143bd335c64Sesolom } elsif ($with < $goal && $without >= $goal) { 1144bd335c64Sesolom $which = 1; 1145bd335c64Sesolom } elsif ($with >= $goal && $without >= $goal) { 1146bd335c64Sesolom $which = ($without < $with); 1147bd335c64Sesolom } else { 1148bd335c64Sesolom $which = ($without > $with); 1149bd335c64Sesolom } 1150bd335c64Sesolom 1151bd335c64Sesolom # Return the load of our best case scenario, followed by all the ivecs 1152bd335c64Sesolom # which compose that goal. 1153bd335c64Sesolom 1154bd335c64Sesolom if ($which == 1) { # without 1155bd335c64Sesolom syslog('debug', "$idx: going without"); 1156bd335c64Sesolom return ($without, @goals_without); 1157bd335c64Sesolom } else { 1158bd335c64Sesolom syslog('debug', "$idx: going with"); 1159bd335c64Sesolom return ($with, $ivecs->[$idx], @goals_with); 1160bd335c64Sesolom } 1161bd335c64Sesolom # Not reached 1162bd335c64Sesolom} 1163bd335c64Sesolom 1164bd335c64Sesolom 1165bd335c64Sesolom 1166bd335c64Sesolom 1167bd335c64Sesolomsyslog('debug', "intrd is starting".($debug ? " (debug)" : "")); 1168bd335c64Sesolom 1169bd335c64Sesolommy @deltas = (); 1170bd335c64Sesolommy $deltas_tottime = 0; # sum of maxsnap-minsnap across @deltas 1171bd335c64Sesolommy $avggoodness; 1172bd335c64Sesolommy $baseline_goodness = 0; 1173bd335c64Sesolommy $compdelta; 1174bd335c64Sesolom 1175bd335c64Sesolommy $do_reconfig; 1176bd335c64Sesolom 1177bd335c64Sesolom# temp variables 1178bd335c64Sesolommy $goodness; 1179bd335c64Sesolommy $deltatime; 1180bd335c64Sesolommy $olddelta; 1181bd335c64Sesolommy $olddeltatime; 1182bd335c64Sesolommy $delta; 1183bd335c64Sesolommy $newstat; 1184bd335c64Sesolommy $below_statslen; 1185bd335c64Sesolommy $newtime; 1186bd335c64Sesolommy $ret; 1187bd335c64Sesolom 1188bd335c64Sesolom 1189bd335c64Sesolommy $gotsig = 0; 1190bd335c64Sesolom$SIG{INT} = sub { $gotsig = 1; }; # don't die in the middle of retargeting 1191bd335c64Sesolom$SIG{HUP} = $SIG{INT}; 1192bd335c64Sesolom$SIG{TERM} = $SIG{INT}; 1193bd335c64Sesolom 1194bd335c64Sesolommy $ks; 1195bd335c64Sesolomif ($using_scengen == 0) { 1196bd335c64Sesolom $ks = Sun::Solaris::Kstat->new(); 1197bd335c64Sesolom} else { 1198bd335c64Sesolom $ks = myks_update(); # supplied by the simulator 1199bd335c64Sesolom} 1200bd335c64Sesolom 1201bd335c64Sesolom# If no pci_intrs kstats were found, we need to exit, but we can't because 1202bd335c64Sesolom# SMF will restart us and/or report an error to the administrator. But 1203bd335c64Sesolom# there's nothing an administrator can do. So print out a message for SMF 1204bd335c64Sesolom# logs and silently pause forever. 1205bd335c64Sesolom 1206bd335c64Sesolomif (!exists($ks->{pci_intrs})) { 1207bd335c64Sesolom print STDERR "$cmdname: no interrupts were found; ". 1208bd335c64Sesolom "your PCI bus may not yet be supported\n"; 1209bd335c64Sesolom pause() while $gotsig == 0; 1210bd335c64Sesolom exit 0; 1211bd335c64Sesolom} 1212bd335c64Sesolom 1213bd335c64Sesolommy $stat = getstat($ks); 1214bd335c64Sesolom 1215bd335c64Sesolom 1216bd335c64Sesolom 1217bd335c64Sesolomfor (;;) { 1218bd335c64Sesolom sub clear_deltas { 1219bd335c64Sesolom @deltas = (); 1220bd335c64Sesolom $deltas_tottime = 0; 1221bd335c64Sesolom $stat = 0; # prevent next gen_delta() from setting {missing} 1222bd335c64Sesolom } 1223bd335c64Sesolom 1224bd335c64Sesolom # 1. Sleep, update the kstats, and save the new stats in $newstat. 1225bd335c64Sesolom 1226bd335c64Sesolom exit 0 if $gotsig; # if we got ^C / SIGTERM, exit 1227bd335c64Sesolom if ($using_scengen == 0) { 1228bd335c64Sesolom sleep($sleeptime); 1229bd335c64Sesolom exit 0 if $gotsig; # if we got ^C / SIGTERM, exit 1230bd335c64Sesolom $ks->update(); 1231bd335c64Sesolom } else { 1232bd335c64Sesolom $ks = myks_update(); 1233bd335c64Sesolom } 1234bd335c64Sesolom $newstat = getstat($ks); 1235bd335c64Sesolom 1236bd335c64Sesolom # $stat or $newstat could be zero if they're uninitialized, or if 1237bd335c64Sesolom # getstat() failed. If $stat is zero, move $newstat to $stat, sleep 1238bd335c64Sesolom # and try again. If $newstat is zero, then we also sleep and try 1239bd335c64Sesolom # again, hoping the problem will clear up. 1240bd335c64Sesolom 1241bd335c64Sesolom next if (!ref $newstat); 1242bd335c64Sesolom if (!ref $stat) { 1243bd335c64Sesolom $stat = $newstat; 1244bd335c64Sesolom next; 1245bd335c64Sesolom } 1246bd335c64Sesolom 1247bd335c64Sesolom 1248bd335c64Sesolom # 2. Compare $newstat with the prior set of values, result in %$delta. 1249bd335c64Sesolom 1250bd335c64Sesolom $delta = generate_delta($stat, $newstat); 1251bd335c64Sesolom dumpdelta($delta) if $debug; # Dump most recent stats to stdout. 1252bd335c64Sesolom $stat = $newstat; # The new stats now become the old stats. 1253bd335c64Sesolom 1254bd335c64Sesolom 1255bd335c64Sesolom # 3. If $delta->{missing}, then there has been a reconfiguration of 1256bd335c64Sesolom # either cpus or interrupts (probably both). We need to toss out our 1257bd335c64Sesolom # old set of statistics and start from scratch. 1258bd335c64Sesolom # 1259bd335c64Sesolom # Also, if the delta covers a very long range of time, then we've 1260bd335c64Sesolom # been experiencing a system overload that has resulted in intrd 1261bd335c64Sesolom # not being allowed to run effectively for a while now. As above, 1262bd335c64Sesolom # toss our old statistics and start from scratch. 1263bd335c64Sesolom 1264bd335c64Sesolom $deltatime = $delta->{maxsnap} - $delta->{minsnap}; 1265bd335c64Sesolom if ($delta->{missing} > 0 || $deltatime > $statslen) { 1266bd335c64Sesolom clear_deltas(); 1267bd335c64Sesolom syslog('debug', "evaluating interrupt assignments"); 1268bd335c64Sesolom next; 1269bd335c64Sesolom } 1270bd335c64Sesolom 1271bd335c64Sesolom 1272bd335c64Sesolom # 4. Incorporate new delta into the list of deltas, and associated 1273bd335c64Sesolom # statistics. If we've just now received $statslen deltas, then it's 1274bd335c64Sesolom # time to evaluate a reconfiguration. 1275bd335c64Sesolom 1276bd335c64Sesolom $below_statslen = ($deltas_tottime < $statslen); 1277bd335c64Sesolom $deltas_tottime += $deltatime; 1278bd335c64Sesolom $do_reconfig = ($below_statslen && $deltas_tottime >= $statslen); 1279bd335c64Sesolom push(@deltas, $delta); 1280bd335c64Sesolom 1281bd335c64Sesolom # 5. Remove old deltas if total time is more than $statslen. We use 1282bd335c64Sesolom # @deltas as a moving average of the last $statslen seconds. Shift 1283bd335c64Sesolom # off the olders deltas, but only if that doesn't cause us to fall 1284bd335c64Sesolom # below $statslen seconds. 1285bd335c64Sesolom 1286bd335c64Sesolom while (@deltas > 1) { 1287bd335c64Sesolom $olddelta = $deltas[0]; 1288bd335c64Sesolom $olddeltatime = $olddelta->{maxsnap} - $olddelta->{minsnap}; 1289bd335c64Sesolom $newtime = $deltas_tottime - $olddeltatime; 1290bd335c64Sesolom last if ($newtime < $statslen); 1291bd335c64Sesolom 1292bd335c64Sesolom shift(@deltas); 1293bd335c64Sesolom $deltas_tottime = $newtime; 1294bd335c64Sesolom } 1295bd335c64Sesolom 1296bd335c64Sesolom # 6. The brains of the operation are here. First, check if we're 1297bd335c64Sesolom # imbalanced, and if so set $do_reconfig. If $do_reconfig is set, 1298bd335c64Sesolom # either because of imbalance or above in step 4, we evaluate a 1299bd335c64Sesolom # new configuration. 1300bd335c64Sesolom # 1301bd335c64Sesolom # First, take @deltas and generate a single "compressed" delta 1302bd335c64Sesolom # which summarizes them all. Pass that to do_reconfig and see 1303bd335c64Sesolom # what it does with it: 1304bd335c64Sesolom # 1305bd335c64Sesolom # $ret == -1 : failure 1306bd335c64Sesolom # $ret == 0 : current config is optimal (or close enough) 1307bd335c64Sesolom # $ret == 1 : reconfiguration has occurred 1308bd335c64Sesolom # 1309bd335c64Sesolom # If $ret is -1 or 1, dump all our deltas and start from scratch. 1310bd335c64Sesolom # Step 4 above will set do_reconfig soon thereafter. 1311bd335c64Sesolom # 1312bd335c64Sesolom # If $ret is 0, then nothing has happened because we're already 1313bd335c64Sesolom # good enough. Set baseline_goodness to current goodness. 1314bd335c64Sesolom 1315bd335c64Sesolom $compdelta = compress_deltas(\@deltas); 1316bd335c64Sesolom if (VERIFY(ref($compdelta) eq "HASH", "couldn't compress deltas")) { 1317bd335c64Sesolom clear_deltas(); 1318bd335c64Sesolom next; 1319bd335c64Sesolom } 1320bd335c64Sesolom $compdelta->{goodness} = goodness($compdelta); 1321bd335c64Sesolom dumpdelta($compdelta) if $debug; 1322bd335c64Sesolom 1323bd335c64Sesolom $goodness = $compdelta->{goodness}; 1324bd335c64Sesolom syslog('debug', "GOODNESS: %5.2f%%", $goodness * 100); 1325bd335c64Sesolom 1326bd335c64Sesolom if ($deltas_tottime >= $statslen && 1327bd335c64Sesolom imbalanced($goodness, $baseline_goodness)) { 1328bd335c64Sesolom $do_reconfig = 1; 1329bd335c64Sesolom } 1330bd335c64Sesolom 1331bd335c64Sesolom if ($do_reconfig) { 1332bd335c64Sesolom $ret = do_reconfig($compdelta); 1333bd335c64Sesolom 1334bd335c64Sesolom if ($ret != 0) { 1335bd335c64Sesolom clear_deltas(); 1336bd335c64Sesolom syslog('debug', "do_reconfig FAILED!") if $ret == -1; 1337bd335c64Sesolom } else { 1338bd335c64Sesolom syslog('debug', "setting new baseline of $goodness"); 1339bd335c64Sesolom $baseline_goodness = $goodness; 1340bd335c64Sesolom } 1341bd335c64Sesolom } 1342bd335c64Sesolom syslog('debug', "---------------------------------------"); 1343bd335c64Sesolom} 1344