1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 |
|
---|
6 | print '===== Analyze poll-gsdl.pl Output =====' . "\n";
|
---|
7 |
|
---|
8 | if (!defined($ARGV[0]) || !-f 'memory-' . $ARGV[0] . '.log')
|
---|
9 | {
|
---|
10 | print 'Error! Memory report not specified or file not found.' . "\n";
|
---|
11 | print 'Usage: poll-report.pl <pid>' . "\n\n";
|
---|
12 | exit(0);
|
---|
13 | }
|
---|
14 |
|
---|
15 | my $polling_pid = 0;
|
---|
16 | if ($ARGV[0] =~ /^(\d+)$/)
|
---|
17 | {
|
---|
18 | $polling_pid = $1;
|
---|
19 | }
|
---|
20 | else
|
---|
21 | {
|
---|
22 | print 'Error! Memory report filename not valid.' . "\n";
|
---|
23 | print 'Usage: memreport.pl <pid>' . "\n\n";
|
---|
24 | exit(0);
|
---|
25 | }
|
---|
26 |
|
---|
27 | open(FIN, '<:utf8', 'memory-' . $polling_pid . '.log') or die('Error! Failed to open file for reading: memory-' . $ARGV[0] . '.log');
|
---|
28 |
|
---|
29 | # We'll write out the results to a CSV for easy graphing!
|
---|
30 | my $filename = 'memory-' . $polling_pid . '.csv';
|
---|
31 | open(FOUT, '>:utf8', $filename) or die('Error! Failed to open file for writing: ' . $filename);
|
---|
32 | print FOUT "Time,Type,CPU,PCPU,PMEM,Type,CPU,PCPU,PMEM\n";
|
---|
33 |
|
---|
34 | # We'll store the details for both interesting child processes of Greenstone
|
---|
35 | # importing, namely import.pl and txt2tdb.
|
---|
36 | my $starttime = 0;
|
---|
37 | my $timestamp = -1;
|
---|
38 | my $import_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
|
---|
39 | my $tdb_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
|
---|
40 | my $line = '';
|
---|
41 | print 'Parsing log: ';
|
---|
42 | while ($line = <FIN>)
|
---|
43 | {
|
---|
44 | # Look for a timestamp to begin a polling entry
|
---|
45 | if ($line =~ /\[(\d+.\d+)\]/)
|
---|
46 | {
|
---|
47 | # Write any existing data to file
|
---|
48 | if ($import_data->{'cpu'} > -1)
|
---|
49 | {
|
---|
50 | &printRecord($timestamp, $import_data, $tdb_data);
|
---|
51 | }
|
---|
52 | if ($starttime == 0)
|
---|
53 | {
|
---|
54 | $starttime = $1;
|
---|
55 | }
|
---|
56 | $timestamp = $1 - $starttime;
|
---|
57 | print 'T';
|
---|
58 | # Reset the data
|
---|
59 | $import_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
|
---|
60 | $tdb_data = {'cpu'=>-1,'pcpu'=>0.0, 'pmem'=>0.0};
|
---|
61 | }
|
---|
62 | # We only process other lines if we are in an entry
|
---|
63 | # - lines of the form: <pid> <cpu> <%cpu> <%mem> <cmd>
|
---|
64 | elsif ($timestamp >= 0 && $line =~ /^\s*(\d+)\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(.*)\r?\n$/)
|
---|
65 | {
|
---|
66 | my $pid = $1;
|
---|
67 | my $cpu = $2;
|
---|
68 | my $pcpu = $3;
|
---|
69 | my $pmem = $4;
|
---|
70 | my $cmd = $5;
|
---|
71 |
|
---|
72 | # There are two processes we are interested in:
|
---|
73 | # 1. The import process itself
|
---|
74 | if ($cmd =~ /^\/usr\/bin\/perl\s+.*import\.pl.+/)
|
---|
75 | {
|
---|
76 | print '.';
|
---|
77 | $import_data->{'cpu'} = $cpu;
|
---|
78 | $import_data->{'pcpu'} += $pcpu;
|
---|
79 | $import_data->{'pmem'} += $pmem;
|
---|
80 | }
|
---|
81 | # 2. The TDB process
|
---|
82 | elsif ($cmd =~ /^txt2tdb/)
|
---|
83 | {
|
---|
84 | print '.';
|
---|
85 | $tdb_data->{'cpu'} = $cpu;
|
---|
86 | $tdb_data->{'pcpu'} += $pcpu;
|
---|
87 | $tdb_data->{'pmem'} += $pmem;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | print "\n";
|
---|
92 | # Write any remaining data to file
|
---|
93 | if ($import_data->{'cpu'} > -1)
|
---|
94 | {
|
---|
95 | &printRecord($timestamp, $import_data, $tdb_data);
|
---|
96 | }
|
---|
97 |
|
---|
98 | close(FOUT);
|
---|
99 | close(FIN);
|
---|
100 |
|
---|
101 | print '===== Complete =====' . "\n";
|
---|
102 | exit(0);
|
---|
103 |
|
---|
104 | # /**
|
---|
105 | # */
|
---|
106 | sub printRecord
|
---|
107 | {
|
---|
108 | my ($timestamp, $import_data, $tdb_data) = @_;
|
---|
109 | print FOUT sprintf('% 5.1f', $timestamp) . ',';
|
---|
110 | print FOUT 'IMPORT,';
|
---|
111 | print FOUT sprintf('% 2d', $import_data->{'cpu'}) . ',';
|
---|
112 | print FOUT sprintf('% 5.1f', $import_data->{'pcpu'}) . ',';
|
---|
113 | print FOUT sprintf('% 5.1f', $import_data->{'pmem'}) . ',';
|
---|
114 | print FOUT 'TDB,';
|
---|
115 | print FOUT sprintf('% 2d', $tdb_data->{'cpu'}) . ',';
|
---|
116 | print FOUT sprintf('% 5.1f', $tdb_data->{'pcpu'}) . ',';
|
---|
117 | print FOUT sprintf('% 5.1f', $tdb_data->{'pmem'}) . "\n";
|
---|
118 |
|
---|
119 | }
|
---|
120 | # /** printRecord() **/
|
---|