-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpick_main_node
executable file
·135 lines (119 loc) · 3.66 KB
/
pick_main_node
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl -w
# The MIT License
#
# Copyright (c) 2014 Goo Jun <[email protected]> Adrian Tan <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# This was modified from Goo's pick_mainnode script
#
# 1. modified to take into account all the nodes running on the main cluster
# before distributing the jobs. For this to work, the user's pass phrase
# for his private key should be removed.
# 2. modified to take in the number of seconds to randomize for sleep
# time. 0 seconds is the default.
use strict;
use warnings;
my $executablePath = __FILE__;
$executablePath =~ s/[^\/]+$//;
my $defaultUser = "atks";
my $configFile = $executablePath . ".config";
if (-e $configFile)
{
open(IN, $configFile) || die "Cannot open config file";
$defaultUser = <IN>;
chomp($defaultUser);
close(IN);
}
else
{
print STDERR "Please create a .config file containing your user name in the directory of cluster tools.\n";
exit(1);
}
sleep(int(rand()*(@ARGV ? $ARGV[0]: 0)));
my $usr = $defaultUser;
my %TNODE = ();
my %cnt = ();
my @nodes = ();
open(IN, $executablePath . "good_node_list.txt");
while(<IN>)
{
chomp;
my ($node, $newNode) = split("\t");
$TNODE{$newNode} = $node;
$cnt{$node} = 0;
push(@nodes, $node);
}
close(IN);
for my $gateway ("fantasia", "wonderland", "snowwhite", "dumbo", "slurm")
{
my $cmd = "ssh $usr@" . $gateway . " 'mosps -eu | tail -n +2'";
if ($gateway eq "slurm")
{
$cmd = "squeue | tail -n +2 | tr -s ' ' '\\t' | cut -f2,5,9 | perl -lane '{print \"\$F[1]\\t\$F[0]\\t\$F[2]\"}'";
}
else
{
$cmd = "ssh $usr@" . $gateway . " 'mosps -eu | tail -n +2'";
}
my $c = `$cmd`;
my @jobs = split("\n", $c);
for my $job (@jobs)
{
my ($user, $pid, $node, $rest) = split(/\s+/, $job);
if ($gateway eq "slurm")
{
if ($node eq "(None)" || $node eq "(Resources)" || $node !~ /\d+/)
{
next;
}
if (!exists($TNODE{$node}))
{
my $cmd1 = "ssh $usr\@" . "fantasia \"mosctl whois $node\"";
#print $cmd1 . "\n";
my $newNode = `$cmd1`;
$TNODE{$node} = $newNode;
$node = $newNode;
}
else
{
$node = $TNODE{$node};
}
}
$cnt{$node}++;
}
}
my %tie = ();
my $min = 10000;
foreach my $n (@nodes)
{
if ($cnt{$n}<$min)
{
%tie = ();
$tie{$n} = 1;
$min = $cnt{$n};
}
elsif ($cnt{$n}==$min)
{
$tie{$n} = 1;
}
}
my @tie = keys(%tie);
my $pick = $tie[int(rand(@tie))];
print $pick;