45503dd0d72dedd94fdf7dde0ae6afa93b2a1417
[p2p-testing-infrastructure.git] / ControlScripts / schedule_client.sh
1 #!/bin/bash
2 #
3 # 2010 Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
4 #
5 # Schedule clients
6 # The script
7 #  * starts/stops a client
8 #  * manages when client completes
9
10 # use _DEBUG="off" to turn off debug printing
11 _DEBUG="on"
12
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."
17 else
18         source globalconfig
19 fi
20
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."
25 else
26         source ../ClientWorkingFolders/TmpLogs/node_config
27 fi
28
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."
33 else
34         source client_script_mappings
35 fi
36
37 client_pid=0
38
39 declare start_time
40 declare stop_time
41 declare -a suspend_resume
42
43 parse_periods()
44 {
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"
49
50         stop_time=${tmp_array[$(($len-1))]}
51
52         for ((i = 1; i < $len-1; i++)); do
53                 suspend_resume[$(($i - 1))]=${tmp_array[$i]}
54         done
55 }
56
57 suspend_client()
58 {
59         local client_pid=$1
60         DEBUG echo "suspending client ..."
61         kill -STOP ${client_pid}
62 }
63
64 resume_client()
65 {
66         local client_pid=$1
67         DEBUG echo "resuming client ..."
68         kill -CONT ${client_pid}
69 }
70
71 # test whether time in period (t1, t2) is infinte ("-" sign)
72 # if that is the case, sleep indefinitely, waiting for shut down
73 test_infinite()
74 {
75         time="$1"
76
77         if test "$time" = "-"; then
78                 sleep 1000d
79         fi
80 }
81
82 cleanup()
83 {
84         echo "*** cleanup"
85         stop_client ${CLIENT_TYPE}
86         kill ${mon_pid}
87 }
88
89 # use cleanup function when receiving signals
90 trap cleanup 0 1 2 3 15
91
92 parse_periods
93
94 DEBUG echo "start_time is ${start_time}"
95 DEBUG echo "suspend_resume is ${suspend_resume[@]}"
96 DEBUG echo "stop_time is ${stop_time}"
97
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"
102 sleep ${to_sleep}
103
104 # store own PID in environment variable
105 declare SCHEDULE_CLIENT_PID
106 SCHEDULE_CLIENT_PID=$$
107 export SCHEDULE_CLIENT_PID
108
109 #do_nothing_handler()
110 #{
111 #  echo "*** do_nothing_handler" # do nothing, successfully
112 #}
113
114 #trap do_nothing_handler 10 # wait_for_usr1 is simple handler (waits for SIGUSR1 from start_client process)
115
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}
119 background_pid=$!
120 DEBUG echo "$(basename $0): background_pid is ${background_pid}"
121
122 # wait for client to start
123 while true; do
124         #echo "ps --ppid ${background_pid} -o pid -o cmd"
125         #ps --ppid ${background_pid} -o pid -o cmd
126         #echo "ps --ppid ${background_pid} -o 'pid=' -o 'cmd=' | grep './swift' | grep -v 'grep' | grep -v 'ps'"
127         #ps --ppid ${background_pid} -o 'pid=' -o 'cmd=' | grep './swift' | grep -v 'grep' | grep -v 'ps'
128         #client_pid=$(ps --ppid ${background_pid} -o 'pid=' -o 'cmd=' | grep './swift' | grep -v 'grep' | grep -v 'ps' | awk '{print $1;}')
129         #client_pid=$(ps --ppid ${background_pid} | grep -v '/bin/.*sh' | grep -v 'PID' | tail -1 | awk '{print $1;}')
130         #client_pid=$(pgrep -P ${background_pid})
131         #if ! test -z ${client_pid}; then # ps ended successfully
132
133         pidof swift
134         client_pid=$(pidof swift)
135
136         if [ ! -z "$client_pid" ]; then
137                 break
138         fi
139 done
140 DEBUG echo "$(basename $0): client_pid is ${client_pid}"
141
142 if [ -z $(ps -p "$background_pid" -o pid=) ]; then
143         DEBUG echo "background pid is dead"
144 fi
145
146 if [ -z $(ps -p "$client_pid" -o pid=) ]; then
147         DEBUG echo "client pid is dead"
148 fi
149
150 # Monitor client's resources
151 ./hook_monitor_pid.sh "$client_pid" >> $REMOTE_PATH/P2P-Testing-Infrastructure/ClientWorkingFolders/TmpLogs/$(hostname).log.mon 2>&1 &
152 ps -ef
153 mon_pid=$!
154
155 old_time=${start_time}
156 for ((i = 0; i < ${#suspend_resume[@]}; i += 2)); do
157         # Sleeping while the client runs. When i wake up i will suspend the client
158         test_infinite ${suspend_resume[$i]}
159
160         to_sleep=$((${suspend_resume[$i]} - ${old_time}))
161         sleep ${to_sleep}
162         client_pid=$(pgrep -P ${background_pid})
163         DEBUG echo "client_pid is ${client_pid}"
164         suspend_client ${client_pid}
165         old_time=${suspend_resume[$i]}
166         
167         # Client is suspended. Sleeping until the client should be restarted. When i wake up i will start the client
168         test_infinite ${suspend_resume[$(($i+1))]}
169
170         to_sleep=$((${suspend_resume[$(($i+1))]} - ${old_time}))
171         # Saving sleep time to log file - this script's output is redirected to the client log file
172         echo "SLEEP $to_sleep"
173         sleep ${to_sleep}
174         resume_client ${client_pid}
175         old_time=${suspend_resume[$(($i+1))]}
176 done
177
178 # Sleeping while the client runs. When i wake up i will suspend the client for the last time
179 test_infinite ${stop_time}
180
181 to_sleep=$((${stop_time} - ${old_time}))
182 sleep ${to_sleep}
183
184 # stop client and monitoring script
185 cleanup