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/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"
38 namespace po
= boost::program_options
;
40 int ErasureCodeBench::setup(int argc
, char** argv
) {
42 po::options_description
desc("Allowed 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")
68 po::parsed_options parsed
=
69 po::command_line_parser(argc
, argv
).options(desc
).allow_unregistered().run();
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();
82 ceph_options
.push_back(i
->c_str());
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);
92 if (vm
.count("help")) {
93 cout
<< desc
<< std::endl
;
97 if (vm
.count("parameter")) {
98 const vector
<string
> &p
= vm
["parameter"].as
< vector
<string
> >();
99 for (vector
<string
>::const_iterator i
= p
.begin();
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
;
107 profile
[strs
[0]] = strs
[1];
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;
121 exhaustive_erasures
= false;
122 if (vm
.count("erased") > 0)
123 erased
= vm
["erased"].as
<vector
<int> >();
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
;
134 cout
<< "parameter k is " << k
<< ". But k needs to be > 0." << endl
;
136 } else if ( m
< 0 ) {
137 cout
<< "parameter m is " << m
<< ". But m needs to be >= 0." << endl
;
141 verbose
= vm
.count("verbose") > 0 ? true : false;
146 int ErasureCodeBench::run() {
147 ErasureCodePluginRegistry
&instance
= ErasureCodePluginRegistry::instance();
148 instance
.disable_dlclose
= true;
150 if (workload
== "encode")
156 int ErasureCodeBench::encode()
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
);
165 cerr
<< messages
.str() << endl
;
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
);
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
);
183 utime_t end_time
= ceph_clock_now();
184 cout
<< (end_time
- begin_time
) << "\t" << (max_iterations
* (in_size
/ 1024)) << endl
;
188 static void display_chunks(const map
<int,bufferlist
> &chunks
,
189 unsigned int chunk_count
) {
191 for (unsigned int chunk
= 0; chunk
< chunk_count
; chunk
++) {
192 if (chunks
.count(chunk
) == 0) {
193 cout
<< "(" << chunk
<< ")";
195 cout
<< " " << chunk
<< " ";
199 cout
<< "(X) is an erased chunk" << endl
;
202 int ErasureCodeBench::decode_erasures(const map
<int,bufferlist
> &all_chunks
,
203 const map
<int,bufferlist
> &chunks
,
205 unsigned want_erasures
,
206 ErasureCodeInterfaceRef erasure_code
)
210 if (want_erasures
== 0) {
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
);
218 map
<int,bufferlist
> decoded
;
219 code
= erasure_code
->decode(want_to_read
, chunks
, &decoded
, 0);
222 for (set
<int>::iterator chunk
= want_to_read
.begin();
223 chunk
!= want_to_read
.end();
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
;
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
;
240 for (; i
< erasure_code
->get_chunk_count(); i
++) {
241 map
<int,bufferlist
> one_less
= chunks
;
243 code
= decode_erasures(all_chunks
, one_less
, i
+ 1, want_erasures
- 1, erasure_code
);
251 int ErasureCodeBench::decode()
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
);
260 cerr
<< messages
.str() << endl
;
265 in
.append(string(in_size
, 'X'));
266 in
.rebuild_aligned(ErasureCode::SIMD_ALIGN
);
268 set
<int> want_to_encode
;
269 for (int i
= 0; i
< k
+ m
; i
++) {
270 want_to_encode
.insert(i
);
273 map
<int,bufferlist
> encoded
;
274 code
= erasure_code
->encode(want_to_encode
, in
, &encoded
);
278 set
<int> want_to_read
= want_to_encode
;
280 if (erased
.size() > 0) {
281 for (vector
<int>::const_iterator i
= erased
.begin();
285 display_chunks(encoded
, erasure_code
->get_chunk_count());
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
);
294 } else if (erased
.size() > 0) {
295 map
<int,bufferlist
> decoded
;
296 code
= erasure_code
->decode(want_to_read
, encoded
, &decoded
, 0);
300 map
<int,bufferlist
> chunks
= encoded
;
301 for (int j
= 0; j
< erasures
; j
++) {
304 erasure
= rand() % ( k
+ m
);
305 } while(chunks
.count(erasure
) == 0);
306 chunks
.erase(erasure
);
308 map
<int,bufferlist
> decoded
;
309 code
= erasure_code
->decode(want_to_read
, chunks
, &decoded
, 0);
314 utime_t end_time
= ceph_clock_now();
315 cout
<< (end_time
- begin_time
) << "\t" << (max_iterations
* (in_size
/ 1024)) << endl
;
319 int main(int argc
, char** argv
) {
320 ErasureCodeBench ecbench
;
322 int err
= ecbench
.setup(argc
, argv
);
325 return ecbench
.run();
326 } catch(po::error
&e
) {
327 cerr
<< e
.what() << endl
;
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 \