forked from ApolloAuto/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apollo_base.sh
456 lines (397 loc) · 11.7 KB
/
apollo_base.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/env bash
###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
APOLLO_ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
BOLD='\033[1m'
RED='\033[0;31m'
GREEN='\033[32m'
WHITE='\033[34m'
YELLOW='\033[33m'
NO_COLOR='\033[0m'
function info() {
(>&2 echo -e "[${WHITE}${BOLD}INFO${NO_COLOR}] $*")
}
function error() {
(>&2 echo -e "[${RED}ERROR${NO_COLOR}] $*")
}
function warning() {
(>&2 echo -e "${YELLOW}[WARNING] $*${NO_COLOR}")
}
function ok() {
(>&2 echo -e "[${GREEN}${BOLD} OK ${NO_COLOR}] $*")
}
function print_delim() {
echo '============================'
}
function get_now() {
echo $(date +%s)
}
function print_time() {
END_TIME=$(get_now)
ELAPSED_TIME=$(echo "$END_TIME - $START_TIME" | bc -l)
MESSAGE="Took ${ELAPSED_TIME} seconds"
info "${MESSAGE}"
}
function success() {
print_delim
ok "$1"
print_time
print_delim
}
function fail() {
print_delim
error "$1"
print_time
print_delim
exit -1
}
function check_in_docker() {
if [ -f /.dockerenv ]; then
APOLLO_IN_DOCKER=true
else
APOLLO_IN_DOCKER=false
fi
export APOLLO_IN_DOCKER
}
function set_lib_path() {
if [ "$RELEASE_DOCKER" == 1 ];then
local CYBER_SETUP="/apollo/cyber/setup.bash"
if [ -e "${CYBER_SETUP}" ]; then
source "${CYBER_SETUP}"
fi
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/apollo/lib:/usr/local/apollo/local_integ/lib
export LD_LIBRARY_PATH=/usr/local/adolc/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/Qt5.5.1/5.5/gcc_64/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/fast-rtps/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/apollo/libtorch/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/apollo/libtorch_gpu/lib:$LD_LIBRARY_PATH
PY_LIB_PATH=/apollo/lib
PY_TOOLS_PATH=/apollo/modules/tools
else
local CYBER_SETUP="/apollo/cyber/setup.bash"
if [ -e "${CYBER_SETUP}" ]; then
source "${CYBER_SETUP}"
fi
PY_LIB_PATH=${APOLLO_ROOT_DIR}/py_proto
PY_TOOLS_PATH=${APOLLO_ROOT_DIR}/modules/tools
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/apollo/lib:/apollo/bazel-genfiles/external/caffe/lib
export LD_LIBRARY_PATH=/usr/local/adolc/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/apollo/third_party/rss/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/apollo/libtorch/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/apollo/libtorch_gpu/lib:$LD_LIBRARY_PATH
fi
export PYTHONPATH=/usr/local/lib/python2.7/dist-packages:${PY_LIB_PATH}:${PY_TOOLS_PATH}:${PYTHONPATH}
if [ -e /usr/local/cuda-8.0/ ];then
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=/usr/local/cuda-8.0/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/usr/local/cuda-8.0/include:$CPLUS_INCLUDE_PATH
fi
}
function create_data_dir() {
local DATA_DIR=""
if [ "$RELEASE_DOCKER" != "1" ];then
DATA_DIR="${APOLLO_ROOT_DIR}/data"
else
DATA_DIR="${HOME}/data"
fi
mkdir -p "${DATA_DIR}/log"
mkdir -p "${DATA_DIR}/bag"
mkdir -p "${DATA_DIR}/core"
}
function determine_bin_prefix() {
APOLLO_BIN_PREFIX=$APOLLO_ROOT_DIR
if [ -e "${APOLLO_ROOT_DIR}/bazel-bin" ]; then
APOLLO_BIN_PREFIX="${APOLLO_ROOT_DIR}/bazel-bin"
fi
export APOLLO_BIN_PREFIX
}
function find_device() {
# ${1} = device pattern
local device_list=$(find /dev -name "${1}")
if [ -z "${device_list}" ]; then
warning "Failed to find device with pattern \"${1}\" ..."
else
local devices=""
for device in $(find /dev -name "${1}"); do
ok "Found device: ${device}."
devices="${devices} --device ${device}:${device}"
done
echo "${devices}"
fi
}
function setup_device() {
if [ $(uname -s) != "Linux" ]; then
echo "Not on Linux, skip mapping devices."
return
fi
# setup CAN device
for INDEX in `seq 0 3`
do
# soft link if sensorbox exist
if [ -e /dev/zynq_can${INDEX} ] && [ ! -e /dev/can${INDEX} ]; then
sudo ln -s /dev/zynq_can${INDEX} /dev/can${INDEX}
fi
if [ ! -e /dev/can${INDEX} ]; then
sudo mknod --mode=a+rw /dev/can${INDEX} c 52 $INDEX
fi
done
MACHINE_ARCH=$(uname -m)
if [ "$MACHINE_ARCH" == 'aarch64' ]; then
sudo ip link set can0 type can bitrate 500000
sudo ip link set can0 up
fi
# setup nvidia device
sudo /sbin/modprobe nvidia
sudo /sbin/modprobe nvidia-uvm
if [ ! -e /dev/nvidia0 ];then
sudo mknod -m 666 /dev/nvidia0 c 195 0
fi
if [ ! -e /dev/nvidiactl ];then
sudo mknod -m 666 /dev/nvidiactl c 195 255
fi
if [ ! -e /dev/nvidia-uvm ];then
sudo mknod -m 666 /dev/nvidia-uvm c 243 0
fi
if [ ! -e /dev/nvidia-uvm-tools ];then
sudo mknod -m 666 /dev/nvidia-uvm-tools c 243 1
fi
if [ ! -e /dev/nvidia-uvm-tools ];then
sudo mknod -m 666 /dev/nvidia-uvm-tools c 243 1
fi
}
function decide_task_dir() {
# Try to find largest NVMe drive.
DISK="$(df | grep "^/dev/nvme" | sort -nr -k 4 | \
awk '{print substr($0, index($0, $6))}')"
# Try to find largest external drive.
if [ -z "${DISK}" ]; then
DISK="$(df | grep "/media/${DOCKER_USER}" | sort -nr -k 4 | \
awk '{print substr($0, index($0, $6))}')"
fi
if [ -z "${DISK}" ]; then
echo "Cannot find portable disk. Fallback to apollo data dir."
DISK="/apollo"
fi
# Create task dir.
BAG_PATH="${DISK}/data/bag"
TASK_ID=$(date +%Y-%m-%d-%H-%M-%S)
TASK_DIR="${BAG_PATH}/${TASK_ID}"
mkdir -p "${TASK_DIR}"
echo "Record bag to ${TASK_DIR}..."
export TASK_ID="${TASK_ID}"
export TASK_DIR="${TASK_DIR}"
}
function is_stopped_customized_path() {
MODULE_PATH=$1
MODULE=$2
NUM_PROCESSES="$(pgrep -c -f "modules/${MODULE_PATH}/launch/${MODULE}.launch")"
if [ "${NUM_PROCESSES}" -eq 0 ]; then
return 1
else
return 0
fi
}
function start_customized_path() {
MODULE_PATH=$1
MODULE=$2
shift 2
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 1 ]; then
eval "nohup cyber_launch start /apollo/modules/${MODULE_PATH}/launch/${MODULE}.launch &"
sleep 0.5
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 0 ]; then
echo "Launched module ${MODULE}."
return 0
else
echo "Could not launch module ${MODULE}. Is it already built?"
return 1
fi
else
echo "Module ${MODULE} is already running - skipping."
return 2
fi
}
function start() {
MODULE=$1
shift
start_customized_path $MODULE $MODULE "$@"
}
function start_prof_customized_path() {
MODULE_PATH=$1
MODULE=$2
shift 2
echo "Make sure you have built with 'bash apollo.sh build_prof'"
LOG="${APOLLO_ROOT_DIR}/data/log/${MODULE}.out"
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 1 ]; then
PROF_FILE="/tmp/$MODULE.prof"
rm -rf $PROF_FILE
BINARY=${APOLLO_BIN_PREFIX}/modules/${MODULE_PATH}/${MODULE}
eval "CPUPROFILE=$PROF_FILE $BINARY \
--flagfile=modules/${MODULE_PATH}/conf/${MODULE}.conf \
--log_dir=${APOLLO_ROOT_DIR}/data/log $@ </dev/null >${LOG} 2>&1 &"
sleep 0.5
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 0 ]; then
echo -e "Launched module ${MODULE} in prof mode. \nExport profile by command:"
echo -e "${YELLOW}google-pprof --pdf $BINARY $PROF_FILE > ${MODULE}_prof.pdf${NO_COLOR}"
return 0
else
echo "Could not launch module ${MODULE}. Is it already built?"
return 1
fi
else
echo "Module ${MODULE} is already running - skipping."
return 2
fi
}
function start_prof() {
MODULE=$1
shift
start_prof_customized_path $MODULE $MODULE "$@"
}
function start_fe_customized_path() {
MODULE_PATH=$1
MODULE=$2
shift 2
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 1 ]; then
eval "cyber_launch start /apollo/modules/${MODULE_PATH}/launch/${MODULE}.launch"
else
echo "Module ${MODULE} is already running - skipping."
return 2
fi
}
function start_fe() {
MODULE=$1
shift
start_fe_customized_path $MODULE $MODULE "$@"
}
function start_gdb_customized_path() {
MODULE_PATH=$1
MODULE=$2
shift 2
eval "gdb --args ${APOLLO_BIN_PREFIX}/modules/${MODULE_PATH}/${MODULE} \
--flagfile=modules/${MODULE_PATH}/conf/${MODULE}.conf \
--log_dir=${APOLLO_ROOT_DIR}/data/log $@"
}
function start_gdb() {
MODULE=$1
shift
start_gdb_customized_path $MODULE $MODULE "$@"
}
function stop_customized_path() {
MODULE_PATH=$1
MODULE=$2
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 1 ]; then
echo "${MODULE} process is not running!"
return
fi
cyber_launch stop "/apollo/modules/${MODULE_PATH}/launch/${MODULE}.launch"
if [ $? -eq 0 ]; then
echo "Successfully stopped module ${MODULE}."
else
echo "Module ${MODULE} is not running - skipping."
fi
}
function stop() {
MODULE=$1
stop_customized_path $MODULE $MODULE
}
# Note: This 'help' function here will overwrite the bash builtin command 'help'.
# TODO: add a command to query known modules.
function help() {
cat <<EOF
Invoke ". scripts/apollo_base.sh" within docker to add the following commands to the environment:
Usage: COMMAND [<module_name>]
COMMANDS:
help: show this help message
start: start the module in background
start_fe: start the module without putting in background
start_gdb: start the module with gdb
stop: stop the module
EOF
}
function run_customized_path() {
local module_path=$1
local module=$2
local cmd=$3
shift 3
case $cmd in
start)
start_customized_path $module_path $module "$@"
;;
start_fe)
start_fe_customized_path $module_path $module "$@"
;;
start_gdb)
start_gdb_customized_path $module_path $module "$@"
;;
start_prof)
start_prof_customized_path $module_path $module "$@"
;;
stop)
stop_customized_path $module_path $module
;;
help)
help
;;
*)
start_customized_path $module_path $module $cmd "$@"
;;
esac
}
# Write log to a file about the env when record a bag.
function record_bag_env_log() {
if [ -z "${TASK_ID}" ]; then
TASK_ID=$(date +%Y-%m-%d-%H-%M)
fi
git status >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Not in Git repo, maybe because you are in release container."
echo "Skip log environment."
return
fi
commit=$(git log -1)
echo -e "Date:$(date)\n" >> Bag_Env_$TASK_ID.log
git branch | awk '/\*/ { print "current branch: " $2; }' >> Bag_Env_$TASK_ID.log
echo -e "\nNewest commit:\n$commit" >> Bag_Env_$TASK_ID.log
echo -e "\ngit diff:" >> Bag_Env_$TASK_ID.log
git diff >> Bag_Env_$TASK_ID.log
echo -e "\n\n\n\n" >> Bag_Env_$TASK_ID.log
echo -e "git diff --staged:" >> Bag_Env_$TASK_ID.log
git diff --staged >> Bag_Env_$TASK_ID.log
}
# run command_name module_name
function run() {
local module=$1
shift
run_customized_path $module $module "$@"
}
CYBER_SETUP="/apollo/cyber/setup.bash"
if [ -e "${CYBER_SETUP}" ]; then
source "${CYBER_SETUP}"
fi
check_in_docker
create_data_dir
if [ -z $APOLLO_BASE_SOURCED ]; then
set_lib_path
determine_bin_prefix
export APOLLO_BASE_SOURCED=1
fi