• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "common/debug.h"
16 #include "prefetcher/read_ahead.h"
17 #include "prefetcher/task_id.h"
18 
19 #include <android-base/parseint.h>
20 #include <android-base/logging.h>
21 
22 #include <iostream>
23 #include <optional>
24 #include <string_view>
25 #include <string>
26 #include <vector>
27 
28 #include <signal.h>
29 #include <unistd.h>
30 
31 
32 namespace iorap::prefetcher {
33 
UsageClient(char ** argv)34 static void UsageClient(char** argv) {
35   std::cerr << "UsageClient: " << argv[0] << " <path-to-compiled-trace.pb> [... pathN]" << std::endl;
36   std::cerr << "" << std::endl;
37   std::cerr << "  Run the readahead daemon which can prefetch files given a command." << std::endl;
38   std::cerr << "" << std::endl;
39   std::cerr << "  Optional flags:" << std::endl;
40   std::cerr << "    --help,-h                  Print this UsageClient." << std::endl;
41   std::cerr << "    --verbose,-v               Set verbosity (default off)." << std::endl;
42   std::cerr << "    --task-duration-ms,-tdm    Set task duration (default: 0ms)." << std::endl;
43   std::cerr << "    --use-sockets,-us          Use AF_UNIX sockets (default: off)" << std::endl;
44   std::cerr << "    --wait,-w                  Wait for key stroke before continuing (default off)." << std::endl;
45   exit(1);
46 }
47 
MainClient(int argc,char ** argv)48 int MainClient(int argc, char** argv) {
49   android::base::InitLogging(argv);
50   android::base::SetLogger(android::base::StderrLogger);
51 
52   bool wait_for_keystroke = false;
53   bool enable_verbose = false;
54 
55   bool command_format_text = false; // false = binary.
56 
57   unsigned int arg_task_duration_ms = 10000;
58   std::vector<std::string> arg_input_filenames;
59   bool arg_use_sockets = false;
60 
61   LOG(VERBOSE) << "argparse: argc=" << argc;
62 
63   for (int arg = 1; arg < argc; ++arg) {
64     std::string argstr = argv[arg];
65     bool has_arg_next = (arg+1)<argc;
66     std::string arg_next = has_arg_next ? argv[arg+1] : "";
67 
68     LOG(VERBOSE) << "argparse: argv[" << arg << "]=" << argstr;
69 
70     if (argstr == "--help" || argstr == "-h") {
71       UsageClient(argv);
72     } else if (argstr == "--use-sockets" || argstr == "-us") {
73       arg_use_sockets = true;
74     } else if (argstr == "--verbose" || argstr == "-v") {
75       enable_verbose = true;
76     } else if (argstr == "--wait" || argstr == "-w") {
77       wait_for_keystroke = true;
78     } else if (argstr == "--task-duration-ms" || argstr == "-tdm") {
79       if (!has_arg_next) {
80         LOG(ERROR) << "--task-duration-ms: requires uint parameter";
81         UsageClient(argv);
82       } else if (!::android::base::ParseUint(arg_next, &arg_task_duration_ms)) {
83         LOG(ERROR) << "--task-duration-ms: requires non-negative parameter";
84         UsageClient(argv);
85       }
86     } else {
87       arg_input_filenames.push_back(argstr);
88     }
89   }
90 
91   if (enable_verbose) {
92     android::base::SetMinimumLogSeverity(android::base::VERBOSE);
93 
94     LOG(VERBOSE) << "Verbose check";
95     LOG(VERBOSE) << "Debug check: " << ::iorap::kIsDebugBuild;
96   } else {
97     android::base::SetMinimumLogSeverity(android::base::DEBUG);
98   }
99 
100   LOG(VERBOSE) << "argparse: argc=" << argc;
101 
102   for (int arg = 1; arg < argc; ++arg) {
103     std::string argstr = argv[arg];
104 
105     LOG(VERBOSE) << "argparse: argv[" << arg << "]=" << argstr;
106   }
107 
108   // Useful to attach a debugger...
109   // 1) $> iorap.cmd.readahead -w <args>
110   // 2) $> gdbclient <pid>
111   if (wait_for_keystroke) {
112     LOG(INFO) << "Self pid: " << getpid();
113 
114     raise(SIGSTOP);
115     // LOG(INFO) << "Press any key to continue...";
116     // std::cin >> wait_for_keystroke;
117   }
118 
119   // auto system_call = std::make_unique<SystemCallImpl>();
120   // TODO: mock readahead calls?
121   //
122   // Uncomment this if we want to leave the process around to inspect it from adb shell.
123   // sleep(100000);
124 
125   int return_code = 0;
126 
127   LOG(VERBOSE) << "Hello world";
128 
129   ReadAhead read_ahead{arg_use_sockets};  // Don't count the time it takes to fork+exec.
130 
131   size_t task_id_counter = 0;
132   for (const std::string& compiled_trace_path : arg_input_filenames) {
133     TaskId task_id{task_id_counter++, compiled_trace_path};
134 
135     LOG(DEBUG) << "main: ReadAhead BeginTask: "
136                  << "task_duration_ms=" << arg_task_duration_ms << ","
137                  << task_id;
138 
139     read_ahead.BeginTask(task_id);
140     usleep(arg_task_duration_ms*1000);
141 
142     LOG(DEBUG) << "main: ReadAhead FinishTask: " << task_id;
143 
144     read_ahead.FinishTask(task_id);
145   }
146   LOG(VERBOSE) << "main: Terminating";
147 
148   // 0 -> successfully executed all commands.
149   // 1 -> failed along the way (#on_error and also see the error logs).
150   return return_code;
151 }
152 
153 }  // namespace iorap::prefetcher
154 
155 #if defined(IORAP_PREFETCHER_MAIN_CLIENT)
main(int argc,char ** argv)156 int main(int argc, char** argv) {
157   return ::iorap::prefetcher::MainClient(argc, argv);
158 }
159 
160 #endif  // IORAP_PREFETCHER_MAIN_CLIENT
161