main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. //#define TRACE
  16. #include "InterpretedVirtualMachine.hpp"
  17. #include "CompiledVirtualMachine.hpp"
  18. #include "AssemblyGeneratorX86.hpp"
  19. #include "Stopwatch.hpp"
  20. #include "blake2/blake2.h"
  21. #include <fstream>
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <exception>
  25. #include <cstring>
  26. #include "Program.hpp"
  27. #include <string>
  28. #include <thread>
  29. #include <atomic>
  30. #include "dataset.hpp"
  31. #include "Cache.hpp"
  32. #include "hashAes1Rx4.hpp"
  33. const uint8_t seed[32] = { 191, 182, 222, 175, 249, 89, 134, 104, 241, 68, 191, 62, 162, 166, 61, 64, 123, 191, 227, 193, 118, 60, 188, 53, 223, 133, 175, 24, 123, 230, 55, 74 };
  34. const uint8_t blockTemplate__[] = {
  35. 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
  36. 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
  37. 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
  38. 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
  39. };
  40. void dump(const char* buffer, uint64_t count, const char* name) {
  41. std::ofstream fout(name, std::ios::out | std::ios::binary);
  42. fout.write(buffer, count);
  43. fout.close();
  44. }
  45. constexpr char hexmap[] = "0123456789abcdef";
  46. void outputHex(std::ostream& os, const char* data, int length) {
  47. for (int i = 0; i < length; ++i) {
  48. os << hexmap[(data[i] & 0xF0) >> 4];
  49. os << hexmap[data[i] & 0x0F];
  50. }
  51. }
  52. void readOption(const char* option, int argc, char** argv, bool& out) {
  53. for (int i = 0; i < argc; ++i) {
  54. if (strcmp(argv[i], option) == 0) {
  55. out = true;
  56. return;
  57. }
  58. }
  59. out = false;
  60. }
  61. void readIntOption(const char* option, int argc, char** argv, int& out, int defaultValue) {
  62. for (int i = 0; i < argc - 1; ++i) {
  63. if (strcmp(argv[i], option) == 0 && (out = atoi(argv[i + 1])) > 0) {
  64. return;
  65. }
  66. }
  67. out = defaultValue;
  68. }
  69. void readInt(int argc, char** argv, int& out, int defaultValue) {
  70. for (int i = 0; i < argc; ++i) {
  71. if (*argv[i] != '-' && (out = atoi(argv[i])) > 0) {
  72. return;
  73. }
  74. }
  75. out = defaultValue;
  76. }
  77. class AtomicHash {
  78. public:
  79. AtomicHash() {
  80. for (int i = 0; i < 4; ++i)
  81. hash[i].store(0);
  82. }
  83. void xorWith(uint64_t update[4]) {
  84. for (int i = 0; i < 4; ++i)
  85. hash[i].fetch_xor(update[i]);
  86. }
  87. void print(std::ostream& os) {
  88. for (int i = 0; i < 4; ++i)
  89. print(hash[i], os);
  90. os << std::endl;
  91. }
  92. private:
  93. static void print(std::atomic<uint64_t>& hash, std::ostream& os) {
  94. auto h = hash.load();
  95. outputHex(std::cout, (char*)&h, sizeof(h));
  96. }
  97. std::atomic<uint64_t> hash[4];
  98. };
  99. void printUsage(const char* executable) {
  100. std::cout << "Usage: " << executable << " [OPTIONS]" << std::endl;
  101. std::cout << "Supported options:" << std::endl;
  102. std::cout << " --help shows this message" << std::endl;
  103. std::cout << " --mine mining mode: 4 GiB dataset, x86-64 compiled VM" << std::endl;
  104. std::cout << " (default: portable verification mode)" << std::endl;
  105. std::cout << " --largePages use large pages" << std::endl;
  106. std::cout << " --softAes use software AES (default: x86 AES-NI)" << std::endl;
  107. std::cout << " --threads T use T threads (default: 1)" << std::endl;
  108. std::cout << " --nonces N run N nonces (default: 1000)" << std::endl;
  109. std::cout << " --genAsm generate x86-64 asm code for nonce N" << std::endl;
  110. std::cout << " --genNative generate RandomX code for nonce N" << std::endl;
  111. }
  112. void generateAsm(int nonce) {
  113. uint64_t hash[8];
  114. uint8_t blockTemplate[sizeof(blockTemplate__)];
  115. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  116. int* noncePtr = (int*)(blockTemplate + 39);
  117. *noncePtr = nonce;
  118. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  119. RandomX::AssemblyGeneratorX86 asmX86;
  120. RandomX::Program p;
  121. fillAes1Rx4<false>(hash, sizeof(p), &p);
  122. asmX86.generateProgram(p);
  123. asmX86.printCode(std::cout);
  124. }
  125. void generateNative(int nonce) {
  126. uint64_t hash[4];
  127. uint8_t blockTemplate[sizeof(blockTemplate__)];
  128. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  129. int* noncePtr = (int*)(blockTemplate + 39);
  130. *noncePtr = nonce;
  131. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  132. alignas(16) RandomX::Program prog;
  133. fillAes1Rx4<false>((void*)hash, sizeof(prog), &prog);
  134. for (int i = 0; i < RandomX::ProgramLength; ++i) {
  135. prog(i).dst %= 8;
  136. prog(i).src %= 8;
  137. }
  138. std::cout << prog << std::endl;
  139. }
  140. template<bool softAes>
  141. void mine(RandomX::VirtualMachine* vm, std::atomic<int>& atomicNonce, AtomicHash& result, int noncesCount, int thread, uint8_t* scratchpad) {
  142. alignas(16) uint64_t hash[8];
  143. uint8_t blockTemplate[sizeof(blockTemplate__)];
  144. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  145. int* noncePtr = (int*)(blockTemplate + 39);
  146. int nonce = atomicNonce.fetch_add(1);
  147. while (nonce < noncesCount) {
  148. //std::cout << "Thread " << thread << " nonce " << nonce << std::endl;
  149. *noncePtr = nonce;
  150. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  151. fillAes1Rx4<softAes>((void*)hash, RandomX::ScratchpadSize, scratchpad);
  152. vm->setScratchpad(scratchpad);
  153. //dump((char*)((RandomX::CompiledVirtualMachine*)vm)->getProgram(), RandomX::CodeSize, "code-1337-jmp.txt");
  154. for (int chain = 0; chain < RandomX::ChainLength - 1; ++chain) {
  155. fillAes1Rx4<softAes>((void*)hash, sizeof(RandomX::Program), vm->getProgramBuffer());
  156. vm->initialize();
  157. vm->execute();
  158. vm->getResult<false>(nullptr, 0, hash);
  159. }
  160. fillAes1Rx4<softAes>((void*)hash, sizeof(RandomX::Program), vm->getProgramBuffer());
  161. vm->initialize();
  162. vm->execute();
  163. vm->getResult<softAes>(scratchpad, RandomX::ScratchpadSize, hash);
  164. result.xorWith(hash);
  165. if (RandomX::trace) {
  166. std::cout << "Nonce: " << nonce << " ";
  167. outputHex(std::cout, (char*)hash, sizeof(hash));
  168. std::cout << std::endl;
  169. }
  170. nonce = atomicNonce.fetch_add(1);
  171. }
  172. }
  173. int main(int argc, char** argv) {
  174. bool softAes, genAsm, miningMode, help, largePages, async, genNative;
  175. int programCount, threadCount;
  176. readOption("--help", argc, argv, help);
  177. if (help) {
  178. printUsage(argv[0]);
  179. return 0;
  180. }
  181. readOption("--softAes", argc, argv, softAes);
  182. readOption("--genAsm", argc, argv, genAsm);
  183. readOption("--mine", argc, argv, miningMode);
  184. readIntOption("--threads", argc, argv, threadCount, 1);
  185. readIntOption("--nonces", argc, argv, programCount, 1000);
  186. readOption("--largePages", argc, argv, largePages);
  187. readOption("--async", argc, argv, async);
  188. readOption("--genNative", argc, argv, genNative);
  189. if (genAsm) {
  190. generateAsm(programCount);
  191. return 0;
  192. }
  193. if (genNative) {
  194. generateNative(programCount);
  195. return 0;
  196. }
  197. if (softAes)
  198. std::cout << "Using software AES." << std::endl;
  199. std::atomic<int> atomicNonce(0);
  200. AtomicHash result;
  201. std::vector<RandomX::VirtualMachine*> vms;
  202. std::vector<std::thread> threads;
  203. RandomX::dataset_t dataset;
  204. std::cout << "RandomX - " << (miningMode ? "mining" : "verification") << " mode" << std::endl;
  205. std::cout << "Initializing..." << std::endl;
  206. try {
  207. Stopwatch sw(true);
  208. if (softAes) {
  209. RandomX::datasetInitCache<true>(seed, dataset, largePages);
  210. }
  211. else {
  212. RandomX::datasetInitCache<false>(seed, dataset, largePages);
  213. }
  214. if (RandomX::trace) {
  215. std::cout << "Keys: " << std::endl;
  216. for (unsigned i = 0; i < dataset.cache->getKeys().size(); ++i) {
  217. outputHex(std::cout, (char*)&dataset.cache->getKeys()[i], sizeof(__m128i));
  218. }
  219. std::cout << std::endl;
  220. std::cout << "Cache: " << std::endl;
  221. outputHex(std::cout, (char*)dataset.cache->getCache(), sizeof(__m128i));
  222. std::cout << std::endl;
  223. }
  224. if (!miningMode) {
  225. std::cout << "Cache (256 MiB) initialized in " << sw.getElapsed() << " s" << std::endl;
  226. }
  227. else {
  228. RandomX::Cache* cache = dataset.cache;
  229. RandomX::datasetAlloc(dataset, largePages);
  230. if (threadCount > 1) {
  231. auto perThread = RandomX::DatasetBlockCount / threadCount;
  232. auto remainder = RandomX::DatasetBlockCount % threadCount;
  233. for (int i = 0; i < threadCount; ++i) {
  234. auto count = perThread + (i == threadCount - 1 ? remainder : 0);
  235. threads.push_back(std::thread(&RandomX::datasetInit, cache, dataset, i * perThread, count));
  236. }
  237. for (unsigned i = 0; i < threads.size(); ++i) {
  238. threads[i].join();
  239. }
  240. }
  241. else {
  242. RandomX::datasetInit(cache, dataset, 0, RandomX::DatasetBlockCount);
  243. }
  244. RandomX::Cache::dealloc(cache, largePages);
  245. threads.clear();
  246. std::cout << "Dataset (4 GiB) initialized in " << sw.getElapsed() << " s" << std::endl;
  247. }
  248. std::cout << "Initializing " << threadCount << " virtual machine(s)..." << std::endl;
  249. for (int i = 0; i < threadCount; ++i) {
  250. RandomX::VirtualMachine* vm;
  251. if (miningMode) {
  252. vm = new RandomX::CompiledVirtualMachine();
  253. }
  254. else {
  255. vm = new RandomX::InterpretedVirtualMachine(softAes, async);
  256. }
  257. vm->setDataset(dataset);
  258. vms.push_back(vm);
  259. }
  260. uint8_t* scratchpadMem;
  261. if (largePages) {
  262. scratchpadMem = (uint8_t*)allocLargePagesMemory(threadCount * RandomX::ScratchpadSize);
  263. }
  264. else {
  265. scratchpadMem = (uint8_t*)_mm_malloc(threadCount * RandomX::ScratchpadSize, RandomX::CacheLineSize);
  266. }
  267. std::cout << "Running benchmark (" << programCount << " nonces) ..." << std::endl;
  268. sw.restart();
  269. if (threadCount > 1) {
  270. for (unsigned i = 0; i < vms.size(); ++i) {
  271. if (softAes)
  272. threads.push_back(std::thread(&mine<true>, vms[i], std::ref(atomicNonce), std::ref(result), programCount, i, scratchpadMem + RandomX::ScratchpadSize * i));
  273. else
  274. threads.push_back(std::thread(&mine<false>, vms[i], std::ref(atomicNonce), std::ref(result), programCount, i, scratchpadMem + RandomX::ScratchpadSize * i));
  275. }
  276. for (unsigned i = 0; i < threads.size(); ++i) {
  277. threads[i].join();
  278. }
  279. }
  280. else {
  281. if(softAes)
  282. mine<true>(vms[0], std::ref(atomicNonce), std::ref(result), programCount, 0, scratchpadMem);
  283. else
  284. mine<false>(vms[0], std::ref(atomicNonce), std::ref(result), programCount, 0, scratchpadMem);
  285. if (miningMode)
  286. std::cout << "Average program size: " << ((RandomX::CompiledVirtualMachine*)vms[0])->getTotalSize() / programCount / RandomX::ChainLength << std::endl;
  287. }
  288. double elapsed = sw.getElapsed();
  289. std::cout << "Calculated result: ";
  290. result.print(std::cout);
  291. /*if(programCount == 1000)
  292. std::cout << "Reference result: 3e1c5f9b9d0bf8ffa250f860bf5f7ab76ac823b206ddee6a592660119a3640c6" << std::endl;*/
  293. if (!miningMode) {
  294. std::cout << "Performance: " << 1000 * elapsed / programCount << " ms per hash" << std::endl;
  295. }
  296. else {
  297. std::cout << "Performance: " << programCount / elapsed << " hashes per second" << std::endl;
  298. }
  299. }
  300. catch (std::exception& e) {
  301. std::cout << "ERROR: " << e.what() << std::endl;
  302. return 1;
  303. }
  304. return 0;
  305. }