Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rbd_replay / rbd-replay.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2014 Adam Crume <adamcrume@gmail.com>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include <vector>
16 #include <boost/thread.hpp>
17 #include "common/ceph_argparse.h"
18 #include "global/global_init.h"
19 #include "Replayer.hpp"
20 #include "rbd_replay_debug.hpp"
21 #include "ImageNameMap.hpp"
22
23
24 using namespace std;
25 using namespace rbd_replay;
26
27
28 static const char* get_remainder(const char *string, const char *prefix) {
29   while (*prefix) {
30     if (*prefix++ != *string++) {
31       return NULL;
32     }
33   }
34   return string;
35 }
36
37 static void usage(const char* program) {
38   cout << "Usage: " << program << " --conf=<config_file> <replay_file>" << std::endl;
39   cout << "Options:" << std::endl;
40   cout << "  -p, --pool-name <pool>          Name of the pool to use.  Default: rbd" << std::endl;
41   cout << "  --latency-multiplier <float>    Multiplies inter-request latencies.  Default: 1" << std::endl;
42   cout << "  --read-only                     Only perform non-destructive operations." << std::endl;
43   cout << "  --map-image <rule>              Add a rule to map image names in the trace to" << std::endl;
44   cout << "                                  image names in the replay cluster." << std::endl;
45   cout << "  --dump-perf-counters            *Experimental*" << std::endl;
46   cout << "                                  Dump performance counters to standard out before" << std::endl;
47   cout << "                                  an image is closed. Performance counters may be dumped" << std::endl;
48   cout << "                                  multiple times if multiple images are closed, or if" << std::endl;
49   cout << "                                  the same image is opened and closed multiple times." << std::endl;
50   cout << "                                  Performance counters and their meaning may change between" << std::endl;
51   cout << "                                  versions." << std::endl;
52   cout << std::endl;
53   cout << "Image mapping rules:" << std::endl;
54   cout << "A rule of image1@snap1=image2@snap2 would map snap1 of image1 to snap2 of" << std::endl;
55   cout << "image2." << std::endl;
56 }
57
58 int main(int argc, const char **argv) {
59   vector<const char*> args;
60
61   argv_to_vec(argc, argv, args);
62   env_to_vec(args);
63   auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
64                          CODE_ENVIRONMENT_UTILITY, 0);
65
66   std::vector<const char*>::iterator i;
67   string pool_name;
68   float latency_multiplier = 1;
69   bool readonly = false;
70   ImageNameMap image_name_map;
71   std::string val;
72   std::ostringstream err;
73   bool dump_perf_counters = false;
74   for (i = args.begin(); i != args.end(); ) {
75     if (ceph_argparse_double_dash(args, i)) {
76       break;
77     } else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
78       pool_name = val;
79     } else if (ceph_argparse_witharg(args, i, &latency_multiplier, err, "--latency-multiplier",
80                                      (char*)NULL)) {
81       if (!err.str().empty()) {
82         cerr << err.str() << std::endl;
83         return 1;
84       }
85     } else if (ceph_argparse_flag(args, i, "--read-only", (char*)NULL)) {
86       readonly = true;
87     } else if (ceph_argparse_witharg(args, i, &val, "--map-image", (char*)NULL)) {
88       ImageNameMap::Mapping mapping;
89       if (image_name_map.parse_mapping(val, &mapping)) {
90         image_name_map.add_mapping(mapping);
91       } else {
92         cerr << "Unable to parse mapping string: '" << val << "'" << std::endl;
93         return 1;
94       }
95     } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
96       usage(argv[0]);
97       return 0;
98     } else if (ceph_argparse_flag(args, i, "--dump-perf-counters", (char*)NULL)) {
99       dump_perf_counters = true;
100     } else if (get_remainder(*i, "-")) {
101       cerr << "Unrecognized argument: " << *i << std::endl;
102       return 1;
103     } else {
104       ++i;
105     }
106   }
107
108   common_init_finish(g_ceph_context);
109
110   string replay_file;
111   if (!args.empty()) {
112     replay_file = args[0];
113   }
114
115   if (replay_file.empty()) {
116     cerr << "No replay file specified." << std::endl;
117     return 1;
118   }
119
120   unsigned int nthreads = boost::thread::hardware_concurrency();
121   Replayer replayer(2 * nthreads + 1);
122   replayer.set_latency_multiplier(latency_multiplier);
123   replayer.set_pool_name(pool_name);
124   replayer.set_readonly(readonly);
125   replayer.set_image_name_map(image_name_map);
126   replayer.set_dump_perf_counters(dump_perf_counters);
127   replayer.run(replay_file);
128 }