Skip to content

Commit

Permalink
Merge pull request #46 from alephium/log-with-timestamp
Browse files Browse the repository at this point in the history
Add timestamp to miner log
  • Loading branch information
polarker authored Dec 30, 2021
2 parents 6b7dabc + e159a20 commit ca58239
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
4 changes: 3 additions & 1 deletion src/blake3/blake3-common.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef ALEPHIUM_BLAKE3_COMMON_H
#define ALEPHIUM_BLAKE3_COMMON_H

#include "../log.h"

#define INLINE __forceinline__
#define TRY(x) \
{ \
Expand All @@ -9,7 +11,7 @@
cudaError_t err = cudaGetLastError(); \
if (err != cudaSuccess) \
{ \
printf("cudaError %d (%s) calling '%s' (%s line %d)\n", err, cudaGetErrorString(err), #x, __FILE__, __LINE__); \
LOGERR("cudaError %d (%s) calling '%s' (%s line %d)\n", err, cudaGetErrorString(err), #x, __FILE__, __LINE__); \
exit(1); \
} \
}
Expand Down
33 changes: 33 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef ALEPHIUM_LOG_H
#define ALEPHIUM_LOG_H

#include <stdio.h>
#include <time.h>

#ifdef _WIN32
#define localtime_r(a, b) localtime_s(b, a)
#endif

#define LOG_TIMESTAMP(stream) \
{ \
time_t now = time(NULL); \
struct tm time; \
localtime_r(&now, &time); \
char str[24]; \
strftime(str, 24, "%Y-%m-%d %H:%M:%S", &time); \
fprintf(stream, "%s | ", str); \
}

#define LOG_WITH_TS(stream, format, ...) \
{ \
LOG_TIMESTAMP(stream); \
fprintf(stream, format, ##__VA_ARGS__); \
}

#define LOG_WITHOUT_TS(format, ...) fprintf(stdout, format, ##__VA_ARGS__);

#define LOG(format, ...) LOG_WITH_TS(stdout, format, ##__VA_ARGS__);

#define LOGERR(format, ...) LOG_WITH_TS(stderr, format, ##__VA_ARGS__);

#endif // ALEPHIUM_LOG_H
43 changes: 23 additions & 20 deletions src/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "template.h"
#include "mining.h"
#include "getopt.h"

#include "log.h"

std::atomic<uint32_t> found_solutions{0};

Expand Down Expand Up @@ -51,8 +51,7 @@ void on_write_end(uv_write_t *req, int status)
{
if (status < 0)
{
fprintf(stderr, "error on_write_end %d", status);
exit(1);
LOGERR("error on_write_end %d\n", status);
}
free(req);
}
Expand Down Expand Up @@ -85,7 +84,7 @@ void mine(mining_worker_t *worker)
int32_t to_mine_index = next_chain_to_mine();
if (to_mine_index == -1)
{
printf("waiting for new tasks\n");
LOG("waiting for new tasks\n");
worker->timer.data = worker;
uv_timer_start(&worker->timer, mine_with_timer, 500, 0);
} else {
Expand All @@ -95,7 +94,7 @@ void mine(mining_worker_t *worker)
start_worker_mining(worker);

duration_t elapsed = Time::now() - start;
// printf("=== mining time: %fs\n", elapsed.count());
// LOG("=== mining time: %fs\n", elapsed.count());
}
}

Expand Down Expand Up @@ -191,12 +190,12 @@ void log_hashrate(uv_timer_t *timer)
if (current_time > start_time)
{
duration_t eplased = current_time - start_time;
printf("hashrate: %.0f MH/s ", total_mining_count.load() / eplased.count() / 1000000);
LOG("hashrate: %.0f MH/s ", total_mining_count.load() / eplased.count() / 1000000);
for (int i = 0; i < gpu_count; i++)
{
printf("gpu%d: %.0f MH/s ", i, device_mining_count[i].load() / eplased.count() / 1000000);
LOG_WITHOUT_TS("gpu%d: %.0f MH/s ", i, device_mining_count[i].load() / eplased.count() / 1000000);
}
printf("solutions: %u\n", found_solutions.load(std::memory_order_relaxed));
LOG_WITHOUT_TS("solutions: %u\n", found_solutions.load(std::memory_order_relaxed));
}
}

Expand Down Expand Up @@ -250,7 +249,7 @@ void on_read(uv_stream_t *server, ssize_t nread, const uv_buf_t *buf)
{
if (nread < 0)
{
fprintf(stderr, "error on_read %ld: might be that the full node is not synced, or miner wallets are not setup, try to reconnect\n", nread);
LOGERR("error on_read %ld: might be that the full node is not synced, or miner wallets are not setup, try to reconnect\n", nread);
uv_timer_start(&reconnect_timer, try_to_reconnect, 5000, 0);
return;
}
Expand All @@ -274,7 +273,7 @@ void on_read(uv_stream_t *server, ssize_t nread, const uv_buf_t *buf)
break;

case SUBMIT_RESULT:
printf("submitted: %d -> %d: %d \n", message->submit_result->from_group, message->submit_result->to_group, message->submit_result->status);
LOG("submitted: %d -> %d: %d \n", message->submit_result->from_group, message->submit_result->to_group, message->submit_result->status);
break;
}
free_server_message_except_jobs(message);
Expand All @@ -288,11 +287,11 @@ void on_connect(uv_connect_t *req, int status)
{
if (status < 0)
{
fprintf(stderr, "connection error %d: might be that the full node is not reachable, try to reconnect\n", status);
LOGERR("connection error %d: might be that the full node is not reachable, try to reconnect\n", status);
uv_timer_start(&reconnect_timer, try_to_reconnect, 5000, 0);
return;
}
printf("the server is connected %d %p\n", status, req);
LOG("the server is connected %d %p\n", status, req);

tcp = req->handle;
uv_read_start(req->handle, alloc_buffer, on_read);
Expand Down Expand Up @@ -325,7 +324,7 @@ int hostname_to_ip(char *ip_address, char *hostname)
int res = getaddrinfo(hostname, NULL, &hints, &servinfo);
if (res != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
LOGERR("getaddrinfo: %s\n", gai_strerror(res));
return 1;
}

Expand All @@ -349,19 +348,19 @@ int main(int argc, char **argv)
int rc = WSAStartup(MAKEWORD(2, 2), &wsa);
if (rc != 0)
{
printf("Initialize winsock failed: %d", rc);
LOGERR("Initialize winsock failed: %d\n", rc);
exit(1);
}
#endif

printf("Running gpu-miner version : %s\n", MINER_VERSION);
LOG("Running gpu-miner version : %s\n", MINER_VERSION);

int gpu_count = 0;
cudaGetDeviceCount(&gpu_count);
printf("GPU count: %d\n", gpu_count);
LOG("GPU count: %d\n", gpu_count);
for (int i = 0; i < gpu_count; i++)
{
printf("GPU #%d has #%d cores\n", i, get_device_cores(i));
LOG("GPU #%d has #%d cores\n", i, get_device_cores(i));
use_device[i] = true;
}

Expand Down Expand Up @@ -396,18 +395,22 @@ int main(int argc, char **argv)
{
int device = atoi(argv[optind]);
if (device < 0 || device >= gpu_count) {
printf("Invalid gpu index %d\n", device);
LOGERR("Invalid gpu index %d\n", device);
exit(1);
}
use_device[device] = true;
}
break;
default:
printf("Invalid command %c\n", command);
LOGERR("Invalid command %c\n", command);
exit(1);
}
}
printf("will connect to broker @%s:%d\n", broker_ip, port);
LOG("will connect to broker @%s:%d\n", broker_ip, port);

#ifdef __linux__
signal(SIGPIPE, SIG_IGN);
#endif

mining_workers_init(gpu_count);
setup_gpu_worker_count(gpu_count, gpu_count * parallel_mining_works_per_gpu);
Expand Down
18 changes: 10 additions & 8 deletions src/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string.h>
#include <stdlib.h>

#include "log.h"

typedef struct blob_t {
uint8_t *blob;
ssize_t len;
Expand Down Expand Up @@ -40,7 +42,7 @@ char *bytes_to_hex(uint8_t *bytes, ssize_t len)
void print_hex(const char* prefix, uint8_t *data, ssize_t nread)
{
char *hex_string = bytes_to_hex(data, nread);
printf("%s: %s\n", prefix, hex_string);
LOG("%s: %s\n", prefix, hex_string);
free(hex_string);
}

Expand Down Expand Up @@ -180,7 +182,7 @@ bool extract_bool(uint8_t **bytes)
case 1:
return true;
default:
fprintf(stderr, "Invaid bool value");
LOGERR("Invaid bool value\n");
exit(1);
}
}
Expand All @@ -204,15 +206,15 @@ void extract_blob(uint8_t **bytes, blob_t *blob)
memcpy(blob->blob, *bytes, size);
*bytes = *bytes + size;

// printf("blob: %ld\n", blob->len);
// printf("%s\n", bytes_to_hex(blob->blob, blob->len));
// LOG("blob: %ld\n", blob->len);
// LOG("%s\n", bytes_to_hex(blob->blob, blob->len));
}

void extract_job(uint8_t **bytes, job_t *job)
{
job->from_group = extract_size(bytes);
job->to_group = extract_size(bytes);
// printf("group: %d, %d\n", job->from_group, job->to_group);
// LOG("group: %d, %d\n", job->from_group, job->to_group);
extract_blob(bytes, &job->header_blob);
extract_blob(bytes, &job->txs_blob);
extract_blob(bytes, &job->target);
Expand All @@ -222,7 +224,7 @@ void extract_jobs(uint8_t **bytes, jobs_t *jobs)
{
ssize_t jobs_size = extract_size(bytes);

// printf("jobs: %ld\n", jobs_size);
// LOG("jobs: %ld\n", jobs_size);

jobs->len = jobs_size;
jobs->jobs = (job_t **)malloc(jobs_size * sizeof(job_t*));
Expand Down Expand Up @@ -265,7 +267,7 @@ server_message_t *decode_server_message(blob_t *blob)
server_message->jobs = (jobs_t *)malloc(sizeof(jobs_t));
extract_jobs(&pos, server_message->jobs);

// printf("%p, %p, %p\n", bytes, pos, bytes + len);
// LOG("%p, %p, %p\n", bytes, pos, bytes + len);
break;

case 1:
Expand All @@ -275,7 +277,7 @@ server_message_t *decode_server_message(blob_t *blob)
break;

default:
fprintf(stderr, "Invalid server message kind");
LOGERR("Invalid server message kind\n");
exit(1);
}

Expand Down
3 changes: 2 additions & 1 deletion src/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ALEPHIUM_MINING_H

#include "blake3.cu"
#include "log.h"

//#define SHOW_MINING_TIME 1

Expand Down Expand Up @@ -37,7 +38,7 @@ void start_worker_mining(mining_worker_t *worker)
TRY( cudaEventElapsedTime(&time, startEvent, stopEvent) );
TRY( cudaEventDestroy(&startEvent) );
TRY( cudaEventDestroy(&stopEvent) );
printf(" === mining time: %f\n", time);
LOG(" === mining time: %f\n", time);
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "blake3.cu"
#include "uv.h"
#include "template.h"
#include "log.h"

typedef struct mining_worker_t {
uint32_t id;
Expand Down Expand Up @@ -95,7 +96,7 @@ void mining_worker_init(mining_worker_t *self, uint32_t id, int device_id) {
cudaSetDevice(device_id);
TRY(cudaStreamCreate(&(self->stream)));
config_cuda(device_id, &self->grid_size, &self->block_size, &self->is_inline_miner);
printf("Worker %d: device id %d, grid size %d, block size %d. Using %s kernel\n", self->id, self->device_id,
LOG("Worker %d: device id %d, grid size %d, block size %d. Using %s kernel\n", self->id, self->device_id,
self->grid_size, self->block_size, self->is_inline_miner ? "inline" : "reference");

TRY(cudaMallocHost(hasher_ptr(self, true), hasher_len(self)));
Expand Down

0 comments on commit ca58239

Please sign in to comment.