-
Notifications
You must be signed in to change notification settings - Fork 2
/
chia-plotter.sh
executable file
·180 lines (149 loc) · 4.17 KB
/
chia-plotter.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
#!/bin/bash
set -euo pipefail
script_root="$(cd "$(dirname "$(readlink "$([[ "${OSTYPE}" == linux* ]] && echo "-f")" "$0")")"; pwd)"
source "${script_root}/lib/utils.sh"
APPNAME=chia-plot
RESERVE_MEMORY_MB=6144
usage() {
if [ $# -eq 0 ]; then
echo ""
echo "${APPNAME}"
echo ""
echo "Usage:"
echo ""
printf "%s\n" " ${APPNAME} <subcommand> [arguments]"
echo ""
echo "Where subcommand is:"
echo ""
echo " plot <tmp-dir> [tmp2-dir] <dest-dir>"
echo " Plot to dest-dir using tmp-dir. Optionally specify tmp2-dir."
echo ""
echo " list"
echo " List all plotting sessions."
echo ""
echo " attach <tmp-dir>"
echo " Attach to a session related to specified tmp dir."
echo ""
echo " kill <tmp-dir>"
echo " Kill a session related to specified tmp dir."
echo ""
echo " kill-all"
echo " Kill all plotting sessions."
echo ""
echo " clean <dir>"
echo " Clean all temporary files on specified disk."
echo " Be careful not to do this while plotting is"
echo " in progress."
echo ""
exit 1
fi
}
get_total_memory_mb() {
require calc "Please install calc: apt install calc"
calc "floor($(grep MemTotal /proc/meminfo | awk '{print $2}') / 1024)"
}
get_memory_for_plotting_mb() {
local total
total="$(get_total_memory_mb)"
calc "${total} - ${RESERVE_MEMORY_MB}"
}
get_cpu_threads() {
nproc --all
}
get_memory_for_single_plotter_mb() {
local total
total="$(get_memory_for_plotting_mb)"
local cpu_theads
cpu_threads="$(get_cpu_threads)"
calc "${total} / ${cpu_threads}"
}
do_plot() {
[ "$#" -eq 3 ] || [ "$#" -eq 2 ] || fatal "Expected tmp and dest dir names OR tmp, tmp2 and dest dir names"
local tmp_dir
local tmp2_dir
local dest_dir
if [ "$#" -eq 3 ]; then
tmp_dir="$1"
tmp2_dir="$2"
dest_dir="$3"
else
tmp_dir="$1"
tmp2_dir="${tmp_dir}"
dest_dir="$2"
fi
require ts "Please install ts: apt install moreutils"
require screen "Please install screen: apt install screen"
if screen -list | grep -q "chia-$(basename "${tmp_dir}")"; then
fatal "Session already exists"
fi
local total_memory
total_memory="$(get_total_memory_mb)"
local memory_for_plotting
memory_for_plotting="$(get_memory_for_plotting_mb)"
local cpu_theads
cpu_threads="$(get_cpu_threads)"
local memory_for_single_plotter
memory_for_single_plotter="$(calc "min(3400, floor(${memory_for_plotting} / ${cpu_threads}))")"
printf "Total memory (MB): %d\n" "${total_memory}"
printf "Memory for plotting (MB): %d\n" "${memory_for_plotting}"
printf "Number of CPU threads: %d\n" "${cpu_threads}"
printf "Memory for single plotter (MB): %d (2000 min, 3400 recomended)\n" "${memory_for_single_plotter}"
printf "Tmp dir: %s\n" "${tmp_dir}"
printf "Tmp2 dir: %s\n" "${tmp2_dir}"
printf "Dest dir: %s\n" "${dest_dir}"
echo ""
echo "Starting plotting..."
echo screen -dmS "chia-$(basename "${tmp_dir}")" "${script_root}/lib/run-single-plotter.sh" "${tmp_dir}" "${tmp2_dir}" "${dest_dir}" "${memory_for_single_plotter}"
screen -dmS "chia-$(basename "${tmp_dir}")" "${script_root}/lib/run-single-plotter.sh" "${tmp_dir}" "${tmp2_dir}" "${dest_dir}" "${memory_for_single_plotter}"
}
do_list() {
screen -ls | grep chia-
}
do_attach() {
[ "$#" -eq 1 ] || fatal "Expected dir name"
local dir="$1"
screen -r "chia-$(basename "${dir}")"
}
do_kill() {
[ "$#" -eq 1 ] || fatal "Expected dir name"
local dir="$1"
screen -XS "chia-$(basename "${dir}")" quit
}
do_kill_all() {
screen -ls | grep chia- | cut -d. -f1 | awk '{print $1}' | xargs kill
}
do_clean() {
[ "$#" -eq 1 ] || fatal "Expected dir name"
local dir="$1"
printf "Cleaning all temporary files on: %s\n" "${dir}"
find "${dir}" -name "plot-*.tmp" -exec rm -f {} \;
}
[ "$#" -ge 1 ] || {
usage
exit 1
}
subcommand="$1"
shift
case "$subcommand" in
plot)
do_plot "$@"
;;
list)
do_list "$@"
;;
attach)
do_attach "$@"
;;
kill)
do_kill "$@"
;;
kill-all)
do_kill_all "#@"
;;
clean)
do_clean "$@"
;;
*)
fatal "Unknown subcommand $subcommand"
;;
esac