| 1 | |||
| 2 | package App::FQStat::Scanner; | ||
| 3 | # App::FQStat is (c) 2007-2009 Steffen Mueller | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Time::HiRes qw/sleep/; | ||
| 11 | use String::Trigram (); | ||
| 12 | use DateTime (); | ||
| 13 | use Time::Zone (); | ||
| 14 | use App::FQStat::Debug; | ||
| 15 | |||
| 16 | # run qstat | ||
| 17 | sub run_qstat { | ||
| 18 | warnenter if ::DEBUG; | ||
| 19 | my $forced = shift; | ||
| 20 | lock($::ScannerStartRun); | ||
| 21 | |||
| 22 | if (not defined $::ScannerThread) { | ||
| 23 | warnline "Creating new (initial?) scanner thread" if ::DEBUG; | ||
| 24 | $::ScannerThread = threads->new(\&App::FQStat::Scanner::scanner_thread); | ||
| 25 | } | ||
| 26 | elsif ($::ScannerThread->is_joinable()) { | ||
| 27 | warnline "Joining scanner thread" if ::DEBUG; | ||
| 28 | my $return = $::ScannerThread->join(); | ||
| 29 | ($::Records, $::NoActiveNodes) = @$return; | ||
| 30 | $::Summary = []; | ||
| 31 | $::Initialized = 1; | ||
| 32 | { lock($::RecordsChanged); $::RecordsChanged = 1; } | ||
| 33 | warnline "Joined scanner thread. Creating new scanner thread" if ::DEBUG; | ||
| 34 | $::ScannerThread = threads->new(\&App::FQStat::Scanner::scanner_thread); | ||
| 35 | } | ||
| 36 | elsif (!$::ScannerThread->is_running()) { | ||
| 37 | warnline "scanner thread not running. Creating new scanner thread" if ::DEBUG; | ||
| 38 | undef $::ScannerThread; | ||
| 39 | $::ScannerThread = threads->new(\&App::FQStat::Scanner::scanner_thread); | ||
| 40 | } | ||
| 41 | elsif ($forced) { | ||
| 42 | warnline "scanner thread running. Force in effect, setting StartRun" if ::DEBUG; | ||
| 43 | $::ScannerStartRun = 1; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub scanner_thread { | ||
| 48 | warnenter if ::DEBUG; | ||
| 49 | { | ||
| 50 | lock($::ScannerStartRun); | ||
| 51 | $::ScannerStartRun = 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | my @lines; | ||
| 55 | my @args; | ||
| 56 | { | ||
| 57 | lock($::SummaryMode); | ||
| 58 | if ($::SummaryMode) { | ||
| 59 | push @args, '-u', '*'; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | lock($::User); | ||
| 63 | push @args, '-u', ( (defined($::User) && $::User ne '') ? $::User : '*'); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | my $timebefore = time(); | ||
| 68 | my $qstat = App::FQStat::Config::get("qstatcmd"); | ||
| 69 | my $output = App::FQStat::System::run_capture($qstat, @args); | ||
| 70 | if (not defined $output) { | ||
| 71 | die "Running 'qstat' failed!"; | ||
| 72 | } | ||
| 73 | my $duration = time()-$timebefore; | ||
| 74 | |||
| 75 | # Update the update interval according to the time it takes | ||
| 76 | { | ||
| 77 | lock($::Interval); | ||
| 78 | if ($duration >= $::Interval) { | ||
| 79 | $::Interval = ($duration > $::Interval*1.8 ? $duration+1.0 : $::Interval*1.8); | ||
| 80 | } | ||
| 81 | elsif ($duration < $::Interval and $duration > $::UserInterval) { | ||
| 82 | $::Interval = ($::Interval/1.1 > $::UserInterval ? $::Interval/1.1 : $::UserInterval); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | @lines = split /\n/, $output; | ||
| 87 | shift @lines; | ||
| 88 | shift @lines; | ||
| 89 | |||
| 90 | my $noActiveNodes = 0; | ||
| 91 | foreach my $line (@lines) { | ||
| 92 | $line =~ s/^\s+//; | ||
| 93 | my $rec = [split /\s+/, $line]; | ||
| 94 | $rec->[7] = '' if not $rec->[7] =~ /\D/; | ||
| 95 | my @date = split /\//, $rec->[5]; | ||
| 96 | @date = @date[1, 0, 2]; | ||
| 97 | my @jobdesc; | ||
| 98 | @jobdesc = ( | ||
| 99 | $rec->[0], # F_id | ||
| 100 | $rec->[1], # F_prio | ||
| 101 | $rec->[2], # F_name | ||
| 102 | $rec->[3], # F_user | ||
| 103 | $rec->[4], # F_status | ||
| 104 | join('.', @date), # F_date | ||
| 105 | $rec->[6], # F_time | ||
| 106 | $rec->[7], # F_queue | ||
| 107 | ); | ||
| 108 | $noActiveNodes++ if $rec->[4] =~ /^\s*r\s*$/; | ||
| 109 | $line = \@jobdesc; | ||
| 110 | } | ||
| 111 | |||
| 112 | reverse_records(\@lines) if $::RecordsReversed; # retain state of reversal | ||
| 113 | |||
| 114 | sort_current(\@lines); | ||
| 115 | |||
| 116 | lock($::DisplayOffset); | ||
| 117 | lock(@::Termsize); | ||
| 118 | my $limit = @lines - $::Termsize[1]+4; | ||
| 119 | if ($::DisplayOffset and $::DisplayOffset > $limit) { | ||
| 120 | $::DisplayOffset = $limit; | ||
| 121 | } | ||
| 122 | |||
| 123 | sleep 0.1; # Note to self: fractional sleep without HiRes => CPU=100% | ||
| 124 | warnline "End of scanner_thread" if ::DEBUG; | ||
| 125 | return [\@lines, $noActiveNodes]; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | # sorts the qstat output by $::SortField | ||
| 134 | sub sort_current { | ||
| 135 | warnenter if ::DEBUG; | ||
| 136 | my $lines = shift; | ||
| 137 | my $sortfield; | ||
| 138 | { | ||
| 139 | lock($::SortField); | ||
| 140 | if (not defined $::SortField or $::SortField eq '' or not exists $::Columns{$::SortField}) { | ||
| 141 | warnline "Nothing to sort" if ::DEBUG; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | $sortfield = $::SortField; | ||
| 145 | } | ||
| 146 | my $key = $sortfield; | ||
| 147 | my $key_index = ::RECORD_KEY_CONSTANT()->{$key}; | ||
| 148 | |||
| 149 | my $order; | ||
| 150 | $order = $::Columns{$sortfield}{order} unless $sortfield eq 'status'; | ||
| 151 | $order = 'status' if $sortfield eq 'status'; | ||
| 152 | |||
| 153 | warnline "Sorting: key=$key order=$order" if ::DEBUG; | ||
| 154 | |||
| 155 | return if not defined $order; | ||
| 156 | |||
| 157 | my $time = time(); # for debugging / profiling | ||
| 158 | |||
| 159 | if ($order eq 'status') { | ||
| 160 | |||
| 161 | @$lines = | ||
| 162 | map { $_->[0] } | ||
| 163 | sort { $a->[1] <=> $b->[1] } | ||
| 164 | map { | ||
| 165 | my $s = $_->[::F_status]; | ||
| 166 | if ($s =~ /[Ed]/) { $s = 0 } | ||
| 167 | elsif ($s =~ /r/) { $s = 1 } | ||
| 168 | elsif ($s =~ /t/) { $s = 2 } | ||
| 169 | elsif ($s =~ /w/) { $s = 3 } | ||
| 170 | else { $s = 4 } | ||
| 171 | [$_, $s] | ||
| 172 | } | ||
| 173 | @$lines; | ||
| 174 | } | ||
| 175 | elsif ($order eq 'time') { | ||
| 176 | ::debug "Sorting by time"; | ||
| 177 | @$lines = | ||
| 178 | map { $_->[0] } | ||
| 179 | sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] or $a->[3] <=> $b->[3] } | ||
| 180 | map { [$_, split(/:/, $_->[$key_index])] } | ||
| 181 | @$lines; | ||
| 182 | } | ||
| 183 | elsif ($order eq 'date') { | ||
| 184 | ::debug "Sorting by date"; | ||
| 185 | @$lines = | ||
| 186 | map { $_->[0] } | ||
| 187 | sort { $b->[1] <=> $a->[1] or $b->[2] <=> $a->[2] or $b->[3] <=> $a->[3] } | ||
| 188 | map { [$_, split(/\./, $_->[$key_index])] } | ||
| 189 | @$lines; | ||
| 190 | } | ||
| 191 | elsif ($order eq 'num') { | ||
| 192 | ::debug "Sorting numerically"; | ||
| 193 | @$lines = | ||
| 194 | sort { $a->[$key_index] <=> $b->[$key_index] } | ||
| 195 | @$lines; | ||
| 196 | } | ||
| 197 | elsif ($order eq 'num_highlow') { | ||
| 198 | ::debug "Sorting numerically high to low"; | ||
| 199 | @$lines = | ||
| 200 | sort { $b->[$key_index] <=> $a->[$key_index] } | ||
| 201 | @$lines; | ||
| 202 | } | ||
| 203 | else { # default to alpha | ||
| 204 | ::debug "Sorting alphabetically"; | ||
| 205 | @$lines = | ||
| 206 | sort { $a->[$key_index] cmp $b->[$key_index] } | ||
| 207 | @$lines; | ||
| 208 | } | ||
| 209 | |||
| 210 | { | ||
| 211 | lock($::RecordsReversed); | ||
| 212 | lock($::RecordsChanged); | ||
| 213 | reverse_records($::Records) if $::RecordsReversed; | ||
| 214 | $::RecordsChanged = 1 if $::RecordsReversed; | ||
| 215 | } | ||
| 216 | |||
| 217 | if (::DEBUG()) { | ||
| 218 | my $diff = time()-$time; | ||
| 219 | ::debug "Sorting took $diff seconds."; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | # reverse the current set of records | ||
| 225 | sub reverse_records { | ||
| 226 | warnenter if ::DEBUG; | ||
| 227 | my $lines = shift; | ||
| 228 | @$lines = reverse @$lines; | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | # calculate the job summary | ||
| 233 | sub calculate_summary { | ||
| 234 | warnenter if ::DEBUG; | ||
| 235 | $::Summary = []; | ||
| 236 | |||
| 237 | my $offset = Time::Zone::tz_local_offset(); | ||
| 238 | my $curtime = time() + $offset; | ||
| 239 | |||
| 240 | # cluster by user name | ||
| 241 | my %user_clusters; | ||
| 242 | foreach my $job (@$::Records) { | ||
| 243 | my $user = $job->[::F_user]; | ||
| 244 | $user_clusters{$user} ||= []; | ||
| 245 | push @{$user_clusters{$user}}, $job; | ||
| 246 | } | ||
| 247 | |||
| 248 | if (App::FQStat::Config::get("summary_clustering")) { | ||
| 249 | my $trigram = String::Trigram->new( | ||
| 250 | minSim => App::FQStat::Config::get("summary_clustering_similarity"), | ||
| 251 | warp => 1.2, | ||
| 252 | cmpBase => [], | ||
| 253 | ); | ||
| 254 | |||
| 255 | my %user_name_clusters; | ||
| 256 | |||
| 257 | # cluster by similarity | ||
| 258 | foreach my $user (keys %user_clusters) { | ||
| 259 | $trigram->reInit([]); | ||
| 260 | |||
| 261 | my %jname_clusters; | ||
| 262 | my $jobs = $user_clusters{$user}; | ||
| 263 | foreach my $job (@$jobs) { | ||
| 264 | my $jname = $job->[::F_name]; | ||
| 265 | # ignore numbers | ||
| 266 | $jname =~ s/\d+//g; | ||
| 267 | |||
| 268 | if (keys %jname_clusters) { | ||
| 269 | my @bestmatch; | ||
| 270 | my $sim = $trigram->getBestMatch($jname, \@bestmatch); | ||
| 271 | if (@bestmatch and $sim) { | ||
| 272 | push @{ $jname_clusters{$bestmatch[0]} }, $job; | ||
| 273 | } | ||
| 274 | else { | ||
| 275 | $jname_clusters{$jname} ||= []; | ||
| 276 | push @{ $jname_clusters{$jname} }, $job; | ||
| 277 | $trigram->extendBase([$jname]); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | else { | ||
| 281 | $jname_clusters{$jname} = [$job]; | ||
| 282 | $trigram->extendBase([$jname]); | ||
| 283 | } | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | foreach my $jname (keys %jname_clusters) { | ||
| 288 | my $clustername = "$user;$jname"; | ||
| 289 | $user_name_clusters{$clustername} ||= []; | ||
| 290 | push @{$user_name_clusters{$clustername}}, @{$jname_clusters{$jname}}; | ||
| 291 | delete $jname_clusters{$jname}; | ||
| 292 | } | ||
| 293 | |||
| 294 | } # end foreach user cluster | ||
| 295 | |||
| 296 | %user_clusters = %user_name_clusters; | ||
| 297 | } | ||
| 298 | |||
| 299 | # actually calculate the summaries for each cluster | ||
| 300 | foreach my $user (keys %user_clusters) { | ||
| 301 | my $jobs = $user_clusters{$user}; | ||
| 302 | my %n_status = (r => 0, E => 0, h => 0, 'qw' => 0); | ||
| 303 | my $prio_sum = 0; | ||
| 304 | my $nprio = 0; | ||
| 305 | my $runtime_sum = 0; | ||
| 306 | my $njobs_started = 0; | ||
| 307 | my $max_runtime = 0; | ||
| 308 | |||
| 309 | foreach my $job (@$jobs) { | ||
| 310 | my $prio = $job->[::F_prio]; | ||
| 311 | $nprio++, $prio_sum += $prio if $prio > 1.e-2; | ||
| 312 | |||
| 313 | # find job status | ||
| 314 | for ($job->[::F_status]) { | ||
| 315 | if (/^[rt]$/) { $n_status{r}++; } | ||
| 316 | elsif (/(?:^d|E)/) { $n_status{E}++; } | ||
| 317 | elsif (/h(?:qw|r|t)/) { $n_status{h}++; } | ||
| 318 | else { $n_status{qw}++; } | ||
| 319 | } | ||
| 320 | |||
| 321 | if ($job->[::F_status] =~ /^h?[rt]$/) { | ||
| 322 | my ($day, $month, $year) = split /\./, $job->[::F_date]; | ||
| 323 | my ($hour, $minute, $second) = split /:/, $job->[::F_time]; | ||
| 324 | my $dt = DateTime->new(year => $year, month => $month, day => $day, | ||
| 325 | hour => $hour, minute => $minute, second => $second); | ||
| 326 | my $runtime = $curtime - $dt->epoch(); | ||
| 327 | $runtime_sum += $runtime; | ||
| 328 | $max_runtime = $runtime if $runtime > $max_runtime; | ||
| 329 | $njobs_started++; | ||
| 330 | } | ||
| 331 | |||
| 332 | } | ||
| 333 | |||
| 334 | ($user, my $jobname) = split /;/, $user; | ||
| 335 | |||
| 336 | my $runtime = ''; | ||
| 337 | if ($njobs_started) { | ||
| 338 | my $seconds = $runtime_sum/$njobs_started; | ||
| 339 | my $hours = int($seconds / 3600); | ||
| 340 | my $minutes = int($seconds / 60 - $hours*60); | ||
| 341 | $seconds = int($seconds) % 60; | ||
| 342 | $runtime = sprintf('%02u:%02u:%02u', $hours, $minutes, $seconds); | ||
| 343 | |||
| 344 | $seconds = $max_runtime; | ||
| 345 | $hours = int($seconds / 3600); | ||
| 346 | $minutes = int($seconds / 60 - $hours*60); | ||
| 347 | $seconds = int($seconds) % 60; | ||
| 348 | $max_runtime = sprintf('%02u:%02u:%02u', $hours, $minutes, $seconds); | ||
| 349 | } | ||
| 350 | else { | ||
| 351 | $max_runtime = ''; | ||
| 352 | } | ||
| 353 | |||
| 354 | my $line = [ $user, $jobname, @n_status{'r', 'E', 'h', 'qw'}, ($nprio?$prio_sum/$nprio:0), $runtime, $njobs_started, $max_runtime ]; | ||
| 355 | push @$::Summary, $line; | ||
| 356 | } # end for each user | ||
| 357 | |||
| 358 | @$::Summary = | ||
| 359 | sort { | ||
| 360 | $b->[3] <=> $a->[3] #errors | ||
| 361 | or $b->[8] <=> $a->[8] #nodes-used | ||
| 362 | or $b->[2] <=> $a->[2] #running | ||
| 363 | } | ||
| 364 | @$::Summary; | ||
| 365 | |||
| 366 | return(1); | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | 1; | ||
| 371 |