diff --git a/kodev b/kodev index 336d95f6a..d724910b2 100755 --- a/kodev +++ b/kodev @@ -17,6 +17,64 @@ function check_submodules() { fi } +# Takes two arguments: +# $1 arguments to pass to pgrep +# $2 process name to pgrep for +function gnuplot_wrapper() { + # inspired by https://gist.github.com/nicolasazrak/32d68ed6c845a095f75f037ecc2f0436 + trap capture_ctrl_c INT + TEMP_DIR=$(mktemp --directory /tmp/tmp.koreaderXXX) + LOG="$TEMP_DIR/memory.log" + SCRIPT_PNG="$TEMP_DIR/script_png.p" + SCRIPT_SHOW="$TEMP_DIR/script_show.p" + IMAGE_PNG="$TEMP_DIR/graph.png" + + echo "Memory plot output to $TEMP_DIR" + + cat >"$SCRIPT_PNG" <"$SCRIPT_SHOW" <"${LOG}" + while true; do + # shellcheck disable=SC2086 + ps -p "$(pgrep --delimiter ' ' $1 "$2")" -o pid=,vsz=,rss= >>"${LOG}" + sleep 1 + done & + LOOP_PID=$! + gnuplot "$SCRIPT_SHOW" & + GNUPLOT_PID=$! +} + function setup_env() { files=$(ls -d ./koreader-emulator-*/koreader) assert_ret_zero $? "Emulator not found, please build it first." @@ -288,8 +346,7 @@ function kodev-wbuilder() { echo "[*] Running wbuilder.lua..." pushd "${EMU_DIR}" && { EMULATE_READER_W=540 EMULATE_READER_H=720 ./luajit ./tools/wbuilder.lua - } || exit - popd + } && popd || exit } function kodev-run() { @@ -305,6 +362,7 @@ OPTIONS: --disable-touch use this if you want to simulate keyboard only devices -s=FOO --simulate=FOO simulate dimension and other specs for a given device model supported model: kobo-aura-one, kindle3, hidpi + --graph graph memory use (requires gnuplot) " screen_width=540 screen_height=720 @@ -318,6 +376,9 @@ OPTIONS: --no-build) no_build=true ;; + --graph) + graph_memory=true + ;; -w | --screen-width) screen_width=${VALUE} ;; @@ -374,6 +435,10 @@ OPTIONS: exit 1 fi + if [ ${graph_memory} ]; then + gnuplot_wrapper "--parent $$" "reader.lua" + fi + echo "[*] Running KOReader with arguments: $*..." pushd "${EMU_DIR}" && { if [ $# -lt 1 ]; then @@ -389,8 +454,11 @@ OPTIONS: ./reader.lua -d "$args" RETURN_VALUE=$? done - } || exit - popd + } && popd || exit + + if [ ${graph_memory} ]; then + capture_ctrl_c + fi } function kodev-test() { @@ -407,6 +475,9 @@ OPTIONS: PARAM=$(echo "$1" | awk -F= '{print $1}') VALUE=$(echo "$1" | awk -F= '{print $2}') case $PARAM in + --graph) + graph_memory=true + ;; --tags) opts="--tags=${VALUE}" ;; @@ -435,6 +506,7 @@ OPTIONS: check_submodules && make setup_env + make "${EMU_DIR}/.busted" pushd "${EMU_DIR}" && { test_path="./spec/$1/unit" @@ -450,8 +522,7 @@ OPTIONS: --lazy \ -o "./spec/$1/unit/verbose_print" \ --exclude-tags=notest "${test_path}" - } || exit - popd + } && popd || exit } function kodev-cov() { @@ -514,8 +585,7 @@ OPTIONS: +$((LUACOV_REPORT_SUMMARY - 1)) \ luacov.report.out fi - } || exit - popd + } && popd || exit } function kodev-log() { @@ -609,8 +679,7 @@ case $1 in kodev-build pushd "${EMU_DIR}" && { ./luajit -i setupkoenv.lua - } || exit - popd + } && popd || exit ;; log) shift 1