support parallelism in worker

pull/445/head
romkatv 5 years ago
parent a652d49bd9
commit da498aef57

@ -1,15 +1,59 @@
# invoked in worker: _p9k_worker_main <timeout> # invoked in worker: _p9k_worker_main <timeout>
function _p9k_worker_main() { function _p9k_worker_main() {
local _p9k_worker_buf _p9k_worker_cmd zmodload zsh/system || return
zmodload zsh/zselect || return
zselect -t0
(( $? == 1 )) || return
local req fd
local -A inflight # fd => id$'\x1f'sync
local -a ready
local _p9k_worker_tmout # empty or non-negative int, in hundredths of a second
while true; do
if zselect -a ready ${_p9k_worker_tmout:+-t$_p9k_worker_tmout} 0 ${(k)inflight}; then
[[ $ready[1] == -r ]] || return
for fd in ${ready:1}; do
if [[ $fd == 0 ]]; then
local buf=
while true; do while true; do
if sysread -t $1 '_p9k_worker_buf[$#_p9k_worker_method+1]'; then sysread -t 0 'buf[$#buf+1]' && continue
_p9k_worker_cmd=${_p9k_worker_buf%%$'\x1e'*} (( $? == 4 )) || return
if (( $#_p9k_worker_cmd != $#_p9k_worker_buf )); then [[ $buf[-1] == (|$'\x1e') ]] && break
_p9k_worker_buf[1,$#_p9k_worker_cmd+1]="" sysread 'buf[$#buf+1]' || return
eval $_p9k_worker_cmd done
for req in ${(ps:\x1e:)buf}; do
local parts=("${(@ps:\x1f:)req}") # id cond async sync
if () { eval $parts[2] }; then
if [[ -n $parts[3] ]]; then
sysopen -r -o cloexec -u fd <(
local REPLY=; eval $parts[3]; print -rn -- $REPLY) || return
inflight[$fd]=$parts[1]$'\x1f'$parts[4]
continue
fi
local REPLY=
() { eval $parts[4] }
fi fi
if [[ -n $parts[1] ]]; then
print -rn -- d$parts[1]$'\x1e' || return
fi
done
else else
(( $? == 4 )) || return local REPLY=
while true; do
sysread -i $fd 'REPLY[$#REPLY+1]' && continue
(( $? == 5 )) || return
break
done
local parts=("${(@ps:\x1f:)inflight[$fd]}") # id sync
() { eval $parts[2] }
if [[ -n $parts[1] ]]; then
print -rn -- d$parts[1]$'\x1e' || return
fi
unset "inflight[$fd]"
fi
done
else
(( $? == 1 )) || return
(( $+functions[_p9k_worker_on_timeout] )) && _p9k_worker_on_timeout (( $+functions[_p9k_worker_on_timeout] )) && _p9k_worker_on_timeout
fi fi
done done
@ -21,8 +65,8 @@ typeset -g _p9k__worker_resp_fd
typeset -g _p9k__worker_shell_pid typeset -g _p9k__worker_shell_pid
typeset -g _p9k__worker_file_prefix typeset -g _p9k__worker_file_prefix
typeset -ga _p9k__worker_params typeset -ga _p9k__worker_params
typeset -gA _p9k__worker_functions typeset -gA _p9k__worker_request_map
typeset -gA _p9k__worker_requests typeset -ga _p9k__worker_request_queue
# invoked in master: _p9k_worker_reply <list>... # invoked in master: _p9k_worker_reply <list>...
# #
@ -33,14 +77,15 @@ function _p9k_worker_reply() { eval $1 }
# usage: _p9k_worker_reply <list> # usage: _p9k_worker_reply <list>
function _p9k_worker_reply_remote() { print -rn -- e$1$'\x1e' } function _p9k_worker_reply_remote() { print -rn -- e$1$'\x1e' }
# invoked in worker: _p9k_worker_done <request-id> # invoked in worker: _p9k_worker_on_timeout
function _p9k_worker_done() { print -rn -- d$1$'\x1e' } function _p9k_worker_on_timeout() {
_p9k_worker_tmout=
_p9k_worker_reply _p9k_worker_keep_alive
}
# invoked in master: _p9k_worker_eval <list> # invoked in master: _p9k_worker_keep_alive
function _p9k_worker_eval() { function _p9k_worker_keep_alive() {
print -rnu $_p9k__worker_req_fd -- $1$'\x1e' && return _p9k_worker_invoke "" "_p9k_worker_tmout=100" "" ""
_p9k_worker_stop
return 1
} }
# invoked in master: _p9k_worker_send_params [param]... # invoked in master: _p9k_worker_send_params [param]...
@ -48,7 +93,7 @@ function _p9k_worker_send_params() {
[[ -z $_p9k__worker_resp_fd || $# == 0 ]] && return [[ -z $_p9k__worker_resp_fd || $# == 0 ]] && return
if [[ -n $_p9k__worker_req_fd ]]; then if [[ -n $_p9k__worker_req_fd ]]; then
{ {
typeset -p -- $* && print -rn -- $'\x1e' && return print -rn -- $'\x1f' && typeset -p -- $* && print -rn -- $'\x1f\x1f\x1e' && return
} >&$_p9k__worker_req_fd } >&$_p9k__worker_req_fd
_p9k_worker_stop _p9k_worker_stop
return 1 return 1
@ -57,30 +102,27 @@ function _p9k_worker_send_params() {
fi fi
} }
# invoked in master: _p9k_worker_invoke <request-id> <func> [arg]... # invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync>
function _p9k_worker_invoke() { function _p9k_worker_invoke() {
if [[ -n $_p9k__worker_resp_fd ]]; then if [[ -n $_p9k__worker_resp_fd ]]; then
if [[ -n $_p9k__worker_req_fd ]]; then local req=$1$'\x1f'$2$'\x1f'$3$'\x1f'$4$'\x1e'
local req if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then
if (( ! $+_p9k__worker_functions[$2] )); then [[ -n $1 ]] && _p9k__worker_request_map[$1]=
req+="function $2() { $functions[$2] ; }"$'\n' print -rnu $_p9k__worker_req_fd -- $req
_p9k__worker_functions[$2]= return
fi fi
if (( ! $+_p9k__worker_requests[$1] )); then if [[ -n $1 ]]; then
req+="${(j: :)${(@q)${@:2}}}"$'\n' (( $+_p9k__worker_request_map[$1] )) || _p9k__worker_request_queue+=$1
_p9k__worker_requests[$1]= _p9k__worker_request_map[$1]=$req
else else
_p9k__worker_requests[$1]="${(j: :)${(@q)${@:2}}}" _p9k__worker_request_queue+=$req
fi
if [[ -n $req ]]; then
_p9k_worker_eval $req"_p9k_worker_done ${(q)1}"
fi fi
else else
_p9k__worker_functions[$2]= if () { eval $2 }; then
_p9k__worker_requests[$1]="${(j: :)${(@q)${@:2}}}" local REPLY=
() { eval $3 }
() { eval $4 }
fi fi
else
"${@:2}"
fi fi
} }
@ -104,8 +146,8 @@ function _p9k_worker_stop() {
_p9k__worker_resp_fd= _p9k__worker_resp_fd=
_p9k__worker_shell_pid= _p9k__worker_shell_pid=
_p9k__worker_params=() _p9k__worker_params=()
_p9k__worker_requests=() _p9k__worker_request_map=()
_p9k__worker_functions=() _p9k__worker_request_queue=()
return 0 return 0
} }
@ -128,12 +170,12 @@ function _p9k_worker_receive() {
local arg=$resp[2,-1] local arg=$resp[2,-1]
case $resp[1] in case $resp[1] in
d) d)
local req=$_p9k__worker_requests[$arg] local req=$_p9k__worker_request_map[$arg]
if [[ -n $req ]]; then if [[ -n $req ]]; then
_p9k__worker_requests[$arg]= _p9k__worker_request_map[$arg]=
_p9k_worker_eval $req$'\n'"_p9k_worker_done ${(q)arg}" || return print -rnu $_p9k__worker_req_fd -- $req || return
else else
unset "_p9k__worker_requests[$arg]" unset "_p9k__worker_request_map[$arg]"
fi fi
;; ;;
e) e)
@ -157,29 +199,32 @@ function _p9k_worker_receive() {
setopt no_hist_expand extended_glob no_prompt_bang prompt_percent prompt_subst no_aliases setopt no_hist_expand extended_glob no_prompt_bang prompt_percent prompt_subst no_aliases
zmodload zsh/system zmodload zsh/system
zmodload zsh/datetime zmodload zsh/datetime
function _p9k_worker_main() { $functions[_p9k_worker_main] }
function _p9k_worker_reply() { $functions[_p9k_worker_reply_remote] }" || return function _p9k_worker_reply() { $functions[_p9k_worker_reply_remote] }" || return
local f if (( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )); then
for f in _p9k_worker_done _p9k_worker_main ${(k)_p9k__worker_functions}; do print -r -- "function _p9k_worker_on_timeout() {
print -r -- "function $f() { $functions[$f] }" || return $functions[_p9k_worker_on_timeout] }" || return
done fi
print -r -- "_p9k_worker_main" || return
print -rn -- $'\x1e' || return
if (( $#_p9k__worker_params )); then if (( $#_p9k__worker_params )); then
print -rn -- $'\x1f' || return
typeset -p -- $_p9k__worker_params || return typeset -p -- $_p9k__worker_params || return
print -rn -- $'\x1f\x1f\x1e' || return
_p9k__worker_params=() _p9k__worker_params=()
fi fi
local id list local req=
for id list in "${(@kv)_p9k__worker_requests}"; do for req in $_p9k__worker_request_queue; do
print -rl -- $list "_p9k_worker_done ${(q)id}" || return if [[ $req != *$'\x1e' ]]; then
_p9k__worker_requests[$id]= local id=$req
done req=$_p9k__worker_request_map[$id]
if (( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )); then _p9k__worker_request_map[$id]=
print -r -- "
function _p9k_worker_on_timeout() { _p9k_worker_reply '' }
_p9k_worker_main 1" || return
else
print -r -- "_p9k_worker_main -1" || return
fi fi
print -rn -- $'\x1e' || return print -rnu $_p9k__worker_req_fd -- $req || return
done
_p9k__worker_request_queue=()
} >&$_p9k__worker_req_fd } >&$_p9k__worker_req_fd
(( _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME )) && _p9k_worker_keep_alive
;; ;;
*) *)
return 1 return 1
@ -212,7 +257,7 @@ function _p9k_worker_start() {
fi fi
log_file=/tmp/log # todo: remove log_file=/tmp/log # todo: remove
trace= trace=x
local fifo=$_p9k__worker_file_prefix.fifo local fifo=$_p9k__worker_file_prefix.fifo
local cmd=( local cmd=(
@ -242,7 +287,7 @@ function _p9k_reset_prompt() {
emulate -L zsh -o prompt_subst # -o xtrace emulate -L zsh -o prompt_subst # -o xtrace
POWERLEVEL9K_WORKER_LOG_LEVEL=DEBUG POWERLEVEL9K_WORKER_LOG_LEVEL=DEBUG
_POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME=1 _POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME=0
typeset -F _p9k__last_prompt_update_time typeset -F _p9k__last_prompt_update_time
zmodload zsh/datetime zmodload zsh/datetime
@ -253,27 +298,50 @@ typeset -F start_time=EPOCHREALTIME
_p9k_worker_start _p9k_worker_start
echo -E - $((1000*(EPOCHREALTIME-start_time))) echo -E - $((1000*(EPOCHREALTIME-start_time)))
function compute_foo() { function foo_cond() {
local f="${(q+)1} ${(q+)bar} $((foo_counter++))" typeset -gi foo_counter
_p9k_worker_reply "typeset -g foo=${(q)f}" typeset -g foo="[$bar] cond $1 $((foo_counter++))"
} }
bar='rofl $ {' function foo_async() {
sleep 1
REPLY="$foo / async $1"
}
function foo_sync() {
REPLY+=" / sync $1"
_p9k_worker_reply "typeset -g foo=${(q)REPLY}"
}
() {
typeset -g RPROMPT='$foo %*'
typeset -g bar='lol'
_p9k_worker_send_params bar _p9k_worker_send_params bar
local f
for f in foo_{cond,async,sync}; do
_p9k_worker_invoke "" "function $f() { $functions[$f] }" "" ""
done
() { () {
local -i i local -i i
for i in {1..10}; do for i in {1..10}; do
_p9k_worker_invoke f compute_foo $i _p9k_worker_invoke foo$i "foo_cond c$i\$\{" "foo_async a$i\$\{" "foo_sync s$i\$\{"
done done
} }
}
RPROMPT='$foo %*'
function in_worker() { function in_worker() {
_p9k_worker_reply 'echo roundtrip: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log' _p9k_worker_reply 'echo roundtrip: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log'
} }
_p9k_worker_invoke w in_worker $EPOCHREALTIME _p9k_worker_invoke "" "function in_worker() { $functions[in_worker] }" "" ""
# for i in {1..100}; do _p9k_worker_invoke w$i in_worker $EPOCHREALTIME; done _p9k_worker_invoke w "in_worker $EPOCHREALTIME" "" ""
# for i in {1..100}; do _p9k_worker_invoke w$i "in_worker $EPOCHREALTIME"; done
# TODO:
#
# - Segment API: _p9k_prompt_foo_worker_{params,cond,async,sync}.
# - _p9k_worker_request -- cacheable variable that contains full request to worker.
# - _p9k_set_prompt sends stuff to worker or evals it.
# - _p9k_on_expand has _REALTIME check at the top and sends keep-alive to worker.

Loading…
Cancel
Save