in reply to Perl threads loss of performance with system call

I have observed that the terms "short" and "long" sleep-times, as you put it, depend on the number of threads (=time to fill the @pool=when to join). For example in my system, a sleep of 0.7s will display delays but only when the number of threads is greater than 300. While a sleep of 1s does show these problems but for 500+ threads. This code my @pool = map { threads->create(\&mysub) } 1 .. 50; creates a thread, runs it and then adds it to the @pool and repeats. The important thing to notice is that the thread starts execution immediately while others are still being created. And when thread execution involves system (=fork+exec+waitpid) which has to exit very shortly, somewhere there is a race, given that threads and fork involve replicate program state. Now this is a rough sketch and probably silly, I am very far from being an expert on Perl gastroenterology.

I have a possible workaround, which is lame in that it will probably take longer to finish ... Just make the created threads to wait for the pool to fill up and then do the actual work (system command). That can be achieved with a shared flag like below:

use threads; use threads::shared; use strict; use Time::HiRes qw/clock_gettime CLOCK_REALTIME usleep/; my $flag:shared = 1; print "starting at ".clock_gettime(CLOCK_REALTIME)."\n"; my @pool; for(1..50){ push @pool, threads->create(\&mysub); } print "giving the flag at ".clock_gettime(CLOCK_REALTIME)."\n"; $flag = 0; # all threads go! $_->join for (@pool); sub mysub { my $tid = threads->tid; while( $flag ){ print "thread $tid created and waiting for the flag at ".clock_get +time(CLOCK_REALTIME)."\n"; usleep(1000); } #my $x = 0.0001 + rand(0.0015); my $x = 0.001; #my $x = rand() >= 0.5 ? 0.001 : 1; print "thread $tid is working with sleep=$x, at ".clock_gettime(CL +OCK_REALTIME)."\n"; my $id; for my $i (1..100){ $id = "i=$i, tid=$tid"; # print ` ` echo -n "start($id/\$\$) on cpu "\`cat /proc/\$\$/stat | cut - +d' ' -f39\`": "; date +%s.%N sleep $x echo -n " stop($id/\$\$) on cpu "\`cat /proc/\$\$/stat | cut - +d' ' -f39\`": "; date +%s.%N `; } print "ended " . threads->tid . " at ".clock_gettime(CLOCK_REALTIM +E)."\n"; } __END__

An explanation of what's going on should probably involve the fact that system involves fork, exec and waitpid (see Learning Perl, ch 14). And that both threads and fork replicate the program state. And choroba's citing the doc here:

Thinking of mixing fork() and threads? Please lie down and wait until the feeling passes. Be aware that the semantics of fork() vary between platforms. For example, some Unix systems copy all the current threads into the child process, while others only copy the thread that called fork(). You have been warned!

bw, bliako

Replies are listed 'Best First'.
Re^2: Perl threads loss of performance with system call
by daniel85 (Novice) on Jul 14, 2021 at 12:25 UTC
    This is what I actually tried to do at the very beginning. However, if you keep an eye on your "top", you will see that at most 2-3 threads are running concurrently at any time, while most of them are inactive. Indeed, the total execution time is longer with "sleep 0.001" (or echo A), than it is with "sleep 1".

      i use htop. I did not see what you describe. All cores were active. Still we are talking about very small time intervals. In any event you now have some tools to debug this and confirm any suspicions you may have. For example taskset will report on the current cpu (relocations are common though).

      bw, bliako