chiark / gitweb /
abeecc763497a704391de6fa56d2ed63118d3d05
[elogind.git] / src / readahead / readahead.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29
30 #include "util.h"
31 #include "def.h"
32 #include "readahead-common.h"
33
34 unsigned arg_files_max = 16*1024;
35 off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
36 usec_t arg_timeout = 2*USEC_PER_MINUTE;
37
38 static int help(void) {
39
40         printf("%s [OPTIONS...] collect [DIRECTORY]\n\n"
41                "Collect read-ahead data on early boot.\n\n"
42                "  -h --help                 Show this help\n"
43                "     --max-files=INT        Maximum number of files to read ahead\n"
44                "     --file-size-max=BYTES  Maximum size of files to read ahead\n"
45                "     --timeout=USEC         Maximum time to spend collecting data\n\n\n",
46                program_invocation_short_name);
47
48         printf("%s [OPTIONS...] replay [DIRECTORY]\n\n"
49                "Replay collected read-ahead data on early boot.\n\n"
50                "  -h --help                 Show this help\n"
51                "     --file-size-max=BYTES  Maximum size of files to read ahead\n\n\n",
52                program_invocation_short_name);
53
54         printf("%s [OPTIONS...] analyze [PACK FILE]\n\n"
55                "Analyze collected read-ahead data.\n\n"
56                "  -h --help                 Show this help\n",
57                program_invocation_short_name);
58
59         return 0;
60 }
61
62 static int parse_argv(int argc, char *argv[]) {
63
64         enum {
65                 ARG_FILES_MAX = 0x100,
66                 ARG_FILE_SIZE_MAX,
67                 ARG_TIMEOUT
68         };
69
70         static const struct option options[] = {
71                 { "help",          no_argument,       NULL, 'h'                },
72                 { "files-max",     required_argument, NULL, ARG_FILES_MAX      },
73                 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX  },
74                 { "timeout",       required_argument, NULL, ARG_TIMEOUT        },
75                 { NULL,            0,                 NULL, 0                  }
76         };
77
78         int c;
79
80         assert(argc >= 0);
81         assert(argv);
82
83         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
84
85                 switch (c) {
86
87                 case 'h':
88                         help();
89                         return 0;
90
91                 case ARG_FILES_MAX:
92                         if (safe_atou(optarg, &arg_files_max) < 0 || arg_files_max <= 0) {
93                                 log_error("Failed to parse maximum number of files %s.", optarg);
94                                 return -EINVAL;
95                         }
96                         break;
97
98                 case ARG_FILE_SIZE_MAX: {
99                         unsigned long long ull;
100
101                         if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
102                                 log_error("Failed to parse maximum file size %s.", optarg);
103                                 return -EINVAL;
104                         }
105
106                         arg_file_size_max = (off_t) ull;
107                         break;
108                 }
109
110                 case ARG_TIMEOUT:
111                         if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
112                                 log_error("Failed to parse timeout %s.", optarg);
113                                 return -EINVAL;
114                         }
115
116                         break;
117
118                 case '?':
119                         return -EINVAL;
120
121                 default:
122                         log_error("Unknown option code %c", c);
123                         return -EINVAL;
124                 }
125         }
126
127         if (optind != argc-1 &&
128             optind != argc-2) {
129                 help();
130                 return -EINVAL;
131         }
132
133         return 1;
134 }
135
136 int main(int argc, char *argv[]) {
137         int r;
138
139         log_set_target(LOG_TARGET_SAFE);
140         log_parse_environment();
141         log_open();
142
143         umask(0022);
144
145         r = parse_argv(argc, argv);
146         if (r <= 0)
147                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
148
149         if (streq(argv[optind], "collect"))
150                 return main_collect(argv[optind+1]);
151         else if (streq(argv[optind], "replay"))
152                 return main_replay(argv[optind+1]);
153         else if (streq(argv[optind], "analyze"))
154                 return main_analyze(argv[optind+1]);
155
156         log_error("Unknown verb %s.", argv[optind]);
157         return EXIT_FAILURE;
158 }