For completeness I'm copying below the results of running strace.
I've compared the percentage of time spent in wait4 + select + futex.
All the versions seem to spend roughly the same proportion of time,
with your version running under GOMAXPROCS=1 spending the least.
This shows that my concern for wasting time in synchronisation is
actually negligible.
awk |
wait4 |
97.72% |
Walters1 |
wait4+futex+select |
99.33% |
Walters2 |
wait4+futex+select |
98.88% |
Kunitz1 |
wait4+futex+select |
98.15% |
Kunitz2 |
wait4+futex+select |
98.90% |
Pike1 |
wait4+futex+select |
96.92% |
Pike2 |
wait4+futex+select |
98.43% |
# awk
time sh -c 'awk "{print $0}" < bigfile.txt >
/dev/null'
7.70user 0.26system 0:07.96elapsed 100%CPU
(0avgtext+0avgdata 1164maxresident)k
0inputs+0outputs (0major+587minor)pagefaults 0swaps
strace -c -f sh -c 'awk "{print $0}" < bigfile.txt >
/dev/null'
Process 17745 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
97.72 1.893230 1893230 1 wait4
2.14 0.041376 0 439835 read
0.09 0.001732 0 21148 write
0.01 0.000219 9 25 mmap
0.01 0.000167 13 13 open
0.01 0.000143 10 14 mprotect
0.00 0.000095 6 17 close
0.00 0.000084 9 9 9 access
0.00 0.000082 6 14 fstat
0.00 0.000069 6 11
rt_sigaction
0.00 0.000060 9 7 4 stat
0.00 0.000047 8 6 brk
0.00 0.000046 23 2 munmap
0.00 0.000024 6 4 fcntl
0.00 0.000023 12 2 execve
0.00 0.000018 4 5 dup2
0.00 0.000013 7 2
getgroups
0.00 0.000013 7 2
arch_prctl
0.00 0.000007 2 3 3 ioctl
0.00 0.000007 7 1
sigaltstack
0.00 0.000006 6 1 getpid
0.00 0.000006 6 1 clone
0.00 0.000006 6 1 getppid
0.00 0.000005 5 1
rt_sigreturn
0.00 0.000005 5 1 geteuid
------ ----------- ----------- --------- ---------
----------------
100.00 1.937483 461126 16 total
# Go Walters GOMAXPROCS=1
time sh -c 'GOMAXPROCS=1 ./printWalters < bigfile.txt
> /dev/null'
25.30user 0.67system 0:25.23elapsed 102%CPU
(0avgtext+0avgdata 1364maxresident)k
0inputs+0outputs (0major+617minor)pagefaults 0swaps
strace -c -f sh -c 'GOMAXPROCS=1 ./printWalters <
bigfile.txt > /dev/null'
Process 17937 attached
Process 17938 attached
Process 17939 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
49.89 6.282862 6282862 1 wait4
42.55 5.358752 35 151968 6079 futex
6.89 0.867736 2 449130 select
0.34 0.042767 0 439828 read
0.25 0.032070 0 439826 write
0.07 0.008992 0 46729
sched_yield
0.00 0.000148 2 65
rt_sigaction
0.00 0.000050 4 14 mmap
0.00 0.000022 6 4 open
0.00 0.000021 3 8 close
0.00 0.000018 5 4 mprotect
0.00 0.000016 2 7
rt_sigprocmask
0.00 0.000016 4 4 dup2
0.00 0.000016 5 3 clone
0.00 0.000015 5 3 3 access
0.00 0.000012 6 2 munmap
0.00 0.000012 3 4
arch_prctl
0.00 0.000010 3 3 brk
0.00 0.000010 5 2 execve
0.00 0.000010 3 4 fcntl
0.00 0.000008 4 2 stat
0.00 0.000008 3 3
sigaltstack
0.00 0.000006 3 2 fstat
0.00 0.000005 5 1
rt_sigreturn
0.00 0.000005 3 2 gettid
0.00 0.000003 3 1 getpid
0.00 0.000003 3 1 geteuid
0.00 0.000003 3 1
sched_getaffinity
0.00 0.000002 2 1 getppid
------ ----------- ----------- --------- ---------
----------------
100.00 12.593598 1527623 6082 total
# Go Walters GOMAXPROCS=2
time sh -c 'GOMAXPROCS=2 ./printWalters < bigfile.txt
> /dev/null'
41.81user 2.96system 0:44.97elapsed 99%CPU
(0avgtext+0avgdata 1420maxresident)k
0inputs+0outputs (0major+630minor)pagefaults 0swaps
strace -c -f sh -c 'GOMAXPROCS=2 ./printWalters <
bigfile.txt > /dev/null'
Process 18278 attached
Process 18279 attached
Process 18280 attached
Process 18281 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
52.17 9.978744 9978744 1 wait4
37.75 7.221550 11 638286 24889 futex
8.96 1.714025 4 480140 select
0.41 0.079150 0 210129
sched_yield
0.41 0.078424 0 439828 read
0.29 0.055326 0 439826 write
0.00 0.000390 6 65
rt_sigaction
0.00 0.000120 9 14 mmap
0.00 0.000103 10 10
rt_sigprocmask
0.00 0.000049 12 4 open
0.00 0.000046 12 4 mprotect
0.00 0.000045 6 8 close
0.00 0.000042 11 4 clone
0.00 0.000041 10 4
sigaltstack
0.00 0.000031 16 2 munmap
0.00 0.000029 10 3 3 access
0.00 0.000028 9 3 brk
0.00 0.000028 7 4 fcntl
0.00 0.000028 6 5
arch_prctl
0.00 0.000026 13 2 execve
0.00 0.000021 5 4 dup2
0.00 0.000018 9 2 stat
0.00 0.000015 8 2 fstat
0.00 0.000012 4 3 gettid
0.00 0.000008 8 1
sched_getaffinity
0.00 0.000006 6 1 getpid
0.00 0.000006 6 1 geteuid
0.00 0.000006 6 1 getppid
0.00 0.000004 4 1
rt_sigreturn
------ ----------- ----------- --------- ---------
----------------
100.00 19.128321 2208358 24892 total
# Go Kunitz GOMAXPROCS=1
time sh -c 'GOMAXPROCS=1 ./printKunitz < bigfile.txt
> /dev/null'
4.27user 0.24system 0:04.51elapsed 100%CPU
(0avgtext+0avgdata 3264maxresident)k
0inputs+0outputs (0major+1080minor)pagefaults 0swaps
strace -c -f sh -c 'GOMAXPROCS=1 ./printKunitz <
bigfile.txt > /dev/null'
Process 18514 attached
Process 18515 attached
Process 18516 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
49.96 0.238406 238406 1 wait4
48.07 0.229384 368 623 select
1.65 0.007855 5 1721 read
0.12 0.000558 29 19 3 futex
0.04 0.000196 3 65
rt_sigaction
0.03 0.000137 17 8 close
0.02 0.000119 6 19 mmap
0.02 0.000087 0 1636 write
0.01 0.000052 13 4 open
0.01 0.000044 11 4 mprotect
0.01 0.000039 13 3 3 access
0.01 0.000033 5 7
rt_sigprocmask
0.01 0.000028 9 3 clone
0.01 0.000027 14 2 munmap
0.01 0.000024 6 4 fcntl
0.00 0.000021 7 3 brk
0.00 0.000020 5 4 dup2
0.00 0.000019 10 2 stat
0.00 0.000018 5 4
arch_prctl
0.00 0.000016 8 2 fstat
0.00 0.000015 8 2 execve
0.00 0.000014 5 3
sigaltstack
0.00 0.000013 3 5
sched_yield
0.00 0.000011 6 2 gettid
0.00 0.000006 6 1 getpid
0.00 0.000006 6 1 geteuid
0.00 0.000006 6 1 getppid
0.00 0.000005 5 1
rt_sigreturn
0.00 0.000005 5 1
sched_getaffinity
------ ----------- ----------- --------- ---------
----------------
100.00 0.477164 4151 6 total
# Go Kunitz GOMAXPROCS=2
time sh -c 'GOMAXPROCS=2 ./printKunitz < bigfile.txt
> /dev/null'
4.34user 0.19system 0:04.52elapsed 100%CPU
(0avgtext+0avgdata 3272maxresident)k
0inputs+0outputs (0major+1084minor)pagefaults 0swaps
strace -c -f sh -c 'GOMAXPROCS=2 ./printKunitz <
bigfile.txt > /dev/null'
Process 18565 attached
Process 18566 attached
Process 18567 attached
Process 18568 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
33.17 0.263213 263213 1 wait4
33.12 0.262834 570 461 3 futex
32.61 0.258794 486 532 select
1.07 0.008469 5 1721 read
0.01 0.000059 0 1636 write
0.01 0.000049 3 18 mmap
0.00 0.000023 6 4 open
0.00 0.000021 3 8 close
0.00 0.000019 0 65
rt_sigaction
0.00 0.000018 5 4 mprotect
0.00 0.000015 5 3 3 access
0.00 0.000014 7 2 munmap
0.00 0.000014 4 4 dup2
0.00 0.000010 3 4 fcntl
0.00 0.000009 5 2 stat
0.00 0.000009 5 2 execve
0.00 0.000008 3 3 brk
0.00 0.000006 3 2 fstat
0.00 0.000006 1 5
arch_prctl
0.00 0.000004 4 1
rt_sigreturn
0.00 0.000004 1 4 clone
0.00 0.000004 4 1
sched_getaffinity
0.00 0.000002 2 1 getpid
0.00 0.000002 2 1 geteuid
0.00 0.000002 2 1 getppid
0.00 0.000000 0 10
rt_sigprocmask
0.00 0.000000 0 9
sched_yield
0.00 0.000000 0 4
sigaltstack
0.00 0.000000 0 3 gettid
------ ----------- ----------- --------- ---------
----------------
100.00 0.793608 4512 6 total
# Go Pike GOMAXPROCS=1
time sh -c 'GOMAXPROCS=1 ./printPike < bigfile.txt >
/dev/null'
4.45user 0.32system 0:04.77elapsed 100%CPU
(0avgtext+0avgdata 1088maxresident)k
0inputs+0outputs (0major+536minor)pagefaults 0swaps
strace -c -f sh -c 'GOMAXPROCS=1 ./printPike <
bigfile.txt > /dev/null'
Process 18616 attached
Process 18617 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
48.51 5.051889 5051889 1 wait4
48.41 5.041850 2331 2163 select
1.70 0.176679 0 439828 read
1.37 0.143186 0 418679 write
0.00 0.000185 3 65
rt_sigaction
0.00 0.000051 4 14 mmap
0.00 0.000030 10 3 3 access
0.00 0.000025 6 4 open
0.00 0.000024 3 8 close
0.00 0.000019 5 4 mprotect
0.00 0.000018 5 4
rt_sigprocmask
0.00 0.000017 4 4 dup2
0.00 0.000015 8 2 execve
0.00 0.000014 7 2 munmap
0.00 0.000014 5 3
arch_prctl
0.00 0.000011 4 3 brk
0.00 0.000011 3 4 fcntl
0.00 0.000009 5 2
sigaltstack
0.00 0.000008 4 2 stat
0.00 0.000007 7 1
rt_sigreturn
0.00 0.000006 3 2 fstat
0.00 0.000006 3 2 clone
0.00 0.000005 5 1 gettid
0.00 0.000004 4 1
sched_getaffinity
0.00 0.000002 2 1 getpid
0.00 0.000002 2 1 geteuid
0.00 0.000002 2 1 getppid
0.00 0.000000 0 4 futex
------ ----------- ----------- --------- ---------
----------------
100.00 10.414089 860809 3 total
# Go Pike GOMAXPROCS=2
time sh -c 'GOMAXPROCS=2 ./printPike < bigfile.txt >
/dev/null'
4.52user 0.27system 0:04.77elapsed 100%CPU
(0avgtext+0avgdata 1096maxresident)k
0inputs+0outputs (0major+540minor)pagefaults 0swaps
strace -c -f sh -c 'GOMAXPROCS=2 ./printPike <
bigfile.txt > /dev/null'
Process 18770 attached
Process 18771 attached
Process 18772 attached
Process 18773 attached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- ---------
----------------
49.14 10.084456 8641 1167 80 futex
24.69 5.067420 5067420 1 wait4
24.60 5.049329 2346 2152 select
0.86 0.176492 0 439828 read
0.70 0.143627 0 418679 write
0.00 0.000174 1 183
sched_yield
0.00 0.000166 3 65
rt_sigaction
0.00 0.000054 5 10
rt_sigprocmask
0.00 0.000050 4 14 mmap
0.00 0.000024 6 4 open
0.00 0.000023 3 8 close
0.00 0.000021 5 4
sigaltstack
0.00 0.000017 4 4 dup2
0.00 0.000017 4 4 clone
0.00 0.000016 4 4 mprotect
0.00 0.000014 7 2 munmap
0.00 0.000014 5 3 3 access
0.00 0.000013 3 5
arch_prctl
0.00 0.000012 4 3 gettid
0.00 0.000010 5 2 stat
0.00 0.000010 3 4 fcntl
0.00 0.000009 5 2 execve
0.00 0.000008 3 3 brk
0.00 0.000006 6 1
rt_sigreturn
0.00 0.000005 3 2 fstat
0.00 0.000003 3 1 getpid
0.00 0.000003 3 1 getppid
0.00 0.000003 3 1
sched_getaffinity
0.00 0.000002 2 1 geteuid
------ ----------- ----------- --------- ---------
----------------
100.00 20.521998 862158 83 total