NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/55808: fread is inefficient in unbuffered mode



>Number:         55808
>Category:       lib
>Synopsis:       fread is inefficient in unbuffered mode
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Nov 16 20:30:00 +0000 2020
>Originator:     Roland Illig
>Release:        8.0
>Organization:
none
>Environment:
NetBSD nb8.roland-illig.de 8.0 NetBSD 8.0 (GENERIC.PROF) #0: Sun May 17 14:12:24 UTC 2020  rillig%nb8.roland-illig.de@localhost:/home/rillig/proj/src8/sys/arch/amd64/compile/GENERIC.PROF amd64
>Description:
cat <<\EOF > fread-demo.c
#include <stdio.h>

int main(void)
{
        char buf[4096];

        setbuf(stdin, NULL);
        fread(buf, 1, 4096, stdin);
        return 0;
}
EOF

gcc --coverage fread-demo.c -o fread-demo

ktrace ./fread-demo < fread-demo

kdump | grep -w "RET.*read" | uniq -c

The output starts with 4096, which means that read(2) is called 4096 times to read a buffer of size 4096.  This is obviously inefficient.

I experienced this when running gcov in src/usr.bin/make.

cd src/usr.bin/make
USETOOLS=no make clean
time env USETOOLS=no USE_COVERAGE=yes make -s all test

This takes about 14 minutes.  Without the call to setbuf in gcov_open, it takes only 50 seconds.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97834

https://github.com/NetBSD/src/blob/netbsd-8/lib/libc/stdio/fread.c#L81

The call to __srefill looks suspicious since that function does not get any information about how many bytes it should read.

>How-To-Repeat:

>Fix:



Home | Main Index | Thread Index | Old Index