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> >();
125 k
= stoi(profile
["k"]);
126 m
= stoi(profile
["m"]);
129 cout
<< "parameter k is " << k
<< ". But k needs to be > 0." << endl
;
131 } else if ( m
< 0 ) {
132 cout
<< "parameter m is " << m
<< ". But m needs to be >= 0." << endl
;
136 verbose
= vm
.count("verbose") > 0 ? true : false;
141 int ErasureCodeBench::run() {
142 ErasureCodePluginRegistry
&instance
= ErasureCodePluginRegistry::instance();
143 instance
.disable_dlclose
= true;
145 if (workload
== "encode")
151 int ErasureCodeBench::encode()
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
);
160 cerr
<< messages
.str() << endl
;
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
);
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
);
178 utime_t end_time
= ceph_clock_now();
179 cout
<< (end_time
- begin_time
) << "\t" << (max_iterations
* (in_size
/ 1024)) << endl
;
183 static void display_chunks(const map
<int,bufferlist
> &chunks
,
184 unsigned int chunk_count
) {
186 for (unsigned int chunk
= 0; chunk
< chunk_count
; chunk
++) {
187 if (chunks
.count(chunk
) == 0) {
188 cout
<< "(" << chunk
<< ")";
190 cout
<< " " << chunk
<< " ";
194 cout
<< "(X) is an erased chunk" << endl
;
197 int ErasureCodeBench::decode_erasures(const map
<int,bufferlist
> &all_chunks
,
198 const map
<int,bufferlist
> &chunks
,
200 unsigned want_erasures
,
201 ErasureCodeInterfaceRef erasure_code
)
205 if (want_erasures
== 0) {
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
);
213 map
<int,bufferlist
> decoded
;
214 code
= erasure_code
->decode(want_to_read
, chunks
, &decoded
, 0);
217 for (set
<int>::iterator chunk
= want_to_read
.begin();
218 chunk
!= want_to_read
.end();
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
;
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
;
235 for (; i
< erasure_code
->get_chunk_count(); i
++) {
236 map
<int,bufferlist
> one_less
= chunks
;
238 code
= decode_erasures(all_chunks
, one_less
, i
+ 1, want_erasures
- 1, erasure_code
);
246 int ErasureCodeBench::decode()
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
);
255 cerr
<< messages
.str() << endl
;
260 in
.append(string(in_size
, 'X'));
261 in
.rebuild_aligned(ErasureCode::SIMD_ALIGN
);
263 set
<int> want_to_encode
;
264 for (int i
= 0; i
< k
+ m
; i
++) {
265 want_to_encode
.insert(i
);
268 map
<int,bufferlist
> encoded
;
269 code
= erasure_code
->encode(want_to_encode
, in
, &encoded
);
273 set
<int> want_to_read
= want_to_encode
;
275 if (erased
.size() > 0) {
276 for (vector
<int>::const_iterator i
= erased
.begin();
280 display_chunks(encoded
, erasure_code
->get_chunk_count());
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
);
289 } else if (erased
.size() > 0) {
290 map
<int,bufferlist
> decoded
;
291 code
= erasure_code
->decode(want_to_read
, encoded
, &decoded
, 0);
295 map
<int,bufferlist
> chunks
= encoded
;
296 for (int j
= 0; j
< erasures
; j
++) {
299 erasure
= rand() % ( k
+ m
);
300 } while(chunks
.count(erasure
) == 0);
301 chunks
.erase(erasure
);
303 map
<int,bufferlist
> decoded
;
304 code
= erasure_code
->decode(want_to_read
, chunks
, &decoded
, 0);
309 utime_t end_time
= ceph_clock_now();
310 cout
<< (end_time
- begin_time
) << "\t" << (max_iterations
* (in_size
/ 1024)) << endl
;
314 int main(int argc
, char** argv
) {
315 ErasureCodeBench ecbench
;
317 int err
= ecbench
.setup(argc
, argv
);
320 return ecbench
.run();
321 } catch(po::error
&e
) {
322 cerr
<< e
.what() << endl
;
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 \