chiark / gitweb /
readahead: use 20K instead of 16K as temporary request nr bump
[elogind.git] / src / readahead / readahead-common.c
index 67214ec3790639e1da651120fa94c05f230f7439..10b0ccc548e6abbe17a28588426b8ab59a640c19 100644 (file)
@@ -6,16 +6,16 @@
   Copyright 2010 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
   (at your option) any later version.
 
   systemd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  General Public License for more details.
+  Lesser General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
@@ -71,14 +71,16 @@ int fs_on_ssd(const char *p) {
         if (major(st.st_dev) == 0)
                 return false;
 
-        if (!(udev = udev_new()))
+        udev = udev_new();
+        if (!udev)
                 return -ENOMEM;
 
-        if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
+        udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
+        if (!udev_device)
                 goto finish;
 
-        if ((devtype = udev_device_get_property_value(udev_device, "DEVTYPE")) &&
-            streq(devtype, "partition"))
+        devtype = udev_device_get_property_value(udev_device, "DEVTYPE");
+        if (devtype && streq(devtype, "partition"))
                 look_at = udev_device_get_parent(udev_device);
         else
                 look_at = udev_device;
@@ -87,21 +89,25 @@ int fs_on_ssd(const char *p) {
                 goto finish;
 
         /* First, try high-level property */
-        if ((id = udev_device_get_property_value(look_at, "ID_SSD"))) {
+        id = udev_device_get_property_value(look_at, "ID_SSD");
+        if (id) {
                 b = streq(id, "1");
                 goto finish;
         }
 
         /* Second, try kernel attribute */
-        if ((rotational = udev_device_get_sysattr_value(look_at, "queue/rotational")))
+        rotational = udev_device_get_sysattr_value(look_at, "queue/rotational");
+        if (rotational)
                 if ((b = streq(rotational, "0")))
                         goto finish;
 
         /* Finally, fallback to heuristics */
-        if (!(look_at = udev_device_get_parent(look_at)))
+        look_at = udev_device_get_parent(look_at);
+        if (!look_at)
                 goto finish;
 
-        if ((model = udev_device_get_sysattr_value(look_at, "model")))
+        model = udev_device_get_sysattr_value(look_at, "model");
+        if (model)
                 b = !!strstr(model, "SSD");
 
 finish:
@@ -208,9 +214,13 @@ finish:
         return m;
 }
 
-#define BUMP_REQUEST_NR (16*1024)
+/* We use 20K instead of the more human digestable 16K here. Why?
+   Simply so that it is more unlikely that users end up picking this
+   value too so that we can recognize better whether the user changed
+   the value while we had it temporarily bumped. */
+#define BUMP_REQUEST_NR (20*1024)
 
-int bump_request_nr(const char *p) {
+int block_bump_request_nr(const char *p) {
         struct stat st;
         uint64_t u;
         char *ap = NULL, *line = NULL;
@@ -267,3 +277,83 @@ finish:
 
         return r;
 }
+
+int block_get_readahead(const char *p, uint64_t *bytes) {
+        struct stat st;
+        char *ap = NULL, *line = NULL;
+        int r;
+        dev_t d;
+        uint64_t u;
+
+        assert(p);
+        assert(bytes);
+
+        if (stat(p, &st) < 0)
+                return -errno;
+
+        if (major(st.st_dev) == 0)
+                return 0;
+
+        d = st.st_dev;
+        block_get_whole_disk(d, &d);
+
+        if (asprintf(&ap, "/sys/dev/block/%u:%u/bdi/read_ahead_kb", major(d), minor(d)) < 0) {
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        r = read_one_line_file(ap, &line);
+        if (r < 0)
+                goto finish;
+
+        r = safe_atou64(line, &u);
+        if (r < 0)
+                goto finish;
+
+        *bytes = u * 1024ULL;
+
+finish:
+        free(ap);
+        free(line);
+
+        return r;
+}
+
+int block_set_readahead(const char *p, uint64_t bytes) {
+        struct stat st;
+        char *ap = NULL, *line = NULL;
+        int r;
+        dev_t d;
+
+        assert(p);
+        assert(bytes);
+
+        if (stat(p, &st) < 0)
+                return -errno;
+
+        if (major(st.st_dev) == 0)
+                return 0;
+
+        d = st.st_dev;
+        block_get_whole_disk(d, &d);
+
+        if (asprintf(&ap, "/sys/dev/block/%u:%u/bdi/read_ahead_kb", major(d), minor(d)) < 0) {
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (asprintf(&line, "%llu", (unsigned long long) bytes / 1024ULL) < 0) {
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        r = write_one_line_file(ap, line);
+        if (r < 0)
+                goto finish;
+
+finish:
+        free(ap);
+        free(line);
+
+        return r;
+}