tsup exeperiment scripts
[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 sleep 5
124 while true; do
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
134
135 #       pidof swift
136 #       client_pid=$(pidof swift)
137
138         if [ ! -z "$client_pid" ]; then
139                 break
140         fi
141 done
142
143 DEBUG echo "$(basename $0): client_pid is ${client_pid}"
144
145 if [ -z $(ps -p "$background_pid" -o pid=) ]; then
146         DEBUG echo "background pid is dead"
147 fi
148
149 if [ -z $(ps -p "$client_pid" -o pid=) ]; then
150         DEBUG echo "client pid is dead"
151 fi
152
153 # Monitor client's resources
154 ./hook_monitor_pid.sh "$client_pid" >> $REMOTE_PATH/P2P-Testing-Infrastructure/ClientWorkingFolders/TmpLogs/$(hostname).log.mon 2>&1 &
155 ps -ef
156 mon_pid=$!
157
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]}
162
163         to_sleep=$((${suspend_resume[$i]} - ${old_time}))
164         sleep ${to_sleep}
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]}
169         
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))]}
172
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"
176         sleep ${to_sleep}
177         resume_client ${client_pid}
178         old_time=${suspend_resume[$(($i+1))]}
179 done
180
181 # Sleeping while the client runs. When i wake up i will suspend the client for the last time
182 test_infinite ${stop_time}
183
184 to_sleep=$((${stop_time} - ${old_time}))
185 sleep ${to_sleep}
186
187 # stop client and monitoring script
188 cleanup