Add new udafs and RMR support to gsprintconsole_ves
[com/gs-lite.git] / bin / monitor_gs.pl
1 #! /usr/bin/perl
2
3 # ------------------------------------------------
4 #   Copyright 2014 AT&T Intellectual Property
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # -------------------------------------------
17
18 use English;
19
20 $interval = 10;
21 $to_tty = 0;
22 if(scalar(@ARGV)>0){
23         if(int($ARGV[0])>0){
24                 $interval = int($ARGV[0]);
25         }else{
26                 if($ARGV[0] eq '-'){
27                         $to_tty = 1;
28                 }else{
29                         print STDERR "Argument 1 ($ARGV[0]) ignored.\n";
30                 }       
31         }
32 }
33 if(scalar(@ARGV)>1){
34         if(int($ARGV[1])>0){
35                 $interval = int($ARGV[1]);
36         }else{
37                 if($ARGV[1] eq '-'){
38                         $to_tty = 1;
39                 }else{
40                         print STDERR "Argument 2 ($ARGV[1]) ignored.\n";
41                 }       
42         }
43 }
44 if(scalar(@ARGV)>2){
45         print STDERR "Only 2 arguments accepted, additional args ignored.\n";
46 }
47
48 open O, ">resource_log.csv" or die "Can't open resource_log.csv\n";
49
50
51
52 # get the page size
53 open I,"getconf PAGESIZE |";
54 $line=<I>;
55 chomp($line);
56 $pagesize = int($line);
57 close(I);
58
59
60 $effective_uid = $REAL_USER_ID;
61 $ps_cmd = "ps -U $effective_uid |";
62
63 %hfta_pid=();
64
65 open P,$ps_cmd;
66 while($line=<P>){
67         chomp($line);
68         $line =~ s/^\s+//;
69         @flds = split /\s+/,$line;
70         if($flds[3] =~ /rts/){
71                 push @rts_pids,int($flds[0]);
72         }
73         if($flds[3] =~ /hfta_(\d+)/){
74                 $hfta_pid{$1} = int($flds[0]);
75         }
76 }
77
78 $rts_pids = sort {$a<=>$b} @rts_pids;
79
80 $now=time();
81 print O "START monitoring at time $now\n";
82 print O "ts,proc,pid,utime,stime,vm_size,rss,pagesize\n";
83 if(to_tty){
84         print "START monitoring at time $now\n";
85         print "ts,proc,pid,utime,stime,vm_size,rss,pagesize\n";
86 }
87 #print "RTS PIDs:\n";
88 #foreach $p (@rts_pids){
89 #       print "$p ";
90 #}
91 #print "\nHFTA PIDs:\n";
92 #  foreach $h (sort {$a<=>$b} keys(%hfta_pid)){
93 #       print "($h,",$hfta_pid{$h},") ";
94 #}
95 #print "\n";
96
97 while(1){
98         $now=time();
99         foreach $p (@rts_pids){
100                 get_proc_stats($p);
101                 print O "$now,rts,$p,$user_time,$sys_time,$vm_size,$resident_set_size,$pagesize\n";
102                 if(to_tty){
103                         print "$now,rts,$p,$user_time,$sys_time,$vm_size,$resident_set_size,$pagesize\n";
104                 }
105         }
106         foreach $h (sort {$a<=>$b} keys(%hfta_pid)){
107                 $p=$hfta_pid{$h};
108                 get_proc_stats($p);
109                 print O "$now,hfta_$h,$p,$user_time,$sys_time,$vm_size,$resident_set_size,$pagesize\n";
110                 if(to_tty){
111                         print "$now,hfta_$h,$p,$user_time,$sys_time,$vm_size,$resident_set_size,$pagesize\n";
112                 }
113         }
114
115         sleep($interval);
116 }
117
118
119
120 sub get_proc_stats{
121 my $pid = shift;
122 my $line;
123 my @flds;
124 my $pos;
125
126
127 open I_PROC_STATS,"/proc/$pid/stat";
128
129 $line=<I_PROC_STATS>;
130 close I_PROC_STATS;
131
132 #print "getstats($pid): $line\n";
133
134 chomp($line);
135 @flds = split / +/,$line;
136
137 $pos=0;
138 $pid = $flds[$pos]; $pos++;
139 $fname = $flds[$pos]; $pos++;
140 $state = $flds[$pos]; $pos++;
141 $pppid = $flds[$pos]; $pos++;
142 $pgrp = $flds[$pos]; $pos++;
143 $session = $flds[$pos]; $pos++;
144 $tty_n = $flds[$pos]; $pos++;
145 $tpgid = $flds[$pos]; $pos++;
146 $flags = $flds[$pos]; $pos++;
147 $minor_faults = $flds[$pos]; $pos++;
148 $child_minor_faults = $flds[$pos]; $pos++;
149 $major_faults = $flds[$pos0]; $pos++;
150 $child_major_faults = $flds[$pos0]; $pos++;
151 $user_time = $flds[$pos]; $pos++;
152 $sys_time = $flds[$pos]; $pos++;
153 $child_user_time = $flds[$pos]; $pos++;
154 $child_sys_time = $flds[$pos]; $pos++;
155 $priority = $flds[$pos]; $pos++;
156 $nice = $flds[$pos]; $pos++;
157 $num_threads = $flds[$pos]; $pos++;
158 $interval_timer_realvalue = $flds[$pos]; $pos++;
159 $start_time = $flds[$pos]; $pos++;
160 $vm_size = $flds[$pos]; $pos++;
161 $resident_set_size = $flds[$pos]; $pos++;
162 $rss_limit = $flds[$pos]; $pos++;
163 $startcode = $flds[$pos]; $pos++;
164 $endcode = $flds[$pos]; $pos++;
165 $startstack = $flds[$pos]; $pos++;
166 $curr_kernel_stack_ptr = $flds[$pos]; $pos++;
167 $curr_instruction_ptr = $flds[$pos]; $pos++;
168 $signal = $flds[$pos]; $pos++;
169 $sigblocked = $flds[$pos]; $pos++;
170 $sigignore = $flds[$pos]; $pos++;
171 $wchan = $flds[$pos]; $pos++;
172 $nswap = $flds[$pos]; $pos++;
173 $exit_signal = $flds[$pos]; $pos++;
174 $processor = $flds[$pos]; $pos++;
175 $rt_priority = $flds[$pos]; $pos++;
176 $policy = $flds[$pos]; $pos++;
177 $aggr_block_io_delays = $flds[$pos]; $pos++;
178 $guest_time = $flds[$pos]; $pos++;
179 $cguest_time = $flds[$pos]; $pos++;
180 }