main.cpp 9.5 KB

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