Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit f4b246b

Browse files
committedSep 14, 2018
Use globals for global objects
The io_service, farm, and pool manager instances are frequently used global objects. There is a single instance of each. Avoid a lot of parameter passing by accessing these objects via global reference or pointer.
1 parent bf31e16 commit f4b246b

File tree

10 files changed

+154
-160
lines changed

10 files changed

+154
-160
lines changed
 

‎ethminer/main.cpp

+37-35
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ using namespace std;
4444
using namespace dev;
4545
using namespace dev::eth;
4646

47+
boost::asio::io_service g_io_service; // The IO service itself
48+
dev::eth::Farm* g_farm;
49+
dev::eth::PoolManager* g_mgr;
50+
4751
struct MiningChannel : public LogChannel
4852
{
4953
static const char* name() { return EthGreen " m"; }
@@ -70,7 +74,7 @@ class MinerCLI
7074
Stratum
7175
};
7276

73-
MinerCLI() : m_io_work(m_io_service), m_io_work_timer(m_io_service), m_io_strand(m_io_service)
77+
MinerCLI() : m_io_work_timer(g_io_service), m_io_strand(g_io_service)
7478
{
7579
// Post first deadline timer to give io_service
7680
// initial work
@@ -79,10 +83,10 @@ class MinerCLI
7983
boost::bind(&MinerCLI::io_work_timer_handler, this, boost::asio::placeholders::error)));
8084

8185
// Start io_service in it's own thread
82-
m_io_thread = std::thread{boost::bind(&boost::asio::io_service::run, &m_io_service)};
86+
m_io_thread = std::thread{boost::bind(&boost::asio::io_service::run, &g_io_service)};
8387

8488
// Io service is now live and running
85-
// All components using io_service should post to reference of m_io_service
89+
// All components using io_service should post to reference of g_io_service
8690
// and should not start/stop or even join threads (which heavily time consuming)
8791
}
8892

@@ -101,7 +105,7 @@ class MinerCLI
101105
void stop_io_service()
102106
{
103107
// Here we stop all io_service's related activities
104-
m_io_service.stop();
108+
g_io_service.stop();
105109
m_io_thread.join();
106110
}
107111

@@ -772,7 +776,7 @@ class MinerCLI
772776
genesis.setNumber(m_benchmarkBlock);
773777
genesis.setDifficulty(u256(1) << 64);
774778

775-
Farm f(m_io_service, m_show_hwmonitors, m_show_power);
779+
g_farm = new Farm(m_show_hwmonitors, m_show_power);
776780
map<string, Farm::SealerDescriptor> sealers;
777781
#if ETH_ETHASHCL
778782
sealers["opencl"] = Farm::SealerDescriptor{&CLMiner::instances,
@@ -782,10 +786,13 @@ class MinerCLI
782786
sealers["cuda"] = Farm::SealerDescriptor{&CUDAMiner::instances,
783787
[](FarmFace& _farm, unsigned _index) { return new CUDAMiner(_farm, _index); }};
784788
#endif
785-
f.setSealers(sealers);
786-
f.onSolutionFound([&](Solution, unsigned const& miner_index) { (void)miner_index; return false; });
789+
g_farm->setSealers(sealers);
790+
g_farm->onSolutionFound([&](Solution, unsigned const& miner_index) {
791+
(void)miner_index;
792+
return false;
793+
});
787794

788-
f.setTStartTStop(m_tstart, m_tstop);
795+
g_farm->setTStartTStop(m_tstart, m_tstop);
789796

790797
string platformInfo = _m == MinerType::CL ? "CL" : "CUDA";
791798
cout << "Benchmarking on platform: " << platformInfo << endl;
@@ -794,9 +801,9 @@ class MinerCLI
794801
// genesis.prep();
795802

796803
if (_m == MinerType::CL)
797-
f.start("opencl", false);
804+
g_farm->start("opencl", false);
798805
else if (_m == MinerType::CUDA)
799-
f.start("cuda", false);
806+
g_farm->start("cuda", false);
800807

801808
WorkPackage current = WorkPackage(genesis);
802809

@@ -809,14 +816,14 @@ class MinerCLI
809816
{
810817
current.header = h256::random();
811818
current.boundary = genesis.boundary();
812-
f.setWork(current);
819+
g_farm->setWork(current);
813820
if (!i)
814821
cout << "Warming up..." << endl;
815822
else
816823
cout << "Trial " << i << "... " << flush << endl;
817824
this_thread::sleep_for(chrono::seconds(i ? _trialDuration : _warmupDuration));
818825

819-
auto mp = f.miningProgress();
826+
auto mp = g_farm->miningProgress();
820827
if (!i)
821828
continue;
822829
auto rate = uint64_t(mp.hashRate);
@@ -857,8 +864,7 @@ class MinerCLI
857864

858865
if (m_mode == OperationMode::Stratum)
859866
{
860-
client = new EthStratumClient(
861-
m_io_service, m_worktimeout, m_responsetimeout, m_report_hashrate);
867+
client = new EthStratumClient(m_worktimeout, m_responsetimeout, m_report_hashrate);
862868
}
863869
else if (m_mode == OperationMode::Farm)
864870
{
@@ -883,46 +889,45 @@ class MinerCLI
883889
}
884890

885891
// sealers, m_minerType
886-
Farm f(m_io_service, m_show_hwmonitors, m_show_power);
887-
f.setSealers(sealers);
892+
g_farm = new Farm(m_show_hwmonitors, m_show_power);
893+
g_farm->setSealers(sealers);
888894

889-
PoolManager mgr(m_io_service, client, f, m_minerType, m_maxFarmRetries, m_failovertimeout);
895+
g_mgr = new PoolManager(client, m_minerType, m_maxFarmRetries, m_failovertimeout);
890896

891-
f.setTStartTStop(m_tstart, m_tstop);
897+
g_farm->setTStartTStop(m_tstart, m_tstop);
892898

893899
// If we are in simulation mode we add a fake connection
894900
if (m_mode == OperationMode::Simulation)
895901
{
896902
URI con(URI("http://-:0"));
897-
mgr.clearConnections();
898-
mgr.addConnection(con);
903+
g_mgr->clearConnections();
904+
g_mgr->addConnection(con);
899905
}
900906
else
901907
{
902908
for (auto conn : m_endpoints)
903909
{
904910
cnote << "Configured pool " << conn.Host() + ":" + to_string(conn.Port());
905-
mgr.addConnection(conn);
911+
g_mgr->addConnection(conn);
906912
}
907913
}
908914

909915
#if API_CORE
910916

911-
ApiServer api(m_io_service, m_api_address, abs(m_api_port), (m_api_port < 0) ? true : false,
912-
m_api_password, f, mgr);
917+
ApiServer api(m_api_address, m_api_port, m_api_password);
913918
api.start();
914919

915-
http_server.run(m_http_address, m_http_port, &f, &mgr, m_show_hwmonitors, m_show_power);
920+
http_server.run(m_http_address, m_http_port, m_show_hwmonitors, m_show_power);
916921

917922
#endif
918923

919924
// Start PoolManager
920-
mgr.start();
925+
g_mgr->start();
921926

922927
unsigned interval = m_displayInterval;
923928

924929
// Run CLI in loop
925-
while (g_running && mgr.isRunning())
930+
while (g_running && g_mgr->isRunning())
926931
{
927932
// Wait at the beginning of the loop to give some time
928933
// services to start properly. Otherwise we get a "not-connected"
@@ -933,23 +938,23 @@ class MinerCLI
933938
interval -= 2;
934939
continue;
935940
}
936-
if (mgr.isConnected())
941+
if (g_mgr->isConnected())
937942
{
938-
auto solstats = f.getSolutionStats();
943+
auto solstats = g_farm->getSolutionStats();
939944
{
940945
ostringstream os;
941-
os << f.miningProgress() << ' ';
946+
os << g_farm->miningProgress() << ' ';
942947
if (!(g_logOptions & LOG_PER_GPU))
943948
os << solstats << ' ';
944-
os << f.farmLaunchedFormatted();
949+
os << g_farm->farmLaunchedFormatted();
945950
minelog << os.str();
946951
}
947952

948953
if (g_logOptions & LOG_PER_GPU)
949954
{
950955
ostringstream statdetails;
951956
statdetails << "Solutions " << solstats << ' ';
952-
for (size_t i = 0; i < f.getMiners().size(); i++)
957+
for (size_t i = 0; i < g_farm->getMiners().size(); i++)
953958
{
954959
if (i) statdetails << " ";
955960
statdetails << "gpu" << i << ":" << solstats.getString(i);
@@ -975,7 +980,7 @@ class MinerCLI
975980

976981
#endif
977982

978-
mgr.stop();
983+
g_mgr->stop();
979984
stop_io_service();
980985

981986
cnote << "Terminated!";
@@ -987,9 +992,6 @@ class MinerCLI
987992

988993
/// Global boost's io_service
989994
std::thread m_io_thread; // The IO service thread
990-
boost::asio::io_service m_io_service; // The IO service itself
991-
boost::asio::io_service::work m_io_work; // The IO work which prevents io_service.run() to
992-
// return on no work thus terminating thread
993995
boost::asio::deadline_timer m_io_work_timer; // A dummy timer to keep io_service with something
994996
// to do and prevent io shutdown
995997
boost::asio::io_service::strand m_io_strand; // A strand to serialize posts in multithreaded

0 commit comments

Comments
 (0)
This repository has been archived.