Source-Changes-HG archive

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

[src/trunk]: src/sbin/mount_tmpfs Provide variants of the -s option to allow ...



details:   https://anonhg.NetBSD.org/src/rev/4231b824aa55
branches:  trunk
changeset: 325085:4231b824aa55
user:      martin <martin%NetBSD.org@localhost>
date:      Wed Dec 04 13:30:35 2013 +0000

description:
Provide variants of the -s option to allow limiting the tmpfs dynamically
at mount time to 1/Nth or to N percent of the available ram.

diffstat:

 sbin/mount_tmpfs/mount_tmpfs.8 |  20 +++++++++++-
 sbin/mount_tmpfs/mount_tmpfs.c |  69 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 4 deletions(-)

diffs (136 lines):

diff -r 644c5525f7d5 -r 4231b824aa55 sbin/mount_tmpfs/mount_tmpfs.8
--- a/sbin/mount_tmpfs/mount_tmpfs.8    Wed Dec 04 11:43:52 2013 +0000
+++ b/sbin/mount_tmpfs/mount_tmpfs.8    Wed Dec 04 13:30:35 2013 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: mount_tmpfs.8,v 1.15 2013/06/02 13:27:12 wiz Exp $
+.\"    $NetBSD: mount_tmpfs.8,v 1.16 2013/12/04 13:30:35 martin Exp $
 .\"
 .\" Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -85,6 +85,24 @@
 main memory and swap space) will be used.
 Note that four megabytes are always reserved for the system and cannot
 be assigned to the file system.
+.Ar Size
+can alternatively be specified as a percentage of the available
+system ram by using the notation
+.Ar ram%n
+where
+.Ar n
+is a number between 1 and 100.
+Similarily it can be specified as a fraction of the available system
+ram by using
+.Ar ram/n
+where
+.Ar n
+is the divisor.
+(Using
+.Ar ram%25
+and
+.Ar ram/4
+will result in the same limit.)
 .It Fl u Ar user
 Specifies the user name or UID of the root inode of the file system.
 Defaults to the mount point's UID.
diff -r 644c5525f7d5 -r 4231b824aa55 sbin/mount_tmpfs/mount_tmpfs.c
--- a/sbin/mount_tmpfs/mount_tmpfs.c    Wed Dec 04 11:43:52 2013 +0000
+++ b/sbin/mount_tmpfs/mount_tmpfs.c    Wed Dec 04 13:30:35 2013 +0000
@@ -1,4 +1,5 @@
-/*     $NetBSD: mount_tmpfs.c,v 1.25 2013/06/02 13:27:20 wiz Exp $     */
+
+/*     $NetBSD: mount_tmpfs.c,v 1.26 2013/12/04 13:30:35 martin Exp $  */
 
 /*
  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
@@ -32,12 +33,13 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mount_tmpfs.c,v 1.25 2013/06/02 13:27:20 wiz Exp $");
+__RCSID("$NetBSD: mount_tmpfs.c,v 1.26 2013/12/04 13:30:35 martin Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
+#include <sys/sysctl.h>
 
 #include <fs/tmpfs/tmpfs_args.h>
 
@@ -66,6 +68,63 @@
 /* --------------------------------------------------------------------- */
 
 static void    usage(void) __dead;
+static int64_t ram_fract(const char *arg);
+static int64_t ram_percent(const char *arg);
+static int64_t ram_factor(float f);
+
+/* --------------------------------------------------------------------- */
+
+/* return f * available system ram */
+static int64_t
+ram_factor(float f)
+{
+       uint64_t ram;
+       size_t len;
+
+       len = sizeof(ram);
+       if (sysctlbyname("hw.physmem64", &ram, &len, NULL, 0))
+               err(EXIT_FAILURE, "can't get \"hw.physmem64\": %s", strerror(errno));
+
+       return (int64_t)((float)ram * f);
+}
+
+/* return fraction of available ram given by arg */
+static int64_t
+ram_fract(const char *arg)
+{
+       char *endp;
+       float f;
+
+       f = strtof(arg, &endp);
+       if (endp && *endp != 0)
+               err(EXIT_FAILURE, "syntax error in ram fraction: ram/%s"
+                   " at %s", arg, endp);
+       if (f <= 0.0f)
+               err(EXIT_FAILURE, "ram fraction must be a positive number:"
+                    " ram/%s", arg);
+
+       return ram_factor(1.0f/f);
+}
+
+/* --------------------------------------------------------------------- */
+
+/* return percentage of available ram given by arg */
+static int64_t
+ram_percent(const char *arg)
+{
+       char *endp;
+       float f;
+
+       f = strtof(arg, &endp);
+       if (endp && *endp != 0)
+               err(EXIT_FAILURE, "syntax error in ram percentage: ram%%%s"
+                   " at %s", arg, endp);
+       if (f <= 0.0f || f >= 100.0f)
+               err(EXIT_FAILURE, "ram percentage must be a between 0 and 100"
+                    " ram%%%s", arg);
+
+       return ram_factor(f/100.0f);
+}
 
 /* --------------------------------------------------------------------- */
 
@@ -122,7 +181,11 @@
                        break;
 
                case 's':
-                       if (dehumanize_number(optarg, &tmpnumber) == -1)
+                       if (strncmp(optarg, "ram/", 4) == 0)
+                               tmpnumber = ram_fract(optarg+4);
+                       else if (strncmp(optarg, "ram%", 4) == 0)
+                               tmpnumber = ram_percent(optarg+4);
+                       else if (dehumanize_number(optarg, &tmpnumber) == -1)
                                err(EXIT_FAILURE, "failed to parse size `%s'",
                                    optarg);
                        args->ta_size_max = tmpnumber;



Home | Main Index | Thread Index | Old Index