#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use CondorTest;
use CondorUtils;
use Check::SimpleJob;
#use Check::CondorLog;
use Check::ProcInfo;
use File::Basename;
use File::Spec;
use File::Copy;

my $configfile = "";
my $ClusterId = 0;
my $testname =  "lib_param_conditionals";


my $firstappend_condor_config = '
';

$configfile = CondorTest::CreateLocalConfig($firstappend_condor_config,"name1");

my $condorinfo =CondorTest::StartCondorWithParams(
	condor_name => "name1",
	fresh_local => "TRUE",
	config_minimal => "TRUE",
	do_not_start => "TRUE",
	condorlocalsrc => "$configfile",
);

my $unit_test = "x_conditional_params.exe";

if(!(-f "$unit_test")) {
	die "Needed unit_test <$unit_test> not available\n";
}

# HACK! dump a memory usage snapshot, this is a convenient place for this because
# the unit test exe is pretty basic, but it does link to libcondorutils.so
#
print("HACK: running $unit_test -memory-shapshot basic_condorutils_mem.smaps\n");
system("$unit_test -memory-snapshot basic_condorutils_mem.smaps");
#if (-f "basic_condorutils_mem.smaps") { smap_sum_verbose("basic_condorutils_mem.smaps"); }

my $test_status = 0;
$test_status = system("$unit_test -v");
if($test_status != 0) {
	die "failure: $unit_test returned $test_status\n";
}

#FIXME: write this part of the test.
#print "checking if/elif/else nesting using condor_config_val\n";

CondorTest::RegisterResult(1,{test_name => $testname});

CondorTest::EndTest();


# scan /proc/<PID>/smaps and add up dirty private pages.
#
sub smap_sum_verbose {

   my $file = shift;

   my $lib = ""; my $perm; my $inode; my $exe;
   #my $fd; my $addr1; my $addr2;
   my %tot;
   $tot{Heap} = 0;
   $tot{Stack} = 0;
   $tot{LibData} = 0;

   open (SMAP, "$file");
   while (<SMAP>) {
      print $_;
      if ($_ =~ /^([0-9A-Fa-f]+)-([0-9A-Fa-f]+)\s+(r[w-][x-]p)\s+(\S+\s+\S+)\s+(.*)$/) {

         $perm = $3; # perm
         $inode = $5; $lib=""; if ($inode =~ /^(\S+)\s+(.*)$/) { $inode = $1; $lib = $2; }
         #$addr1 = $1; $addr2 = $2; $fd = $4;

         if ( ! ($lib =~ /\.so/) && $lib =~ /condor_/) { $exe = $lib; }
         #
      } elsif ($_ =~ /^(\w+):\s+(\d+)/) {

         my $n = $2;
         $tot{$1} += $n;
         if ($1 =~ /Private_Dirty/) { 
            if ($perm eq "r--p") { $tot{LibRO} += $n; }
            if ($lib eq "") { 
               $tot{Anon} += $n;
            } else {
               if ($lib eq "[heap]") { $tot{Heap} += $n; }
               elsif ($lib eq "[stack]") { $tot{Stack} += $n; }
               elsif ($perm eq "rw-p") { $tot{LibData} += $n; }
               
               if ($lib =~ /condor_util/) { $tot{Utils} += $n; }
               elsif ($lib =~ /condor_/)  { $tot{Exec} += $n; }
               elsif ($lib =~ /condor/)   { 
                  if ($lib =~ /globus|voms/) { $tot{Globus} += $n; }
                  elsif ($lib =~ /classad/)  { $tot{Classad} += $n; }
                  else                       { $tot{Condor} += $n; }
               } elsif ($lib =~ /lib/) { $tot{System} += $n; }
            }
         }
      }
   } #while

   my $lbl = $pid;
   if ($exe) { 
      $exe =~ s/.*\///g;
      $lbl = "$pid:$exe";
   }

   print 'PID:Process            Total Dynamic =(Heap+Stack+Anon) LibRO LibData =(Exec+Utils+Condor+Clasad+Globus+System) Clean' . "\n";
   printf "%-22.22s %5d %7d %6d %5d %5d %5d %7d %6d %5d %6d %6d %6d %6d %6d\n", $lbl,
          $tot{Private_Dirty}, $tot{Heap}+$tot{Stack}+$tot{Anon}, $tot{Heap}, $tot{Stack}, $tot{Anon},
          $tot{LibRO}, $tot{LibData}, $tot{Exec}, $tot{Utils}, $tot{Other}, $tot{Classad}, $tot{Globus}, $tot{System},
          $tot{Private_Clean};
}
