Stefano Duo | 046c854 | 2020-09-07 16:30:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <chrono> |
| 17 | #include <functional> |
| 18 | #include <iostream> |
| 19 | #include <ratio> |
| 20 | #include <sstream> |
| 21 | #include <string> |
| 22 | #include <unordered_map> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <dirent.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | static constexpr char VERSION[] = "0"; |
| 33 | |
| 34 | // Self-contained class for collecting and reporting benchmark metrics |
| 35 | // (currently only execution time). |
| 36 | class Collector { |
| 37 | using time_point = std::chrono::time_point<std::chrono::steady_clock>; |
| 38 | using time_unit = std::chrono::duration<double, std::milli>; |
| 39 | |
| 40 | struct Metric { |
| 41 | std::string workload; |
| 42 | time_unit exec_time; |
| 43 | Metric(const std::string& workload, const time_unit& exec_time) |
| 44 | : workload(workload), exec_time(exec_time) {} |
| 45 | }; |
| 46 | |
| 47 | static constexpr char TIME_UNIT[] = "ms"; |
| 48 | static constexpr char VERSION[] = "0"; |
| 49 | std::vector<Metric> metrics; |
| 50 | time_point reset_time; |
| 51 | |
| 52 | public: |
| 53 | Collector() { reset(); } |
| 54 | |
| 55 | void reset() { reset_time = std::chrono::steady_clock::now(); } |
| 56 | |
| 57 | void collect_metric(const std::string& workload) { |
| 58 | auto elapsed = std::chrono::steady_clock::now() - reset_time; |
| 59 | metrics.emplace_back(workload, std::chrono::duration_cast<time_unit>(elapsed)); |
| 60 | } |
| 61 | |
| 62 | void report_metrics() { |
| 63 | for (const Metric& metric : metrics) |
| 64 | std::cout << VERSION << ";" << metric.workload << ";" << metric.exec_time.count() << ";" |
| 65 | << TIME_UNIT << std::endl; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | struct Command { |
| 70 | static constexpr char CREATE[] = "create"; |
| 71 | static constexpr char DELETE[] = "delete"; |
| 72 | static constexpr char MOVE[] = "move"; |
| 73 | static constexpr char HARDLINK[] = "hardlink"; |
| 74 | static constexpr char SYMLINK[] = "symlink"; |
| 75 | static constexpr char READDIR[] = "readdir"; |
| 76 | std::string workload; |
| 77 | std::string from_dir; |
| 78 | std::string from_basename; |
| 79 | std::string to_dir; |
| 80 | std::string to_basename; |
| 81 | bool drop_state; |
| 82 | int n_file; |
| 83 | |
| 84 | Command() { reset(); } |
| 85 | |
| 86 | std::string to_string() const { |
| 87 | std::stringstream string_repr; |
| 88 | string_repr << "Command {\n"; |
| 89 | string_repr << "\t.workload = " << workload << ",\n"; |
| 90 | string_repr << "\t.from_dir = " << from_dir << ",\n"; |
| 91 | string_repr << "\t.from_basename = " << from_basename << ",\n"; |
| 92 | string_repr << "\t.to_dir = " << to_dir << ",\n"; |
| 93 | string_repr << "\t.to_basename = " << to_basename << ",\n"; |
| 94 | string_repr << "\t.drop_state = " << drop_state << ",\n"; |
| 95 | string_repr << "\t.n_file = " << n_file << "\n"; |
| 96 | string_repr << "}\n"; |
| 97 | return string_repr.str(); |
| 98 | } |
| 99 | |
| 100 | void reset() { |
| 101 | workload = ""; |
| 102 | from_dir = to_dir = "./"; |
| 103 | from_basename = "from_file"; |
| 104 | to_basename = "to_file"; |
| 105 | drop_state = true; |
| 106 | n_file = 0; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | void print_version() { |
| 111 | std::cout << VERSION << std::endl; |
| 112 | } |
| 113 | |
| 114 | void print_commands(const std::vector<Command>& commands) { |
| 115 | for (const Command& command : commands) std::cout << command.to_string(); |
| 116 | } |
| 117 | |
| 118 | void usage(std::ostream& ostr, const std::string& program_name) { |
| 119 | Command command; |
| 120 | |
| 121 | ostr << "Usage: " << program_name << " [global_options] {[workload_options] -w WORKLOAD_T}\n"; |
| 122 | ostr << "WORKLOAD_T = {" << Command::CREATE << ", " << Command::DELETE << ", " << Command::MOVE |
| 123 | << ", " << Command::HARDLINK << ", " << Command::SYMLINK << "}\n"; |
| 124 | ostr << "Global options\n"; |
| 125 | ostr << "\t-v: Print version.\n"; |
| 126 | ostr << "\t-p: Print parsed workloads and exit.\n"; |
| 127 | ostr << "Workload options\n"; |
| 128 | ostr << "\t-d DIR\t\t: Work directory for " << Command::CREATE << "/" << Command::DELETE |
| 129 | << " (default '" << command.from_dir << "').\n"; |
| 130 | ostr << "\t-f FROM-DIR\t: Source directory for " << Command::MOVE << "/" << Command::SYMLINK |
| 131 | << "/" << Command::HARDLINK << " (default '" << command.from_dir << "').\n"; |
| 132 | ostr << "\t-t TO-DIR\t: Destination directory for " << Command::MOVE << "/" << Command::SYMLINK |
| 133 | << "/" << Command::HARDLINK << " (default '" << command.to_dir << "').\n"; |
| 134 | ostr << "\t-n N_FILES\t: Number of files to create/delete etc. (default " << command.n_file |
| 135 | << ").\n"; |
| 136 | ostr << "\t-s\t\t: Do not drop state (caches) before running the workload (default " |
| 137 | << !command.drop_state << ").\n"; |
| 138 | ostr << "NOTE: -w WORKLOAD_T defines a new command and must come after its workload_options." |
| 139 | << std::endl; |
| 140 | } |
| 141 | |
| 142 | void drop_state() { |
| 143 | // Drop inode/dentry/page caches. |
| 144 | std::system("sync; echo 3 > /proc/sys/vm/drop_caches"); |
| 145 | } |
| 146 | |
| 147 | static constexpr int OPEN_DIR_FLAGS = O_RDONLY | O_DIRECTORY | O_PATH | O_CLOEXEC; |
| 148 | |
| 149 | bool create_files(const std::string& dir, int n_file, const std::string& basename) { |
| 150 | int dir_fd = open(dir.c_str(), OPEN_DIR_FLAGS); |
| 151 | if (dir_fd == -1) { |
| 152 | int error = errno; |
| 153 | std::cerr << "Failed to open work directory '" << dir << "', error '" << strerror(error) |
| 154 | << "'." << std::endl; |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | for (int i = 0; i < n_file; i++) { |
| 159 | std::string filename = basename + std::to_string(i); |
| 160 | int fd = openat(dir_fd, filename.c_str(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0777); |
| 161 | close(fd); |
| 162 | } |
| 163 | |
| 164 | close(dir_fd); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool delete_files(const std::string& dir, int n_file, const std::string& basename) { |
| 169 | int dir_fd = open(dir.c_str(), OPEN_DIR_FLAGS); |
| 170 | if (dir_fd == -1) { |
| 171 | int error = errno; |
| 172 | std::cerr << "Failed to open work directory '" << dir << "', error '" << strerror(error) |
| 173 | << "'." << std::endl; |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | for (int i = 0; i < n_file; i++) { |
| 178 | std::string filename = basename + std::to_string(i); |
| 179 | unlinkat(dir_fd, filename.c_str(), 0); |
| 180 | } |
| 181 | |
| 182 | close(dir_fd); |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool move_files(const std::string& from_dir, const std::string& to_dir, int n_file, |
| 187 | const std::string& from_basename, const std::string& to_basename) { |
| 188 | int from_dir_fd = open(from_dir.c_str(), OPEN_DIR_FLAGS); |
| 189 | if (from_dir_fd == -1) { |
| 190 | int error = errno; |
| 191 | std::cerr << "Failed to open source directory '" << from_dir << "', error '" |
| 192 | << strerror(error) << "'." << std::endl; |
| 193 | return false; |
| 194 | } |
| 195 | int to_dir_fd = open(to_dir.c_str(), OPEN_DIR_FLAGS); |
| 196 | if (to_dir_fd == -1) { |
| 197 | int error = errno; |
| 198 | std::cerr << "Failed to open destination directory '" << to_dir << "', error '" |
| 199 | << strerror(error) << "'." << std::endl; |
| 200 | close(from_dir_fd); |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | for (int i = 0; i < n_file; i++) { |
| 205 | std::string from_filename = from_basename + std::to_string(i); |
| 206 | std::string to_filename = to_basename + std::to_string(i); |
| 207 | renameat(from_dir_fd, from_filename.c_str(), to_dir_fd, to_filename.c_str()); |
| 208 | } |
| 209 | |
| 210 | close(from_dir_fd); |
| 211 | close(from_dir_fd); |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | bool hardlink_files(const std::string& from_dir, const std::string& to_dir, int n_file, |
| 216 | const std::string& from_basename, const std::string& to_basename) { |
| 217 | int from_dir_fd = open(from_dir.c_str(), OPEN_DIR_FLAGS); |
| 218 | if (from_dir_fd == -1) { |
| 219 | int error = errno; |
| 220 | std::cerr << "Failed to open source directory '" << from_dir << "', error '" |
| 221 | << strerror(error) << "'." << std::endl; |
| 222 | return false; |
| 223 | } |
| 224 | int to_dir_fd = open(to_dir.c_str(), OPEN_DIR_FLAGS); |
| 225 | if (to_dir_fd == -1) { |
| 226 | int error = errno; |
| 227 | std::cerr << "Failed to open destination directory '" << to_dir << "', error '" |
| 228 | << strerror(error) << "'." << std::endl; |
| 229 | close(from_dir_fd); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | for (int i = 0; i < n_file; i++) { |
| 234 | std::string from_filename = from_basename + std::to_string(i); |
| 235 | std::string to_filename = to_basename + std::to_string(i); |
| 236 | linkat(from_dir_fd, from_filename.c_str(), to_dir_fd, to_filename.c_str(), 0); |
| 237 | } |
| 238 | |
| 239 | close(from_dir_fd); |
| 240 | close(to_dir_fd); |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | bool symlink_files(std::string from_dir, const std::string& to_dir, int n_file, |
| 245 | const std::string& from_basename, const std::string& to_basename) { |
| 246 | if (from_dir.back() != '/') from_dir.push_back('/'); |
| 247 | int to_dir_fd = open(to_dir.c_str(), OPEN_DIR_FLAGS); |
| 248 | if (to_dir_fd == -1) { |
| 249 | int error = errno; |
| 250 | std::cerr << "Failed to open destination directory '" << to_dir << "', error '" |
| 251 | << strerror(error) << "'." << std::endl; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | for (int i = 0; i < n_file; i++) { |
| 256 | std::string from_filepath = from_dir + from_basename + std::to_string(i); |
| 257 | std::string to_filename = to_basename + std::to_string(i); |
| 258 | symlinkat(from_filepath.c_str(), to_dir_fd, to_filename.c_str()); |
| 259 | } |
| 260 | |
| 261 | close(to_dir_fd); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | bool exhaustive_readdir(const std::string& from_dir) { |
| 266 | DIR* dir = opendir(from_dir.c_str()); |
| 267 | if (dir == nullptr) { |
| 268 | int error = errno; |
| 269 | std::cerr << "Failed to open working directory '" << from_dir << "', error '" |
| 270 | << strerror(error) << "'." << std::endl; |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | while (readdir(dir) != nullptr) |
| 275 | ; |
| 276 | |
| 277 | closedir(dir); |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | void create_workload(Collector* collector, const Command& command) { |
| 282 | if (command.drop_state) drop_state(); |
| 283 | collector->reset(); |
| 284 | if (create_files(command.from_dir, command.n_file, command.from_basename)) |
| 285 | collector->collect_metric(command.workload); |
| 286 | |
| 287 | delete_files(command.from_dir, command.n_file, command.from_basename); |
| 288 | } |
| 289 | |
| 290 | void delete_workload(Collector* collector, const Command& command) { |
| 291 | if (!create_files(command.from_dir, command.n_file, command.from_basename)) return; |
| 292 | |
| 293 | if (command.drop_state) drop_state(); |
| 294 | collector->reset(); |
| 295 | if (delete_files(command.from_dir, command.n_file, command.from_basename)) |
| 296 | collector->collect_metric(command.workload); |
| 297 | } |
| 298 | |
| 299 | void move_workload(Collector* collector, const Command& command) { |
| 300 | if (!create_files(command.from_dir, command.n_file, command.from_basename)) return; |
| 301 | |
| 302 | if (command.drop_state) drop_state(); |
| 303 | collector->reset(); |
| 304 | if (move_files(command.from_dir, command.to_dir, command.n_file, command.from_basename, |
| 305 | command.to_basename)) |
| 306 | collector->collect_metric(command.workload); |
| 307 | |
| 308 | delete_files(command.to_dir, command.n_file, command.to_basename); |
| 309 | } |
| 310 | |
| 311 | void hardlink_workload(Collector* collector, const Command& command) { |
| 312 | if (!create_files(command.from_dir, command.n_file, command.from_basename)) return; |
| 313 | |
| 314 | if (command.drop_state) drop_state(); |
| 315 | collector->reset(); |
| 316 | if (hardlink_files(command.from_dir, command.to_dir, command.n_file, command.from_basename, |
| 317 | command.to_basename)) |
| 318 | collector->collect_metric(command.workload); |
| 319 | |
| 320 | delete_files(command.from_dir, command.n_file, command.from_basename); |
| 321 | delete_files(command.to_dir, command.n_file, command.to_basename); |
| 322 | } |
| 323 | |
| 324 | void symlink_workload(Collector* collector, const Command& command) { |
| 325 | if (!create_files(command.from_dir, command.n_file, command.from_basename)) return; |
| 326 | |
| 327 | if (command.drop_state) drop_state(); |
| 328 | collector->reset(); |
| 329 | if (symlink_files(command.from_dir, command.to_dir, command.n_file, command.from_basename, |
| 330 | command.to_basename)) |
| 331 | collector->collect_metric(command.workload); |
| 332 | |
| 333 | delete_files(command.to_dir, command.n_file, command.to_basename); |
| 334 | delete_files(command.from_dir, command.n_file, command.from_basename); |
| 335 | } |
| 336 | |
| 337 | void readdir_workload(Collector* collector, const Command& command) { |
| 338 | if (!create_files(command.from_dir, command.n_file, command.from_basename)) return; |
| 339 | |
| 340 | if (command.drop_state) drop_state(); |
| 341 | collector->reset(); |
| 342 | if (exhaustive_readdir(command.from_dir)) collector->collect_metric(command.workload); |
| 343 | |
| 344 | delete_files(command.from_dir, command.n_file, command.from_basename); |
| 345 | } |
| 346 | |
| 347 | using workload_executor_t = std::function<void(Collector*, const Command&)>; |
| 348 | |
| 349 | std::unordered_map<std::string, workload_executor_t> executors = { |
| 350 | {Command::CREATE, create_workload}, {Command::DELETE, delete_workload}, |
| 351 | {Command::MOVE, move_workload}, {Command::HARDLINK, hardlink_workload}, |
| 352 | {Command::SYMLINK, symlink_workload}, {Command::READDIR, readdir_workload}}; |
| 353 | |
| 354 | int main(int argc, char** argv) { |
| 355 | std::vector<Command> commands; |
| 356 | Command command; |
| 357 | int opt; |
| 358 | |
| 359 | while ((opt = getopt(argc, argv, "hvpsw:d:f:t:n:")) != -1) { |
| 360 | switch (opt) { |
| 361 | case 'h': |
| 362 | usage(std::cout, argv[0]); |
| 363 | return EXIT_SUCCESS; |
| 364 | case 'v': |
| 365 | print_version(); |
| 366 | return EXIT_SUCCESS; |
| 367 | case 'p': |
| 368 | print_commands(commands); |
| 369 | return EXIT_SUCCESS; |
| 370 | case 's': |
| 371 | command.drop_state = false; |
| 372 | break; |
| 373 | case 'w': |
| 374 | command.workload = optarg; |
| 375 | commands.push_back(command); |
| 376 | command.reset(); |
| 377 | break; |
| 378 | case 'd': |
| 379 | case 'f': |
| 380 | command.from_dir = optarg; |
| 381 | break; |
| 382 | case 't': |
| 383 | command.to_dir = optarg; |
| 384 | break; |
| 385 | case 'n': |
| 386 | command.n_file = std::stoi(optarg); |
| 387 | break; |
| 388 | default: |
| 389 | usage(std::cerr, argv[0]); |
| 390 | return EXIT_FAILURE; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | Collector collector; |
| 395 | for (const Command& command : commands) { |
| 396 | auto executor = executors.find(command.workload); |
| 397 | if (executor == executors.end()) continue; |
| 398 | executor->second(&collector, command); |
| 399 | } |
| 400 | collector.report_metrics(); |
| 401 | |
| 402 | return EXIT_SUCCESS; |
| 403 | } |