chiark / gitweb /
readahead-replay: delay ready notification until we queued first read
[elogind.git] / src / readahead-collect.c
index 5ff3711068ab3acdcfba02b442e5751a47d51c14..6b9fb4650b78e2fe60499c7fe0d7f183966b2ada 100644 (file)
 
 #define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
 
+/* fixme:
+ *
+ * - detect ssd/lvm/... on btrfs
+ * - read ahead directories
+ * - sd_readahead_cancel
+ */
+
 static int btrfs_defrag(int fd) {
         struct btrfs_ioctl_vol_args data;
 
@@ -255,6 +262,9 @@ static int collect(const char *root) {
                 ssize_t n;
                 struct fanotify_event_metadata *m;
 
+                if (hashmap_size(files) > READAHEAD_FILES_MAX)
+                        break;
+
                 if (poll(pollfd, _FD_MAX, -1) < 0) {
 
                         if (errno == EINTR)
@@ -299,10 +309,7 @@ static int collect(const char *root) {
                                                 ul = fd_first_block(m->fd);
 
                                                 if ((k = hashmap_put(files, p, ULONG_TO_PTR(ul))) < 0) {
-
-                                                        if (k != -EEXIST)
-                                                                log_warning("set_put() failed: %s", strerror(-k));
-
+                                                        log_warning("set_put() failed: %s", strerror(-k));
                                                         free(p);
                                                 }
                                         }
@@ -316,7 +323,6 @@ static int collect(const char *root) {
 
                         m = FAN_EVENT_NEXT(m, n);
                 }
-
         }
 
         if (fanotify_fd >= 0) {
@@ -353,7 +359,7 @@ static int collect(const char *root) {
         if (on_ssd || on_btrfs) {
 
                 /* On SSD or on btrfs, just write things out in the
-                 * order the files where accessed. */
+                 * order the files were accessed. */
 
                 HASHMAP_FOREACH_KEY(q, p, files, i)
                         pack_file(pack, p, on_btrfs);
@@ -435,13 +441,17 @@ finish:
 }
 
 int main(int argc, char *argv[]) {
-        /* log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); */
+
+        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
         log_parse_environment();
         log_open();
 
-        log_set_max_level(LOG_DEBUG);
+        if (!enough_ram()) {
+                log_info("Disabling readahead collector due to low memory.");
+                return 0;
+        }
 
-        if (collect("/") < 0)
+        if (collect(argc >= 2 ? argv[1] : "/") < 0)
                 return 1;
 
         return 0;