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