summaryrefslogtreecommitdiffstats
path: root/bin/cp
diff options
context:
space:
mode:
authorivoras <ivoras@FreeBSD.org>2008-10-30 14:05:57 +0000
committerivoras <ivoras@FreeBSD.org>2008-10-30 14:05:57 +0000
commit1f332d86bf5e4cfc63df682d8859c4f4fac21026 (patch)
tree346118ee48560d0625d63c6017022305731d9dd9 /bin/cp
parentfbca48f8d5b2e879565c548e81b7c3da7d1b36dc (diff)
downloadFreeBSD-src-1f332d86bf5e4cfc63df682d8859c4f4fac21026.zip
FreeBSD-src-1f332d86bf5e4cfc63df682d8859c4f4fac21026.tar.gz
Teach cat(1) and cp(1) to use a larger buffer if enough memory is present
in the system. A simple heuristics is used to detect what is "enough" memory: if number of physmem pages is greater than 32k (equalling 128 MB on machines with 4 kB pages). Typical immediate result of these changes is reduction in context switches and the goal is to increase efficiency by using large buffers: before: /usr/bin/time -hlp cat file1 > file2 ... 163 voluntary context switches 11194 involuntary context switches after: /usr/bin/time -hlp ./cat file1 > file2 ... 417 voluntary context switches 272 involuntary context switches Reviewed by: hackers@ (no objections to earlier version of cat patch) Approved by: gnn (mentor) MFC after: 4 months
Diffstat (limited to 'bin/cp')
-rw-r--r--bin/cp/utils.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/bin/cp/utils.c b/bin/cp/utils.c
index 03ddbbc..aaa12d6 100644
--- a/bin/cp/utils.c
+++ b/bin/cp/utils.c
@@ -57,10 +57,22 @@ __FBSDID("$FreeBSD$");
#define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
+/* Memory strategy threshold, in pages: if physmem is larger then this, use a
+ * large buffer */
+#define PHYSPAGES_THRESHOLD (32*1024)
+
+/* Maximum buffer size in bytes - do not allow it to grow larger than this */
+#define BUFSIZE_MAX (2*1024*1024)
+
+/* Small (default) buffer size in bytes. It's inefficient for this to be
+ * smaller than MAXPHYS */
+#define BUFSIZE_SMALL (MAXPHYS)
+
int
copy_file(const FTSENT *entp, int dne)
{
- static char buf[MAXBSIZE];
+ static char *buf = NULL;
+ static size_t bufsize;
struct stat *fs;
ssize_t wcount;
size_t wresid;
@@ -174,8 +186,23 @@ copy_file(const FTSENT *entp, int dne)
} else
#endif
{
+ if (buf == NULL) {
+ /*
+ * Note that buf and bufsize are static. If
+ * malloc() fails, it will fail at the start
+ * and not copy only some files.
+ */
+ if (sysconf(_SC_PHYS_PAGES) >
+ PHYSPAGES_THRESHOLD)
+ bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
+ else
+ bufsize = BUFSIZE_SMALL;
+ buf = malloc(bufsize);
+ if (buf == NULL)
+ err(1, "Not enough memory");
+ }
wtotal = 0;
- while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
+ while ((rcount = read(from_fd, buf, bufsize)) > 0) {
for (bufp = buf, wresid = rcount; ;
bufp += wcount, wresid -= wcount) {
wcount = write(to_fd, bufp, wresid);
OpenPOWER on IntegriCloud