File size: 11,460 Bytes
06c11b0 | 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 | #!/usr/bin/env bash
# run_evaluate_dataset_replay_parallel.sh
# Micromamba environment: /data/hongzefu/maniskillenv1114
#
# Usage examples:
# 1) Start parallel replay(default 16 envs, one process per env_id; auto-enter aggregated log monitor after start)
# bash run_evaluate_dataset_replay_parallel.sh start
# 2) Start parallel replay(manually specify env_ids)
# bash run_evaluate_dataset_replay_parallel.sh start --env_ids PickXtimes,StopCube
# 3) show only current active run status
# bash run_evaluate_dataset_replay_parallel.sh status
# 4) reconnect log monitor (Ctrl+C exits monitor only, does not stop jobs)
# bash run_evaluate_dataset_replay_parallel.sh monitor
# 5) stop all processes in the current active run
# bash run_evaluate_dataset_replay_parallel.sh stop
# 6) restart (stop first, then start with new env_ids; defaults to 16 if omitted)
# bash run_evaluate_dataset_replay_parallel.sh restart --env_ids PickXtimes,StopCube
set -u
set -o pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PYTHON_SCRIPT="${SCRIPT_DIR}/evaluate_dataset_replay-parallel.py"
MICROMAMBA_ENV="/data/hongzefu/maniskillenv1114"
PYTHON_BIN="${MICROMAMBA_ENV}/bin/python"
DEFAULT_ENV_IDS_CSV="PickXtimes,StopCube,SwingXtimes,BinFill,VideoUnmaskSwap,VideoUnmask,ButtonUnmaskSwap,ButtonUnmask,VideoRepick,VideoPlaceButton,VideoPlaceOrder,PickHighlight,InsertPeg,MoveCube,PatternLock,RouteStick"
LOG_ROOT="${SCRIPT_DIR}/logs/evaluate_dataset_replay_parallel"
ACTIVE_RUN_FILE="${LOG_ROOT}/active_run"
mkdir -p "${LOG_ROOT}"
show_usage() {
echo "Usage: $0 {start|monitor|status|stop|restart} [--env_ids A,B,C]"
echo ""
echo "Commands:"
echo " start [--env_ids A,B,C] Start one process per envid and attach monitor."
echo " If omitted, defaults to all 16 env ids."
echo " monitor Monitor logs of the current active run."
echo " status Show status of the current active run."
echo " stop Stop all processes in the current active run."
echo " restart [--env_ids A,B,C] Stop current active run, then start a new run."
echo " If omitted, defaults to all 16 env ids."
}
trim_whitespace() {
local value="$1"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
printf "%s" "${value}"
}
is_pid_alive() {
local pid="$1"
ps -p "${pid}" > /dev/null 2>&1
}
is_process_tree_alive() {
local pid="$1"
[ -z "${pid}" ] && return 1
if is_pid_alive "${pid}"; then
return 0
fi
ps -o pid= --ppid "${pid}" 2>/dev/null | grep -q .
}
kill_process_tree() {
local pid="$1"
local signal="${2:-15}"
[ -z "${pid}" ] && return
local children
children=$(ps -o pid= --ppid "${pid}" 2>/dev/null)
if [ -n "${children}" ]; then
for child in ${children}; do
kill_process_tree "${child}" "${signal}"
done
fi
kill "-${signal}" "${pid}" 2>/dev/null || true
}
get_active_run_dir() {
if [ ! -f "${ACTIVE_RUN_FILE}" ]; then
return 1
fi
local run_dir
run_dir="$(cat "${ACTIVE_RUN_FILE}")"
if [ -z "${run_dir}" ] || [ ! -d "${run_dir}" ]; then
return 1
fi
printf "%s\n" "${run_dir}"
}
parse_env_ids_csv() {
local csv="$1"
IFS=',' read -r -a raw_env_ids <<< "${csv}"
ENV_IDS=()
local env_id
for env_id in "${raw_env_ids[@]}"; do
env_id="$(trim_whitespace "${env_id}")"
if [ -n "${env_id}" ]; then
ENV_IDS+=("${env_id}")
fi
done
if [ "${#ENV_IDS[@]}" -eq 0 ]; then
return 1
fi
}
extract_env_ids_arg() {
local env_ids_csv=""
while [ "$#" -gt 0 ]; do
case "$1" in
--env_ids)
if [ -z "${2:-}" ]; then
echo "Error: --env_ids requires a value."
return 1
fi
env_ids_csv="$2"
shift 2
;;
*)
echo "Error: unknown argument '$1'."
return 1
;;
esac
done
if [ -z "${env_ids_csv}" ]; then
env_ids_csv="${DEFAULT_ENV_IDS_CSV}"
echo "Info: --env_ids not provided, using default 16 env ids." >&2
fi
printf "%s\n" "${env_ids_csv}"
}
validate_runtime() {
if [ ! -f "${PYTHON_SCRIPT}" ]; then
echo "Error: script not found: ${PYTHON_SCRIPT}"
return 1
fi
if [ ! -d "${MICROMAMBA_ENV}" ]; then
echo "Error: micromamba env not found: ${MICROMAMBA_ENV}"
return 1
fi
if [ ! -x "${PYTHON_BIN}" ]; then
echo "Error: python binary not executable: ${PYTHON_BIN}"
return 1
fi
}
run_has_alive_process() {
local run_dir="$1"
local pids_file="${run_dir}/pids.tsv"
[ -f "${pids_file}" ] || return 1
local env_id pid log_file
while IFS=$'\t' read -r env_id pid log_file; do
[ -z "${pid}" ] && continue
if is_process_tree_alive "${pid}"; then
return 0
fi
done < "${pids_file}"
return 1
}
monitor_run() {
local run_dir="${1:-}"
if [ -z "${run_dir}" ]; then
if ! run_dir="$(get_active_run_dir)"; then
echo "No active run found."
return 1
fi
fi
local pids_file="${run_dir}/pids.tsv"
if [ ! -s "${pids_file}" ]; then
echo "No pids.tsv found for run: ${run_dir}"
return 1
fi
local log_files=()
local env_id pid log_file
while IFS=$'\t' read -r env_id pid log_file; do
[ -z "${log_file}" ] && continue
log_files+=("${log_file}")
done < "${pids_file}"
if [ "${#log_files[@]}" -eq 0 ]; then
echo "No log files registered in ${pids_file}"
return 1
fi
echo "Monitoring run: ${run_dir}"
echo "Press Ctrl+C to exit monitor. Processes keep running."
tail -n 0 -F "${log_files[@]}"
}
status_run() {
local run_dir
if ! run_dir="$(get_active_run_dir)"; then
echo "Status: no active run."
return 0
fi
local pids_file="${run_dir}/pids.tsv"
if [ ! -f "${pids_file}" ]; then
echo "Status: active_run points to ${run_dir}, but pids.tsv is missing."
return 1
fi
local total=0
local alive=0
local env_id pid log_file state
echo "Active run: ${run_dir}"
while IFS=$'\t' read -r env_id pid log_file; do
[ -z "${pid}" ] && continue
total=$((total + 1))
if is_process_tree_alive "${pid}"; then
state="RUNNING"
alive=$((alive + 1))
else
state="EXITED"
fi
printf " [%s] pid=%s state=%s log=%s\n" "${env_id}" "${pid}" "${state}" "${log_file}"
done < "${pids_file}"
echo "Summary: alive=${alive}/${total}"
}
stop_run() {
local run_dir
if ! run_dir="$(get_active_run_dir)"; then
echo "No active run to stop."
return 0
fi
local pids_file="${run_dir}/pids.tsv"
if [ ! -f "${pids_file}" ]; then
echo "pids.tsv missing for run ${run_dir}. Clearing active run pointer."
rm -f "${ACTIVE_RUN_FILE}"
return 0
fi
local pids=()
local env_id pid log_file
while IFS=$'\t' read -r env_id pid log_file; do
[ -z "${pid}" ] && continue
pids+=("${pid}")
done < "${pids_file}"
if [ "${#pids[@]}" -eq 0 ]; then
echo "No PIDs recorded for run ${run_dir}."
rm -f "${ACTIVE_RUN_FILE}"
return 0
fi
echo "Stopping run: ${run_dir}"
local p
for p in "${pids[@]}"; do
kill_process_tree "${p}" 15
done
local i has_alive
for i in {1..15}; do
has_alive=0
for p in "${pids[@]}"; do
if is_process_tree_alive "${p}"; then
has_alive=1
break
fi
done
[ "${has_alive}" -eq 0 ] && break
sleep 1
done
for p in "${pids[@]}"; do
if is_process_tree_alive "${p}"; then
kill_process_tree "${p}" 9
fi
done
sleep 1
local remaining=0
for p in "${pids[@]}"; do
if is_process_tree_alive "${p}"; then
remaining=$((remaining + 1))
fi
done
rm -f "${ACTIVE_RUN_FILE}"
if [ "${remaining}" -eq 0 ]; then
echo "Stop complete: all processes from active run have exited."
else
echo "Stop complete with warnings: ${remaining} process trees still alive."
return 1
fi
}
start_run() {
local env_ids_csv="$1"
if ! validate_runtime; then
return 1
fi
if ! parse_env_ids_csv "${env_ids_csv}"; then
echo "Error: --env_ids is empty after parsing."
return 1
fi
local current_run
if current_run="$(get_active_run_dir 2>/dev/null)"; then
if run_has_alive_process "${current_run}"; then
echo "Error: active run is still alive: ${current_run}"
echo "Use: $0 stop"
return 1
fi
fi
local run_id
run_id="$(date +%Y%m%d_%H%M%S)"
local run_dir="${LOG_ROOT}/${run_id}"
mkdir -p "${run_dir}"
local pids_file="${run_dir}/pids.tsv"
: > "${pids_file}"
echo "Starting run: ${run_dir}"
local env_id safe_env log_file pid
for env_id in "${ENV_IDS[@]}"; do
safe_env="$(printf "%s" "${env_id}" | tr '/ ' '__')"
log_file="${run_dir}/${safe_env}.log"
if command -v stdbuf >/dev/null 2>&1; then
nohup env PATH="${MICROMAMBA_ENV}/bin:${PATH}" \
PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=utf-8 \
stdbuf -oL -eL "${PYTHON_BIN}" -u "${PYTHON_SCRIPT}" --envid "${env_id}" > "${log_file}" 2>&1 &
else
nohup env PATH="${MICROMAMBA_ENV}/bin:${PATH}" \
PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=utf-8 \
"${PYTHON_BIN}" -u "${PYTHON_SCRIPT}" --envid "${env_id}" > "${log_file}" 2>&1 &
fi
pid=$!
printf "%s\t%s\t%s\n" "${env_id}" "${pid}" "${log_file}" >> "${pids_file}"
echo " started envid=${env_id} pid=${pid} log=${log_file}"
done
printf "%s\n" "${run_dir}" > "${ACTIVE_RUN_FILE}"
echo "Run is detached with nohup. active_run=${run_dir}"
monitor_run "${run_dir}"
}
restart_run() {
local env_ids_csv="$1"
stop_run || true
start_run "${env_ids_csv}"
}
COMMAND="${1:-}"
case "${COMMAND}" in
start)
shift
ENV_IDS_CSV="$(extract_env_ids_arg "$@")" || { show_usage; exit 1; }
start_run "${ENV_IDS_CSV}"
;;
monitor)
shift
if [ "$#" -ne 0 ]; then
echo "Error: monitor takes no extra arguments."
show_usage
exit 1
fi
monitor_run
;;
status)
shift
if [ "$#" -ne 0 ]; then
echo "Error: status takes no extra arguments."
show_usage
exit 1
fi
status_run
;;
stop)
shift
if [ "$#" -ne 0 ]; then
echo "Error: stop takes no extra arguments."
show_usage
exit 1
fi
stop_run
;;
restart)
shift
ENV_IDS_CSV="$(extract_env_ids_arg "$@")" || { show_usage; exit 1; }
restart_run "${ENV_IDS_CSV}"
;;
*)
show_usage
exit 1
;;
esac
|