1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 |
|
---|
6 | BEGIN
|
---|
7 | {
|
---|
8 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
9 | }
|
---|
10 |
|
---|
11 | # For WNOHANG flag
|
---|
12 | use POSIX ":sys_wait_h";
|
---|
13 | # For intervals less than 1 second
|
---|
14 | use Time::HiRes qw(usleep gettimeofday);
|
---|
15 |
|
---|
16 | # Pragma to autoflush STDOUT
|
---|
17 | $| = 1;
|
---|
18 |
|
---|
19 | if (!defined $ARGV[0])
|
---|
20 | {
|
---|
21 | &printUsage();
|
---|
22 | }
|
---|
23 | my $collection = $ARGV[0];
|
---|
24 | my $threads = 1;
|
---|
25 | if (defined $ARGV[1])
|
---|
26 | {
|
---|
27 | if ($ARGV[1] =~ /^\d+$/)
|
---|
28 | {
|
---|
29 | $threads = $ARGV[1];
|
---|
30 | }
|
---|
31 | else
|
---|
32 | {
|
---|
33 | &printUsage();
|
---|
34 | }
|
---|
35 | }
|
---|
36 | my $epoc = 100;
|
---|
37 | if (defined $ARGV[2])
|
---|
38 | {
|
---|
39 | if ($ARGV[2] =~ /^\d+$/)
|
---|
40 | {
|
---|
41 | $epoc = $ARGV[2];
|
---|
42 | }
|
---|
43 | else
|
---|
44 | {
|
---|
45 | &printUsage();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | # Remove the current archives directory
|
---|
50 | `rm_archives.pl $collection`;
|
---|
51 |
|
---|
52 | # Flush the memory disk cache
|
---|
53 | # - save our current default editor
|
---|
54 | my $current_editor = $ENV{'EDITOR'};
|
---|
55 | # - replace default editor with a script that simply clobbers the contents
|
---|
56 | # of any file it's handed with the number "3"
|
---|
57 | $ENV{'EDITOR'} = 'reset_memcache_editor.sh';
|
---|
58 | # - we now call sudoedit on the system file. How sudoedit works is that it
|
---|
59 | # starts by making a temp copy of the system file with appropriate
|
---|
60 | # permissions allowing the user to edit. It then passes the path to the
|
---|
61 | # temp file to the default editor - typically this would be an interactive
|
---|
62 | # editor like 'vi'. However, we've just replaced the editor with a custom
|
---|
63 | # script that just writes '3' as the content of the tmp file. Finally, when
|
---|
64 | # the editor exits, sudoedit copies the tmp file over the top of the system
|
---|
65 | # file, restoring appropriate root-level permissions
|
---|
66 | `sudoedit /proc/sys/vm/drop_caches`;
|
---|
67 | # - restore the default editor, just in case something in Greenstone
|
---|
68 | # depends on this being a reasonable value
|
---|
69 | $ENV{'EDITOR'} = $current_editor;
|
---|
70 |
|
---|
71 | my $import_cmd = 'taskset -c 0 import.pl -keepold -manifest manifest.xml -verbosity 0 ' . $collection . ' 2>&1';
|
---|
72 | if ($threads > 1)
|
---|
73 | {
|
---|
74 | $import_cmd = 'parallel_import.pl -removeold -verbosity 0 -epoch ' . $epoc . ' -jobs ' . $threads . ' ' . $collection . ' 2>&1';
|
---|
75 | }
|
---|
76 | my $polling_cmd = 'ps -u jmt12 -o pid,psr,pcpu,pmem,cmd --sort pid';
|
---|
77 |
|
---|
78 | print "===== GS Import with PS Poll ====\n";
|
---|
79 |
|
---|
80 | # Forking children... get off my front lawn!
|
---|
81 | my $child_pid = fork();
|
---|
82 |
|
---|
83 | # Ensure fork() worked
|
---|
84 | if (!defined($child_pid))
|
---|
85 | {
|
---|
86 | die("Fork didn't.");
|
---|
87 | }
|
---|
88 |
|
---|
89 | # Parent process polls memory until child is done
|
---|
90 | if ($child_pid > 0)
|
---|
91 | {
|
---|
92 | print " - parent monitoring child process $child_pid\n";
|
---|
93 |
|
---|
94 | open(POLLOUT, '>:utf8', 'memory-' . $child_pid . '.log') or die('Failed to open file for writing: memory-' . $child_pid . ".log\n");
|
---|
95 | my $result = 0;
|
---|
96 | while ($result >= 0)
|
---|
97 | {
|
---|
98 | my ($seconds, $useconds) = gettimeofday();
|
---|
99 | print POLLOUT '[' . $seconds . '.' . $useconds . "]\n";
|
---|
100 | my $result = `$polling_cmd 2>&1`;
|
---|
101 | print POLLOUT $result . "\n";
|
---|
102 | usleep(500000); # Half a second
|
---|
103 | # I believe result is non-zero iff the child process has completed, in
|
---|
104 | # which case result should be the child process id (again)
|
---|
105 | $result = waitpid($child_pid, WNOHANG);
|
---|
106 | # Watch for errors in waitpid
|
---|
107 | print "Result: " . $result . "\n";
|
---|
108 | }
|
---|
109 | close(POLLOUT);
|
---|
110 | print "===== Complete! =====\n\n";
|
---|
111 | exit(0);
|
---|
112 | }
|
---|
113 | # Child process runs import command
|
---|
114 | else
|
---|
115 | {
|
---|
116 | print " - child process running GS import\n";
|
---|
117 | my $result = `$import_cmd`;
|
---|
118 | print $result . "\n";
|
---|
119 | exit(0);
|
---|
120 | }
|
---|
121 |
|
---|
122 | # Doubt we'll ever get here, but who knows.
|
---|
123 | exit(0);
|
---|
124 |
|
---|
125 | sub printUsage
|
---|
126 | {
|
---|
127 | die("Usage: poll-gsdl.pl <collection> [<threads> [<epoc>]]\n");
|
---|
128 | }
|
---|
129 |
|
---|
130 | 1;
|
---|