Pipes, cat buffer size

Ivan Voras ivoras at freebsd.org
Sat Oct 18 21:17:19 UTC 2008


Hi,

I'm working on a program that's intended to be used as a "filter", as in
"something | myprogram > file". I'm trying it with cat and I'm seeing my
read()s return small blocks, 64 kB in size. I suppose this is because
cat writes in 64 kB blocks. So:

a) Is there a way to programatically, per-process, set the pipe buffer
size? The program in question is a compressor and it's particularly
inefficient when given small blocks and I'm wondering if the system can
buffer enough data for it.
b) Is there any objection to the following patch to cat:

Index: cat.c
===================================================================
--- cat.c       (revision 184033)
+++ cat.c       (working copy)
@@ -247,7 +247,16 @@
        if (buf == NULL) {
                if (fstat(wfd, &sbuf))
                        err(1, "%s", filename);
-               bsize = MAX(sbuf.st_blksize, 1024);
+               if (S_ISREG(sbuf.st_mode)) {
+                       /* If there's plenty of RAM, use a 1 MB
+                        * copy buffer, else use a 128 kB buffer */
+                       if (sysconf(_SC_PHYS_PAGES) > 32768)
+                               bsize = 1*1024*1024;
+                       else
+                               bsize = 128*1024;
+               } else
+                       bsize = MAX(sbuf.st_blksize,
+                                       (blksize_t)sysconf(_SC_PAGESIZE));
                if ((buf = malloc(bsize)) == NULL)
                        err(1, "buffer");
        }

?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 258 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20081018/79155b4e/signature.pgp


More information about the freebsd-hackers mailing list