checkstats.in 21.9 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008, 2014, 2017 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35
#
use strict;
use English;
use Getopt::Std;

#
# Check DB consistency.
#
sub usage() {
    print STDOUT "Usage: checkstats [-d] [-v] [-i] [-b] \n".
	"Use the -d option to see debugging output instead of emailing it.\n";
    exit(-1);
}
36
my $optlist  = "vdibtr";
37 38 39
my $debug    = 0;
my $verbose  = 1;
my $impotent = 0;
40 41
my $backup   = 1;
my $restore  = 0;
42 43
my $testrun  = 0;
my $ESTATS   = "experiment_stats_backup";
44
my $TSTATS   = "testbed_stats";
45
my $RESOURCES= "experiment_resources_backup";
46 47 48
my $USTATS   = "user_stats_backup";
my $GSTATS   = "group_stats_backup";
my $PSTATS   = "project_stats_backup";
49
my %project_stats = ();
50 51
my %group_stats = ();
my %user_stats  = ();
52
my %experiment_stats  = ();
53

54 55 56 57 58 59
#
# Simple sanity check. When we think we can fixup a swapin/swapout time,
# make sure the resulting duration does not exceed this value.
#
my $maxduration = (7 * 24 * 60 * 60);

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
sub fatal($);
sub Regenerate();
sub CheckStatConsistency($);

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use User;
use Group;

#
# Only real root can call this.
# 
#if ($UID != 0) {
#    print STDERR "You must be root to run this script!\n";
#    exit(-1);
#}

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV) {
    usage();
}
if (defined($options{"d"})) {
    $debug++;
}
if (defined($options{"v"})) {
    $verbose++;
}
if (defined($options{"i"})) {
    $impotent++;
}
114 115 116
if (defined($options{"t"})) {
    $testrun++;
}
117 118 119
if (defined($options{"b"})) {
    $backup++;
}
120 121 122
if (defined($options{"r"})) {
    $restore++;
}
123

124
if ($backup || $testrun) {
125 126 127 128 129 130
    DBQueryFatal("create table if not exists experiment_resources_backup ".
		 "like experiment_resources");
    DBQueryFatal("delete from experiment_resources_backup");
    DBQueryFatal("insert into experiment_resources_backup ".
		 "select * from experiment_resources");

131 132 133 134 135 136
    DBQueryFatal("create table if not exists experiment_stats_backup ".
		 "like experiment_stats");
    DBQueryFatal("delete from experiment_stats_backup");
    DBQueryFatal("insert into experiment_stats_backup ".
		 "select * from experiment_stats");

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    DBQueryFatal("create table if not exists user_stats_backup ".
		 "like user_stats");
    DBQueryFatal("delete from user_stats_backup");
    DBQueryFatal("insert into user_stats_backup ".
		 "select * from user_stats");

    DBQueryFatal("create table if not exists group_stats_backup ".
		 "like group_stats");
    DBQueryFatal("delete from group_stats_backup");
    DBQueryFatal("insert into group_stats_backup ".
		 "select * from group_stats");

    DBQueryFatal("create table if not exists project_stats_backup ".
		 "like project_stats");
    DBQueryFatal("delete from project_stats_backup");
    DBQueryFatal("insert into project_stats_backup ".
		 "select * from project_stats");
}

sub MyDBQ($)
{
    my ($query) = @_;

    if ($impotent) {
	print "$query\n"
	    if ($verbose > 2);
	return;
    }
    DBQueryFatal($query);
}

168
if ($impotent) {
169
    $ESTATS   = "experiment_stats";
170 171 172 173 174 175
    $RESOURCES= "experiment_resources";
    $USTATS   = "user_stats";
    $GSTATS   = "group_stats";
    $PSTATS   = "project_stats";
}

176 177 178 179 180
#
# Look for resource records that are inconsistent.
#
my $query_result =
    DBQueryFatal("select r.*,s.rsrcidx,e.state, ".
181
		 "    UNIX_TIMESTAMP(s.created) as created, ".
182
		 "    UNIX_TIMESTAMP(s.destroyed) as destroyed, ".
183
		 "    UNIX_TIMESTAMP(s.swapin_last) as swapin_last, ".
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		 "    UNIX_TIMESTAMP(s.swapout_last) as swapout_last ".
		 "  from $RESOURCES as r ".
		 "left join $ESTATS as s on s.exptidx=r.exptidx ".
		 "left join experiments as e on e.idx=r.exptidx ".
		 "order by s.exptidx,UNIX_TIMESTAMP(r.tstamp)");

while (my $row = $query_result->fetchrow_hashref()) {
    my $exptidx      = $row->{'exptidx'};
    my $swapin_time  = $row->{'swapin_time'};
    my $swapmod_time = $row->{'swapmod_time'};
    my $swapout_time = $row->{'swapout_time'};
    my $pnodes       = $row->{'pnodes'};
    my $vnodes       = $row->{'vnodes'};
    my $state        = $row->{'state'};
    my $thisidx      = $row->{'idx'};
    my $rsrcidx      = $row->{'rsrcidx'};
    my $lastidx      = $row->{'lastidx'};
201
    my $created      = $row->{'created'};
202
    my $destroyed    = $row->{'destroyed'};
203
    my $swapin_last  = $row->{'swapin_last'};
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    my $swapout_last = $row->{'swapout_last'};
    my $byswapmod    = $row->{'byswapmod'};
    my $stats_result;

    #
    # nodes and no swapin_time. See if we can fix things up using the
    # $TSTATS records,
    #
    if (($pnodes || $vnodes) && !$swapin_time) {
	if ($byswapmod) {
	    $stats_result =
		DBQueryFatal("select UNIX_TIMESTAMP(start_time) ".
			     "  from $TSTATS ".
			     "where rsrcidx=$thisidx and ".
			     "      action='swapmod' and exitcode=0");
	    if ($stats_result->numrows) {
		my ($newtime) = $stats_result->fetchrow_array();
		print "$exptidx (rsrc:$thisidx); ".
		    "setting swapin/swapmod to $newtime from testbed_stats\n"
		    if ($verbose);
		
		MyDBQ("update $RESOURCES set ".
		      "  swapin_time=$newtime ".
		      "where idx=$thisidx");
		MyDBQ("update $RESOURCES set ".
		      "  swapmod_time=$newtime ".
		      "where idx=$lastidx");
		next;
	    }
	}
	#
	# Check for a failure that left crud in the resource record.
	#
	$stats_result =
	    DBQueryFatal("select UNIX_TIMESTAMP(start_time) ".
			 "  from $TSTATS ".
			 "where rsrcidx=$thisidx and ".
			 "      ((action='start' and exitcode!=0) or  ".
			 "       (action='preload' and exitcode=0))");
	if ($stats_result->numrows) {
	    print "$exptidx (rsrc:$thisidx); ".
		"setting node counts to zero cause startexp failed\n"
		if ($verbose);
	    MyDBQ("update $RESOURCES set ".
		  "  pnodes=0,vnodes=0,jailnodes=0,delaynodes=0,plabnodes=0 ".
		  "where idx=$thisidx");
	    next;
	}

	#
	# Look for a testbed stats record with a swapin/swapout
	#
256
	my $outtime;
257 258 259 260 261 262 263 264 265 266 267 268
	if (!$byswapmod) {
	    $stats_result =
		DBQueryFatal("select action,UNIX_TIMESTAMP(end_time) ".
			     "  from $TSTATS ".
			     "where rsrcidx=$thisidx and exitcode=0 and ".
			     "      (action='swapin' or action='swapout') ".
			     "order by end_time");
	    if ($stats_result->numrows == 2) {
		my ($action1,$time1) = $stats_result->fetchrow_array();
		my ($action2,$time2) = $stats_result->fetchrow_array();

		if ($action1 eq "swapin" and $time2 > $time1) {
269
		    my $d = $time2 - $time1;
270
		    print "$exptidx (rsrc:$thisidx); ".
271
			"setting swapin/swapout to $time1/$time2 (duration $d)\n"
272 273
			if ($verbose);
		    
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		    if ($d > $maxduration) {
			print "*** fixup would exceed max duration, skipped\n";
		    } else {
			MyDBQ("update $RESOURCES set ".
			      "  swapin_time=$time1,swapout_time=$time2 ".
			      "where idx=$thisidx");
			next;
		    }
		}
	    }

	    #
	    # Choose a reasonable swapin from the experiments stats record,
	    # but only if the current resource record is the actual last
	    # record and we have a swapout time to sanity check with.
	    #
	    $outtime = $swapout_time;
	    $outtime = $swapout_last if (!defined($outtime));
	    $outtime = $destroyed if (!defined($outtime));
	    if ($thisidx == $rsrcidx && $outtime && defined($created)) {
		if (defined($swapin_last) && $swapin_last <= $outtime) {
		    my $d = $outtime - $swapin_last;
		    print "$exptidx (rsrc:$thisidx) created at $created; ".
			"setting swapin to $swapin_last (duration $d)\n"
			if ($verbose);

		    if ($d > $maxduration) {
			print "*** fixup would exceed max duration, skipped\n";
		    } else {
			MyDBQ("update $RESOURCES set ".
			      "  swapin_time=$swapin_last ".
			      "where idx=$thisidx");
			next;
		    }
		}
		if ($created <= $outtime) {
		    my $d = $outtime - $created;
		    print "$exptidx (rsrc:$thisidx) created at $created; ".
			"setting swapin to $created (duration $d)\n"
			if ($verbose);

		    if ($d > $maxduration) {
			print "*** fixup would exceed max duration, skipped\n";
		    } else {
			MyDBQ("update $RESOURCES set ".
			      "  swapin_time=$created ".
			      "where idx=$thisidx");
			next;
		    }
323 324 325 326 327 328 329
		}
	    }
	}

	#
	# If we get here, we did not know how to fix it or ignore it.
	#
330 331 332 333 334 335 336
	my $v0 = $byswapmod;
	my $v1 = (defined($outtime) ? $outtime : "NULL");
	my $v2 = (defined($swapin_last) ? $swapin_last : "NULL");
	my $v3 = (defined($created) ? $created : "NULL");

	print "*** HELP: $exptidx (rsrc:$thisidx); p:$pnodes, v:$vnodes, ";
	print "byswapmod:$v0, outtime:$v1, swapin_last:$v2, created:$v3\n";
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    }
}

$query_result =
    DBQueryFatal("select r.*,s.rsrcidx,e.state, ".
		 "    UNIX_TIMESTAMP(s.destroyed) as destroyed, ".
		 "    UNIX_TIMESTAMP(s.swapout_last) as swapout_last ".
		 "  from $RESOURCES as r ".
		 "left join $ESTATS as s on s.exptidx=r.exptidx ".
		 "left join experiments as e on e.idx=r.exptidx ".
		 "order by s.exptidx,UNIX_TIMESTAMP(r.tstamp)");

while (my $row = $query_result->fetchrow_hashref()) {
    my $exptidx      = $row->{'exptidx'};
    my $swapin_time  = $row->{'swapin_time'};
    my $swapmod_time = $row->{'swapmod_time'};
    my $swapout_time = $row->{'swapout_time'};
    my $pnodes       = $row->{'pnodes'};
    my $vnodes       = $row->{'vnodes'};
    my $state        = $row->{'state'};
    my $thisidx      = $row->{'idx'};
    my $rsrcidx      = $row->{'rsrcidx'};
    my $lastidx      = $row->{'lastidx'};
    my $destroyed    = $row->{'destroyed'};
    my $swapout_last = $row->{'swapout_last'};
    my $byswapmod    = $row->{'byswapmod'};

    if ($swapin_time && !($swapout_time || $swapmod_time)) {
365 366
	my $tsswapout;

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	if (defined($state) && $state eq "active" && $thisidx == $rsrcidx) {
	    print "$exptidx (rsrc:$rsrcidx) is active; p:$pnodes, v:$vnodes\n"
		if ($verbose);
	    next;
	}
	#
	# If there are no pnodes or vnodes, just clear the swapin time.
	#
	if (! ($pnodes || $vnodes)) {
	    print "$exptidx (rsrc:$thisidx) no pnodes, clearing swapin.\n"
		if ($verbose);
	    
	    MyDBQ("update $RESOURCES set ".
		  " swapin_time=0 ".
		  "where idx=$thisidx");
	    next;
	}

	#
	# No recorded swapout, look in testbed_stats to see if we have
	# something there.
	#
	if ($pnodes || $vnodes) {
	    my $stats_result =
		DBQueryFatal("select UNIX_TIMESTAMP(start_time) ".
			     "  from $TSTATS ".
			     "where rsrcidx=$thisidx and action='swapout'");
	    if ($stats_result->numrows) {
395 396 397
		($tsswapout) = $stats_result->fetchrow_array();
		if (defined($tsswapout)) {
		    my $d = $tsswapout - $swapin_time;
398
		    print "$exptidx (rsrc:$thisidx); ".
399
			"setting swapout to $tsswapout (duration $d) from testbed_stats\n"
400 401
			if ($verbose);

402 403 404 405 406 407 408 409
		    if ($d > $maxduration) {
			print "*** fixup would exceed max duration, skipped\n";
		    } else {
			MyDBQ("update $RESOURCES set ".
			      "  swapout_time=$tsswapout ".
			      "where idx=$thisidx");
			next;
		    }
410 411 412 413 414 415 416 417 418 419 420 421
		}
	    }
	}

	#
	# No recorded swapout time but there are nodes, so choose a
	# reasonable swapout from the experiments stats record, but only
	# if the current resource record is the actual last record.
	#
	if (($pnodes || $vnodes) && $thisidx == $rsrcidx &&
	    defined($destroyed)) {
	    if (defined($swapout_last) && $swapout_last > $swapin_time) {
422
		my $d = $swapout_last - $swapin_time;
423
		print "$exptidx (rsrc:$thisidx) destroyed at $destroyed; ".
424
		    "setting swapout to $swapout_last (duration $d)\n"
425 426
		    if ($verbose);

427 428 429 430 431 432 433 434
		if ($d > $maxduration) {
		    print "*** fixup would exceed max duration, skipped\n";
		} else {
		    MyDBQ("update $RESOURCES set ".
			  "  swapout_time=$swapout_last ".
			  "where idx=$thisidx");
		    next;
		}
435
	    }
436 437
	    if ($destroyed > $swapin_time) {
		my $d = $destroyed - $swapin_time;
438
		print "$exptidx (rsrc:$thisidx) destroyed at $destroyed; ".
439
		    "setting swapout to $destroyed (duration $d)\n"
440 441
		    if ($verbose);

442 443 444 445 446 447 448 449
		if ($d > $maxduration) {
		    print "*** fixup would exceed max duration, skipped\n";
		} else {
		    MyDBQ("update $RESOURCES set ".
			  "  swapout_time=$destroyed ".
			  "where idx=$thisidx");
		    next;
		}
450 451 452 453 454 455
	    }
	}

	#
	# If we get here, we did not know how to fix it or ignore it.
	#
456 457 458 459 460 461 462
	my $v0 = $swapin_time;
	my $v1 = (defined($tsswapout) ? $tsswapout : "NULL");
	my $v2 = (defined($swapout_last) ? $swapout_last : "NULL");
	my $v3 = (defined($destroyed) ? $destroyed : "NULL");

	print "*** HELP: $exptidx (rsrc:$thisidx); p:$pnodes, v:$vnodes, ";
	print "swapin:$v0, swapout:$v1, swapout_last:$v2, destroyed:$v3\n";
463 464 465 466
    }
}

Regenerate();
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

if ($restore) {
    DBQueryFatal("rename table ".
		 "   experiment_stats to experiment_stats_save, ".
		 "   experiment_resources to experiment_resources_save, ".
		 "   project_stats to project_stats_save, ".
		 "   group_stats to group_stats_save, ".
		 "   user_stats to user_stats_save");
    
    DBQueryFatal("rename table ".
		 "   experiment_stats_backup to experiment_stats, ".
		 "   experiment_resources_backup to experiment_resources, ".
		 "   project_stats_backup to project_stats, ".
		 "   group_stats_backup to group_stats, ".
		 "   user_stats_backup to user_stats");
}
483 484 485 486 487 488 489 490 491 492 493
exit(0);

#
# This regenerates the epoch stats from the resources tables once they
# have been scanned and corrected.
#
sub Regenerate()
{
    print "Checking user/group stats tables for inconsistencies ... \n";
    
    my $query_result =
494 495 496 497 498 499 500 501 502
	DBQueryFatal("select * from $ESTATS");
    while (my $row = $query_result->fetchrow_hashref()) {
	my $exptidx = $row->{'exptidx'};
	
	$experiment_stats{"$exptidx"} = {'swapin_duration' => 0,
					 'oldstats' => $row};
    }
    
    $query_result =
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
	DBQueryFatal("select * from $USTATS");
    while (my $row = $query_result->fetchrow_hashref()) {
	my $uid_idx = $row->{'uid_idx'};
	
	$user_stats{"$uid_idx"} = {'allexpt_duration' => 0,
				   'allexpt_vnodes'   => 0,
				   'allexpt_vnode_duration' => 0,
				   'allexpt_pnodes'   => 0,
				   'allexpt_pnode_duration' => 0,
				   'oldstats' => $row};
    }
    
    $query_result =
	DBQueryFatal("select * from $GSTATS");
    while (my $row = $query_result->fetchrow_hashref()) {
	my $gid_idx = $row->{'gid_idx'};
	
	$group_stats{"$gid_idx"} = {'allexpt_duration' => 0,
				    'allexpt_vnodes'   => 0,
				    'allexpt_vnode_duration' => 0,
				    'allexpt_pnodes'   => 0,
				    'allexpt_pnode_duration' => 0,
				    'oldstats' => $row};
    }

    $query_result =
	DBQueryFatal("select s.exptidx,s.gid_idx,u.uid_idx,r.pnodes,r.vnodes,".
		     "   swapin_time,swapout_time,swapmod_time,byswapmod, ".
531 532
		     "   e.state,r.idx,r.lastidx,byswapin, ".
		     "   s.pid_idx,s.pid,s.gid,s.eid ".
533 534 535 536 537 538 539 540 541 542
		     " from $RESOURCES as r ".
		     "left join $ESTATS as s on ".
		     "     r.exptidx=s.exptidx ".
		     "left join experiments as e on e.idx=s.exptidx ".
		     "left join users as u on u.uid_idx=r.uid_idx ".
		     "order by s.exptidx,UNIX_TIMESTAMP(r.tstamp)");


    while (my $row = $query_result->fetchrow_hashref()) {
	my $exptidx      = $row->{"exptidx"};
543
	my $pid_idx      = $row->{"pid_idx"};
544
	my $gid_idx      = $row->{"gid_idx"};
545 546 547
	my $pid          = $row->{"pid"};
	my $gid          = $row->{"gid"};
	my $eid          = $row->{"eid"};
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	my $uid_idx      = $row->{"uid_idx"};
	my $pnodes       = $row->{"pnodes"};
	my $vnodes       = $row->{"vnodes"};
	my $swapin_time  = $row->{"swapin_time"};
	my $swapout_time = $row->{"swapout_time"};
	my $swapmod_time = $row->{"swapmod_time"};
	my $byswapmod    = $row->{"byswapmod"};
	my $byswapin     = $row->{"byswapin"};
	my $state        = $row->{"state"};
	my $rsrcidx      = $row->{"idx"};
	my $lastidx      = $row->{"lastidx"};
	my $swapseconds  = 0;
	my $begin        = 0;
	my $end          = 0;

	# Skip if no resources we care about.
564 565 566 567 568 569
	if (! ($pnodes || $vnodes)) {
	    print "$exptidx: skipping resource record $rsrcidx; ".
		"no pnodes or vnodes\n"
		if ($verbose);
	    next;
	}
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

	#
	# If no swapin for the record skip it, but not supposed to happen.
	#
	if ($swapin_time == 0) {
	    print "$exptidx: skipping resource record $rsrcidx; ".
		"no swapin time set\n"
		if ($verbose);
	    next;
	}

	# Ditto no swapout or swapmod and not active.
	if ($swapout_time == 0 && $swapmod_time == 0) {
	    next
		if (defined($state) and $state eq "active");
	    
	    print "$exptidx: skipping resource record $rsrcidx; ".
		"no swapout/swapmod time set\n"
		if ($verbose);
	    next;
	}
	elsif ($swapout_time) {
	    $swapseconds = $swapout_time - $swapin_time;
	}
	else {
	    $swapseconds = $swapmod_time - $swapin_time;
	}

	if ($swapseconds < 0) {
	    print "$exptidx: skipping resource record $rsrcidx; ".
		"swapseconds is negative\n"
		if ($verbose);
	    next;
	}
604 605 606 607
	if (exists($experiment_stats{$exptidx})) {
	    my $record = $experiment_stats{"$exptidx"};
	    $record->{'swapin_duration'} += $swapseconds;
	}
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

	if (defined($uid_idx) && $uid_idx) {
	    my $record = $user_stats{"$uid_idx"};

	    if ($uid_idx == 12 || $uid_idx == 1182) {
		print "$exptidx: $rsrcidx, s:$swapseconds, p:$pnodes, v:$vnodes\n";
	    }

	    $record->{'allexpt_duration'} += $swapseconds;
	    $record->{'allexpt_vnodes'} += $vnodes;
	    $record->{'allexpt_pnodes'} += $pnodes;
	    $record->{'allexpt_vnode_duration'} += ($vnodes * $swapseconds);
	    $record->{'allexpt_pnode_duration'} += ($pnodes * $swapseconds);
	}
	if (defined($gid_idx) && $gid_idx) {
	    my $record = $group_stats{"$gid_idx"};

	    $record->{'allexpt_duration'} += $swapseconds;
	    $record->{'allexpt_vnodes'} += $vnodes;
	    $record->{'allexpt_pnodes'} += $pnodes;
	    $record->{'allexpt_vnode_duration'} += ($vnodes * $swapseconds);
	    $record->{'allexpt_pnode_duration'} += ($pnodes * $swapseconds);
630 631 632 633 634 635 636 637 638 639

	    if ($pid_idx != $gid_idx) {
		my $record = $group_stats{"$pid_idx"};

		$record->{'allexpt_duration'} += $swapseconds;
		$record->{'allexpt_vnodes'} += $vnodes;
		$record->{'allexpt_pnodes'} += $pnodes;
		$record->{'allexpt_vnode_duration'} += ($vnodes * $swapseconds);
		$record->{'allexpt_pnode_duration'} += ($pnodes * $swapseconds);
	    }
640 641
	}
    }
642 643 644
    foreach my $exptidx (keys(%experiment_stats)) {
	my $record = $experiment_stats{"$exptidx"};
	my $pid    = $record->{"oldstats"}->{'pid'};
645
	my $gid    = $record->{"oldstats"}->{'gid'};
646 647
	my $eid    = $record->{"oldstats"}->{'eid'};

648
	print "experiment: $pid/$gid/$eid/$exptidx: ".
649 650 651 652 653 654 655 656 657 658
	    "duration: " . $record->{'swapin_duration'} . "\n"
	    if ($verbose > 2);

	# Warn of any changes.
	if (CheckStatConsistency($record) != 0) {
	    print "experiment $pid/$eid/$exptidx: Mismatch in stats records\n";
	    FixStat("experiment_stats", $record);
	}
    }
    
659 660
    foreach my $uid_idx (keys(%user_stats)) {
	my $record = $user_stats{"$uid_idx"};
661
	my $uid    = $record->{'oldstats'}->{'uid'};
662

663
	print "user: $uid/$uid_idx: ".
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	    "duration: " . $record->{'allexpt_duration'} . ", ".
	    "pnodes: "   . $record->{'allexpt_pnodes'}   . ", ".
	    "vnodes: "   . $record->{'allexpt_vnodes'}   . ", ".
	    "ptotal: "   . $record->{'allexpt_pnode_duration'} . ", ".
	    "vtotal: "   . $record->{'allexpt_vnode_duration'} . "\n"
	    if ($verbose > 2);

	# Warn of any changes.
	if (CheckStatConsistency($record) != 0) {
	    print "user $uid_idx: Mismatch in stats records\n";
	    FixStat("user_stats", $record);
	}
    }
    foreach my $gid_idx (keys(%group_stats)) {
	my $record   = $group_stats{"$gid_idx"};
	my $oldstats = $record->{'oldstats'};
680 681
	my $pid      = $oldstats->{'pid'};
	my $gid      = $oldstats->{'gid'};
682

683
	print "group: $pid/$gid/$gid_idx: ".
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	    "duration: " . $record->{'allexpt_duration'} . ", ".
	    "pnodes: "   . $record->{'allexpt_pnodes'}   . ", ".
	    "vnodes: "   . $record->{'allexpt_vnodes'}   . ", ".
	    "ptotal: "   . $record->{'allexpt_pnode_duration'} . ", ".
	    "vtotal: "   . $record->{'allexpt_vnode_duration'} . "\n"
	    if ($verbose > 2);

	# Warn of any changes.
	if (CheckStatConsistency($record) != 0) {
	    if ($oldstats->{'pid_idx'} == $oldstats->{'gid_idx'}) {
		print "project $gid_idx: Mismatch in stats records\n";
		FixStat("project_stats", $record)
	    }
	    else {
		print "group $gid_idx: Mismatch in stats records\n";
	    }
	    FixStat("group_stats", $record);
	}
    }
}

sub CheckStatConsistency($)
{
    my ($record) = @_;
    my $oldstats = $record->{'oldstats'};
    my $rval     = 0;

    foreach my $key (keys(%{ $record })) {
	next
	    if ($key eq "oldstats");

	return -1
	    if ($record->{$key} != $oldstats->{$key});
    }
    return 0;
}

sub FixStat($$)
{
    my ($which, $record) = @_;
    my $oldstats = $record->{'oldstats'};
    my $sets;

    foreach my $key (keys(%{ $record })) {
	next
	    if ($key eq "oldstats");

	if ($record->{$key} != $oldstats->{$key}) {
	    my $set = "$key=" . $record->{$key};
		
	    if (defined($sets)) {
		$sets .= ", $set";
	    }
	    else {
		$sets = $set;
	    }
	}
    }
    return
	if (!defined($sets));

    my $query = "update ";
    if ($which eq "user_stats") {
	$query .= "$USTATS set $sets where uid_idx=" . $oldstats->{'uid_idx'};
    }
    elsif ($which eq "group_stats") {
	$query .= "$GSTATS set $sets where gid_idx=" . $oldstats->{'gid_idx'};
    }
    elsif ($which eq "project_stats") {
	$query .= "$PSTATS set $sets where pid_idx=". $oldstats->{'pid_idx'};
    }
755 756 757
    elsif ($which eq "experiment_stats") {
	$query .= "$ESTATS set $sets where exptidx=". $oldstats->{'exptidx'};
    }
758 759 760 761
    
    MyDBQ($query);
    return 0;
}