-
Notifications
You must be signed in to change notification settings - Fork 2
/
parallelize.php
73 lines (56 loc) · 2.3 KB
/
parallelize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
// Parallelizes parse_entries. Script requires PHP extensions pcntl and ssh2.
// Behavior is to have {$count} processes on each machine.
// Looks for a public/private key pair in current working directory, named "constituency_rsa" and "constituency_rsa.pub".
// Machines, username, remote path to constituency dir, and location of private key file (public is $identity . ".pub")
// File should have $machines array, $username, $remotePath, $identity
include("parallelize_config.php");
if($argc < 5)
die("Usage: php parallelize.php start end count parse_entries|judge_constituency [args]\n" .
"Creates count copies of parse_entries or judge_constituency on each target machine, dividing up the entries evenly.");
$start = intval($argv[1]);
$end = intval($argv[2]);
$count = intval($argv[3]);
$newArgv = array_slice($argv, 5);
$numMachines = count($machines);
$perProc = ($end - $start + 1) / ($count * $numMachines);
$i = 0;
$j = 0;
// fork a whole bunch of times
// $j is machine index, $i is process index
while($i < $count - 1 && pcntl_fork() > 0)
$i++;
while($j < $numMachines - 1 && pcntl_fork() > 0)
$j++;
$processIdx = $j * $count + $i;
$newStart = intval($perProc * $processIdx + $start);
$newEnd = intval($perProc * ($processIdx + 1) + $start - 1);
$machine = $machines[$j];
$connection = ssh2_connect($machine);
if(!$connection)
die("Couldn't connect to machine.\n");
// We use public key authentication because many machines don't have password authentication activated.
// Keyboard-interactive is not password.
if(!ssh2_auth_pubkey_file($connection, $username, $identity . ".pub", $identity))
die("Couldn't authenticate.\n");
if($_ENV['CONSTITUENCY_TABLES'])
$extra = "export CONSTITUENCY_TABLES='{$_ENV['CONSTITUENCY_TABLES']}' && ";
else
$extra = "";
$stream = ssh2_exec($connection, $extra . "cd $remotePath && 2>&1 php {$argv[4]}.php -range $newStart $newEnd " . implode(" ", $newArgv));
if(!file_exists("logs/"))
mkdir("logs");
$logFile = fopen("logs/$machine-$i.txt", "w");
if(!$logFile)
die("Couldn't open file for logging.");
// copy stream data repeatedly, without taking up all the processing power
while(!feof($stream)) {
$bytes = fread($stream, 65536);
fwrite(STDOUT, $bytes);
fwrite($logFile, $bytes);
usleep(200000);
}
echo(stream_get_contents($stream));
fclose($stream);
fclose($logFile);
?>