1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph distributed storage system
6 * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
7 * Copyright (C) 2014 Red Hat <contact@redhat.com>
9 * Author: Loic Dachary <loic@dachary.org>
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.
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>
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"
37 namespace po = boost::program_options;
39 int ErasureCodeBench::setup(int argc, char** argv) {
41 po::options_description desc("Allowed 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")
67 po::parsed_options parsed =
68 po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
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();
81 ceph_options.push_back(i->c_str());
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);
91 if (vm.count("help")) {
92 cout << desc << std::endl;
96 if (vm.count("parameter")) {
97 const vector<string> &p = vm["parameter"].as< vector<string> >();
98 for (vector<string>::const_iterator i = p.begin();
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;
106 profile[strs[0]] = strs[1];
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;
120 exhaustive_erasures = false;
121 if (vm.count("erased") > 0)
122 erased = vm["erased"].as<vector<int> >();
124 k = atoi(profile["k"].c_str());
125 m = atoi(profile["m"].c_str());
128 cout << "parameter k is " << k << ". But k needs to be > 0." << endl;
130 } else if ( m < 0 ) {
131 cout << "parameter m is " << m << ". But m needs to be >= 0." << endl;
135 verbose = vm.count("verbose") > 0 ? true : false;
140 int ErasureCodeBench::run() {
141 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
142 instance.disable_dlclose = true;
144 if (workload == "encode")
150 int ErasureCodeBench::encode()
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);
159 cerr << messages.str() << endl;
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;
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);
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);
186 utime_t end_time = ceph_clock_now();
187 cout << (end_time - begin_time) << "\t" << (max_iterations * (in_size / 1024)) << endl;
191 static void display_chunks(const map<int,bufferlist> &chunks,
192 unsigned int chunk_count) {
194 for (unsigned int chunk = 0; chunk < chunk_count; chunk++) {
195 if (chunks.count(chunk) == 0) {
196 cout << "(" << chunk << ")";
198 cout << " " << chunk << " ";
202 cout << "(X) is an erased chunk" << endl;
205 int ErasureCodeBench::decode_erasures(const map<int,bufferlist> &all_chunks,
206 const map<int,bufferlist> &chunks,
208 unsigned want_erasures,
209 ErasureCodeInterfaceRef erasure_code)
213 if (want_erasures == 0) {
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);
221 map<int,bufferlist> decoded;
222 code = erasure_code->decode(want_to_read, chunks, &decoded);
225 for (set<int>::iterator chunk = want_to_read.begin();
226 chunk != want_to_read.end();
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;
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;
243 for (; i < erasure_code->get_chunk_count(); i++) {
244 map<int,bufferlist> one_less = chunks;
246 code = decode_erasures(all_chunks, one_less, i + 1, want_erasures - 1, erasure_code);
254 int ErasureCodeBench::decode()
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);
263 cerr << messages.str() << endl;
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;
275 in.append(string(in_size, 'X'));
276 in.rebuild_aligned(ErasureCode::SIMD_ALIGN);
278 set<int> want_to_encode;
279 for (int i = 0; i < k + m; i++) {
280 want_to_encode.insert(i);
283 map<int,bufferlist> encoded;
284 code = erasure_code->encode(want_to_encode, in, &encoded);
288 set<int> want_to_read = want_to_encode;
290 if (erased.size() > 0) {
291 for (vector<int>::const_iterator i = erased.begin();
295 display_chunks(encoded, erasure_code->get_chunk_count());
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);
304 } else if (erased.size() > 0) {
305 map<int,bufferlist> decoded;
306 code = erasure_code->decode(want_to_read, encoded, &decoded);
310 map<int,bufferlist> chunks = encoded;
311 for (int j = 0; j < erasures; j++) {
314 erasure = rand() % ( k + m );
315 } while(chunks.count(erasure) == 0);
316 chunks.erase(erasure);
318 map<int,bufferlist> decoded;
319 code = erasure_code->decode(want_to_read, chunks, &decoded);
324 utime_t end_time = ceph_clock_now();
325 cout << (end_time - begin_time) << "\t" << (max_iterations * (in_size / 1024)) << endl;
329 int main(int argc, char** argv) {
330 ErasureCodeBench ecbench;
332 int err = ecbench.setup(argc, argv);
335 return ecbench.run();
336 } catch(po::error &e) {
337 cerr << e.what() << endl;
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 \