3 # 2010 Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
7 # * starts/stops a client
8 # * manages when client completes
10 # use _DEBUG="off" to turn off debug printing
13 # Read the global configuration file
14 # Check if the global configuration file exists
15 if [ ! -e globalconfig ]; then
16 echo "Warning: The global config file globalconfig does not exist."
21 # Read the node-specific configuration file
22 # Check if the node-specific configuration file exists
23 if [ ! -e ../ClientWorkingFolders/TmpLogs/node_config ]; then
24 echo "Warning: The global config file ../ClientWorkingFolders/TmpLogs/node_config does not exist."
26 source ../ClientWorkingFolders/TmpLogs/node_config
29 # Read client mappings configuration file
30 # Check if the client mappings configuration file exists
31 if [ ! -e client_script_mappings ]; then
32 echo "Warning: The client mappings config file client_script_mappings does not exist."
34 source client_script_mappings
41 declare -a suspend_resume
45 local tmp_array=($(echo "$PERIODS" | sed 's/[,()]/ /g'))
46 start_time=${tmp_array[0]}
47 local len=${#tmp_array[@]}
48 DEBUG echo "PERIODS: $PERIODS"
50 stop_time=${tmp_array[$(($len-1))]}
52 for ((i = 1; i < $len-1; i++)); do
53 suspend_resume[$(($i - 1))]=${tmp_array[$i]}
60 DEBUG echo "suspending client ..."
61 kill -STOP ${client_pid}
67 DEBUG echo "resuming client ..."
68 kill -CONT ${client_pid}
71 # test whether time in period (t1, t2) is infinte ("-" sign)
72 # if that is the case, sleep indefinitely, waiting for shut down
77 if test "$time" = "-"; then
85 stop_client ${CLIENT_TYPE}
89 # use cleanup function when receiving signals
90 trap cleanup 0 1 2 3 15
94 DEBUG echo "start_time is ${start_time}"
95 DEBUG echo "suspend_resume is ${suspend_resume[@]}"
96 DEBUG echo "stop_time is ${stop_time}"
98 # Initial sleep (before the client is started for the first time)
99 to_sleep=${start_time}
100 # Saving sleep time to log file - this script's output is redirected to the client log file
101 echo "SLEEP $to_sleep"
104 # store own PID in environment variable
105 declare SCHEDULE_CLIENT_PID
106 SCHEDULE_CLIENT_PID=$$
107 export SCHEDULE_CLIENT_PID
109 #do_nothing_handler()
111 # echo "*** do_nothing_handler" # do nothing, successfully
114 #trap do_nothing_handler 10 # wait_for_usr1 is simple handler (waits for SIGUSR1 from start_client process)
116 # Starting the client for the first time
117 DEBUG echo "$(basename $0): CLIENT_TYPE is ${CLIENT_TYPE}"
118 start_client ${CLIENT_TYPE} ${DL_BW} ${UL_BW} ${NO_CONNECTIONS}
120 DEBUG echo "$(basename $0): background_pid is ${background_pid}"
122 # wait for client to start
125 #echo "ps --ppid ${background_pid} -o pid -o cmd"
126 #ps --ppid ${background_pid} -o pid -o cmd
127 #echo "ps --ppid ${background_pid} -o 'pid=' -o 'cmd=' | grep './swift' | grep -v 'grep' | grep -v 'ps'"
128 #ps --ppid ${background_pid} -o 'pid=' -o 'cmd=' | grep './swift' | grep -v 'grep' | grep -v 'ps'
129 #client_pid=$(ps --ppid ${background_pid} -o 'pid=' -o 'cmd=' | grep './swift' | grep -v 'grep' | grep -v 'ps' | awk '{print $1;}')
130 #client_pid=$(ps --ppid ${background_pid} | grep -v '/bin/.*sh' | grep -v 'PID' | tail -1 | awk '{print $1;}')
131 client_pid=$(pgrep -P ${background_pid})
132 DEBUG echo "background_pid=${background_pid}; client_pid=${client_pid}"
133 #if ! test -z ${client_pid}; then # ps ended successfully
136 # client_pid=$(pidof swift)
138 if [ ! -z "$client_pid" ]; then
143 DEBUG echo "$(basename $0): client_pid is ${client_pid}"
145 if [ -z $(ps -p "$background_pid" -o pid=) ]; then
146 DEBUG echo "background pid is dead"
149 if [ -z $(ps -p "$client_pid" -o pid=) ]; then
150 DEBUG echo "client pid is dead"
153 # Monitor client's resources
154 ./hook_monitor_pid.sh "$client_pid" >> $REMOTE_PATH/P2P-Testing-Infrastructure/ClientWorkingFolders/TmpLogs/$(hostname).log.mon 2>&1 &
158 old_time=${start_time}
159 for ((i = 0; i < ${#suspend_resume[@]}; i += 2)); do
160 # Sleeping while the client runs. When i wake up i will suspend the client
161 test_infinite ${suspend_resume[$i]}
163 to_sleep=$((${suspend_resume[$i]} - ${old_time}))
165 client_pid=$(pgrep -P ${background_pid})
166 DEBUG echo "client_pid is ${client_pid}"
167 suspend_client ${client_pid}
168 old_time=${suspend_resume[$i]}
170 # Client is suspended. Sleeping until the client should be restarted. When i wake up i will start the client
171 test_infinite ${suspend_resume[$(($i+1))]}
173 to_sleep=$((${suspend_resume[$(($i+1))]} - ${old_time}))
174 # Saving sleep time to log file - this script's output is redirected to the client log file
175 echo "SLEEP $to_sleep"
177 resume_client ${client_pid}
178 old_time=${suspend_resume[$(($i+1))]}
181 # Sleeping while the client runs. When i wake up i will suspend the client for the last time
182 test_infinite ${stop_time}
184 to_sleep=$((${stop_time} - ${old_time}))
187 # stop client and monitoring script