On Wed, 6 Sept 2023 at 14:16, Roberto A. Foglietta
<roberto.foglie...@gmail.com> wrote:
>
> On Wed, 6 Sept 2023 at 12:09, Roberto A. Foglietta
> <roberto.foglie...@gmail.com> wrote:
>
> > However, execution time does not change so much
>
> Instead when multiple processes names are passed as arguments, the
> execution time gap increases even more because pidof execution time
> depends on the number of arguments while the function listed here
> below does not. Therefore, I think that there is an algorithm change
> to plan for the pidof applet that will move it from O(N) to O(1).

I push further my investigation to check some details that might have
influenced it:

- replicate the basic pidof applet functionality for a fair comparison
- account the time date applet takes to give the output

Here we are:

== SCRIPT ==

comw_pid()
{
    local str
#   RAF: the /proc/$pid/comm contains the first 15 chars + ending \n
    str=$(echo -n $*| tr ' ' '\n'| sed -e "s,\(.\{15\}\).*,^\\1$,"| tr '\n' '|')
    str=$(grep -nE "$str" /proc/[1-9]*/comm 2>/dev/null | cut -d/ -f3)
    echo $str | grep --color=never .
}

# The str and datecmd values can change among systems

dtc=$(for i in $(seq 1 100); do a=$($datecmd +%s%N); b=$($datecmd
+%s%N); echo $((b-a)); done);
str=$(echo "$dtc" | wc -l; echo "$dtc" | sort -n | head -n1; echo
"$dtc" | sort -nr | head -n1)
printf "tries: %d, min: %d, max: %d\n" $str; min=$(echo "$dtc" | sort
-n | head -n1)

a=$($datecmd +%s%N); comw_pid $procstr; b=$($datecmd +%s%N); dta=$((b-a-min));
c=$($datecmd +%s%N); pidof $procstr; d=$($datecmd +%s%N); dtb=$((d-c-min));
echo exec.times: $dta ns, $dtb ns, ratio: $(( (dtb*100) / dta ))%
echo "pidof is $(readlink $(which pidof))"

== RESULTS ==

Running on RedFish OS on busybox ash

procstr="yamui yamui-powerkey yamui-screensaverd dropbear telnetd sshd
sshd.bin httpd";
datecmd=date

tries: 100, min: 4136563, max: 5006771
19681 673 675 680 725
725 680 673 19681 675
exec.times: 117145936 ns, 714783020 ns, ratio: 610%
pidof is /sbin/busybox-static

Running on Ubuntu 22.04 LTS busybox ash

procstr="bash nautilus gedit dnsmasq thermald snapd hello world"
datecmd=/bin/date

tries: 100, min: 1610318, max: 3667151
1148885 1243481 1262 1271 14404 14447 14468 14489 1505 1508232 15566
255975 256074 256157 27375 3891 3892 432851 472771 51819 51823 679241
679272 95110
1508232 1243481 679272 679241 472771 432851 256157 256074 255975 95110
51819 27375 15566 14489 14468 14447 14404 1148885 51823 3892 3891 1505
1271 1262
exec.times: 33390016 ns, 49760852 ns, ratio: 149%
pidof is /sbin/killall5

The two strings are equivalent: 6 existing procs + 2 words

== == == ==

As far as I can understand, these data do not contradict my previous
conclusion but sustain it even further.  Moreover, also
sysvinit-utils: /sbin/killall5 seems less performant than a script
with a O(1) approach. Improving the pidof in busybox of 6x times (at
least) means being 4x faster instead of being 2x slower than
sysvinit-utils applet, by a rule of thumb.

Best regards, R-
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to