forked from xiaoyuyang0901/Apollo-code-with-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apollo_base.sh
364 lines (318 loc) · 9.39 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
#!/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.
###############################################################################
TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
source ${TOP_DIR}/scripts/apollo.bashrc
HOST_ARCH="$(uname -m)"
function set_lib_path() {
local CYBER_SETUP="${APOLLO_ROOT_DIR}/cyber/setup.bash"
[ -e "${CYBER_SETUP}" ] && . "${CYBER_SETUP}"
pathprepend ${APOLLO_ROOT_DIR}/modules/tools PYTHONPATH
pathprepend ${APOLLO_ROOT_DIR}/modules/teleop/common PYTHONPATH
pathprepend /apollo/modules/teleop/common/scripts
}
function create_data_dir() {
local DATA_DIR="${APOLLO_ROOT_DIR}/data"
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 setup_device_for_aarch64() {
local can_dev="/dev/can0"
local socket_can_dev="can0"
if [ ! -e "${can_dev}" ]; then
warning "No CAN device named ${can_dev}. "
fi
if [[ -x "$(command -v ip)" ]]; then
if ! ip link show type can | grep "${socket_can_dev}" &>/dev/null; then
warning "No SocketCAN device named ${socket_can_dev}."
else
sudo ip link set can0 type can bitrate 500000
sudo ip link set can0 up
fi
else
warning "ip command not found."
fi
}
function setup_device_for_amd64() {
# setup CAN device
local NUM_PORTS=8
for i in $(seq 0 $((${NUM_PORTS} - 1))); do
if [[ -e /dev/can${i} ]]; then
continue
elif [[ -e /dev/zynq_can${i} ]]; then
# soft link if sensorbox exist
sudo ln -s /dev/zynq_can${i} /dev/can${i}
else
break
# sudo mknod --mode=a+rw /dev/can${i} c 52 ${i}
fi
done
# Check Nvidia device
if [[ ! -e /dev/nvidia0 ]]; then
warning "No device named /dev/nvidia0"
fi
if [[ ! -e /dev/nvidiactl ]]; then
warning "No device named /dev/nvidiactl"
fi
if [[ ! -e /dev/nvidia-uvm ]]; then
warning "No device named /dev/nvidia-uvm"
fi
if [[ ! -e /dev/nvidia-uvm-tools ]]; then
warning "No device named /dev/nvidia-uvm-tools"
fi
if [[ ! -e /dev/nvidia-modeset ]]; then
warning "No device named /dev/nvidia-modeset"
fi
}
function setup_device() {
if [ "$(uname -s)" != "Linux" ]; then
info "Not on Linux, skip mapping devices."
return
fi
if [[ "${HOST_ARCH}" == "x86_64" ]]; then
setup_device_for_amd64
else
setup_device_for_aarch64
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
info "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}"
info "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 -f "modules/${MODULE_PATH}/launch/${MODULE}.launch" | grep -cv '^1$')"
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_ROOT_DIR}/modules/${MODULE_PATH}/launch/${MODULE}.launch &" &>/dev/null
sleep 0.5
is_stopped_customized_path "${MODULE_PATH}" "${MODULE}"
if [ $? -eq 0 ]; then
ok "Launched module ${MODULE}."
return 0
else
error "Could not launch module ${MODULE}. Is it already built?"
return 1
fi
else
info "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
info "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
ok "Launched module ${MODULE} in prof mode."
echo -e " Export profile by command:\n\t${YELLOW}google-pprof --pdf $BINARY $PROF_FILE > ${MODULE}_prof.pdf${NO_COLOR}"
return 0
else
error "Could not launch module ${MODULE}. Is it already built?"
return 1
fi
else
info "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_ROOT_DIR}/modules/${MODULE_PATH}/launch/${MODULE}.launch"
else
info "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
info "${MODULE} process is not running!"
return
fi
cyber_launch stop "${APOLLO_ROOT_DIR}/modules/${MODULE_PATH}/launch/${MODULE}.launch"
if [ $? -eq 0 ]; then
ok "Successfully stopped module ${MODULE}."
else
info "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
warning "Not in Git repo, maybe because you are in release container."
info "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_module() {
local module=$1
shift
run_customized_path $module $module "$@"
}
unset OMP_NUM_THREADS
if [ $APOLLO_IN_DOCKER = "true" ]; then
create_data_dir
set_lib_path $1
if [ -z $APOLLO_BASE_SOURCED ]; then
determine_bin_prefix
export APOLLO_BASE_SOURCED=1
fi
fi