main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "instructions.hpp"
  29. #include <thread>
  30. #include <atomic>
  31. #include "dataset.hpp"
  32. #include "Cache.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. void dump(const char* buffer, uint64_t count, const char* name) {
  35. std::ofstream fout(name, std::ios::out | std::ios::binary);
  36. fout.write(buffer, count);
  37. fout.close();
  38. }
  39. constexpr char hexmap[] = "0123456789abcdef";
  40. void outputHex(std::ostream& os, const char* data, int length) {
  41. for (int i = 0; i < length; ++i) {
  42. os << hexmap[(data[i] & 0xF0) >> 4];
  43. os << hexmap[data[i] & 0x0F];
  44. }
  45. }
  46. void readOption(const char* option, int argc, char** argv, bool& out) {
  47. for (int i = 0; i < argc; ++i) {
  48. if (strcmp(argv[i], option) == 0) {
  49. out = true;
  50. return;
  51. }
  52. }
  53. out = false;
  54. }
  55. void readIntOption(const char* option, int argc, char** argv, int& out, int defaultValue) {
  56. for (int i = 0; i < argc - 1; ++i) {
  57. if (strcmp(argv[i], option) == 0 && (out = atoi(argv[i + 1])) > 0) {
  58. return;
  59. }
  60. }
  61. out = defaultValue;
  62. }
  63. void readInt(int argc, char** argv, int& out, int defaultValue) {
  64. for (int i = 0; i < argc; ++i) {
  65. if (*argv[i] != '-' && (out = atoi(argv[i])) > 0) {
  66. return;
  67. }
  68. }
  69. out = defaultValue;
  70. }
  71. std::ostream& operator<<(std::ostream& os, const RandomX::RegisterFile& rf) {
  72. for (int i = 0; i < RandomX::RegistersCount; ++i)
  73. os << std::hex << "r" << i << " = " << rf.r[i].u64 << std::endl << std::dec;
  74. for (int i = 0; i < RandomX::RegistersCount; ++i)
  75. os << std::hex << "f" << i << " = " << rf.f[i].u64 << " (" << rf.f[i].f64 << ")" << std::endl << std::dec;
  76. return os;
  77. }
  78. class AtomicHash {
  79. public:
  80. AtomicHash() {
  81. for (int i = 0; i < 4; ++i)
  82. hash[i].store(0);
  83. }
  84. void xorWith(uint64_t update[4]) {
  85. for (int i = 0; i < 4; ++i)
  86. hash[i].fetch_xor(update[i]);
  87. }
  88. void print(std::ostream& os) {
  89. for (int i = 0; i < 4; ++i)
  90. print(hash[i], os);
  91. os << std::endl;
  92. }
  93. private:
  94. void print(std::atomic<uint64_t>& hash, std::ostream& os) {
  95. auto h = hash.load();
  96. outputHex(std::cout, (char*)&h, sizeof(h));
  97. }
  98. std::atomic<uint64_t> hash[4];
  99. };
  100. void printUsage(const char* executable) {
  101. std::cout << "Usage: " << executable << " [OPTIONS]" << std::endl;
  102. std::cout << "Supported options:" << std::endl;
  103. std::cout << "\t--help\t\t\tshows this message" << std::endl;
  104. std::cout << "\t--compiled\t\tuse x86-64 JIT-compiled VM (default: interpreted VM)" << std::endl;
  105. std::cout << "\t--lightClient\t\tuse 'light-client' mode (default: full dataset mode)" << std::endl;
  106. std::cout << "\t--softAes\t\tuse software AES (default: x86 AES-NI)" << std::endl;
  107. std::cout << "\t--threads T\t\tuse T threads (default: 1)" << std::endl;
  108. std::cout << "\t--nonces N\t\trun N nonces (default: 1000)" << std::endl;
  109. std::cout << "\t--genAsm\t\tgenerate x86 asm code for nonce N" << std::endl;
  110. }
  111. void generateAsm(int nonce) {
  112. uint64_t hash[4];
  113. unsigned char blockTemplate[] = {
  114. 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
  115. 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
  116. 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
  117. 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
  118. };
  119. int* noncePtr = (int*)(blockTemplate + 39);
  120. *noncePtr = nonce;
  121. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  122. RandomX::AssemblyGeneratorX86 asmX86;
  123. asmX86.generateProgram(hash);
  124. asmX86.printCode(std::cout);
  125. }
  126. void mine(RandomX::VirtualMachine* vm, std::atomic<int>& atomicNonce, AtomicHash& result, int noncesCount, int thread) {
  127. uint64_t hash[4];
  128. unsigned char blockTemplate[] = {
  129. 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
  130. 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
  131. 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
  132. 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
  133. };
  134. int* noncePtr = (int*)(blockTemplate + 39);
  135. int nonce = atomicNonce.fetch_add(1);
  136. while (nonce < noncesCount) {
  137. //std::cout << "Thread " << thread << " nonce " << nonce << std::endl;
  138. *noncePtr = nonce;
  139. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  140. int spIndex = ((uint8_t*)hash)[24] | ((((uint8_t*)hash)[25] & 63) << 8);
  141. vm->initializeScratchpad(spIndex);
  142. vm->initializeProgram(hash);
  143. //dump((char*)((RandomX::CompiledVirtualMachine*)vm)->getProgram(), RandomX::CodeSize, "code-1337-jmp.txt");
  144. vm->execute();
  145. vm->getResult(hash);
  146. result.xorWith(hash);
  147. if (RandomX::trace) {
  148. std::cout << "Nonce: " << nonce << " ";
  149. outputHex(std::cout, (char*)hash, sizeof(hash));
  150. std::cout << std::endl;
  151. }
  152. nonce = atomicNonce.fetch_add(1);
  153. }
  154. }
  155. int main(int argc, char** argv) {
  156. bool softAes, lightClient, genAsm, compiled, help;
  157. int programCount, threadCount;
  158. readOption("--help", argc, argv, help);
  159. if (help) {
  160. printUsage(argv[0]);
  161. return 0;
  162. }
  163. readOption("--softAes", argc, argv, softAes);
  164. readOption("--lightClient", argc, argv, lightClient);
  165. readOption("--genAsm", argc, argv, genAsm);
  166. readOption("--compiled", argc, argv, compiled);
  167. readIntOption("--threads", argc, argv, threadCount, 1);
  168. readIntOption("--nonces", argc, argv, programCount, 1000);
  169. if (genAsm) {
  170. generateAsm(programCount);
  171. return 0;
  172. }
  173. std::atomic<int> atomicNonce(0);
  174. AtomicHash result;
  175. std::vector<RandomX::VirtualMachine*> vms;
  176. std::vector<std::thread> threads;
  177. RandomX::dataset_t dataset;
  178. if (softAes)
  179. std::cout << "Using software AES." << std::endl;
  180. std::cout << "Initializing..." << std::endl;
  181. try {
  182. Stopwatch sw(true);
  183. if (softAes) {
  184. RandomX::datasetInitCache<true>(seed, dataset);
  185. }
  186. else {
  187. RandomX::datasetInitCache<false>(seed, dataset);
  188. }
  189. if (RandomX::trace) {
  190. std::cout << "Keys: " << std::endl;
  191. for (int i = 0; i < dataset.cache->getKeys().size(); ++i) {
  192. outputHex(std::cout, (char*)&dataset.cache->getKeys()[i], sizeof(__m128i));
  193. }
  194. std::cout << std::endl;
  195. std::cout << "Cache: " << std::endl;
  196. outputHex(std::cout, (char*)dataset.cache->getCache(), sizeof(__m128i));
  197. std::cout << std::endl;
  198. }
  199. if (lightClient) {
  200. std::cout << "Cache (64 MiB) initialized in " << sw.getElapsed() << " s" << std::endl;
  201. }
  202. else {
  203. RandomX::Cache* cache = dataset.cache;
  204. RandomX::datasetAlloc(dataset);
  205. if (threadCount > 1) {
  206. auto perThread = RandomX::DatasetBlockCount / threadCount;
  207. auto remainder = RandomX::DatasetBlockCount % threadCount;
  208. for (int i = 0; i < threadCount; ++i) {
  209. auto count = perThread + (i == threadCount - 1 ? remainder : 0);
  210. if (softAes) {
  211. threads.push_back(std::thread(&RandomX::datasetInit<true>, cache, dataset, i * perThread, count));
  212. }
  213. else {
  214. threads.push_back(std::thread(&RandomX::datasetInit<false>, cache, dataset, i * perThread, count));
  215. }
  216. }
  217. for (int i = 0; i < threads.size(); ++i) {
  218. threads[i].join();
  219. }
  220. }
  221. else {
  222. if (softAes) {
  223. RandomX::datasetInit<true>(cache, dataset, 0, RandomX::DatasetBlockCount);
  224. }
  225. else {
  226. RandomX::datasetInit<false>(cache, dataset, 0, RandomX::DatasetBlockCount);
  227. }
  228. }
  229. delete cache;
  230. threads.clear();
  231. std::cout << "Dataset (4 GiB) initialized in " << sw.getElapsed() << " s" << std::endl;
  232. }
  233. std::cout << "Initializing " << threadCount << " virtual machine(s)..." << std::endl;
  234. for (int i = 0; i < threadCount; ++i) {
  235. RandomX::VirtualMachine* vm;
  236. if (compiled) {
  237. vm = new RandomX::CompiledVirtualMachine(softAes);
  238. }
  239. else {
  240. vm = new RandomX::InterpretedVirtualMachine(softAes);
  241. }
  242. vm->setDataset(dataset, lightClient);
  243. vms.push_back(vm);
  244. }
  245. std::cout << "Running benchmark (" << programCount << " programs) ..." << std::endl;
  246. sw.restart();
  247. if (threadCount > 1) {
  248. for (int i = 0; i < vms.size(); ++i) {
  249. threads.push_back(std::thread(&mine, vms[i], std::ref(atomicNonce), std::ref(result), programCount, i));
  250. }
  251. for (int i = 0; i < threads.size(); ++i) {
  252. threads[i].join();
  253. }
  254. }
  255. else {
  256. mine(vms[0], std::ref(atomicNonce), std::ref(result), programCount, 0);
  257. }
  258. double elapsed = sw.getElapsed();
  259. std::cout << "Calculated result: ";
  260. result.print(std::cout);
  261. if(programCount == 1000)
  262. std::cout << "Reference result: f6bf06465d5fa1b1dc919140b9e9f9e210b07ae6d662988458a172e9a267eb3f" << std::endl;
  263. std::cout << "Performance: " << programCount / elapsed << " programs per second" << std::endl;
  264. /*if (threadCount == 1 && !compiled) {
  265. auto ivm = (RandomX::InterpretedVirtualMachine*)vms[0];
  266. std::cout << ivm->getProgam();
  267. }*/
  268. }
  269. catch (std::exception& e) {
  270. std::cout << "ERROR: " << e.what() << std::endl;
  271. return 1;
  272. }
  273. return 0;
  274. }