1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
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.
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.
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/>.
33 #include "readahead-common.h"
35 unsigned arg_files_max = 16*1024;
36 off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
37 usec_t arg_timeout = 2*USEC_PER_MINUTE;
39 static void help(void) {
40 printf("%1$s [OPTIONS...] collect [DIRECTORY]\n\n"
41 "Collect read-ahead data on early boot.\n\n"
42 " -h --help Show this help\n"
43 " --version Show package version\n"
44 " --files-max=INT Maximum number of files to read ahead\n"
45 " --file-size-max=BYTES Maximum size of files to read ahead\n"
46 " --timeout=USEC Maximum time to spend collecting data\n"
48 "%1$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 " --version Show package version\n"
52 " --file-size-max=BYTES Maximum size of files to read ahead\n"
54 "%1$s [OPTIONS...] analyze [PACK-FILE]\n\n"
55 "Analyze collected read-ahead data.\n\n"
56 " -h --help Show this help\n"
57 " --version Show package version\n",
58 program_invocation_short_name);
61 static int parse_argv(int argc, char *argv[]) {
70 static const struct option options[] = {
71 { "help", no_argument, NULL, 'h' },
72 { "version", no_argument, NULL, ARG_VERSION },
73 { "files-max", required_argument, NULL, ARG_FILES_MAX },
74 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX },
75 { "timeout", required_argument, NULL, ARG_TIMEOUT },
84 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
94 puts(SYSTEMD_FEATURES);
98 if (safe_atou(optarg, &arg_files_max) < 0 || arg_files_max <= 0) {
99 log_error("Failed to parse maximum number of files %s.", optarg);
104 case ARG_FILE_SIZE_MAX: {
105 unsigned long long ull;
107 if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
108 log_error("Failed to parse maximum file size %s.", optarg);
112 arg_file_size_max = (off_t) ull;
117 if (parse_sec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
118 log_error("Failed to parse timeout %s.", optarg);
128 assert_not_reached("Unhandled option");
131 if (optind != argc-1 &&
133 log_error("%s: wrong number of arguments.",
134 program_invocation_short_name);
141 int main(int argc, char *argv[]) {
144 log_set_target(LOG_TARGET_SAFE);
145 log_parse_environment();
150 r = parse_argv(argc, argv);
152 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
154 if (streq(argv[optind], "collect"))
155 return main_collect(argv[optind+1]);
156 else if (streq(argv[optind], "replay"))
157 return main_replay(argv[optind+1]);
158 else if (streq(argv[optind], "analyze"))
159 return main_analyze(argv[optind+1]);
161 log_error("Unknown verb %s.", argv[optind]);