benchmark.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <fstream>
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <exception>
  29. #include <string>
  30. #include <vector>
  31. #include <thread>
  32. #include <atomic>
  33. #include "stopwatch.hpp"
  34. #include "utility.hpp"
  35. #include "../randomx.h"
  36. #include "../dataset.hpp"
  37. #include "../blake2/endian.h"
  38. #include "../common.hpp"
  39. #include "../jit_compiler.hpp"
  40. #ifdef _WIN32
  41. #include <windows.h>
  42. #include <versionhelpers.h>
  43. #endif
  44. #include "affinity.hpp"
  45. const uint8_t blockTemplate_[] = {
  46. 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
  47. 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
  48. 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
  49. 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
  50. };
  51. class AtomicHash {
  52. public:
  53. AtomicHash() {
  54. for (int i = 0; i < 4; ++i)
  55. hash[i].store(0);
  56. }
  57. void xorWith(uint64_t update[4]) {
  58. for (int i = 0; i < 4; ++i)
  59. hash[i].fetch_xor(update[i]);
  60. }
  61. void print(std::ostream& os) {
  62. for (int i = 0; i < 4; ++i)
  63. print(hash[i], os);
  64. os << std::endl;
  65. }
  66. private:
  67. static void print(std::atomic<uint64_t>& hash, std::ostream& os) {
  68. auto h = hash.load();
  69. outputHex(std::cout, (char*)&h, sizeof(h));
  70. }
  71. std::atomic<uint64_t> hash[4];
  72. };
  73. void printUsage(const char* executable) {
  74. std::cout << "Usage: " << executable << " [OPTIONS]" << std::endl;
  75. std::cout << "Supported options:" << std::endl;
  76. std::cout << " --help shows this message" << std::endl;
  77. std::cout << " --mine mining mode: 2080 MiB" << std::endl;
  78. std::cout << " --verify verification mode: 256 MiB" << std::endl;
  79. std::cout << " --jit JIT compiled mode (default: interpreter)" << std::endl;
  80. std::cout << " --secure W^X policy for JIT pages (default: off)" << std::endl;
  81. std::cout << " --largePages use large pages (default: small pages)" << std::endl;
  82. std::cout << " --softAes use software AES (default: hardware AES)" << std::endl;
  83. std::cout << " --threads T use T threads (default: 1)" << std::endl;
  84. std::cout << " --affinity A thread affinity bitmask (default: 0)" << std::endl;
  85. std::cout << " --init Q initialize dataset with Q threads (default: 1)" << std::endl;
  86. std::cout << " --nonces N run N nonces (default: 1000)" << std::endl;
  87. std::cout << " --seed S seed for cache initialization (default: 0)" << std::endl;
  88. std::cout << " --ssse3 use optimized Argon2 for SSSE3 CPUs" << std::endl;
  89. std::cout << " --avx2 use optimized Argon2 for AVX2 CPUs" << std::endl;
  90. std::cout << " --auto select the best options for the current CPU" << std::endl;
  91. std::cout << " --noBatch calculate hashes one by one (default: batch)" << std::endl;
  92. }
  93. struct MemoryException : public std::exception {
  94. };
  95. struct CacheAllocException : public MemoryException {
  96. const char * what() const throw () {
  97. return "Cache allocation failed";
  98. }
  99. };
  100. struct DatasetAllocException : public MemoryException {
  101. const char * what() const throw () {
  102. return "Dataset allocation failed";
  103. }
  104. };
  105. using MineFunc = void(randomx_vm * vm, std::atomic<uint32_t> & atomicNonce, AtomicHash & result, uint32_t noncesCount, int thread, int cpuid);
  106. template<bool batch>
  107. void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result, uint32_t noncesCount, int thread, int cpuid = -1) {
  108. if (cpuid >= 0) {
  109. int rc = set_thread_affinity(cpuid);
  110. if (rc) {
  111. std::cerr << "Failed to set thread affinity for thread " << thread << " (error=" << rc << ")" << std::endl;
  112. }
  113. }
  114. uint64_t hash[RANDOMX_HASH_SIZE / sizeof(uint64_t)];
  115. uint8_t blockTemplate[sizeof(blockTemplate_)];
  116. memcpy(blockTemplate, blockTemplate_, sizeof(blockTemplate));
  117. void* noncePtr = blockTemplate + 39;
  118. auto nonce = atomicNonce.fetch_add(1);
  119. if (batch) {
  120. store32(noncePtr, nonce);
  121. randomx_calculate_hash_first(vm, blockTemplate, sizeof(blockTemplate));
  122. }
  123. while (nonce < noncesCount) {
  124. if (batch) {
  125. nonce = atomicNonce.fetch_add(1);
  126. }
  127. store32(noncePtr, nonce);
  128. (batch ? randomx_calculate_hash_next : randomx_calculate_hash)(vm, blockTemplate, sizeof(blockTemplate), &hash);
  129. result.xorWith(hash);
  130. if (!batch) {
  131. nonce = atomicNonce.fetch_add(1);
  132. }
  133. }
  134. }
  135. int main(int argc, char** argv) {
  136. bool softAes, miningMode, verificationMode, help, largePages, jit, secure;
  137. bool ssse3, avx2, autoFlags, noBatch;
  138. int noncesCount, threadCount, initThreadCount;
  139. uint64_t threadAffinity;
  140. int32_t seedValue;
  141. char seed[4];
  142. readOption("--softAes", argc, argv, softAes);
  143. readOption("--mine", argc, argv, miningMode);
  144. readOption("--verify", argc, argv, verificationMode);
  145. readIntOption("--threads", argc, argv, threadCount, 1);
  146. readUInt64Option("--affinity", argc, argv, threadAffinity, 0);
  147. readIntOption("--nonces", argc, argv, noncesCount, 1000);
  148. readIntOption("--init", argc, argv, initThreadCount, 1);
  149. readIntOption("--seed", argc, argv, seedValue, 0);
  150. readOption("--largePages", argc, argv, largePages);
  151. if (!largePages) {
  152. readOption("--largepages", argc, argv, largePages);
  153. }
  154. readOption("--jit", argc, argv, jit);
  155. readOption("--help", argc, argv, help);
  156. readOption("--secure", argc, argv, secure);
  157. readOption("--ssse3", argc, argv, ssse3);
  158. readOption("--avx2", argc, argv, avx2);
  159. readOption("--auto", argc, argv, autoFlags);
  160. readOption("--noBatch", argc, argv, noBatch);
  161. store32(&seed, seedValue);
  162. std::cout << "RandomX benchmark v1.1.11" << std::endl;
  163. if (help) {
  164. printUsage(argv[0]);
  165. return 0;
  166. }
  167. if (!miningMode && !verificationMode) {
  168. std::cout << "Please select either the fast mode (--mine) or the slow mode (--verify)" << std::endl;
  169. std::cout << "Run '" << argv[0] << " --help' to see all supported options" << std::endl;
  170. return 0;
  171. }
  172. std::atomic<uint32_t> atomicNonce(0);
  173. AtomicHash result;
  174. std::vector<randomx_vm*> vms;
  175. std::vector<std::thread> threads;
  176. randomx_dataset* dataset;
  177. randomx_cache* cache;
  178. randomx_flags flags;
  179. if (autoFlags) {
  180. initThreadCount = std::thread::hardware_concurrency();
  181. flags = randomx_get_flags();
  182. }
  183. else {
  184. flags = RANDOMX_FLAG_DEFAULT;
  185. if (ssse3) {
  186. flags |= RANDOMX_FLAG_ARGON2_SSSE3;
  187. }
  188. if (avx2) {
  189. flags |= RANDOMX_FLAG_ARGON2_AVX2;
  190. }
  191. if (!softAes) {
  192. flags |= RANDOMX_FLAG_HARD_AES;
  193. }
  194. if (jit) {
  195. flags |= RANDOMX_FLAG_JIT;
  196. #ifdef RANDOMX_FORCE_SECURE
  197. flags |= RANDOMX_FLAG_SECURE;
  198. #endif
  199. }
  200. }
  201. if (largePages) {
  202. flags |= RANDOMX_FLAG_LARGE_PAGES;
  203. }
  204. if (miningMode) {
  205. flags |= RANDOMX_FLAG_FULL_MEM;
  206. }
  207. #ifndef RANDOMX_FORCE_SECURE
  208. if (secure) {
  209. flags |= RANDOMX_FLAG_SECURE;
  210. }
  211. #endif
  212. if (flags & RANDOMX_FLAG_ARGON2_AVX2) {
  213. std::cout << " - Argon2 implementation: AVX2" << std::endl;
  214. }
  215. else if (flags & RANDOMX_FLAG_ARGON2_SSSE3) {
  216. std::cout << " - Argon2 implementation: SSSE3" << std::endl;
  217. }
  218. else {
  219. std::cout << " - Argon2 implementation: reference" << std::endl;
  220. }
  221. if (flags & RANDOMX_FLAG_FULL_MEM) {
  222. std::cout << " - full memory mode (2080 MiB)" << std::endl;
  223. }
  224. else {
  225. std::cout << " - light memory mode (256 MiB)" << std::endl;
  226. }
  227. if (flags & RANDOMX_FLAG_JIT) {
  228. std::cout << " - JIT compiled mode ";
  229. if (flags & RANDOMX_FLAG_SECURE) {
  230. std::cout << "(secure)";
  231. }
  232. std::cout << std::endl;
  233. }
  234. else {
  235. std::cout << " - interpreted mode" << std::endl;
  236. }
  237. if (flags & RANDOMX_FLAG_HARD_AES) {
  238. std::cout << " - hardware AES mode" << std::endl;
  239. }
  240. else {
  241. std::cout << " - software AES mode" << std::endl;
  242. }
  243. if (flags & RANDOMX_FLAG_LARGE_PAGES) {
  244. std::cout << " - large pages mode" << std::endl;
  245. }
  246. else {
  247. std::cout << " - small pages mode" << std::endl;
  248. }
  249. if (threadAffinity) {
  250. std::cout << " - thread affinity (" << mask_to_string(threadAffinity) << ")" << std::endl;
  251. }
  252. MineFunc* func;
  253. if (noBatch) {
  254. func = &mine<false>;
  255. }
  256. else {
  257. func = &mine<true>;
  258. std::cout << " - batch mode" << std::endl;
  259. }
  260. std::cout << "Initializing";
  261. if (miningMode)
  262. std::cout << " (" << initThreadCount << " thread" << (initThreadCount > 1 ? "s)" : ")");
  263. std::cout << " ..." << std::endl;
  264. try {
  265. if (nullptr == randomx::selectArgonImpl(flags)) {
  266. throw std::runtime_error("Unsupported Argon2 implementation");
  267. }
  268. if ((flags & RANDOMX_FLAG_JIT) && !RANDOMX_HAVE_COMPILER) {
  269. throw std::runtime_error("JIT compilation is not supported on this platform. Try without --jit");
  270. }
  271. if (!(flags & RANDOMX_FLAG_JIT) && RANDOMX_HAVE_COMPILER) {
  272. std::cout << "WARNING: You are using the interpreter mode. Use --jit for optimal performance." << std::endl;
  273. }
  274. Stopwatch sw(true);
  275. cache = randomx_alloc_cache(flags);
  276. if (cache == nullptr) {
  277. throw CacheAllocException();
  278. }
  279. randomx_init_cache(cache, &seed, sizeof(seed));
  280. if (miningMode) {
  281. dataset = randomx_alloc_dataset(flags);
  282. if (dataset == nullptr) {
  283. throw DatasetAllocException();
  284. }
  285. uint32_t datasetItemCount = randomx_dataset_item_count();
  286. if (initThreadCount > 1) {
  287. auto perThread = datasetItemCount / initThreadCount;
  288. auto remainder = datasetItemCount % initThreadCount;
  289. uint32_t startItem = 0;
  290. for (int i = 0; i < initThreadCount; ++i) {
  291. auto count = perThread + (i == initThreadCount - 1 ? remainder : 0);
  292. threads.push_back(std::thread(&randomx_init_dataset, dataset, cache, startItem, count));
  293. startItem += count;
  294. }
  295. for (unsigned i = 0; i < threads.size(); ++i) {
  296. threads[i].join();
  297. }
  298. }
  299. else {
  300. randomx_init_dataset(dataset, cache, 0, datasetItemCount);
  301. }
  302. randomx_release_cache(cache);
  303. cache = nullptr;
  304. threads.clear();
  305. }
  306. std::cout << "Memory initialized in " << sw.getElapsed() << " s" << std::endl;
  307. std::cout << "Initializing " << threadCount << " virtual machine(s) ..." << std::endl;
  308. for (int i = 0; i < threadCount; ++i) {
  309. randomx_vm *vm = randomx_create_vm(flags, cache, dataset);
  310. if (vm == nullptr) {
  311. if ((flags & RANDOMX_FLAG_HARD_AES)) {
  312. throw std::runtime_error("Cannot create VM with the selected options. Try using --softAes");
  313. }
  314. if (largePages) {
  315. throw std::runtime_error("Cannot create VM with the selected options. Try without --largePages");
  316. }
  317. throw std::runtime_error("Cannot create VM");
  318. }
  319. vms.push_back(vm);
  320. }
  321. std::cout << "Running benchmark (" << noncesCount << " nonces) ..." << std::endl;
  322. sw.restart();
  323. if (threadCount > 1) {
  324. for (unsigned i = 0; i < vms.size(); ++i) {
  325. int cpuid = -1;
  326. if (threadAffinity)
  327. cpuid = cpuid_from_mask(threadAffinity, i);
  328. threads.push_back(std::thread(func, vms[i], std::ref(atomicNonce), std::ref(result), noncesCount, i, cpuid));
  329. }
  330. for (unsigned i = 0; i < threads.size(); ++i) {
  331. threads[i].join();
  332. }
  333. }
  334. else {
  335. func(vms[0], std::ref(atomicNonce), std::ref(result), noncesCount, 0, -1);
  336. }
  337. double elapsed = sw.getElapsed();
  338. for (unsigned i = 0; i < vms.size(); ++i)
  339. randomx_destroy_vm(vms[i]);
  340. if (miningMode)
  341. randomx_release_dataset(dataset);
  342. else
  343. randomx_release_cache(cache);
  344. std::cout << "Calculated result: ";
  345. result.print(std::cout);
  346. if (noncesCount == 1000 && seedValue == 0)
  347. std::cout << "Reference result: 10b649a3f15c7c7f88277812f2e74b337a0f20ce909af09199cccb960771cfa1" << std::endl;
  348. if (!miningMode) {
  349. std::cout << "Performance: " << 1000 * elapsed / noncesCount << " ms per hash" << std::endl;
  350. }
  351. else {
  352. std::cout << "Performance: " << noncesCount / elapsed << " hashes per second" << std::endl;
  353. }
  354. }
  355. catch (MemoryException& e) {
  356. std::cout << "ERROR: " << e.what() << std::endl;
  357. if (largePages) {
  358. #ifdef _WIN32
  359. std::cout << "To use large pages, please enable the \"Lock Pages in Memory\" policy and reboot." << std::endl;
  360. if (!IsWindows8OrGreater()) {
  361. std::cout << "Additionally, you have to run the benchmark from elevated command prompt." << std::endl;
  362. }
  363. #else
  364. std::cout << "To use large pages, please run: sudo sysctl -w vm.nr_hugepages=1250" << std::endl;
  365. #endif
  366. }
  367. return 1;
  368. }
  369. catch (std::exception& e) {
  370. std::cout << "ERROR: " << e.what() << std::endl;
  371. return 1;
  372. }
  373. return 0;
  374. }