Skip to content

Commit ca58239

Browse files
authored
Merge pull request #46 from alephium/log-with-timestamp
Add timestamp to miner log
2 parents 6b7dabc + e159a20 commit ca58239

File tree

6 files changed

+73
-31
lines changed

6 files changed

+73
-31
lines changed

src/blake3/blake3-common.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ALEPHIUM_BLAKE3_COMMON_H
22
#define ALEPHIUM_BLAKE3_COMMON_H
33

4+
#include "../log.h"
5+
46
#define INLINE __forceinline__
57
#define TRY(x) \
68
{ \
@@ -9,7 +11,7 @@
911
cudaError_t err = cudaGetLastError(); \
1012
if (err != cudaSuccess) \
1113
{ \
12-
printf("cudaError %d (%s) calling '%s' (%s line %d)\n", err, cudaGetErrorString(err), #x, __FILE__, __LINE__); \
14+
LOGERR("cudaError %d (%s) calling '%s' (%s line %d)\n", err, cudaGetErrorString(err), #x, __FILE__, __LINE__); \
1315
exit(1); \
1416
} \
1517
}

src/log.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef ALEPHIUM_LOG_H
2+
#define ALEPHIUM_LOG_H
3+
4+
#include <stdio.h>
5+
#include <time.h>
6+
7+
#ifdef _WIN32
8+
#define localtime_r(a, b) localtime_s(b, a)
9+
#endif
10+
11+
#define LOG_TIMESTAMP(stream) \
12+
{ \
13+
time_t now = time(NULL); \
14+
struct tm time; \
15+
localtime_r(&now, &time); \
16+
char str[24]; \
17+
strftime(str, 24, "%Y-%m-%d %H:%M:%S", &time); \
18+
fprintf(stream, "%s | ", str); \
19+
}
20+
21+
#define LOG_WITH_TS(stream, format, ...) \
22+
{ \
23+
LOG_TIMESTAMP(stream); \
24+
fprintf(stream, format, ##__VA_ARGS__); \
25+
}
26+
27+
#define LOG_WITHOUT_TS(format, ...) fprintf(stdout, format, ##__VA_ARGS__);
28+
29+
#define LOG(format, ...) LOG_WITH_TS(stdout, format, ##__VA_ARGS__);
30+
31+
#define LOGERR(format, ...) LOG_WITH_TS(stderr, format, ##__VA_ARGS__);
32+
33+
#endif // ALEPHIUM_LOG_H

src/main.cu

+23-20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "template.h"
1717
#include "mining.h"
1818
#include "getopt.h"
19-
19+
#include "log.h"
2020

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

@@ -51,8 +51,7 @@ void on_write_end(uv_write_t *req, int status)
5151
{
5252
if (status < 0)
5353
{
54-
fprintf(stderr, "error on_write_end %d", status);
55-
exit(1);
54+
LOGERR("error on_write_end %d\n", status);
5655
}
5756
free(req);
5857
}
@@ -85,7 +84,7 @@ void mine(mining_worker_t *worker)
8584
int32_t to_mine_index = next_chain_to_mine();
8685
if (to_mine_index == -1)
8786
{
88-
printf("waiting for new tasks\n");
87+
LOG("waiting for new tasks\n");
8988
worker->timer.data = worker;
9089
uv_timer_start(&worker->timer, mine_with_timer, 500, 0);
9190
} else {
@@ -95,7 +94,7 @@ void mine(mining_worker_t *worker)
9594
start_worker_mining(worker);
9695

9796
duration_t elapsed = Time::now() - start;
98-
// printf("=== mining time: %fs\n", elapsed.count());
97+
// LOG("=== mining time: %fs\n", elapsed.count());
9998
}
10099
}
101100

@@ -191,12 +190,12 @@ void log_hashrate(uv_timer_t *timer)
191190
if (current_time > start_time)
192191
{
193192
duration_t eplased = current_time - start_time;
194-
printf("hashrate: %.0f MH/s ", total_mining_count.load() / eplased.count() / 1000000);
193+
LOG("hashrate: %.0f MH/s ", total_mining_count.load() / eplased.count() / 1000000);
195194
for (int i = 0; i < gpu_count; i++)
196195
{
197-
printf("gpu%d: %.0f MH/s ", i, device_mining_count[i].load() / eplased.count() / 1000000);
196+
LOG_WITHOUT_TS("gpu%d: %.0f MH/s ", i, device_mining_count[i].load() / eplased.count() / 1000000);
198197
}
199-
printf("solutions: %u\n", found_solutions.load(std::memory_order_relaxed));
198+
LOG_WITHOUT_TS("solutions: %u\n", found_solutions.load(std::memory_order_relaxed));
200199
}
201200
}
202201

@@ -250,7 +249,7 @@ void on_read(uv_stream_t *server, ssize_t nread, const uv_buf_t *buf)
250249
{
251250
if (nread < 0)
252251
{
253-
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);
252+
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);
254253
uv_timer_start(&reconnect_timer, try_to_reconnect, 5000, 0);
255254
return;
256255
}
@@ -274,7 +273,7 @@ void on_read(uv_stream_t *server, ssize_t nread, const uv_buf_t *buf)
274273
break;
275274

276275
case SUBMIT_RESULT:
277-
printf("submitted: %d -> %d: %d \n", message->submit_result->from_group, message->submit_result->to_group, message->submit_result->status);
276+
LOG("submitted: %d -> %d: %d \n", message->submit_result->from_group, message->submit_result->to_group, message->submit_result->status);
278277
break;
279278
}
280279
free_server_message_except_jobs(message);
@@ -288,11 +287,11 @@ void on_connect(uv_connect_t *req, int status)
288287
{
289288
if (status < 0)
290289
{
291-
fprintf(stderr, "connection error %d: might be that the full node is not reachable, try to reconnect\n", status);
290+
LOGERR("connection error %d: might be that the full node is not reachable, try to reconnect\n", status);
292291
uv_timer_start(&reconnect_timer, try_to_reconnect, 5000, 0);
293292
return;
294293
}
295-
printf("the server is connected %d %p\n", status, req);
294+
LOG("the server is connected %d %p\n", status, req);
296295

297296
tcp = req->handle;
298297
uv_read_start(req->handle, alloc_buffer, on_read);
@@ -325,7 +324,7 @@ int hostname_to_ip(char *ip_address, char *hostname)
325324
int res = getaddrinfo(hostname, NULL, &hints, &servinfo);
326325
if (res != 0)
327326
{
328-
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
327+
LOGERR("getaddrinfo: %s\n", gai_strerror(res));
329328
return 1;
330329
}
331330

@@ -349,19 +348,19 @@ int main(int argc, char **argv)
349348
int rc = WSAStartup(MAKEWORD(2, 2), &wsa);
350349
if (rc != 0)
351350
{
352-
printf("Initialize winsock failed: %d", rc);
351+
LOGERR("Initialize winsock failed: %d\n", rc);
353352
exit(1);
354353
}
355354
#endif
356355

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

359358
int gpu_count = 0;
360359
cudaGetDeviceCount(&gpu_count);
361-
printf("GPU count: %d\n", gpu_count);
360+
LOG("GPU count: %d\n", gpu_count);
362361
for (int i = 0; i < gpu_count; i++)
363362
{
364-
printf("GPU #%d has #%d cores\n", i, get_device_cores(i));
363+
LOG("GPU #%d has #%d cores\n", i, get_device_cores(i));
365364
use_device[i] = true;
366365
}
367366

@@ -396,18 +395,22 @@ int main(int argc, char **argv)
396395
{
397396
int device = atoi(argv[optind]);
398397
if (device < 0 || device >= gpu_count) {
399-
printf("Invalid gpu index %d\n", device);
398+
LOGERR("Invalid gpu index %d\n", device);
400399
exit(1);
401400
}
402401
use_device[device] = true;
403402
}
404403
break;
405404
default:
406-
printf("Invalid command %c\n", command);
405+
LOGERR("Invalid command %c\n", command);
407406
exit(1);
408407
}
409408
}
410-
printf("will connect to broker @%s:%d\n", broker_ip, port);
409+
LOG("will connect to broker @%s:%d\n", broker_ip, port);
410+
411+
#ifdef __linux__
412+
signal(SIGPIPE, SIG_IGN);
413+
#endif
411414

412415
mining_workers_init(gpu_count);
413416
setup_gpu_worker_count(gpu_count, gpu_count * parallel_mining_works_per_gpu);

src/messages.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <string.h>
99
#include <stdlib.h>
1010

11+
#include "log.h"
12+
1113
typedef struct blob_t {
1214
uint8_t *blob;
1315
ssize_t len;
@@ -40,7 +42,7 @@ char *bytes_to_hex(uint8_t *bytes, ssize_t len)
4042
void print_hex(const char* prefix, uint8_t *data, ssize_t nread)
4143
{
4244
char *hex_string = bytes_to_hex(data, nread);
43-
printf("%s: %s\n", prefix, hex_string);
45+
LOG("%s: %s\n", prefix, hex_string);
4446
free(hex_string);
4547
}
4648

@@ -180,7 +182,7 @@ bool extract_bool(uint8_t **bytes)
180182
case 1:
181183
return true;
182184
default:
183-
fprintf(stderr, "Invaid bool value");
185+
LOGERR("Invaid bool value\n");
184186
exit(1);
185187
}
186188
}
@@ -204,15 +206,15 @@ void extract_blob(uint8_t **bytes, blob_t *blob)
204206
memcpy(blob->blob, *bytes, size);
205207
*bytes = *bytes + size;
206208

207-
// printf("blob: %ld\n", blob->len);
208-
// printf("%s\n", bytes_to_hex(blob->blob, blob->len));
209+
// LOG("blob: %ld\n", blob->len);
210+
// LOG("%s\n", bytes_to_hex(blob->blob, blob->len));
209211
}
210212

211213
void extract_job(uint8_t **bytes, job_t *job)
212214
{
213215
job->from_group = extract_size(bytes);
214216
job->to_group = extract_size(bytes);
215-
// printf("group: %d, %d\n", job->from_group, job->to_group);
217+
// LOG("group: %d, %d\n", job->from_group, job->to_group);
216218
extract_blob(bytes, &job->header_blob);
217219
extract_blob(bytes, &job->txs_blob);
218220
extract_blob(bytes, &job->target);
@@ -222,7 +224,7 @@ void extract_jobs(uint8_t **bytes, jobs_t *jobs)
222224
{
223225
ssize_t jobs_size = extract_size(bytes);
224226

225-
// printf("jobs: %ld\n", jobs_size);
227+
// LOG("jobs: %ld\n", jobs_size);
226228

227229
jobs->len = jobs_size;
228230
jobs->jobs = (job_t **)malloc(jobs_size * sizeof(job_t*));
@@ -265,7 +267,7 @@ server_message_t *decode_server_message(blob_t *blob)
265267
server_message->jobs = (jobs_t *)malloc(sizeof(jobs_t));
266268
extract_jobs(&pos, server_message->jobs);
267269

268-
// printf("%p, %p, %p\n", bytes, pos, bytes + len);
270+
// LOG("%p, %p, %p\n", bytes, pos, bytes + len);
269271
break;
270272

271273
case 1:
@@ -275,7 +277,7 @@ server_message_t *decode_server_message(blob_t *blob)
275277
break;
276278

277279
default:
278-
fprintf(stderr, "Invalid server message kind");
280+
LOGERR("Invalid server message kind\n");
279281
exit(1);
280282
}
281283

src/mining.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define ALEPHIUM_MINING_H
33

44
#include "blake3.cu"
5+
#include "log.h"
56

67
//#define SHOW_MINING_TIME 1
78

@@ -37,7 +38,7 @@ void start_worker_mining(mining_worker_t *worker)
3738
TRY( cudaEventElapsedTime(&time, startEvent, stopEvent) );
3839
TRY( cudaEventDestroy(&startEvent) );
3940
TRY( cudaEventDestroy(&stopEvent) );
40-
printf(" === mining time: %f\n", time);
41+
LOG(" === mining time: %f\n", time);
4142
#endif
4243
}
4344

src/worker.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "blake3.cu"
1414
#include "uv.h"
1515
#include "template.h"
16+
#include "log.h"
1617

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

101102
TRY(cudaMallocHost(hasher_ptr(self, true), hasher_len(self)));

0 commit comments

Comments
 (0)