]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
provide POSIX_FADV_SEQUENTIAL hint to appropriate utils
authorPádraig Brady <P@draigBrady.com>
Tue, 20 Jul 2010 17:51:01 +0000 (18:51 +0100)
committerPádraig Brady <P@draigBrady.com>
Thu, 22 Jul 2010 00:04:47 +0000 (01:04 +0100)
Following on from commit dae35bac, 01-03-2010,
"sort: inform the system about our input access pattern"
apply the same hint to all appropriate utils.
This currently gives around a 5% speedup for reading
large files from fast flash devices on GNU/Linux.

* src/base64.c: Call fadvise (..., FADVISE_SEQUENTIAL);
* src/cat.c: Likewise.
* src/cksum.c: Likewise.
* src/comm.c: Likewise.
* src/cut.c: Likewise.
* src/expand.c: Likewise.
* src/fmt.c: Likewise.
* src/fold.c: Likewise.
* src/join.c: Likewise.
* src/md5sum.c: Likewise.
* src/nl.c: Likewise.
* src/paste.c: Likewise.
* src/pr.c: Likewise.
* src/ptx.c: Likewise.
* src/shuf.c: Likewise.
* src/sum.c: Likewise.
* src/tee.c: Likewise.
* src/tr.c: Likewise.
* src/tsort.c: Likewise.
* src/unexpand.c: Likewise.
* src/uniq.c: Likewise.
* src/wc.c: Likewise, unless we don't actually read().

22 files changed:
src/base64.c
src/cat.c
src/cksum.c
src/comm.c
src/cut.c
src/expand.c
src/fmt.c
src/fold.c
src/join.c
src/md5sum.c
src/nl.c
src/paste.c
src/pr.c
src/ptx.c
src/shuf.c
src/sum.c
src/tee.c
src/tr.c
src/tsort.c
src/unexpand.c
src/uniq.c
src/wc.c

index fddb61c9dd8cdfd910365e2473ab290eb2935b84..1a36c9183be9c833e265479d449aab65be2d763a 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "xstrtol.h"
 #include "quote.h"
 #include "quotearg.h"
@@ -302,6 +303,8 @@ main (int argc, char **argv)
         error (EXIT_FAILURE, errno, "%s", infile);
     }
 
+  fadvise (input_fh, FADVISE_SEQUENTIAL);
+
   if (decode)
     do_decode (input_fh, stdout, ignore_garbage);
   else
index c4a2a9e3ca3a64470b70742a8ee1f45c2fdf806c..47b50535258db466cc47eb77473518c7e88a7277 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -34,6 +34,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "full-write.h"
 #include "quote.h"
 #include "safe-read.h"
@@ -700,6 +701,8 @@ main (int argc, char **argv)
         }
       insize = io_blksize (stat_buf);
 
+      fdadvise (input_desc, 0, 0, FADVISE_SEQUENTIAL);
+
       /* Compare the device and i-node numbers of this input file with
          the corresponding values of the (output file associated with)
          stdout, and skip this input file if they coincide.  Input
index d240eb38623af1776c2007979aad294e43413e45..282c777aa6c90bc43b821979c6061ed16f9f1711 100644 (file)
@@ -43,6 +43,7 @@
 #include <sys/types.h>
 #include <stdint.h>
 #include "system.h"
+#include "fadvise.h"
 #include "xfreopen.h"
 
 #ifdef CRCTAB
@@ -205,6 +206,8 @@ cksum (const char *file, bool print_name)
         }
     }
 
+  fadvise (fp, FADVISE_SEQUENTIAL);
+
   while ((bytes_read = fread (buf, 1, BUFLEN, fp)) > 0)
     {
       unsigned char *cp = buf;
index ff42802cb7a73f89d18942c9b8958462816fe3ce..06b80b0713ec64e62104dfffce27055252df0a40 100644 (file)
@@ -24,6 +24,7 @@
 #include "system.h"
 #include "linebuffer.h"
 #include "error.h"
+#include "fadvise.h"
 #include "hard-locale.h"
 #include "quote.h"
 #include "stdio--.h"
@@ -273,6 +274,8 @@ compare_files (char **infiles)
       if (!streams[i])
         error (EXIT_FAILURE, errno, "%s", infiles[i]);
 
+      fadvise (streams[i], FADVISE_SEQUENTIAL);
+
       thisline[i] = readlinebuffer (all_line[i][alt[i][0]], streams[i]);
       if (ferror (streams[i]))
         error (EXIT_FAILURE, errno, "%s", infiles[i]);
index 5fcf0743a78a5a44a3700c67f3c039de12015e2e..58776d991fb27e06073add7bc1116f4b3ac1a308 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -31,6 +31,7 @@
 #include "system.h"
 
 #include "error.h"
+#include "fadvise.h"
 #include "getndelim2.h"
 #include "hash.h"
 #include "quote.h"
@@ -733,6 +734,8 @@ cut_file (char const *file)
         }
     }
 
+  fadvise (stream, FADVISE_SEQUENTIAL);
+
   cut_stream (stream);
 
   if (ferror (stream))
index be50063b6e474ff75ff29ff11c21965d0df2e69e..249255d15b39bfad352ae6cb41ecc8cac8595627 100644 (file)
@@ -40,6 +40,7 @@
 #include <sys/types.h>
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "xstrndup.h"
 
@@ -243,13 +244,14 @@ next_file (FILE *fp)
       if (STREQ (file, "-"))
         {
           have_read_stdin = true;
-          prev_file = file;
-          return stdin;
+          fp = stdin;
         }
-      fp = fopen (file, "r");
+      else
+        fp = fopen (file, "r");
       if (fp)
         {
           prev_file = file;
+          fadvise (fp, FADVISE_SEQUENTIAL);
           return fp;
         }
       error (0, errno, "%s", file);
index 1a268eeb432914c67a5212693546415f6ed1639f..8a5d8bd8c662b19e1c2c92fd11c41c3733fcb656 100644 (file)
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -27,6 +27,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "xstrtol.h"
 
@@ -463,6 +464,7 @@ set_prefix (char *p)
 static void
 fmt (FILE *f)
 {
+  fadvise (f, FADVISE_SEQUENTIAL);
   tabs = false;
   other_indent = 0;
   next_char = get_prefix (f);
index 9364a03f94588d7f8b3ebe5c511ae5337917fb99..d5858568cab0007cb29b9a735ef373ce0bda37a2 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "xstrtol.h"
 
@@ -142,6 +143,8 @@ fold_file (char const *filename, size_t width)
       return false;
     }
 
+  fadvise (stdin, FADVISE_SEQUENTIAL);
+
   while ((c = getc (istream)) != EOF)
     {
       if (offset_out + 1 >= allocated_out)
index c977116ab8cecf2516655b3ed3196960d75fb3cb..fa18c9d0655c7ee35d3b72e9d05cb90e4b0c0d47 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "hard-locale.h"
 #include "linebuffer.h"
 #include "memcasecmp.h"
@@ -617,6 +618,9 @@ join (FILE *fp1, FILE *fp2)
   int diff;
   bool eof1, eof2;
 
+  fadvise (fp1, FADVISE_SEQUENTIAL);
+  fadvise (fp2, FADVISE_SEQUENTIAL);
+
   /* Read the first line of each file.  */
   initseq (&seq1);
   getseq (fp1, &seq1, 1);
index cbf198645a2fc0ddcf9fea21f1391b39b9132bfa..10d4fa26c702cc37aedd5d1e259ef848219a46b8 100644 (file)
@@ -36,6 +36,7 @@
 # include "sha512.h"
 #endif
 #include "error.h"
+#include "fadvise.h"
 #include "stdio--.h"
 #include "xfreopen.h"
 
@@ -406,6 +407,8 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result)
         }
     }
 
+  fadvise (fp, FADVISE_SEQUENTIAL);
+
   err = DIGEST_STREAM (fp, bin_result);
   if (err)
     {
index a9446f4445450a7ce4528555bdeed0c4dc3020ea..634a975c63554914162071d3b0da7dd27bd2f136 100644 (file)
--- a/src/nl.c
+++ b/src/nl.c
@@ -28,6 +28,7 @@
 #include <regex.h>
 
 #include "error.h"
+#include "fadvise.h"
 #include "linebuffer.h"
 #include "quote.h"
 #include "xstrtol.h"
@@ -439,6 +440,8 @@ nl_file (char const *file)
         }
     }
 
+  fadvise (stream, FADVISE_SEQUENTIAL);
+
   process_file (stream);
 
   if (ferror (stream))
index 36e1cfbc0dd12746b0d5cc6c581e9d708f1d5cb2..dbbf52df5d06b53e9bf59d20d5cca1f18eed003d 100644 (file)
@@ -42,6 +42,7 @@
 #include <sys/types.h>
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quotearg.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -211,6 +212,7 @@ paste_parallel (size_t nfiles, char **fnamptr)
             error (EXIT_FAILURE, errno, "%s", fnamptr[files_open]);
           else if (fileno (fileptr[files_open]) == STDIN_FILENO)
             opened_stdin = true;
+          fadvise (fileptr[files_open], FADVISE_SEQUENTIAL);
         }
     }
 
@@ -367,6 +369,7 @@ paste_serial (size_t nfiles, char **fnamptr)
               ok = false;
               continue;
             }
+          fadvise (fileptr, FADVISE_SEQUENTIAL);
         }
 
       delimptr = delims;       /* Set up for delimiter string. */
index f942151a78f3b479d20e0c89c77684fae06c7ccc..9c2d10c10aa543c15bbf4a52e8c6b154dafcd9f3 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
 #include <sys/types.h>
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "hard-locale.h"
 #include "mbswidth.h"
 #include "quote.h"
@@ -1507,6 +1508,7 @@ open_file (char *name, COLUMN *p)
         error (0, errno, "%s", name);
       return false;
     }
+  fadvise (p->fp, FADVISE_SEQUENTIAL);
   p->status = OPEN;
   p->full_page_printed = false;
   ++total_files;
index c6efb04c0fb913db409ab20364ecca317afe9ac2..ba5aec5192e185daa7c7fe5b31fa221a0e362107 100644 (file)
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -25,6 +25,7 @@
 #include "argmatch.h"
 #include "diacrit.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "quotearg.h"
 #include "regex.h"
@@ -538,6 +539,8 @@ swallow_file_in_memory (const char *file_name, BLOCK *block)
     {
       size_t in_memory_size;
 
+      fdadvise (file_handle, 0, 0, FADVISE_SEQUENTIAL);
+
       block->start = xmalloc ((size_t) stat_block.st_size);
 
       if ((in_memory_size = read (file_handle,
index 67b19af15a4d02901f6d409d017064df633ff4cc..e8ae645d25df4eb5ea4ab1f26ac2e49f7e07b77d 100644 (file)
@@ -23,6 +23,7 @@
 #include "system.h"
 
 #include "error.h"
+#include "fadvise.h"
 #include "getopt.h"
 #include "quote.h"
 #include "quotearg.h"
@@ -378,6 +379,8 @@ main (int argc, char **argv)
           usage (EXIT_FAILURE);
         }
 
+      fadvise (stdin, FADVISE_SEQUENTIAL);
+
       n_lines = read_input (stdin, eolbyte, &input_lines);
       line = input_lines;
     }
index d0d160f7b9197457ec510591431f3b5239f5fa32..4b98e5935834c4c65c07882de061461393c83948 100644 (file)
--- a/src/sum.c
+++ b/src/sum.c
@@ -26,6 +26,7 @@
 #include <getopt.h>
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "human.h"
 #include "safe-read.h"
 #include "xfreopen.h"
@@ -110,6 +111,8 @@ bsd_sum_file (const char *file, int print_name)
         }
     }
 
+  fadvise (fp, FADVISE_SEQUENTIAL);
+
   while ((ch = getc (fp)) != EOF)
     {
       total_bytes++;
index 39d989274df5d2f70a4c726f6e2164bd419a628b..a5b0369fd25dc24767aaa08d5f3c436640a9d8b8 100644 (file)
--- a/src/tee.c
+++ b/src/tee.c
@@ -23,6 +23,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "stdio--.h"
 #include "xfreopen.h"
 
@@ -157,6 +158,8 @@ tee_files (int nfiles, const char **files)
   if (O_BINARY && ! isatty (STDOUT_FILENO))
     xfreopen (NULL, "wb", stdout);
 
+  fadvise (stdin, FADVISE_SEQUENTIAL);
+
   /* In the array of NFILES + 1 descriptors, make
      the first one correspond to standard output.   */
   descriptors[0] = stdout;
index 3722b4d84b47675dab4bcd6f8275c13abc04f976..a5b68106bea2bc3f7d67f4710eff0e0cdac01587 100644 (file)
--- a/src/tr.c
+++ b/src/tr.c
@@ -25,6 +25,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "safe-read.h"
 #include "xfreopen.h"
@@ -1754,6 +1755,8 @@ main (int argc, char **argv)
   if (O_BINARY && ! isatty (STDOUT_FILENO))
     xfreopen (NULL, "wb", stdout);
 
+  fadvise (stdin, FADVISE_SEQUENTIAL);
+
   if (squeeze_repeats && non_option_args == 1)
     {
       set_initialize (s1, complement, in_squeeze_set);
index b5c187b30b218c367264c641385f70f6ef33363a..4f51f3074bf09f82f940d275c53d4266ec9862ee 100644 (file)
@@ -29,6 +29,7 @@
 #include "system.h"
 #include "long-options.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "readtokens.h"
 #include "stdio--.h"
@@ -444,6 +445,8 @@ tsort (const char *file)
   if (!is_stdin && ! freopen (file, "r", stdin))
     error (EXIT_FAILURE, errno, "%s", file);
 
+  fadvise (stdin, FADVISE_SEQUENTIAL);
+
   init_tokenbuffer (&tokenbuffer);
 
   while (1)
index e9e5c6acfb86f7476b84934a623a443747b7f9a0..14b8df08a499a522027e4d7e2e0b3b1ca82cfa88 100644 (file)
@@ -41,6 +41,7 @@
 #include <sys/types.h>
 #include "system.h"
 #include "error.h"
+#include "fadvise.h"
 #include "quote.h"
 #include "xstrndup.h"
 
@@ -262,13 +263,14 @@ next_file (FILE *fp)
       if (STREQ (file, "-"))
         {
           have_read_stdin = true;
-          prev_file = file;
-          return stdin;
+          fp = stdin;
         }
-      fp = fopen (file, "r");
+      else
+        fp = fopen (file, "r");
       if (fp)
         {
           prev_file = file;
+          fadvise (fp, FADVISE_SEQUENTIAL);
           return fp;
         }
       error (0, errno, "%s", file);
index df59b12346a8e43341b21ab3612a2ef0cb814639..86ca8c9579592ab05a0048bb13ea9699aaf9e0da 100644 (file)
@@ -25,6 +25,7 @@
 #include "argmatch.h"
 #include "linebuffer.h"
 #include "error.h"
+#include "fadvise.h"
 #include "hard-locale.h"
 #include "posixver.h"
 #include "quote.h"
@@ -286,6 +287,8 @@ check_file (const char *infile, const char *outfile, char delimiter)
   if (! (STREQ (outfile, "-") || freopen (outfile, "w", stdout)))
     error (EXIT_FAILURE, errno, "%s", outfile);
 
+  fadvise (stdin, FADVISE_SEQUENTIAL);
+
   thisline = &lb1;
   prevline = &lb2;
 
index 6df7feddfcdeb7e841a2e2b9664cb51342186aa2..a1922baf9fe12818f52ed8be31bc200886ea2302 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -29,6 +29,7 @@
 #include "system.h"
 #include "argv-iter.h"
 #include "error.h"
+#include "fadvise.h"
 #include "mbchar.h"
 #include "physmem.h"
 #include "quote.h"
@@ -211,6 +212,10 @@ wc (int fd, char const *file_x, struct fstatus *fstatus)
     }
   count_complicated = print_words || print_linelength;
 
+  /* Advise the kernel of our access pattern only if we will read().  */
+  if (!count_bytes || count_chars || print_lines || count_complicated)
+    fdadvise (fd, 0, 0, FADVISE_SEQUENTIAL);
+
   /* When counting only bytes, save some line- and word-counting
      overhead.  If FD is a `regular' Unix file, using lseek is enough
      to get its `size' in bytes.  Otherwise, read blocks of BUFFER_SIZE
@@ -238,6 +243,7 @@ wc (int fd, char const *file_x, struct fstatus *fstatus)
         }
       else
         {
+          fdadvise (fd, 0, 0, FADVISE_SEQUENTIAL);
           while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
             {
               if (bytes_read == SAFE_READ_ERROR)