chiark / gitweb /
readahead: make candidate for early OOM kill
[elogind.git] / src / readahead-replay.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <fcntl.h>
25 #include <linux/limits.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35
36 #include "missing.h"
37 #include "util.h"
38 #include "set.h"
39 #include "sd-daemon.h"
40 #include "ioprio.h"
41 #include "readahead-common.h"
42
43 static int unpack_file(FILE *pack) {
44         char fn[PATH_MAX];
45         int r = 0, fd = -1;
46         bool any = false;
47         struct stat st;
48
49         assert(pack);
50
51         if (!fgets(fn, sizeof(fn), pack))
52                 return 0;
53
54         char_array_0(fn);
55         truncate_nl(fn);
56
57         if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
58                 log_warning("open(%s) failed: %m", fn);
59         else if (file_verify(fd, fn, &st) <= 0) {
60                 close_nointr_nofail(fd);
61                 fd = -1;
62         }
63
64         for (;;) {
65                 uint32_t b, c;
66
67                 if (fread(&b, sizeof(b), 1, pack) != 1 ||
68                     fread(&c, sizeof(c), 1, pack) != 1) {
69                         log_error("Premature end of pack file.");
70                         r = -EIO;
71                         goto finish;
72                 }
73
74                 if (b == 0 && c == 0)
75                         break;
76
77                 if (c <= b) {
78                         log_error("Invalid pack file.");
79                         r = -EIO;
80                         goto finish;
81                 }
82
83                 log_debug("%s: page %u to %u", fn, b, c);
84
85                 any = true;
86
87                 if (fd >= 0)
88                         if (readahead(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE) < 0) {
89                                 log_warning("readahead() failed: %m");
90                                 goto finish;
91                         }
92         }
93
94         if (!any && fd >= 0) {
95                 /* if no range is encoded in the pack file this is
96                  * intended to mean that the whole file shall be
97                  * read */
98
99                 if (readahead(fd, 0, st.st_size) < 0) {
100                         log_warning("readahead() failed: %m");
101                         goto finish;
102                 }
103         }
104
105 finish:
106         if (fd >= 0)
107                 close_nointr_nofail(fd);
108
109         return r;
110 }
111
112 static int replay(const char *root) {
113         FILE *pack;
114         char line[LINE_MAX];
115         int r = 0;
116         char *pack_fn = NULL, c;
117         bool on_ssd, ready = false;
118         int prio;
119
120         assert(root);
121
122         write_one_line_file("/proc/self/oom_score_adj", "1000");
123
124         if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
125                 log_error("Out of memory");
126                 r = -ENOMEM;
127                 goto finish;
128         }
129
130         if ((!(pack = fopen(pack_fn, "re")))) {
131                 if (errno == -ENOENT)
132                         log_debug("No pack file found.");
133                 else {
134                         log_error("Failed to open pack file: %m");
135                         r = -errno;
136                 }
137
138                 goto finish;
139         }
140
141         if (!(fgets(line, sizeof(line), pack))) {
142                 log_error("Premature end of pack file.");
143                 r = -EIO;
144                 goto finish;
145         }
146
147         char_array_0(line);
148
149         if (!streq(line, CANONICAL_HOST "\n")) {
150                 log_debug("Pack file host type mismatch.");
151                 goto finish;
152         }
153
154         if ((c = getc(pack)) == EOF) {
155                 log_debug("Premature end of pack file.");
156                 r = -EIO;
157                 goto finish;
158         }
159
160         /* We do not retest SSD here, so that we can start replaying
161          * before udev is up.*/
162         on_ssd = c == 'S';
163         log_debug("On SSD: %s", yes_no(on_ssd));
164
165         if (on_ssd)
166                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
167         else
168                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
169
170         if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
171                 log_warning("Failed to set IDLE IO priority class: %m");
172
173         sd_notify(0, "STATUS=Replaying readahead data");
174
175         log_debug("Replaying...");
176
177         while (!feof(pack) && !ferror(pack)) {
178                 int k;
179
180                 if ((k = unpack_file(pack)) < 0) {
181                         r = k;
182                         goto finish;
183                 }
184
185                 if (!ready) {
186                         /* We delay the ready notification until we
187                          * queued at least one read */
188                         sd_notify(0, "READY=1");
189                         ready = true;
190                 }
191         }
192
193         if (!ready)
194                 sd_notify(0, "READY=1");
195
196         if (ferror(pack)) {
197                 log_error("Failed to read pack file.");
198                 r = -EIO;
199                 goto finish;
200         }
201
202         log_debug("Done.");
203
204 finish:
205         if (pack)
206                 fclose(pack);
207
208         free(pack_fn);
209
210         return r;
211 }
212
213 int main(int argc, char*argv[]) {
214
215         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
216         log_parse_environment();
217         log_open();
218
219         if (!enough_ram()) {
220                 log_info("Disabling readahead replay due to low memory.");
221                 return 0;
222         }
223
224         if (replay(argc >= 2 ? argv[1] : "/") < 0)
225                 return 1;
226
227         return 0;
228 }