/
cake-autorate_lib.sh
348 lines (288 loc) · 6.69 KB
/
cake-autorate_lib.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
#!/bin/bash
# cake-autorate_lib.sh -- common functions for use by cake-autorate.sh
# This file is part of cake-autorate.
__set_e=0
if [[ ! ${-} =~ e ]]; then
set -e
__set_e=1
fi
exec {__sleep_fd}<> <(:) || true
sleep_s()
{
# Calling the external sleep binary could be rather slow,
# especially as it is called very frequently and typically on mediocre hardware.
#
# bash's loadable sleep module is not typically available
# in OpenWRT and most embedded systems, and use of the bash
# read command with a timeout offers performance that is
# at least on a par with bash's sleep module.
#
# For benchmarks, check the following links:
# - https://github.com/lynxthecat/cake-autorate/issues/174#issuecomment-1460057382
# - https://github.com/lynxthecat/cake-autorate/issues/174#issuecomment-1460074498
local sleep_duration_s=${1} # (seconds, e.g. 0.5, 1 or 1.5)
read -r -t "${sleep_duration_s}" -u "${__sleep_fd}" || : &
wait "${!}"
}
sleep_us()
{
local sleep_duration_us=${1} # (microseconds)
sleep_duration_s=000000${sleep_duration_us}
sleep_duration_s=$((10#${sleep_duration_s::-6})).${sleep_duration_s: -6}
sleep_s "${sleep_duration_s}"
}
sleep_remaining_tick_time()
{
# sleeps until the end of the tick duration
local t_start_us=${1} # (microseconds)
local tick_duration_us=${2} # (microseconds)
# shellcheck disable=SC2154
sleep_duration_us=$(( t_start_us + tick_duration_us - ${EPOCHREALTIME/./} ))
if (( sleep_duration_us > 0 )); then
sleep_us "${sleep_duration_us}"
fi
}
randomize_array()
{
local -n array=${1}
subset=("${array[@]}")
array=()
for ((set=${#subset[@]}; set>0; set--))
do
idx=$((RANDOM%set))
array+=("${subset[idx]}")
unset "subset[idx]"
subset=("${subset[@]}")
done
}
lock()
{
local path=${1}
while true; do
( set -o noclobber; echo "$$" > "${path:?}" ) 2> /dev/null && return 0
sleep_us 100000
done
}
unlock()
{
local path=${1}
rm -f "${path:?}"
}
_proc_man_set_key()
{
local key=${1}
local value=${2}
lock "${PROC_STATE_FILE_LOCK:?}"
trap 'unlock "${PROC_STATE_FILE_LOCK:?}"' RETURN
local entered=0
while read -r line; do
if [[ ${line} =~ ^${key}= ]]; then
printf '%s\n' "${key}=${value}"
entered=1
else
printf '%s\n' "${line}"
fi
done < "${PROC_STATE_FILE:?}" > "${PROC_STATE_FILE:?}.tmp"
if (( entered == 0 )); then
printf '%s\n' "${key}=${value}" >> "${PROC_STATE_FILE:?}.tmp"
fi
mv "${PROC_STATE_FILE:?}.tmp" "${PROC_STATE_FILE:?}"
return 0
}
_proc_man_get_key_value()
{
local key=${1}
lock "${PROC_STATE_FILE_LOCK:?}"
trap 'unlock "${PROC_STATE_FILE_LOCK:?}"' RETURN
while read -r line; do
if [[ ${line} =~ ^${key}= ]]; then
printf '%s\n' "${line#*=}"
return 0
fi
done < "${PROC_STATE_FILE:?}"
return 1
}
proc_man()
{
local action=${1}
local name=${2}
shift 2
if [[ ! -f "${PROC_STATE_FILE:?}" ]]; then
return 1
fi
# shellcheck disable=SC2311
case "${action}" in
"start")
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )) && kill -0 "${pid}" 2> /dev/null; then
return 1;
fi
"${@}" &
local pid=${!}
_proc_man_set_key "${name}" "${pid}"
;;
"stop")
local pid
pid=$(_proc_man_get_key_value "${name}")
if ! (( pid && pid > 0 )); then
return 0;
fi
kill "${pid}" 2> /dev/null || true
# wait for process to die
killed=0
for ((i=0; i<10; i++));
do
if kill -0 "${pid}" 2> /dev/null; then
sleep_us 100000
else
killed=1
break
fi
done
# if process still alive, kill it with fire
if (( killed == 0 )); then
kill -9 "${pid}" 2> /dev/null || true
fi
_proc_man_set_key "${name}" "-1" "${PROC_STATE_FILE:?}"
;;
"status")
local pid
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )); then
if kill -0 "${pid}" 2> /dev/null; then
printf '%s\n' "running"
else
printf '%s\n' "dead"
fi
else
printf '%s\n' "stopped"
fi
;;
"wait")
local pid
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )); then
wait "${pid}" && return 0
fi
return 1
;;
"signal")
shift 3
local pid
pid=$(_proc_man_get_key_value "${name}")
if (( pid && pid > 0 )); then
kill -s "${1}" "${pid}" 2>/dev/null && return 0
fi
return 1
;;
"initialize")
proc_man_initialize
return $?
;;
*)
printf '%s\n' "unknown action: ${action}" >&2
return 1
;;
esac
return 0
}
proc_man_initialize()
{
true > "${PROC_STATE_FILE:?}"
}
proc_man_start()
{
proc_man start "${@}"
}
proc_man_stop()
{
proc_man stop "${@}"
}
proc_man_status()
{
proc_man status "${@}"
}
proc_man_wait()
{
proc_man wait "${@}"
}
proc_man_signal()
{
proc_man signal "${@}"
}
var_store()
{
trap '' INT
while read -u "${var_store_input_fd}" -r command arg1 arg2 arg3 arg4
do
case ${command} in
WRITE)
declare ${arg1}=${arg2}
printf "${arg3}\n" > ${arg4}
;;
READ)
if [[ -v "${arg1}" ]]
then
printf "${!arg1} ${arg2}\n" > ${arg3}
else
printf "unset ${arg2}\n" > ${arg3}
fi
;;
*)
printf '%s\n' "Unknown command: ${command}" >&2
;;
esac
done
}
var_store_write()
{
local var_name=${1}
local var_value=${2}
write_timestamp=${EPOCHREALTIME}
printf "WRITE ${var_name} ${var_value} ${write_timestamp} ${var_store_output_fifo}\n" >&"${var_store_input_fd}"
read -r received_write_timestamp < ${var_store_output_fifo}
while [[ "${received_write_timestamp}" != "${write_timestamp}" ]]
do
read -r received_write_timestamp < ${var_store_output_fifo}
done
}
var_store_read()
{
local -n var_name=${1}
local -n var_dest=${2:-${1}}
local exit_on_unset=${3:-1}
read_timestamp=${EPOCHREALTIME}
printf "READ ${!var_name} ${read_timestamp} ${var_store_output_fifo}\n" >&"${var_store_input_fd}"
read -r var_dest received_read_timestamp < ${var_store_output_fifo}
while [[ "${received_read_timestamp}" != "${read_timestamp}" ]]
do
read -r var_dest received_read_timestamp < ${var_store_output_fifo}
done
if [[ "${var_dest}" == "unset" ]]
then
if ((exit_on_unset))
then
printf '%s\n' "${!var_name} unset and exit_on_unset enabled." >&2
kill -$$ INT
else
return 1
fi
fi
}
var_store_link()
{
local path=${1}
local name=${2}
var_store_output_fifo="${path}/${name}_var_store_output_fifo"
[[ -p ${var_store_output_fifo} ]] && printf '%s\n' "${name} already linked" >&2
mkfifo ${var_store_output_fifo}
exec <>${var_store_output_fifo}
}
var_store_unlink()
{
[[ -p ${var_store_output_fifo} ]] && rm ${var_store_output_fifo}
}
if (( __set_e == 1 )); then
set +e
fi
unset __set_e