rewrite mt-mapperd based on st threads
[archipelago] / xseg / launch
1 #!/bin/bash
2 #
3 # Launcher for xseg configurations
4
5 NI_SAVED=${NONINTERACTIVE}
6 export NONINTERACTIVE=1
7 source envsetup
8 export NONINTERACTIVE=${NI_SAVED}
9
10 set +e
11
12 function pretty_print {
13         echo "======================="
14         echo "$1"
15         echo "======================="
16 }
17
18 function fail {
19         echo "${1}"
20         exit 1
21 }
22
23 function parse_config {
24         [ -e ~/.xsegrc ] && source ~/.xsegrc
25
26         [ -z "${HOSTNAME}" ] && HOSTNAME=`hostname`
27         [ -z "${XSEG_HOME}" ] && XSEG_HOME="/root/archip/xseg"
28         [ -z "${MODULES_DIR}" ] && MODULES_DIR="${XSEG_HOME}/lib/kernel"
29         [ -z "${SPEC}" ] && SPEC="segdev:xsegbd:512:1024:12"
30         [ -z "${REQS}" ] && REQS=512
31         [ -z "${PORTS}" ] && PORTS=512
32         [ -z "${IMAGES}" ] && IMAGES="/home/user/archip/xseg/peers/user/foo"
33         [ -z "${MAPS}" ] && MAPS="/home/user/archip/xseg/peers/user/foo"
34         [ -z "${PITHOS}" ] && PITHOS="/srv/pithos/data/blocks"
35         [ -z "${PITHOSMAPS}" ] && PITHOSMAPS="/srv/pithos/data/maps"
36         [ -z "${XSEG_LOGS}" ] && XSEG_LOGS="/home/philipgian/archip/logs"
37         [ -z "${DEVICE_PREFIX}" ] && DEVICE_PREFIX="/dev/xsegbd"
38         [ -z "${XSEGBD_SYSFS}" ] && XSEGBD_SYSFS="/sys/bus/xsegbd"
39         [ -z "${CHRDEV_NAME}" ] && CHRDEV_NAME="/dev/`cat /proc/devices | grep segdev | awk '{print $2}'`"
40         [ "${CHRDEV_NAME}" = "/dev/" ] && CHRDEV_NAME="/dev/segdev"
41         [ -z "${CHRDEV_MAJOR}" ] && CHRDEV_MAJOR=`cat /proc/devices | grep segdev | awk '{print $1}'`
42         [ -z "${CHRDEV_MAJOR}" ] && CHRDEV_MAJOR=60
43         [ -z "${NR_OPS}" ] && NR_OPS=128
44         [ -z "${BPORT}" ] && BPORT=0
45         [ -z "${VPORT_START}" ] && VPORT_START=4
46         [ -z "${VPORT_END}" ] && VPORT_END=200
47         [ -z "${MPORT}" ] && MPORT=1
48         [ -z "${MBPORT}" ] && MBPORT=2
49         [ -z "${VTOOL}" ] && VTOOL=3
50 }
51
52
53 function unload_module {
54         rmmod "$1"
55 }
56
57 function unload_all {
58         unload_module "xsegbd"
59         rm "${CHRDEV_NAME}" || true
60         unload_module "xseg_segdev"
61         unload_module "xseg_posix"
62         unload_module "xseg_pthread"
63         unload_module "segdev"
64         unload_module "xseg"
65 }
66
67 function load_module {
68         (lsmod | grep "$1" > /dev/null) || insmod "${MODULES_DIR}/$1.ko" "$2" || exit 1
69 }
70
71 function mk_chardev {
72         ls "${CHRDEV_NAME}" &> /dev/null || \
73         mknod "${CHRDEV_NAME}" c "${CHRDEV_MAJOR}" 0 || exit 1
74 }
75
76 function load_all {
77         load_module "xseg"
78         load_module "segdev"
79         load_module "xseg_posix"
80         load_module "xseg_pthread"
81         load_module "xseg_segdev"
82         mk_chardev
83     "${XSEG_HOME}/peers/user/xseg" "${SPEC}" create || true
84         load_module "xsegbd" "spec=$SPEC"
85 }
86
87 function create_segment {
88         "${XSEG_HOME}/peer/users/xseg" "${SPEC}" create
89 }
90
91 function destroy_segment {
92         "${XSEG_TOOL}" "${SPEC}" destroy
93 }
94
95 function alloc_requests {
96         "${XSEG_TOOL}" "${SPEC}" $1 alloc_requests $2
97 }
98
99 function free_requests {
100         "${XSEG_TOOL}" "${SPEC}" $1 free_requests $2
101 }
102
103 function spawn_filed {
104         pgrep -f "peers/user/filed" || \
105         "${XSEG_HOME}/peers/user/filed" "$IMAGES" -p "$BPORT" -g "${SPEC}" -n \
106         ${NR_OPS}  &> "${XSEG_LOGS}/filed-${HOSTNAME}" &
107 }
108
109 function spawn_pfiled {
110         "${XSEG_HOME}/peers/user/pfiled" -p "$BPORT" -g "${SPEC}" -n \
111         ${NR_OPS} "${PITHOS}" "${IMAGES}" -v 1 &> "${XSEG_LOGS}/pfiled-${HOSTNAME}" &
112 }
113
114 function spawn_pfiledm {
115         "${XSEG_HOME}/peers/user/pfiled" -p "$MBPORT" -g "${SPEC}" -n \
116         ${NR_OPS} "${PITHOSMAPS}" "${MAPS}" -v 1  &> "${XSEG_LOGS}/pfiledm-${HOSTNAME}" &
117 }
118
119 function spawn_sosd {
120         "${XSEG_HOME}/peers/user/mt-sosd" -p "$BPORT" -g "${SPEC}" -n \
121         ${NR_OPS} --pool "blocks" -v 3 &> "${XSEG_LOGS}/sosd-${HOSTNAME}" &
122 }
123
124 function spawn_sosdm {
125         "${XSEG_HOME}/peers/user/mt-sosd" -p "$MBPORT" -g "${SPEC}" -n \
126         ${NR_OPS} --pool "maps" -v 3  &> "${XSEG_LOGS}/sosdm-${HOSTNAME}" &
127 }
128
129 function spawn_vlmcd {
130         pgrep -f "peers/user/st-vlmcd" || \
131         "${XSEG_HOME}/peers/user/st-vlmcd" -t 1 -sp "$VPORT_START" \
132         -ep "$VPORT_END" -bp "$BPORT" -mp "$MPORT" -g \
133         "${SPEC}" -n ${NR_OPS} -v 3 &> "${XSEG_LOGS}/vlmcd-${HOSTNAME}" &
134 #       alloc_requests "$VPORT:0" 128
135 }
136
137 function spawn_mapperdc {
138         pgrep -f "mt-mapperd" || \
139         "${XSEG_HOME}/peers/user/mt-mapperd" -t 1 -sp "$MPORT" -ep "$MPORT"\
140         -bp "$BPORT" -mbp "$MBPORT" -g "${SPEC}"\
141         -n ${NR_OPS} -v 3 &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" & 
142 #       alloc_requests "$MPORT:0" 128
143 }
144
145 function spawn_mapperd {
146         pgrep -f "mapperd.py" || \
147         "${XSEG_HOME}/peers/user/mapperd.py" -p "$MPORT" -b "$BPORT" -g "${SPEC}" \
148         &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" &
149 #       alloc_requests "$MPORT:0" 128
150 }
151
152 # map_volume - Map a volume to an xsegbd device
153 #
154 # @param $1             target/volume name
155 # @param $2             src port
156 # @param $3             dst port
157 function map_volume {
158         echo "$1 $2:$3:${REQS}" > "${XSEGBD_SYSFS}/add" || exit 1
159 }
160
161 # unmap_device - Unmap an xsegbd device/volume
162 #
163 # @param $1             xsegbd device id
164 function unmap_device {
165         echo "$1" > "${XSEGBD_SYSFS}/remove" || exit 1
166 }
167
168 function help {
169         echo "Usage: ./launch [list | <configuration> [start | stop] | insmod | rmmod]"
170 }
171
172 if [ -n "$NONINTERACTIVE" ]; then
173         exit 0
174 fi
175
176 parse_config
177 XSEG_TOOL="${XSEG_HOME}/peers/user/xseg"
178
179 case $1 in
180 list)
181         echo "filed [start|stop]"
182         echo "map [start|stap]"
183         echo " "
184         echo "insmod"
185         echo "rmmod"
186         ;;
187 insmod)
188         load_all
189         mk_chardev
190         ;;
191 rmmod)
192         rm ${CHRDEV_NAME}
193         unload_all
194         ;;
195 config)
196         echo "HOSTNAME=${HOSTNAME}"
197         echo "XSEG_HOME=${XSEG_HOME}"
198         echo "MODULES_DIR=${MODULES_DIR}"
199         echo "SPEC=${SPEC}"
200         echo "REQS=${REQS}"
201         echo "PORTS=${PORTS}"
202         echo "IMAGES=${IMAGES}"
203         echo "PITHOS=${PITHOS}"
204         echo "XSEG_LOGS=${XSEG_LOGS}"
205         echo "DEVICE_PREFIX=${DEVICE_PREFIX}"
206         echo "XSEGBD_SYSFS=${XSEGBD_SYSFS}"
207         echo "CHRDEV_NAME=${CHRDEV_NAME}"
208         echo "CHRDEV_MAJOR=${CHRDEV_MAJOR}"
209         echo "NR_OPS=${NR_OPS}"
210         echo "BPORT=${BPORT}"
211         echo "VPORT=${VPORT}"
212         echo "MPORT=${MPORT}"
213         ;;
214 filed)
215         case $2 in
216         start)
217                 load_all
218                 sleep 0.5
219                 spawn_filed
220                 sleep 0.5
221                 map_volume xsegvol 3 0
222                 ;;
223         stop)
224                 pkill -f peers/user/filed
225                 sleep 0.5
226                 unmap_device 3
227                 unload_all
228                 ;;
229         esac
230         ;;
231 map)
232         case $2 in
233         start)
234                 load_all
235                 sleep 0.5
236                 spawn_pfiled
237                 spawn_mapperd
238                 spawn_vlmcd
239                 ;;
240         stop)
241                 pkill -f peers/user/mt-vlmcd || true
242 #               free_requests "$VPORT:0" 128 || true
243                 pkill -f peers/user/mt-mapperd || true
244 #               free_requests "$MPORT:0" 128 || true
245                 pkill -f peers/user/pfiled || true
246                 sleep 0.5
247                 unload_all
248                 ;;
249         *)
250                 help
251                 ;;
252         esac
253         ;;
254 nomodules)
255         case $2 in
256         start)
257                 spawn_filed
258                 spawn_mapperd
259                 spawn_vlmcd
260                 ;;
261         stop)
262                 pkill -f peers/user/mt-vlmcd || true
263 #               free_requests "$VPORT:0" 128 || true
264                 pkill -f peers/user/mt-mapperd || true
265 #               free_requests "$MPORT:0" 128 || true
266                 pkill -f peers/user/filed || true
267                 ;;
268         *)
269                 help
270                 ;;
271         esac
272         ;;
273 one)
274         case $2 in
275         start)
276                 load_all
277                 sleep 1
278                 spawn_filed 
279                 spawn_mapperdc
280                 spawn_vlmcd 
281                 ;;
282         stop)
283                 pkill -f peers/user/mt-vlmcd || true
284 #               free_requests "$VPORT:0" 128 || true
285                 pkill -f peers/user/mt-mapperd || true
286 #               free_requests "$MPORT:0" 128 || true
287                 pkill -f peers/user/filed || true
288                 sleep 0.5
289                 unload_all
290                 ;;
291         *)
292                 help
293                 ;;
294         esac
295         ;;
296 doublemap)
297         case $2 in
298         start)
299                 load_all
300                 sleep 0.5
301                 spawn_pfiled
302                 spawn_pfiledm
303                 spawn_mapperdc
304                 spawn_vlmcd
305                 ;;
306         stop)
307                 pkill -f peers/user/st-vlmcd || true
308 #               free_requests "$VPORT:0" 128 || true
309                 pkill -f peers/user/mt-mapperd || true
310 #               free_requests "$MPORT:0" 128 || true
311                 pkill -f peers/user/pfiled || true
312                 sleep 0.5
313                 unload_all
314                 ;;
315         *)
316                 help
317                 ;;
318         esac
319         ;;
320 mapperd)
321         case $2 in
322         start)
323                 spawn_mapperdc
324                 ;;
325         stop)
326                 pkill -f peers/user/mt-mapperd || true
327                 ;;
328         *)
329                 help
330                 ;;
331         esac
332         ;;
333 doublesosd)
334         case $2 in
335         start)
336                 load_all
337                 sleep 0.5
338                 spawn_sosd
339                 spawn_sosdm
340                 spawn_mapperdc
341                 spawn_vlmcd
342                 ;;
343         stop)
344                 pkill -f peers/user/st-vlmcd || true
345 #               free_requests "$VPORT:0" 128 || true
346                 pkill -f peers/user/mt-mapperd || true
347 #               free_requests "$MPORT:0" 128 || true
348                 pkill -f peers/user/mt-sosd || true
349                 sleep 0.5
350                 unload_all
351                 ;;
352         *)
353                 help
354                 ;;
355         esac
356         ;;
357 mtmapperd)
358         case $2 in
359         start)
360                 spawn_mapperdc
361                 ;;
362         stop)
363                 pkill -f peers/user/mt-mapperd || true
364                 ;;
365         *)
366                 help
367                 ;;
368         esac
369         ;;
370 *)
371         help
372         ;;
373 esac