]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/erasure-code/ceph_erasure_code_benchmark.cc
import 15.2.0 Octopus source
[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 try {
126 k = stoi(profile["k"]);
127 m = stoi(profile["m"]);
128 } catch (const std::logic_error& e) {
129 cout << "Invalid k and/or m: k=" << profile["k"] << ", m=" << profile["m"]
130 << " (" << e.what() << ")" << endl;
131 return -EINVAL;
132 }
133 if (k <= 0) {
134 cout << "parameter k is " << k << ". But k needs to be > 0." << endl;
135 return -EINVAL;
136 } else if ( m < 0 ) {
137 cout << "parameter m is " << m << ". But m needs to be >= 0." << endl;
138 return -EINVAL;
139 }
140
141 verbose = vm.count("verbose") > 0 ? true : false;
142
143 return 0;
144 }
145
146 int ErasureCodeBench::run() {
147 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
148 instance.disable_dlclose = true;
149
150 if (workload == "encode")
151 return encode();
152 else
153 return decode();
154 }
155
156 int ErasureCodeBench::encode()
157 {
158 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
159 ErasureCodeInterfaceRef erasure_code;
160 stringstream messages;
161 int code = instance.factory(plugin,
162 g_conf().get_val<std::string>("erasure_code_dir"),
163 profile, &erasure_code, &messages);
164 if (code) {
165 cerr << messages.str() << endl;
166 return code;
167 }
168
169 bufferlist in;
170 in.append(string(in_size, 'X'));
171 in.rebuild_aligned(ErasureCode::SIMD_ALIGN);
172 set<int> want_to_encode;
173 for (int i = 0; i < k + m; i++) {
174 want_to_encode.insert(i);
175 }
176 utime_t begin_time = ceph_clock_now();
177 for (int i = 0; i < max_iterations; i++) {
178 map<int,bufferlist> encoded;
179 code = erasure_code->encode(want_to_encode, in, &encoded);
180 if (code)
181 return code;
182 }
183 utime_t end_time = ceph_clock_now();
184 cout << (end_time - begin_time) << "\t" << (max_iterations * (in_size / 1024)) << endl;
185 return 0;
186 }
187
188 static void display_chunks(const map<int,bufferlist> &chunks,
189 unsigned int chunk_count) {
190 cout << "chunks ";
191 for (unsigned int chunk = 0; chunk < chunk_count; chunk++) {
192 if (chunks.count(chunk) == 0) {
193 cout << "(" << chunk << ")";
194 } else {
195 cout << " " << chunk << " ";
196 }
197 cout << " ";
198 }
199 cout << "(X) is an erased chunk" << endl;
200 }
201
202 int ErasureCodeBench::decode_erasures(const map<int,bufferlist> &all_chunks,
203 const map<int,bufferlist> &chunks,
204 unsigned i,
205 unsigned want_erasures,
206 ErasureCodeInterfaceRef erasure_code)
207 {
208 int code = 0;
209
210 if (want_erasures == 0) {
211 if (verbose)
212 display_chunks(chunks, erasure_code->get_chunk_count());
213 set<int> want_to_read;
214 for (unsigned int chunk = 0; chunk < erasure_code->get_chunk_count(); chunk++)
215 if (chunks.count(chunk) == 0)
216 want_to_read.insert(chunk);
217
218 map<int,bufferlist> decoded;
219 code = erasure_code->decode(want_to_read, chunks, &decoded, 0);
220 if (code)
221 return code;
222 for (set<int>::iterator chunk = want_to_read.begin();
223 chunk != want_to_read.end();
224 ++chunk) {
225 if (all_chunks.find(*chunk)->second.length() != decoded[*chunk].length()) {
226 cerr << "chunk " << *chunk << " length=" << all_chunks.find(*chunk)->second.length()
227 << " decoded with length=" << decoded[*chunk].length() << endl;
228 return -1;
229 }
230 bufferlist tmp = all_chunks.find(*chunk)->second;
231 if (!tmp.contents_equal(decoded[*chunk])) {
232 cerr << "chunk " << *chunk
233 << " content and recovered content are different" << endl;
234 return -1;
235 }
236 }
237 return 0;
238 }
239
240 for (; i < erasure_code->get_chunk_count(); i++) {
241 map<int,bufferlist> one_less = chunks;
242 one_less.erase(i);
243 code = decode_erasures(all_chunks, one_less, i + 1, want_erasures - 1, erasure_code);
244 if (code)
245 return code;
246 }
247
248 return 0;
249 }
250
251 int ErasureCodeBench::decode()
252 {
253 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
254 ErasureCodeInterfaceRef erasure_code;
255 stringstream messages;
256 int code = instance.factory(plugin,
257 g_conf().get_val<std::string>("erasure_code_dir"),
258 profile, &erasure_code, &messages);
259 if (code) {
260 cerr << messages.str() << endl;
261 return code;
262 }
263
264 bufferlist in;
265 in.append(string(in_size, 'X'));
266 in.rebuild_aligned(ErasureCode::SIMD_ALIGN);
267
268 set<int> want_to_encode;
269 for (int i = 0; i < k + m; i++) {
270 want_to_encode.insert(i);
271 }
272
273 map<int,bufferlist> encoded;
274 code = erasure_code->encode(want_to_encode, in, &encoded);
275 if (code)
276 return code;
277
278 set<int> want_to_read = want_to_encode;
279
280 if (erased.size() > 0) {
281 for (vector<int>::const_iterator i = erased.begin();
282 i != erased.end();
283 ++i)
284 encoded.erase(*i);
285 display_chunks(encoded, erasure_code->get_chunk_count());
286 }
287
288 utime_t begin_time = ceph_clock_now();
289 for (int i = 0; i < max_iterations; i++) {
290 if (exhaustive_erasures) {
291 code = decode_erasures(encoded, encoded, 0, erasures, erasure_code);
292 if (code)
293 return code;
294 } else if (erased.size() > 0) {
295 map<int,bufferlist> decoded;
296 code = erasure_code->decode(want_to_read, encoded, &decoded, 0);
297 if (code)
298 return code;
299 } else {
300 map<int,bufferlist> chunks = encoded;
301 for (int j = 0; j < erasures; j++) {
302 int erasure;
303 do {
304 erasure = rand() % ( k + m );
305 } while(chunks.count(erasure) == 0);
306 chunks.erase(erasure);
307 }
308 map<int,bufferlist> decoded;
309 code = erasure_code->decode(want_to_read, chunks, &decoded, 0);
310 if (code)
311 return code;
312 }
313 }
314 utime_t end_time = ceph_clock_now();
315 cout << (end_time - begin_time) << "\t" << (max_iterations * (in_size / 1024)) << endl;
316 return 0;
317 }
318
319 int main(int argc, char** argv) {
320 ErasureCodeBench ecbench;
321 try {
322 int err = ecbench.setup(argc, argv);
323 if (err)
324 return err;
325 return ecbench.run();
326 } catch(po::error &e) {
327 cerr << e.what() << endl;
328 return 1;
329 }
330 }
331
332 /*
333 * Local Variables:
334 * compile-command: "cd ../../../build ; make -j4 ceph_erasure_code_benchmark &&
335 * valgrind --tool=memcheck --leak-check=full \
336 * ./bin/ceph_erasure_code_benchmark \
337 * --plugin jerasure \
338 * --parameter directory=lib \
339 * --parameter technique=reed_sol_van \
340 * --parameter k=2 \
341 * --parameter m=2 \
342 * --iterations 1
343 * "
344 * End:
345 */