Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / erasure-code / ceph_erasure_code_benchmark.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 distributed storage system
5  *
6  * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
7  * Copyright (C) 2014 Red Hat <contact@redhat.com>
8  *
9  * Author: Loic Dachary <loic@dachary.org>
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Lesser General Public
13  *  License as published by the Free Software Foundation; either
14  *  version 2.1 of the License, or (at your option) any later version.
15  *
16  */
17
18 #include <boost/scoped_ptr.hpp>
19 #include <boost/lexical_cast.hpp>
20 #include <boost/program_options/option.hpp>
21 #include <boost/program_options/options_description.hpp>
22 #include <boost/program_options/variables_map.hpp>
23 #include <boost/program_options/cmdline.hpp>
24 #include <boost/program_options/parsers.hpp>
25 #include <boost/algorithm/string.hpp>
26
27 #include "global/global_context.h"
28 #include "global/global_init.h"
29 #include "common/ceph_argparse.h"
30 #include "common/config.h"
31 #include "common/Clock.h"
32 #include "include/utime.h"
33 #include "erasure-code/ErasureCodePlugin.h"
34 #include "erasure-code/ErasureCode.h"
35 #include "ceph_erasure_code_benchmark.h"
36
37 namespace po = boost::program_options;
38
39 int ErasureCodeBench::setup(int argc, char** argv) {
40
41   po::options_description desc("Allowed options");
42   desc.add_options()
43     ("help,h", "produce help message")
44     ("verbose,v", "explain what happens")
45     ("size,s", po::value<int>()->default_value(1024 * 1024),
46      "size of the buffer to be encoded")
47     ("iterations,i", po::value<int>()->default_value(1),
48      "number of encode/decode runs")
49     ("plugin,p", po::value<string>()->default_value("jerasure"),
50      "erasure code plugin name")
51     ("workload,w", po::value<string>()->default_value("encode"),
52      "run either encode or decode")
53     ("erasures,e", po::value<int>()->default_value(1),
54      "number of erasures when decoding")
55     ("erased", po::value<vector<int> >(),
56      "erased chunk (repeat if more than one chunk is erased)")
57     ("erasures-generation,E", po::value<string>()->default_value("random"),
58      "If set to 'random', pick the number of chunks to recover (as specified by "
59      " --erasures) at random. If set to 'exhaustive' try all combinations of erasures "
60      " (i.e. k=4,m=3 with one erasure will try to recover from the erasure of "
61      " the first chunk, then the second etc.)")
62     ("parameter,P", po::value<vector<string> >(),
63      "add a parameter to the erasure code profile")
64     ;
65
66   po::variables_map vm;
67   po::parsed_options parsed =
68     po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
69   po::store(
70     parsed,
71     vm);
72   po::notify(vm);
73
74   vector<const char *> ceph_options, def_args;
75   vector<string> ceph_option_strings = po::collect_unrecognized(
76     parsed.options, po::include_positional);
77   ceph_options.reserve(ceph_option_strings.size());
78   for (vector<string>::iterator i = ceph_option_strings.begin();
79        i != ceph_option_strings.end();
80        ++i) {
81     ceph_options.push_back(i->c_str());
82   }
83
84   cct = global_init(
85     &def_args, ceph_options, CEPH_ENTITY_TYPE_CLIENT,
86     CODE_ENVIRONMENT_UTILITY,
87     CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
88   common_init_finish(g_ceph_context);
89   g_ceph_context->_conf->apply_changes(NULL);
90
91   if (vm.count("help")) {
92     cout << desc << std::endl;
93     return 1;
94   }
95
96   if (vm.count("parameter")) {
97     const vector<string> &p = vm["parameter"].as< vector<string> >();
98     for (vector<string>::const_iterator i = p.begin();
99          i != p.end();
100          ++i) {
101       std::vector<std::string> strs;
102       boost::split(strs, *i, boost::is_any_of("="));
103       if (strs.size() != 2) {
104         cerr << "--parameter " << *i << " ignored because it does not contain exactly one =" << endl;
105       } else {
106         profile[strs[0]] = strs[1];
107       }
108     }
109   }
110
111   in_size = vm["size"].as<int>();
112   max_iterations = vm["iterations"].as<int>();
113   plugin = vm["plugin"].as<string>();
114   workload = vm["workload"].as<string>();
115   erasures = vm["erasures"].as<int>();
116   if (vm.count("erasures-generation") > 0 &&
117       vm["erasures-generation"].as<string>() == "exhaustive")
118     exhaustive_erasures = true;
119   else
120     exhaustive_erasures = false;
121   if (vm.count("erased") > 0)
122     erased = vm["erased"].as<vector<int> >();
123
124   k = atoi(profile["k"].c_str());
125   m = atoi(profile["m"].c_str());
126   
127   if (k <= 0) {
128     cout << "parameter k is " << k << ". But k needs to be > 0." << endl;
129     return -EINVAL;
130   } else if ( m < 0 ) {
131     cout << "parameter m is " << m << ". But m needs to be >= 0." << endl;
132     return -EINVAL;
133   } 
134
135   verbose = vm.count("verbose") > 0 ? true : false;
136
137   return 0;
138 }
139
140 int ErasureCodeBench::run() {
141   ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
142   instance.disable_dlclose = true;
143
144   if (workload == "encode")
145     return encode();
146   else
147     return decode();
148 }
149
150 int ErasureCodeBench::encode()
151 {
152   ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
153   ErasureCodeInterfaceRef erasure_code;
154   stringstream messages;
155   int code = instance.factory(plugin,
156                               g_conf->get_val<std::string>("erasure_code_dir"),
157                               profile, &erasure_code, &messages);
158   if (code) {
159     cerr << messages.str() << endl;
160     return code;
161   }
162
163   if (erasure_code->get_data_chunk_count() != (unsigned int)k ||
164       (erasure_code->get_chunk_count() - erasure_code->get_data_chunk_count()
165        != (unsigned int)m)) {
166     cout << "parameter k is " << k << "/m is " << m << ". But data chunk count is "
167       << erasure_code->get_data_chunk_count() <<"/parity chunk count is "
168       << erasure_code->get_chunk_count() - erasure_code->get_data_chunk_count() << endl;
169     return -EINVAL;
170   }
171
172   bufferlist in;
173   in.append(string(in_size, 'X'));
174   in.rebuild_aligned(ErasureCode::SIMD_ALIGN);
175   set<int> want_to_encode;
176   for (int i = 0; i < k + m; i++) {
177     want_to_encode.insert(i);
178   }
179   utime_t begin_time = ceph_clock_now();
180   for (int i = 0; i < max_iterations; i++) {
181     map<int,bufferlist> encoded;
182     code = erasure_code->encode(want_to_encode, in, &encoded);
183     if (code)
184       return code;
185   }
186   utime_t end_time = ceph_clock_now();
187   cout << (end_time - begin_time) << "\t" << (max_iterations * (in_size / 1024)) << endl;
188   return 0;
189 }
190
191 static void display_chunks(const map<int,bufferlist> &chunks,
192                            unsigned int chunk_count) {
193   cout << "chunks ";
194   for (unsigned int chunk = 0; chunk < chunk_count; chunk++) {
195     if (chunks.count(chunk) == 0) {
196       cout << "(" << chunk << ")";
197     } else {
198       cout << " " << chunk << " ";
199     }
200     cout << " ";
201   }
202   cout << "(X) is an erased chunk" << endl;
203 }
204
205 int ErasureCodeBench::decode_erasures(const map<int,bufferlist> &all_chunks,
206                                       const map<int,bufferlist> &chunks,
207                                       unsigned i,
208                                       unsigned want_erasures,
209                                       ErasureCodeInterfaceRef erasure_code)
210 {
211   int code = 0;
212
213   if (want_erasures == 0) {
214     if (verbose)
215       display_chunks(chunks, erasure_code->get_chunk_count());
216     set<int> want_to_read;
217     for (unsigned int chunk = 0; chunk < erasure_code->get_chunk_count(); chunk++)
218       if (chunks.count(chunk) == 0)
219         want_to_read.insert(chunk);
220
221     map<int,bufferlist> decoded;
222     code = erasure_code->decode(want_to_read, chunks, &decoded);
223     if (code)
224       return code;
225     for (set<int>::iterator chunk = want_to_read.begin();
226          chunk != want_to_read.end();
227          ++chunk) {
228       if (all_chunks.find(*chunk)->second.length() != decoded[*chunk].length()) {
229         cerr << "chunk " << *chunk << " length=" << all_chunks.find(*chunk)->second.length()
230              << " decoded with length=" << decoded[*chunk].length() << endl;
231         return -1;
232       }
233       bufferlist tmp = all_chunks.find(*chunk)->second;
234       if (!tmp.contents_equal(decoded[*chunk])) {
235         cerr << "chunk " << *chunk
236              << " content and recovered content are different" << endl;
237         return -1;
238       }
239     }
240     return 0;
241   }
242
243   for (; i < erasure_code->get_chunk_count(); i++) {
244     map<int,bufferlist> one_less = chunks;
245     one_less.erase(i);
246     code = decode_erasures(all_chunks, one_less, i + 1, want_erasures - 1, erasure_code);
247     if (code)
248       return code;
249   }
250
251   return 0;
252 }
253
254 int ErasureCodeBench::decode()
255 {
256   ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
257   ErasureCodeInterfaceRef erasure_code;
258   stringstream messages;
259   int code = instance.factory(plugin,
260                               g_conf->get_val<std::string>("erasure_code_dir"),
261                               profile, &erasure_code, &messages);
262   if (code) {
263     cerr << messages.str() << endl;
264     return code;
265   }
266   if (erasure_code->get_data_chunk_count() != (unsigned int)k ||
267       (erasure_code->get_chunk_count() - erasure_code->get_data_chunk_count()
268        != (unsigned int)m)) {
269     cout << "parameter k is " << k << "/m is " << m << ". But data chunk count is "
270       << erasure_code->get_data_chunk_count() <<"/parity chunk count is "
271       << erasure_code->get_chunk_count() - erasure_code->get_data_chunk_count() << endl;
272     return -EINVAL;
273   }
274   bufferlist in;
275   in.append(string(in_size, 'X'));
276   in.rebuild_aligned(ErasureCode::SIMD_ALIGN);
277
278   set<int> want_to_encode;
279   for (int i = 0; i < k + m; i++) {
280     want_to_encode.insert(i);
281   }
282
283   map<int,bufferlist> encoded;
284   code = erasure_code->encode(want_to_encode, in, &encoded);
285   if (code)
286     return code;
287
288   set<int> want_to_read = want_to_encode;
289
290   if (erased.size() > 0) {
291     for (vector<int>::const_iterator i = erased.begin();
292          i != erased.end();
293          ++i)
294       encoded.erase(*i);
295     display_chunks(encoded, erasure_code->get_chunk_count());
296   }
297
298   utime_t begin_time = ceph_clock_now();
299   for (int i = 0; i < max_iterations; i++) {
300     if (exhaustive_erasures) {
301       code = decode_erasures(encoded, encoded, 0, erasures, erasure_code);
302       if (code)
303         return code;
304     } else if (erased.size() > 0) {
305       map<int,bufferlist> decoded;
306       code = erasure_code->decode(want_to_read, encoded, &decoded);
307       if (code)
308         return code;
309     } else {
310       map<int,bufferlist> chunks = encoded;
311       for (int j = 0; j < erasures; j++) {
312         int erasure;
313         do {
314           erasure = rand() % ( k + m );
315         } while(chunks.count(erasure) == 0);
316         chunks.erase(erasure);
317       }
318       map<int,bufferlist> decoded;
319       code = erasure_code->decode(want_to_read, chunks, &decoded);
320       if (code)
321         return code;
322     }
323   }
324   utime_t end_time = ceph_clock_now();
325   cout << (end_time - begin_time) << "\t" << (max_iterations * (in_size / 1024)) << endl;
326   return 0;
327 }
328
329 int main(int argc, char** argv) {
330   ErasureCodeBench ecbench;
331   try {
332     int err = ecbench.setup(argc, argv);
333     if (err)
334       return err;
335     return ecbench.run();
336   } catch(po::error &e) {
337     cerr << e.what() << endl; 
338     return 1;
339   }
340 }
341
342 /*
343  * Local Variables:
344  * compile-command: "cd ../.. ; make -j4 ceph_erasure_code_benchmark &&
345  *   valgrind --tool=memcheck --leak-check=full \
346  *      ./ceph_erasure_code_benchmark \
347  *      --plugin jerasure \
348  *      --parameter directory=.libs \
349  *      --parameter technique=reed_sol_van \
350  *      --parameter k=2 \
351  *      --parameter m=2 \
352  *      --iterations 1
353  * "
354  * End:
355  */