EthashCPUMiner.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file EthashCPUMiner.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * Determines the PoW algorithm.
  19. */
  20. #include "EthashCPUMiner.h"
  21. #include <thread>
  22. #include <chrono>
  23. #include <boost/algorithm/string.hpp>
  24. #include <random>
  25. #if ETH_CPUID
  26. #define HAVE_STDINT_H
  27. #include <libcpuid/libcpuid.h>
  28. #endif // ETH_CPUID
  29. using namespace std;
  30. using namespace dev;
  31. using namespace eth;
  32. unsigned EthashCPUMiner::s_numInstances = 0;
  33. #if ETH_CPUID
  34. static string jsonEncode(map<string, string> const& _m)
  35. {
  36. string ret = "{";
  37. for (auto const& i: _m)
  38. {
  39. string k = boost::replace_all_copy(boost::replace_all_copy(i.first, "\\", "\\\\"), "'", "\\'");
  40. string v = boost::replace_all_copy(boost::replace_all_copy(i.second, "\\", "\\\\"), "'", "\\'");
  41. if (ret.size() > 1)
  42. ret += ", ";
  43. ret += "\"" + k + "\":\"" + v + "\"";
  44. }
  45. return ret + "}";
  46. }
  47. #endif // ETH_CPUID
  48. EthashCPUMiner::EthashCPUMiner(GenericMiner<EthashProofOfWork>::ConstructionInfo const& _ci):
  49. GenericMiner<EthashProofOfWork>(_ci), Worker("miner" + toString(index()))
  50. {
  51. }
  52. EthashCPUMiner::~EthashCPUMiner()
  53. {
  54. }
  55. void EthashCPUMiner::kickOff()
  56. {
  57. stopWorking();
  58. startWorking();
  59. }
  60. void EthashCPUMiner::pause()
  61. {
  62. stopWorking();
  63. }
  64. void EthashCPUMiner::workLoop()
  65. {
  66. auto tid = std::this_thread::get_id();
  67. static std::mt19937_64 s_eng((utcTime() + std::hash<decltype(tid)>()(tid)));
  68. uint64_t tryNonce = s_eng();
  69. ethash_return_value ethashReturn;
  70. WorkPackage w = work();
  71. EthashAux::FullType dag;
  72. while (!shouldStop() && !dag)
  73. {
  74. while (!shouldStop() && EthashAux::computeFull(w.seedHash, true) != 100)
  75. this_thread::sleep_for(chrono::milliseconds(500));
  76. dag = EthashAux::full(w.seedHash, false);
  77. }
  78. h256 boundary = w.boundary;
  79. unsigned hashCount = 1;
  80. for (; !shouldStop(); tryNonce++, hashCount++)
  81. {
  82. ethashReturn = ethash_full_compute(dag->full, *(ethash_h256_t*)w.headerHash.data(), tryNonce);
  83. h256 value = h256((uint8_t*)&ethashReturn.result, h256::ConstructFromPointer);
  84. if (value <= boundary && submitProof(EthashProofOfWork::Solution{(h64)(u64)tryNonce, h256((uint8_t*)&ethashReturn.mix_hash, h256::ConstructFromPointer)}))
  85. break;
  86. if (!(hashCount % 100))
  87. accumulateHashes(100);
  88. }
  89. }
  90. std::string EthashCPUMiner::platformInfo()
  91. {
  92. string baseline = toString(std::thread::hardware_concurrency()) + "-thread CPU";
  93. #if ETH_CPUID
  94. if (!cpuid_present())
  95. return baseline;
  96. struct cpu_raw_data_t raw;
  97. struct cpu_id_t data;
  98. if (cpuid_get_raw_data(&raw) < 0)
  99. return baseline;
  100. if (cpu_identify(&raw, &data) < 0)
  101. return baseline;
  102. map<string, string> m;
  103. m["vendor"] = data.vendor_str;
  104. m["codename"] = data.cpu_codename;
  105. m["brand"] = data.brand_str;
  106. m["L1 cache"] = toString(data.l1_data_cache);
  107. m["L2 cache"] = toString(data.l2_cache);
  108. m["L3 cache"] = toString(data.l3_cache);
  109. m["cores"] = toString(data.num_cores);
  110. m["threads"] = toString(data.num_logical_cpus);
  111. m["clocknominal"] = toString(cpu_clock_by_os());
  112. m["clocktested"] = toString(cpu_clock_measure(200, 0));
  113. /*
  114. printf(" MMX : %s\n", data.flags[CPU_FEATURE_MMX] ? "present" : "absent");
  115. printf(" MMX-extended: %s\n", data.flags[CPU_FEATURE_MMXEXT] ? "present" : "absent");
  116. printf(" SSE : %s\n", data.flags[CPU_FEATURE_SSE] ? "present" : "absent");
  117. printf(" SSE2 : %s\n", data.flags[CPU_FEATURE_SSE2] ? "present" : "absent");
  118. printf(" 3DNow! : %s\n", data.flags[CPU_FEATURE_3DNOW] ? "present" : "absent");
  119. */
  120. return jsonEncode(m);
  121. #else
  122. return baseline;
  123. #endif // ETH_CPUID
  124. }