main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. 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. void mine(RandomX::VirtualMachine* vm, std::atomic<int>& atomicNonce, AtomicHash& result, int noncesCount, int thread, uint8_t* scratchpad) {
  141. alignas(16) uint64_t hash[8];
  142. uint8_t blockTemplate[sizeof(blockTemplate__)];
  143. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  144. int* noncePtr = (int*)(blockTemplate + 39);
  145. int nonce = atomicNonce.fetch_add(1);
  146. while (nonce < noncesCount) {
  147. //std::cout << "Thread " << thread << " nonce " << nonce << std::endl;
  148. *noncePtr = nonce;
  149. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  150. fillAes1Rx4<false>((void*)hash, RandomX::ScratchpadSize, scratchpad);
  151. vm->setScratchpad(scratchpad);
  152. //dump((char*)((RandomX::CompiledVirtualMachine*)vm)->getProgram(), RandomX::CodeSize, "code-1337-jmp.txt");
  153. for (int chain = 0; chain < RandomX::ChainLength - 1; ++chain) {
  154. fillAes1Rx4<false>((void*)hash, sizeof(RandomX::Program), vm->getProgramBuffer());
  155. vm->initialize();
  156. vm->execute();
  157. vm->getResult<false>(nullptr, 0, hash);
  158. }
  159. fillAes1Rx4<false>((void*)hash, sizeof(RandomX::Program), vm->getProgramBuffer());
  160. vm->initialize();
  161. vm->execute();
  162. vm->getResult<false>(scratchpad, RandomX::ScratchpadSize, hash);
  163. result.xorWith(hash);
  164. if (RandomX::trace) {
  165. std::cout << "Nonce: " << nonce << " ";
  166. outputHex(std::cout, (char*)hash, sizeof(hash));
  167. std::cout << std::endl;
  168. }
  169. nonce = atomicNonce.fetch_add(1);
  170. }
  171. }
  172. int main(int argc, char** argv) {
  173. bool softAes, genAsm, miningMode, help, largePages, async, genNative;
  174. int programCount, threadCount;
  175. readOption("--help", argc, argv, help);
  176. if (help) {
  177. printUsage(argv[0]);
  178. return 0;
  179. }
  180. readOption("--softAes", argc, argv, softAes);
  181. readOption("--genAsm", argc, argv, genAsm);
  182. readOption("--mine", argc, argv, miningMode);
  183. readIntOption("--threads", argc, argv, threadCount, 1);
  184. readIntOption("--nonces", argc, argv, programCount, 1000);
  185. readOption("--largePages", argc, argv, largePages);
  186. readOption("--async", argc, argv, async);
  187. readOption("--genNative", argc, argv, genNative);
  188. if (genAsm) {
  189. generateAsm(programCount);
  190. return 0;
  191. }
  192. if (genNative) {
  193. generateNative(programCount);
  194. return 0;
  195. }
  196. if (softAes)
  197. std::cout << "Using software AES." << std::endl;
  198. std::atomic<int> atomicNonce(0);
  199. AtomicHash result;
  200. std::vector<RandomX::VirtualMachine*> vms;
  201. std::vector<std::thread> threads;
  202. RandomX::dataset_t dataset;
  203. std::cout << "RandomX - " << (miningMode ? "mining" : "verification") << " mode" << std::endl;
  204. std::cout << "Initializing..." << std::endl;
  205. try {
  206. Stopwatch sw(true);
  207. if (softAes) {
  208. RandomX::datasetInitCache<true>(seed, dataset, largePages);
  209. }
  210. else {
  211. RandomX::datasetInitCache<false>(seed, dataset, largePages);
  212. }
  213. if (RandomX::trace) {
  214. std::cout << "Keys: " << std::endl;
  215. for (unsigned i = 0; i < dataset.cache->getKeys().size(); ++i) {
  216. outputHex(std::cout, (char*)&dataset.cache->getKeys()[i], sizeof(__m128i));
  217. }
  218. std::cout << std::endl;
  219. std::cout << "Cache: " << std::endl;
  220. outputHex(std::cout, (char*)dataset.cache->getCache(), sizeof(__m128i));
  221. std::cout << std::endl;
  222. }
  223. if (!miningMode) {
  224. std::cout << "Cache (256 MiB) initialized in " << sw.getElapsed() << " s" << std::endl;
  225. }
  226. else {
  227. RandomX::Cache* cache = dataset.cache;
  228. RandomX::datasetAlloc(dataset, largePages);
  229. if (threadCount > 1) {
  230. auto perThread = RandomX::DatasetBlockCount / threadCount;
  231. auto remainder = RandomX::DatasetBlockCount % threadCount;
  232. for (int i = 0; i < threadCount; ++i) {
  233. auto count = perThread + (i == threadCount - 1 ? remainder : 0);
  234. if (softAes) {
  235. threads.push_back(std::thread(&RandomX::datasetInit<true>, cache, dataset, i * perThread, count));
  236. }
  237. else {
  238. threads.push_back(std::thread(&RandomX::datasetInit<false>, cache, dataset, i * perThread, count));
  239. }
  240. }
  241. for (unsigned i = 0; i < threads.size(); ++i) {
  242. threads[i].join();
  243. }
  244. }
  245. else {
  246. if (softAes) {
  247. RandomX::datasetInit<true>(cache, dataset, 0, RandomX::DatasetBlockCount);
  248. }
  249. else {
  250. RandomX::datasetInit<false>(cache, dataset, 0, RandomX::DatasetBlockCount);
  251. }
  252. }
  253. RandomX::Cache::dealloc(cache, largePages);
  254. threads.clear();
  255. std::cout << "Dataset (4 GiB) initialized in " << sw.getElapsed() << " s" << std::endl;
  256. }
  257. std::cout << "Initializing " << threadCount << " virtual machine(s)..." << std::endl;
  258. for (int i = 0; i < threadCount; ++i) {
  259. RandomX::VirtualMachine* vm;
  260. if (miningMode) {
  261. vm = new RandomX::CompiledVirtualMachine();
  262. }
  263. else {
  264. vm = new RandomX::InterpretedVirtualMachine(softAes, async);
  265. }
  266. vm->setDataset(dataset);
  267. vms.push_back(vm);
  268. }
  269. uint8_t* scratchpadMem;
  270. if (largePages) {
  271. scratchpadMem = (uint8_t*)allocLargePagesMemory(threadCount * RandomX::ScratchpadSize);
  272. }
  273. else {
  274. scratchpadMem = (uint8_t*)_mm_malloc(threadCount * RandomX::ScratchpadSize, RandomX::CacheLineSize);
  275. }
  276. std::cout << "Running benchmark (" << programCount << " nonces) ..." << std::endl;
  277. sw.restart();
  278. if (threadCount > 1) {
  279. for (unsigned i = 0; i < vms.size(); ++i) {
  280. threads.push_back(std::thread(&mine, vms[i], std::ref(atomicNonce), std::ref(result), programCount, i, scratchpadMem + RandomX::ScratchpadSize * i));
  281. }
  282. for (unsigned i = 0; i < threads.size(); ++i) {
  283. threads[i].join();
  284. }
  285. }
  286. else {
  287. mine(vms[0], std::ref(atomicNonce), std::ref(result), programCount, 0, scratchpadMem);
  288. if (miningMode)
  289. std::cout << "Average program size: " << ((RandomX::CompiledVirtualMachine*)vms[0])->getTotalSize() / programCount / RandomX::ChainLength << std::endl;
  290. }
  291. double elapsed = sw.getElapsed();
  292. std::cout << "Calculated result: ";
  293. result.print(std::cout);
  294. /*if(programCount == 1000)
  295. std::cout << "Reference result: 3e1c5f9b9d0bf8ffa250f860bf5f7ab76ac823b206ddee6a592660119a3640c6" << std::endl;*/
  296. if (!miningMode) {
  297. std::cout << "Performance: " << 1000 * elapsed / programCount << " ms per hash" << std::endl;
  298. }
  299. else {
  300. std::cout << "Performance: " << programCount / elapsed << " hashes per second" << std::endl;
  301. }
  302. }
  303. catch (std::exception& e) {
  304. std::cout << "ERROR: " << e.what() << std::endl;
  305. return 1;
  306. }
  307. return 0;
  308. }