MPI Control Script (mpictl.sh)

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
#
# MPI control script.
 
usage()
{
cat <<EOF
 
Usage: $0 options command
 
This script allows to control MPI instances (i.e. start/stop them).
 
OPTIONS:
 
   -c      The configuration home directory (defaults to $MPI_HOME/conf)
 
   -n      The node ID (defaults to 1)
           The node ID is used to select:
           - the server configuration in the configuration home directory (e.g. server-1.xml)
           - the extract directory (e.g. node-1)
           - the startup log file to write in the out directory (e.g. catalina-1.out)
           - the PID file to write in the pid directory (e.g. node-1.pid)
              
   -j      The Java options to set (optional). See examples below...
    
   -h      Show this message
    
COMMANDS:
 
   start      Starts an MPI node
   stop       Stops an MPI node
   showlog    Shows the log of an MPI node with tail -f
   logless    Shows the log of an MPI node with less
   showout    Shows the startup log of an MPI node with tail -f
   showstats  Display timing statistics for an MPI node
    
Java option examples:
 
   Set the maximum heap space for the MPI to 1024 MB and the maximum PermGen space to 256 MB:  
     -Xmx1024m -XX:MaxPermSize=256m
      
   Use a custom log4j configuration:
     -Dlog4j.configuration=/etc/mpi/log4j.xml         
    
   Enable remote monitoring the MPI using JMX
   (Check http://docs.oracle.com/javase/6/docs/technotes/guides/management/agent.html for details):  
     -Dcom.sun.management.jmxremote.port=3000 \
     -Dcom.sun.management.jmxremote.password.file=password.properties \
     -Dcom.sun.management.jmxremote.access.file=access.properties \
     -Djavax.net.ssl.keyStore=keystore \
     -Djavax.net.ssl.keyStorePassword=password
 
   To configure the MPI to prefer IPv4 over IPv6
   (e.g. when running into trouble with a IP multicasting setup for session replication) use:
     -Djava.net.preferIP4Stack=true
 
EOF
}
 
# Set defaults for command line arguments
NODE_ID="1"
MPI_CONFIG_HOME=""
JAVA_OPTS=""
 
# Read command line arguments
while getopts "n:c:j:h" OPTION
do
  case $OPTION in
    n)
      NODE_ID=$OPTARG
      ;;
    c)
      MPI_CONFIG_HOME=$OPTARG
      ;;
    j)
      JAVA_OPTS=$OPTARG
      ;;
    h)
      usage
      exit
      ;;
    \?)
      echo "Unknown option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Missing option argument for -$OPTARG" >&2
      exit 1
      ;;
    *)
      echo "Unimplemented option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done
shift $(($OPTIND-1))
 
# Set variables
MPI_HOME=$(cd $(dirname "$0")/..; pwd)
if [[ ! $MPI_CONFIG_HOME ]]; then
  MPI_CONFIG_HOME="$MPI_HOME/conf"
fi
MPI_JAR="$MPI_HOME/lib/mpi-exec-war.jar"
PERF4J_JAR="$MPI_HOME/lib/perf4j.jar"
SERVER_XML="$MPI_CONFIG_HOME/server-$NODE_ID.xml"
EXTRACT_DIR="$MPI_HOME/node-$NODE_ID"
CATALINA_OUT="$MPI_HOME/out/catalina-$NODE_ID.out"
PID_FILE="$MPI_HOME/pid/node-$NODE_ID.pid"
CURRENT_DATE=`date +"%Y-%m-%d"`
 
# Check files and directories
assert_file_exists_and_is_readable()
{
  if [[ ! -f $1 || ! -r $1 ]]; then
    echo "File $1 doesn't exist or is not readable" >&2
    exit 1
  fi
}
assert_directory_exists_and_is_writable()
{
  if [[ ! -d $1 || ! -w $1 ]]; then
    echo "Directory $1 doesn't exist or is not writable" >&2
    exit 1
  fi
}
assert_file_exists_and_is_readable $MPI_JAR
assert_file_exists_and_is_readable $PERF4J_JAR
assert_file_exists_and_is_readable $SERVER_XML
assert_directory_exists_and_is_writable "$MPI_HOME"
assert_directory_exists_and_is_writable "$MPI_HOME/pid/"
assert_directory_exists_and_is_writable "$MPI_HOME/out/"
 
# Execute command
COMMAND=$1
case $COMMAND in
  start)
    if [[ -e $PID_FILE ]]; then
      echo "MPI node $NODE_ID seems to be already started (there is a PID file for this node)"
      exit 1
    fi
    java $JAVA_OPTS -Dmpi.config.home=$MPI_CONFIG_HOME -jar $MPI_JAR -serverXmlPath=$SERVER_XML -extractDirectory=$EXTRACT_DIR > $CATALINA_OUT 2>&1 &
    echo $! > $PID_FILE
    ;;
  stop)
    if [[ ! -e $PID_FILE ]]; then
      echo "MPI node $NODE_ID seems not to be started (there is no PID file for this node)"
      exit 1
    fi
    PID=$(<$PID_FILE)
    kill $PID
    rm $PID_FILE
    ;;
  showlog)
    tail -100f $MPI_HOME/node-$NODE_ID/logs/mpi.$CURRENT_DATE.log 
    ;;
  logless)
    less $MPI_HOME/node-$NODE_ID/logs/mpi.$CURRENT_DATE.log 
    ;;
  showout)
    tail -100f $MPI_HOME/out/catalina-$NODE_ID.out 
    ;;
  showstats)
    java $JAVA_OPTS -jar $PERF4J_JAR $MPI_HOME/node-$NODE_ID/logs/mpi-statistics.$CURRENT_DATE.log -t 86400000
    ;;
  *)
    usage
    exit 1
    ;;
esac