Botan  2.6.0
Crypto and TLS for C++11
xmss_tools.cpp
Go to the documentation of this file.
1 /*
2  * XMSS Tools
3  * (C) 2017 Matthias Gierlings
4  *
5  * Botan is released under the Simplified BSD License (see license.txt)
6  **/
7 
8 #include <botan/xmss_tools.h>
9 
10 namespace Botan {
11 
12 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
13 
15  {
16  static const size_t threads { bench_threads() };
17  return threads;
18  }
19 
20 size_t XMSS_Tools::bench_threads()
21  {
22  if(std::thread::hardware_concurrency() <= 1)
23  {
24  return 1;
25  }
26  const size_t BENCH_ITERATIONS = 1000;
27  std::vector<std::thread> threads;
28  threads.reserve(std::thread::hardware_concurrency());
29  std::vector<std::chrono::nanoseconds> durations;
30 
31  std::vector<size_t> concurrency { std::thread::hardware_concurrency(),
32  std::thread::hardware_concurrency() / 2 };
33 
34  for(const auto& cc : concurrency)
35  {
36  std::vector<XMSS_Hash> hash(std::thread::hardware_concurrency(),
37  XMSS_Hash("SHA-256"));
38 
39  const std::vector<uint8_t> buffer(hash[0].output_length());
40  std::vector<secure_vector<uint8_t>> data(
41  std::thread::hardware_concurrency(),
42  secure_vector<uint8_t>(hash[0].output_length()));
43  auto start = std::chrono::high_resolution_clock::now();
44  for(size_t i = 0; i < cc; ++i)
45  {
46  auto& hs = hash[i];
47  auto& d = data[i];
48 
49  const size_t n_iters = BENCH_ITERATIONS * (std::thread::hardware_concurrency() / cc);
50  threads.emplace_back(std::thread([n_iters, &hs, &d]()
51  {
52  for(size_t n = 0; n < n_iters; n++)
53  {
54  hs.h(d, d, d);
55  }
56  }
57  ));
58  }
59  durations.emplace_back(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - start));
60  for(auto& t : threads)
61  {
62  t.join();
63  }
64  threads.clear();
65  }
66 
67  if(durations[0].count() < durations[1].count())
68  {
69  return concurrency[0];
70  }
71  else
72  {
73  return concurrency[1];
74  }
75  }
76 
77 #endif
78 
79 }
80 
static size_t max_threads()
Definition: alg_id.cpp:13
MechanismType hash