This repository was archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathFarm.cpp
More file actions
694 lines (610 loc) · 21.2 KB
/
Farm.cpp
File metadata and controls
694 lines (610 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
This file is part of ethminer.
ethminer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethminer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ethminer. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libethcore/Farm.h>
#if ETH_ETHASHCL
#include <libethash-cl/CLMiner.h>
#endif
#if ETH_ETHASHCUDA
#include <libethash-cuda/CUDAMiner.h>
#endif
#if ETH_ETHASHCPU
#include <libethash-cpu/CPUMiner.h>
#endif
namespace dev
{
namespace eth
{
Farm* Farm::m_this = nullptr;
Farm::Farm(std::map<std::string, DeviceDescriptor>& _DevicesCollection,
FarmSettings _settings, CUSettings _CUSettings, CLSettings _CLSettings, CPSettings _CPSettings)
: m_Settings(std::move(_settings)),
m_CUSettings(std::move(_CUSettings)),
m_CLSettings(std::move(_CLSettings)),
m_CPSettings(std::move(_CPSettings)),
m_io_strand(g_io_service),
m_collectTimer(g_io_service),
m_DevicesCollection(_DevicesCollection)
{
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::Farm() begin");
m_this = this;
// Init HWMON if needed
if (m_Settings.hwMon)
{
m_telemetry.hwmon = true;
#if defined(__linux)
bool need_sysfsh = false;
#else
bool need_adlh = false;
#endif
bool need_nvmlh = false;
// Scan devices collection to identify which hw monitors to initialize
for (auto it = m_DevicesCollection.begin(); it != m_DevicesCollection.end(); it++)
{
if (it->second.subscriptionType == DeviceSubscriptionTypeEnum::Cuda)
{
need_nvmlh = true;
continue;
}
if (it->second.subscriptionType == DeviceSubscriptionTypeEnum::OpenCL)
{
if (it->second.clPlatformType == ClPlatformTypeEnum::Nvidia)
{
need_nvmlh = true;
continue;
}
if (it->second.clPlatformType == ClPlatformTypeEnum::Amd)
{
#if defined(__linux)
need_sysfsh = true;
#else
need_adlh = true;
#endif
continue;
}
}
}
#if defined(__linux)
if (need_sysfsh)
sysfsh = wrap_amdsysfs_create();
if (sysfsh)
{
// Build Pci identification mapping as done in miners.
for (int i = 0; i < sysfsh->sysfs_gpucount; i++)
{
std::ostringstream oss;
std::string uniqueId;
oss << std::setfill('0') << std::setw(2) << std::hex
<< (unsigned int)sysfsh->sysfs_pci_bus_id[i] << ":" << std::setw(2)
<< (unsigned int)(sysfsh->sysfs_pci_device_id[i]) << ".0";
uniqueId = oss.str();
map_amdsysfs_handle[uniqueId] = i;
}
}
#else
if (need_adlh)
adlh = wrap_adl_create();
if (adlh)
{
// Build Pci identification as done in miners.
for (int i = 0; i < adlh->adl_gpucount; i++)
{
std::ostringstream oss;
std::string uniqueId;
oss << std::setfill('0') << std::setw(2) << std::hex
<< (unsigned int)adlh->devs[adlh->phys_logi_device_id[i]].iBusNumber << ":"
<< std::setw(2)
<< (unsigned int)(adlh->devs[adlh->phys_logi_device_id[i]].iDeviceNumber)
<< ".0";
uniqueId = oss.str();
map_adl_handle[uniqueId] = i;
}
}
#endif
if (need_nvmlh)
nvmlh = wrap_nvml_create();
if (nvmlh)
{
// Build Pci identification as done in miners.
for (int i = 0; i < nvmlh->nvml_gpucount; i++)
{
std::ostringstream oss;
std::string uniqueId;
oss << std::setfill('0') << std::setw(2) << std::hex
<< (unsigned int)nvmlh->nvml_pci_bus_id[i] << ":" << std::setw(2)
<< (unsigned int)(nvmlh->nvml_pci_device_id[i] >> 3) << ".0";
uniqueId = oss.str();
map_nvml_handle[uniqueId] = i;
}
}
}
// Initialize nonce_scrambler
shuffle();
// Start data collector timer
// It should work for the whole lifetime of Farm
// regardless it's mining state
m_collectTimer.expires_from_now(boost::posix_time::milliseconds(m_collectInterval));
m_collectTimer.async_wait(
m_io_strand.wrap(boost::bind(&Farm::collectData, this, boost::asio::placeholders::error)));
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::Farm() end");
}
Farm::~Farm()
{
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::~Farm() begin");
// Stop data collector (before monitors !!!)
m_collectTimer.cancel();
// Deinit HWMON
#if defined(__linux)
if (sysfsh)
wrap_amdsysfs_destroy(sysfsh);
#else
if (adlh)
wrap_adl_destroy(adlh);
#endif
if (nvmlh)
wrap_nvml_destroy(nvmlh);
// Stop mining (if needed)
if (m_isMining.load(std::memory_order_relaxed))
stop();
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::~Farm() end");
}
/**
* @brief Randomizes the nonce scrambler
*/
void Farm::shuffle()
{
// Given that all nonces are equally likely to solve the problem
// we could reasonably always start the nonce search ranges
// at a fixed place, but that would be boring. Provide a once
// per run randomized start place, without creating much overhead.
random_device engine;
m_nonce_scrambler = uniform_int_distribution<uint64_t>()(engine);
}
void Farm::setWork(WorkPackage const& _newWp)
{
// Set work to each miner giving it's own starting nonce
Guard l(x_minerWork);
// Retrieve appropriate EpochContext
if (m_currentWp.epoch != _newWp.epoch)
{
ethash::epoch_context _ec = ethash::get_global_epoch_context(_newWp.epoch);
m_currentEc.epochNumber = _newWp.epoch;
m_currentEc.lightNumItems = _ec.light_cache_num_items;
m_currentEc.lightSize = ethash::get_light_cache_size(_ec.light_cache_num_items);
m_currentEc.dagNumItems = _ec.full_dataset_num_items;
m_currentEc.dagSize = ethash::get_full_dataset_size(_ec.full_dataset_num_items);
m_currentEc.lightCache = _ec.light_cache;
for (auto const& miner : m_miners)
miner->setEpoch(m_currentEc);
}
m_currentWp = _newWp;
// Check if we need to shuffle per work (ergodicity == 2)
if (m_Settings.ergodicity == 2 && m_currentWp.exSizeBytes == 0)
shuffle();
uint64_t _startNonce;
if (m_currentWp.exSizeBytes > 0)
{
// Equally divide the residual segment among miners
_startNonce = m_currentWp.startNonce;
m_nonce_segment_with =
(unsigned int)log2(pow(2, 64 - (m_currentWp.exSizeBytes * 4)) / m_miners.size());
}
else
{
// Get the randomly selected nonce
_startNonce = m_nonce_scrambler;
}
for (unsigned int i = 0; i < m_miners.size(); i++)
{
m_currentWp.startNonce = _startNonce + ((uint64_t)i << m_nonce_segment_with);
m_miners.at(i)->setWork(m_currentWp);
}
}
/**
* @brief Start a number of miners.
*/
bool Farm::start()
{
// Prevent recursion
if (m_isMining.load(std::memory_order_relaxed))
return true;
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::start() begin");
Guard l(x_minerWork);
// Start all subscribed miners if none yet
if (!m_miners.size())
{
for (auto it = m_DevicesCollection.begin(); it != m_DevicesCollection.end(); it++)
{
TelemetryAccountType minerTelemetry;
#if ETH_ETHASHCUDA
if (it->second.subscriptionType == DeviceSubscriptionTypeEnum::Cuda)
{
minerTelemetry.prefix = "cu";
m_miners.push_back(std::shared_ptr<Miner>(
new CUDAMiner(m_miners.size(), m_CUSettings, it->second)));
}
#endif
#if ETH_ETHASHCL
if (it->second.subscriptionType == DeviceSubscriptionTypeEnum::OpenCL)
{
minerTelemetry.prefix = "cl";
m_miners.push_back(std::shared_ptr<Miner>(
new CLMiner(m_miners.size(), m_CLSettings, it->second)));
}
#endif
#if ETH_ETHASHCPU
if (it->second.subscriptionType == DeviceSubscriptionTypeEnum::Cpu)
{
minerTelemetry.prefix = "cp";
m_miners.push_back(std::shared_ptr<Miner>(
new CPUMiner(m_miners.size(), m_CPSettings, it->second)));
}
#endif
if (minerTelemetry.prefix.empty())
continue;
m_telemetry.miners.push_back(minerTelemetry);
m_miners.back()->startWorking();
}
// Initialize DAG Load mode
Miner::setDagLoadInfo(m_Settings.dagLoadMode, (unsigned int)m_miners.size());
m_isMining.store(true, std::memory_order_relaxed);
}
else
{
for (auto const& miner : m_miners)
miner->startWorking();
m_isMining.store(true, std::memory_order_relaxed);
}
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::start() end");
return m_isMining.load(std::memory_order_relaxed);
}
/**
* @brief Stop all mining activities.
*/
void Farm::stop()
{
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::stop() begin");
// Avoid re-entering if not actually mining.
// This, in fact, is also called by destructor
if (isMining())
{
{
Guard l(x_minerWork);
for (auto const& miner : m_miners)
{
miner->triggerStopWorking();
miner->kick_miner();
}
m_miners.clear();
m_isMining.store(false, std::memory_order_relaxed);
}
}
DEV_BUILD_LOG_PROGRAMFLOW(cnote, "Farm::stop() end");
}
/**
* @brief Pauses the whole collection of miners
*/
void Farm::pause()
{
// Signal each miner to suspend mining
Guard l(x_minerWork);
m_paused.store(true, std::memory_order_relaxed);
for (auto const& m : m_miners)
m->pause(MinerPauseEnum::PauseDueToFarmPaused);
}
/**
* @brief Returns whether or not this farm is paused for any reason
*/
bool Farm::paused()
{
return m_paused.load(std::memory_order_relaxed);
}
/**
* @brief Resumes from a pause condition
*/
void Farm::resume()
{
// Signal each miner to resume mining
// Note ! Miners may stay suspended if other reasons
Guard l(x_minerWork);
m_paused.store(false, std::memory_order_relaxed);
for (auto const& m : m_miners)
m->resume(MinerPauseEnum::PauseDueToFarmPaused);
}
/**
* @brief Stop all mining activities and Starts them again
*/
void Farm::restart()
{
if (m_onMinerRestart)
m_onMinerRestart();
}
/**
* @brief Stop all mining activities and Starts them again (async post)
*/
void Farm::restart_async()
{
// m_io_strand.get_io_service().post(m_io_strand.wrap(boost::bind(&Farm::restart, this)));
m_io_strand.post(m_io_strand.wrap(boost::bind(&Farm::restart, this)));
}
/**
* @brief Spawn a reboot script (reboot.bat/reboot.sh)
* @return false if no matching file was found
*/
bool Farm::reboot(const std::vector<std::string>& args)
{
#if defined(_WIN32)
const char* filename = "reboot.bat";
#else
const char* filename = "reboot.sh";
#endif
return spawn_file_in_bin_dir(filename, args);
}
/**
* @brief Account solutions for miner and for farm
*/
void Farm::accountSolution(unsigned _minerIdx, SolutionAccountingEnum _accounting)
{
if (_accounting == SolutionAccountingEnum::Accepted)
{
m_telemetry.farm.solutions.accepted++;
m_telemetry.farm.solutions.tstamp = std::chrono::steady_clock::now();
m_telemetry.miners.at(_minerIdx).solutions.accepted++;
m_telemetry.miners.at(_minerIdx).solutions.tstamp = std::chrono::steady_clock::now();
return;
}
if (_accounting == SolutionAccountingEnum::Wasted)
{
m_telemetry.farm.solutions.wasted++;
m_telemetry.farm.solutions.tstamp = std::chrono::steady_clock::now();
m_telemetry.miners.at(_minerIdx).solutions.wasted++;
m_telemetry.miners.at(_minerIdx).solutions.tstamp = std::chrono::steady_clock::now();
return;
}
if (_accounting == SolutionAccountingEnum::Rejected)
{
m_telemetry.farm.solutions.rejected++;
m_telemetry.farm.solutions.tstamp = std::chrono::steady_clock::now();
m_telemetry.miners.at(_minerIdx).solutions.rejected++;
m_telemetry.miners.at(_minerIdx).solutions.tstamp = std::chrono::steady_clock::now();
return;
}
if (_accounting == SolutionAccountingEnum::Failed)
{
m_telemetry.farm.solutions.failed++;
m_telemetry.farm.solutions.tstamp = std::chrono::steady_clock::now();
m_telemetry.miners.at(_minerIdx).solutions.failed++;
m_telemetry.miners.at(_minerIdx).solutions.tstamp = std::chrono::steady_clock::now();
return;
}
}
/**
* @brief Gets the solutions account for the whole farm
*/
SolutionAccountType Farm::getSolutions()
{
return m_telemetry.farm.solutions;
}
/**
* @brief Gets the solutions account for single miner
*/
SolutionAccountType Farm::getSolutions(unsigned _minerIdx)
{
try
{
return m_telemetry.miners.at(_minerIdx).solutions;
}
catch (const std::exception&)
{
return SolutionAccountType();
}
}
/**
* @brief Provides the description of segments each miner is working on
* @return a JsonObject
*/
Json::Value Farm::get_nonce_scrambler_json()
{
Json::Value jRes;
jRes["start_nonce"] = toHex(m_nonce_scrambler, HexPrefix::Add);
jRes["device_width"] = m_nonce_segment_with;
jRes["device_count"] = (uint64_t)m_miners.size();
return jRes;
}
void Farm::setTStartTStop(unsigned tstart, unsigned tstop)
{
m_Settings.tempStart = tstart;
m_Settings.tempStop = tstop;
}
void Farm::submitProof(Solution const& _s)
{
g_io_service.post(m_io_strand.wrap(boost::bind(&Farm::submitProofAsync, this, _s)));
}
void Farm::submitProofAsync(Solution const& _s)
{
if (!m_Settings.noEval)
{
Result r = EthashAux::eval(_s.work.epoch, _s.work.header, _s.nonce);
if (r.value > _s.work.boundary)
{
accountSolution(_s.midx, SolutionAccountingEnum::Failed);
cwarn << "GPU " << _s.midx
<< " gave incorrect result. Lower overclocking values if it happens frequently.";
return;
}
m_onSolutionFound(Solution{_s.nonce, r.mixHash, _s.work, _s.tstamp, _s.midx});
}
else
m_onSolutionFound(_s);
#ifdef DEV_BUILD
if (g_logOptions & LOG_SUBMIT)
cnote << "Submit time: "
<< std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - _s.tstamp)
.count()
<< " us.";
#endif
}
// Collects data about hashing and hardware status
void Farm::collectData(const boost::system::error_code& ec)
{
if (ec)
return;
// Reset hashrate (it will accumulate from miners)
float farm_hr = 0.0f;
// Process miners
for (auto const& miner : m_miners)
{
int minerIdx = miner->Index();
float hr = (miner->paused() ? 0.0f : miner->RetrieveHashRate());
farm_hr += hr;
m_telemetry.miners.at(minerIdx).hashrate = hr;
m_telemetry.miners.at(minerIdx).paused = miner->paused();
if (m_Settings.hwMon)
{
HwMonitorInfo hwInfo = miner->hwmonInfo();
unsigned int tempC = 0, fanpcnt = 0, powerW = 0;
if (hwInfo.deviceType == HwMonitorInfoType::NVIDIA && nvmlh)
{
int devIdx = hwInfo.deviceIndex;
if (devIdx == -1 && !hwInfo.devicePciId.empty())
{
if (map_nvml_handle.find(hwInfo.devicePciId) != map_nvml_handle.end())
{
devIdx = map_nvml_handle[hwInfo.devicePciId];
miner->setHwmonDeviceIndex(devIdx);
}
else
{
// This will prevent further tries to map
miner->setHwmonDeviceIndex(-2);
}
}
if (devIdx >= 0)
{
wrap_nvml_get_tempC(nvmlh, devIdx, &tempC);
wrap_nvml_get_fanpcnt(nvmlh, devIdx, &fanpcnt);
if (m_Settings.hwMon == 2)
wrap_nvml_get_power_usage(nvmlh, devIdx, &powerW);
}
}
else if (hwInfo.deviceType == HwMonitorInfoType::AMD)
{
#if defined(__linux)
if (sysfsh)
{
int devIdx = hwInfo.deviceIndex;
if (devIdx == -1 && !hwInfo.devicePciId.empty())
{
if (map_amdsysfs_handle.find(hwInfo.devicePciId) !=
map_amdsysfs_handle.end())
{
devIdx = map_amdsysfs_handle[hwInfo.devicePciId];
miner->setHwmonDeviceIndex(devIdx);
}
else
{
// This will prevent further tries to map
miner->setHwmonDeviceIndex(-2);
}
}
if (devIdx >= 0)
{
wrap_amdsysfs_get_tempC(sysfsh, devIdx, &tempC);
wrap_amdsysfs_get_fanpcnt(sysfsh, devIdx, &fanpcnt);
if (m_Settings.hwMon == 2)
wrap_amdsysfs_get_power_usage(sysfsh, devIdx, &powerW);
}
}
#else
if (adlh) // Windows only for AMD
{
int devIdx = hwInfo.deviceIndex;
if (devIdx == -1 && !hwInfo.devicePciId.empty())
{
if (map_adl_handle.find(hwInfo.devicePciId) != map_adl_handle.end())
{
devIdx = map_adl_handle[hwInfo.devicePciId];
miner->setHwmonDeviceIndex(devIdx);
}
else
{
// This will prevent further tries to map
miner->setHwmonDeviceIndex(-2);
}
}
if (devIdx >= 0)
{
wrap_adl_get_tempC(adlh, devIdx, &tempC);
wrap_adl_get_fanpcnt(adlh, devIdx, &fanpcnt);
if (m_Settings.hwMon == 2)
wrap_adl_get_power_usage(adlh, devIdx, &powerW);
}
}
#endif
}
// If temperature control has been enabled call
// check threshold
if (m_Settings.tempStop)
{
bool paused = miner->pauseTest(MinerPauseEnum::PauseDueToOverHeating);
if (!paused && (tempC >= m_Settings.tempStop))
miner->pause(MinerPauseEnum::PauseDueToOverHeating);
if (paused && (tempC <= m_Settings.tempStart))
miner->resume(MinerPauseEnum::PauseDueToOverHeating);
}
m_telemetry.miners.at(minerIdx).sensors.tempC = tempC;
m_telemetry.miners.at(minerIdx).sensors.fanP = fanpcnt;
m_telemetry.miners.at(minerIdx).sensors.powerW = powerW / ((double)1000.0);
}
m_telemetry.farm.hashrate = farm_hr;
miner->TriggerHashRateUpdate();
}
// Resubmit timer for another loop
m_collectTimer.expires_from_now(boost::posix_time::milliseconds(m_collectInterval));
m_collectTimer.async_wait(
m_io_strand.wrap(boost::bind(&Farm::collectData, this, boost::asio::placeholders::error)));
}
bool Farm::spawn_file_in_bin_dir(const char* filename, const std::vector<std::string>& args)
{
std::string fn = boost::dll::program_location().parent_path().string() +
"/" + // boost::filesystem::path::preferred_separator
filename;
try
{
if (!boost::filesystem::exists(fn))
return false;
/* anything in the file */
if (!boost::filesystem::file_size(fn))
return false;
#if defined(__linux)
struct stat sb;
if (stat(fn.c_str(), &sb) != 0)
return false;
/* just check if any exec flag is set.
still execution can fail (not the uid, not in the group, selinux, ...)
*/
if ((sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
return false;
#endif
/* spawn it (no wait,...) - fire and forget! */
boost::process::spawn(fn, args);
return true;
}
catch (...)
{
}
return false;
}
} // namespace eth
} // namespace dev