Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clHeaders/CL/cl.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma GCC diagnostic ignored "-Wignored-attributes"
/*******************************************************************************
* Copyright (c) 2008-2015 The Khronos Group Inc.
*
Expand Down
94 changes: 92 additions & 2 deletions clHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "clHost.h"
#include "./kernels/equihash_150_5_inc.h"
#include <thread>
#include <sys/socket.h>

namespace beamMiner {

Expand Down Expand Up @@ -86,6 +88,7 @@ void clHost::loadAndCompileKernel(cl::Device &device, uint32_t pl, bool use3G) {
paused.push_back(true);
is3G.push_back(use3G);
solutionCnt.push_back(0);
lastHashrates.push_back(0);

// Create the kernels
vector<cl::Kernel> newKernels;
Expand Down Expand Up @@ -446,7 +449,7 @@ void clHost::startMining() {
solutionCnt[i] = 0;
totalSols += sol;
cout << fixed << setprecision(2) << (double) sol / 15.0 << " sol/s ";

lastHashrates[i] = sol;
}

if (devices.size() > 1) cout << "| Total: " << setprecision(2) << (double) totalSols / 15.0 << " sol/s ";
Expand All @@ -470,7 +473,94 @@ void clHost::startMining() {
}


} // end namespace
std::string clHost::getAPIResponse() {
std::ostringstream response;
response << "{";

uint32_t totalHashrate = 0;
response << "\"devices\":[";
for (int i = 0; i < this->devices.size(); i++) {
uint32_t h = lastHashrates[i];
totalHashrate += h;
if (i != 0) {
response << ",";
}
response << "{";
response << "\"hashrate\":";
response << std::fixed << std::setprecision(2) << (double)h / 15.0;
response << "}";
}
response << "]";

response << ",";
response << "\"totalHashrate\":";
response << std::fixed << std::setprecision(2) << (double)totalHashrate / 15.0;

response << "}";

return response.str();
}

void apiServer(clHost* self) {
while (true) {

int serverSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocketDescriptor == -1) {
perror("creating server socket failed");
return;
}

int enable = 1;
if (setsockopt(serverSocketDescriptor, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == -1) {
perror("unable to set SO_REUSEADDR on API socket");
return;
}
if (setsockopt(serverSocketDescriptor, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) == -1) {
perror("unable to set SO_REUSEPORT on API socket");
return;
}

struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(4030);
if (bind(serverSocketDescriptor, (struct sockaddr*) &serverAddress, sizeof(serverAddress)) != 0) {
perror("binding server socket failed");
return;
}

if (listen(serverSocketDescriptor, 5) != 0) {
perror("listening on server socket failed");
return;
}

std::cout << "API listener started on port 4030" << std::endl;

while (true) {
struct sockaddr_in clientAddress;
socklen_t addressLength = sizeof(clientAddress);
int incomingSocketDescriptor = accept(serverSocketDescriptor, (struct sockaddr*) &clientAddress, &addressLength);
if (incomingSocketDescriptor == -1) {
perror("accept on API socket failed");
}

std::string response = self->getAPIResponse();

if (send(incomingSocketDescriptor, response.c_str(), response.length(), 0) == -1) {
perror("sending API response failed");
}

if (close(incomingSocketDescriptor) == -1) {
perror("closing client socket failed");
}
}
}
}

void clHost::startAPI() {
thread t1(apiServer, this);
t1.detach();
}

} // end namespace
4 changes: 4 additions & 0 deletions clHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class clHost {

// Statistics
vector<int> solutionCnt;
vector<uint32_t> lastHashrates;

// To check if a mining thread stoped and we must resume it
vector<bool> paused;
Expand All @@ -62,6 +63,9 @@ class clHost {
void setup(beamStratum*, vector<int32_t>, bool, bool);
void startMining();
void callbackFunc(cl_int, void*);

std::string getAPIResponse();
void startAPI();
};

}
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ int main(int argc, char* argv[]) {
}

cout << endl;
myClHost.startAPI();
cout << "Start mining:" << endl;
cout << "=============" << endl;
myClHost.startMining();
Expand Down