-
Notifications
You must be signed in to change notification settings - Fork 806
/
miner.c
13619 lines (11854 loc) · 351 KB
/
miner.c
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011-2014 Con Kolivas
* Copyright 2011-2014 Luke Dashjr
* Copyright 2014 Nate Woolls
* Copyright 2012-2013 Andrew Smith
* Copyright 2010 Jeff Garzik
*
* This program 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. See COPYING for more details.
*/
#include "config.h"
#ifdef HAVE_CURSES
#ifdef USE_UNICODE
#define PDC_WIDE
#endif
// Must be before stdbool, since pdcurses typedefs bool :/
#include <curses.h>
#endif
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <stdarg.h>
#include <assert.h>
#include <signal.h>
#include <wctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifndef WIN32
#include <sys/resource.h>
#include <sys/socket.h>
#if defined(HAVE_LIBUDEV) && defined(HAVE_SYS_EPOLL_H)
#include <libudev.h>
#include <sys/epoll.h>
#define HAVE_BFG_HOTPLUG
#endif
#else
#include <winsock2.h>
#include <windows.h>
#include <dbt.h>
#define HAVE_BFG_HOTPLUG
#endif
#include <ccan/opt/opt.h>
#include <jansson.h>
#include <curl/curl.h>
#include <libgen.h>
#include <sha2.h>
#include <utlist.h>
#include <blkmaker.h>
#include <blkmaker_jansson.h>
#include <blktemplate.h>
#include <libbase58.h>
#include "compat.h"
#include "deviceapi.h"
#include "logging.h"
#include "miner.h"
#include "adl.h"
#include "driver-cpu.h"
#include "driver-opencl.h"
#include "scrypt.h"
#include "util.h"
#ifdef USE_AVALON
#include "driver-avalon.h"
#endif
#ifdef HAVE_BFG_LOWLEVEL
#include "lowlevel.h"
#endif
#if defined(unix) || defined(__APPLE__)
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#endif
#ifdef USE_SCRYPT
#include "scrypt.h"
#endif
#include "version.h"
#if defined(USE_AVALON) || defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_MODMINER) || defined(USE_NANOFURY) || defined(USE_X6500) || defined(USE_ZTEX)
# define USE_FPGA
#endif
enum bfg_quit_summary {
BQS_DEFAULT,
BQS_NONE,
BQS_DEVS,
BQS_PROCS,
BQS_DETAILED,
};
struct strategies strategies[] = {
{ "Failover" },
{ "Round Robin" },
{ "Rotate" },
{ "Load Balance" },
{ "Balance" },
};
static char packagename[256];
bool opt_protocol;
bool opt_dev_protocol;
static bool opt_benchmark, opt_benchmark_intense;
static bool want_longpoll = true;
static bool want_gbt = true;
static bool want_getwork = true;
#if BLKMAKER_VERSION > 1
static bool opt_load_bitcoin_conf = true;
static uint32_t coinbase_script_block_id;
static uint32_t template_nonce;
#endif
#if BLKMAKER_VERSION > 0
char *opt_coinbase_sig;
#endif
static enum bfg_quit_summary opt_quit_summary = BQS_DEFAULT;
static bool include_serial_in_statline;
char *request_target_str;
float request_pdiff = 1.0;
double request_bdiff;
static bool want_stratum = true;
int opt_skip_checks;
bool want_per_device_stats;
bool use_syslog;
bool opt_quiet_work_updates = true;
bool opt_quiet;
bool opt_realquiet;
int loginput_size;
bool opt_compact;
bool opt_show_procs;
const int opt_cutofftemp = 95;
int opt_hysteresis = 3;
static int opt_retries = -1;
int opt_fail_pause = 5;
int opt_log_interval = 20;
int opt_queue = 1;
int opt_scantime = 60;
int opt_expiry = 120;
int opt_expiry_lp = 3600;
unsigned long long global_hashrate;
static bool opt_unittest = false;
unsigned unittest_failures;
unsigned long global_quota_gcd = 1;
time_t last_getwork;
#ifdef HAVE_OPENCL
int opt_dynamic_interval = 7;
int nDevs;
int opt_g_threads = -1;
#endif
#ifdef USE_SCRYPT
static char detect_algo = 1;
#else
static char detect_algo;
#endif
bool opt_restart = true;
#ifdef USE_LIBMICROHTTPD
#include "httpsrv.h"
int httpsrv_port = -1;
#endif
#ifdef USE_LIBEVENT
int stratumsrv_port = -1;
#endif
const
int rescan_delay_ms = 1000;
#ifdef HAVE_BFG_HOTPLUG
bool opt_hotplug = 1;
const
int hotplug_delay_ms = 100;
#else
const bool opt_hotplug;
#endif
struct string_elist *scan_devices;
static struct string_elist *opt_set_device_list;
bool opt_force_dev_init;
static struct string_elist *opt_devices_enabled_list;
static bool opt_display_devs;
int total_devices;
struct cgpu_info **devices;
int total_devices_new;
struct cgpu_info **devices_new;
bool have_opencl;
int opt_n_threads = -1;
int mining_threads;
int base_queue;
int num_processors;
#ifdef HAVE_CURSES
bool use_curses = true;
#else
bool use_curses;
#endif
int last_logstatusline_len;
#ifdef HAVE_LIBUSB
bool have_libusb;
#endif
static bool opt_submit_stale = true;
static float opt_shares;
static int opt_submit_threads = 0x40;
bool opt_fail_only;
int opt_fail_switch_delay = 300;
bool opt_autofan;
bool opt_autoengine;
bool opt_noadl;
char *opt_api_allow = NULL;
char *opt_api_groups;
char *opt_api_description = PACKAGE_STRING;
int opt_api_port = 4028;
bool opt_api_listen;
bool opt_api_mcast;
char *opt_api_mcast_addr = API_MCAST_ADDR;
char *opt_api_mcast_code = API_MCAST_CODE;
char *opt_api_mcast_des = "";
int opt_api_mcast_port = 4028;
bool opt_api_network;
bool opt_delaynet;
bool opt_disable_pool;
bool opt_disable_client_reconnect = false;
static bool no_work;
bool opt_worktime;
bool opt_weighed_stats;
char *opt_kernel_path;
char *cgminer_path;
#if defined(USE_BITFORCE)
bool opt_bfl_noncerange;
#endif
#define QUIET (opt_quiet || opt_realquiet)
struct thr_info *control_thr;
struct thr_info **mining_thr;
static int watchpool_thr_id;
static int watchdog_thr_id;
#ifdef HAVE_CURSES
static int input_thr_id;
#endif
int gpur_thr_id;
static int api_thr_id;
static int total_control_threads;
pthread_mutex_t hash_lock;
static pthread_mutex_t *stgd_lock;
pthread_mutex_t console_lock;
cglock_t ch_lock;
static pthread_rwlock_t blk_lock;
static pthread_mutex_t sshare_lock;
pthread_rwlock_t netacc_lock;
pthread_rwlock_t mining_thr_lock;
pthread_rwlock_t devices_lock;
static pthread_mutex_t lp_lock;
static pthread_cond_t lp_cond;
pthread_cond_t gws_cond;
bool shutting_down;
double total_rolling;
double total_mhashes_done;
static struct timeval total_tv_start, total_tv_end;
static struct timeval miner_started;
cglock_t control_lock;
pthread_mutex_t stats_lock;
static pthread_mutex_t submitting_lock;
static int total_submitting;
static struct work *submit_waiting;
notifier_t submit_waiting_notifier;
int hw_errors;
int total_accepted, total_rejected;
int total_getworks, total_stale, total_discarded;
uint64_t total_bytes_rcvd, total_bytes_sent;
double total_diff1, total_bad_diff1;
double total_diff_accepted, total_diff_rejected, total_diff_stale;
static int staged_rollable;
unsigned int new_blocks;
unsigned int found_blocks;
unsigned int local_work;
unsigned int total_go, total_ro;
struct pool **pools;
static struct pool *currentpool = NULL;
int total_pools, enabled_pools;
enum pool_strategy pool_strategy = POOL_FAILOVER;
int opt_rotate_period;
static int total_urls, total_users, total_passes;
static
#ifndef HAVE_CURSES
const
#endif
bool curses_active;
#ifdef HAVE_CURSES
#if !(defined(PDCURSES) || defined(NCURSES_VERSION))
const
#endif
short default_bgcolor = COLOR_BLACK;
static int attr_title = A_BOLD;
#endif
static
#if defined(HAVE_CURSES) && defined(USE_UNICODE)
bool use_unicode;
static
bool have_unicode_degrees;
static
wchar_t unicode_micro = 'u';
#else
const bool use_unicode;
static
const bool have_unicode_degrees;
#ifdef HAVE_CURSES
static
const char unicode_micro = 'u';
#endif
#endif
#ifdef HAVE_CURSES
#define U8_BAD_START "\xef\x80\x81"
#define U8_BAD_END "\xef\x80\x80"
#define AS_BAD(x) U8_BAD_START x U8_BAD_END
/* logstart is where the log window should start */
static int devcursor, logstart, logcursor;
bool selecting_device;
unsigned selected_device;
#endif
static int max_lpdigits;
// current_hash was replaced with goal->current_goal_detail
// current_block_id was replaced with blkchain->currentblk->block_id
static char datestamp[40];
static char best_share[ALLOC_H2B_SHORTV] = "0";
double best_diff = 0;
struct mining_algorithm *mining_algorithms;
struct mining_goal_info *mining_goals;
int active_goals = 1;
int swork_id;
/* For creating a hash database of stratum shares submitted that have not had
* a response yet */
struct stratum_share {
UT_hash_handle hh;
bool block;
struct work *work;
int id;
};
static struct stratum_share *stratum_shares = NULL;
char *opt_socks_proxy = NULL;
static const char def_conf[] = "bfgminer.conf";
static bool config_loaded;
static int include_count;
#define JSON_INCLUDE_CONF "include"
#define JSON_LOAD_ERROR "JSON decode of file '%s' failed\n %s"
#define JSON_LOAD_ERROR_LEN strlen(JSON_LOAD_ERROR)
#define JSON_MAX_DEPTH 10
#define JSON_MAX_DEPTH_ERR "Too many levels of JSON includes (limit 10) or a loop"
char *cmd_idle, *cmd_sick, *cmd_dead;
#if defined(unix) || defined(__APPLE__)
static char *opt_stderr_cmd = NULL;
static int forkpid;
#endif // defined(unix)
#ifdef HAVE_CHROOT
char *chroot_dir;
#endif
#ifdef HAVE_PWD_H
char *opt_setuid;
#endif
struct sigaction termhandler, inthandler;
struct thread_q *getq;
static int total_work;
static bool staged_full;
struct work *staged_work = NULL;
struct schedtime {
bool enable;
struct tm tm;
};
struct schedtime schedstart;
struct schedtime schedstop;
bool sched_paused;
static bool time_before(struct tm *tm1, struct tm *tm2)
{
if (tm1->tm_hour < tm2->tm_hour)
return true;
if (tm1->tm_hour == tm2->tm_hour && tm1->tm_min < tm2->tm_min)
return true;
return false;
}
static bool should_run(void)
{
struct tm tm;
time_t tt;
bool within_range;
if (!schedstart.enable && !schedstop.enable)
return true;
tt = time(NULL);
localtime_r(&tt, &tm);
// NOTE: This is delicately balanced so that should_run is always false if schedstart==schedstop
if (time_before(&schedstop.tm, &schedstart.tm))
within_range = (time_before(&tm, &schedstop.tm) || !time_before(&tm, &schedstart.tm));
else
within_range = (time_before(&tm, &schedstop.tm) && !time_before(&tm, &schedstart.tm));
if (within_range && !schedstop.enable)
/* This is a once off event with no stop time set */
schedstart.enable = false;
return within_range;
}
void get_datestamp(char *f, size_t fsiz, time_t tt)
{
struct tm _tm;
struct tm *tm = &_tm;
if (tt == INVALID_TIMESTAMP)
tt = time(NULL);
localtime_r(&tt, tm);
snprintf(f, fsiz, "[%d-%02d-%02d %02d:%02d:%02d]",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}
static
void get_timestamp(char *f, size_t fsiz, time_t tt)
{
struct tm _tm;
struct tm *tm = &_tm;
localtime_r(&tt, tm);
snprintf(f, fsiz, "[%02d:%02d:%02d]",
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}
static void applog_and_exit(const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 1, 2);
static char exit_buf[512];
static void applog_and_exit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(exit_buf, sizeof(exit_buf), fmt, ap);
va_end(ap);
_applog(LOG_ERR, exit_buf);
exit(1);
}
static
float drv_min_nonce_diff(const struct device_drv * const drv, struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
{
if (drv->drv_min_nonce_diff)
return drv->drv_min_nonce_diff(proc, malgo);
return (malgo->algo == POW_SHA256D) ? 1. : -1.;
}
char *devpath_to_devid(const char *devpath)
{
#ifndef WIN32
if (devpath[0] != '/')
return NULL;
struct stat my_stat;
if (stat(devpath, &my_stat))
return NULL;
char *devs = malloc(6 + (sizeof(dev_t) * 2) + 1);
memcpy(devs, "dev_t:", 6);
bin2hex(&devs[6], &my_stat.st_rdev, sizeof(dev_t));
#else
if (!strncmp(devpath, "\\\\.\\", 4))
devpath += 4;
if (strncasecmp(devpath, "COM", 3) || !devpath[3])
return NULL;
devpath += 3;
char *p;
strtol(devpath, &p, 10);
if (p[0])
return NULL;
const int sz = (p - devpath);
char *devs = malloc(4 + sz + 1);
sprintf(devs, "com:%s", devpath);
#endif
return devs;
}
static
bool devpaths_match(const char * const ap, const char * const bp)
{
char * const a = devpath_to_devid(ap);
if (!a)
return false;
char * const b = devpath_to_devid(bp);
bool rv = false;
if (b)
{
rv = !strcmp(a, b);
free(b);
}
free(a);
return rv;
}
static
int proc_letter_to_number(const char *s, const char ** const rem)
{
int n = 0, c;
for ( ; s[0]; ++s)
{
if (unlikely(n > INT_MAX / 26))
break;
c = tolower(s[0]) - 'a';
if (unlikely(c < 0 || c > 25))
break;
if (unlikely(INT_MAX - c < n))
break;
n = (n * 26) + c;
}
*rem = s;
return n;
}
static
bool cgpu_match(const char * const pattern, const struct cgpu_info * const cgpu)
{
// all - matches anything
// d0 - matches all processors of device 0
// d0-3 - matches all processors of device 0, 1, 2, or 3
// d0a - matches first processor of device 0
// 0 - matches processor 0
// 0-4 - matches processors 0, 1, 2, 3, or 4
// ___ - matches all processors on all devices using driver/name ___
// ___0 - matches all processors of 0th device using driver/name ___
// ___0a - matches first processor of 0th device using driver/name ___
// @* - matches device with serial or path *
// @*@a - matches first processor of device with serial or path *
// ___@* - matches device with serial or path * using driver/name ___
if (!strcasecmp(pattern, "all"))
return true;
const struct device_drv * const drv = cgpu->drv;
const char *p = pattern, *p2;
size_t L;
int n, i, c = -1;
int n2;
int proc_first = -1, proc_last = -1;
struct cgpu_info *device;
if (!(strncasecmp(drv->dname, p, (L = strlen(drv->dname)))
&& strncasecmp(drv-> name, p, (L = strlen(drv-> name)))))
// dname or name
p = &pattern[L];
else
if (p[0] == 'd' && (isdigit(p[1]) || p[1] == '-'))
// d#
++p;
else
if (isdigit(p[0]) || p[0] == '@' || p[0] == '-')
// # or @
{}
else
return false;
L = p - pattern;
while (isspace(p[0]))
++p;
if (p[0] == '@')
{
// Serial/path
const char * const ser = &p[1];
for (p = ser; p[0] != '@' && p[0] != '\0'; ++p)
{}
p2 = (p[0] == '@') ? &p[1] : p;
const size_t serlen = (p - ser);
p = "";
n = n2 = 0;
const char * const devpath = cgpu->device_path ?: "";
const char * const devser = cgpu->dev_serial ?: "";
if ((!strncmp(devpath, ser, serlen)) && devpath[serlen] == '\0')
{} // Match
else
if ((!strncmp(devser, ser, serlen)) && devser[serlen] == '\0')
{} // Match
else
{
char devpath2[serlen + 1];
memcpy(devpath2, ser, serlen);
devpath2[serlen] = '\0';
if (!devpaths_match(devpath, ser))
return false;
}
}
else
{
if (isdigit(p[0]))
n = strtol(p, (void*)&p2, 0);
else
{
n = 0;
p2 = p;
}
if (p2[0] == '-')
{
++p2;
if (p2[0] && isdigit(p2[0]))
n2 = strtol(p2, (void*)&p2, 0);
else
n2 = INT_MAX;
}
else
n2 = n;
if (p == pattern)
{
if (!p[0])
return true;
if (p2 && p2[0])
goto invsyntax;
for (i = n; i <= n2; ++i)
{
if (i >= total_devices)
break;
if (cgpu == devices[i])
return true;
}
return false;
}
}
if (p2[0])
{
proc_first = proc_letter_to_number(&p2[0], &p2);
if (p2[0] == '-')
{
++p2;
if (p2[0])
proc_last = proc_letter_to_number(p2, &p2);
else
proc_last = INT_MAX;
}
else
proc_last = proc_first;
if (p2[0])
goto invsyntax;
}
if (L > 1 || tolower(pattern[0]) != 'd' || !p[0])
{
if ((L == 3 && !strncasecmp(pattern, drv->name, 3)) ||
(!L) ||
(L == strlen(drv->dname) && !strncasecmp(pattern, drv->dname, L)))
{} // Matched name or dname
else
return false;
if (p[0] && (cgpu->device_id < n || cgpu->device_id > n2))
return false;
if (proc_first != -1 && (cgpu->proc_id < proc_first || cgpu->proc_id > proc_last))
return false;
return true;
}
// d#
c = -1;
for (i = 0; ; ++i)
{
if (i == total_devices)
return false;
if (devices[i]->device != devices[i])
continue;
++c;
if (c < n)
continue;
if (c > n2)
break;
for (device = devices[i]; device; device = device->next_proc)
{
if (proc_first != -1 && (device->proc_id < proc_first || device->proc_id > proc_last))
continue;
if (device == cgpu)
return true;
}
}
return false;
invsyntax:
applog(LOG_WARNING, "%s: Invalid syntax: %s", __func__, pattern);
return false;
}
#define TEST_CGPU_MATCH(pattern) \
if (!cgpu_match(pattern, &cgpu)) \
{ \
++unittest_failures; \
applog(LOG_ERR, "%s: Pattern \"%s\" should have matched!", __func__, pattern); \
} \
// END TEST_CGPU_MATCH
#define TEST_CGPU_NOMATCH(pattern) \
if (cgpu_match(pattern, &cgpu)) \
{ \
++unittest_failures; \
applog(LOG_ERR, "%s: Pattern \"%s\" should NOT have matched!", __func__, pattern); \
} \
// END TEST_CGPU_MATCH
static __maybe_unused
void test_cgpu_match()
{
struct device_drv drv = {
.dname = "test",
.name = "TST",
};
struct cgpu_info cgpu = {
.drv = &drv,
.device = &cgpu,
.device_id = 1,
.proc_id = 1,
.proc_repr = "TST 1b",
}, cgpu0a = {
.drv = &drv,
.device = &cgpu0a,
.device_id = 0,
.proc_id = 0,
.proc_repr = "TST 0a",
}, cgpu1a = {
.drv = &drv,
.device = &cgpu0a,
.device_id = 1,
.proc_id = 0,
.proc_repr = "TST 1a",
};
struct cgpu_info *devices_list[3] = {&cgpu0a, &cgpu1a, &cgpu,};
devices = devices_list;
total_devices = 3;
TEST_CGPU_MATCH("all")
TEST_CGPU_MATCH("d1")
TEST_CGPU_NOMATCH("d2")
TEST_CGPU_MATCH("d0-5")
TEST_CGPU_NOMATCH("d0-0")
TEST_CGPU_NOMATCH("d2-5")
TEST_CGPU_MATCH("d-1")
TEST_CGPU_MATCH("d1-")
TEST_CGPU_NOMATCH("d-0")
TEST_CGPU_NOMATCH("d2-")
TEST_CGPU_MATCH("2")
TEST_CGPU_NOMATCH("3")
TEST_CGPU_MATCH("1-2")
TEST_CGPU_MATCH("2-3")
TEST_CGPU_NOMATCH("1-1")
TEST_CGPU_NOMATCH("3-4")
TEST_CGPU_MATCH("TST")
TEST_CGPU_MATCH("test")
TEST_CGPU_MATCH("tst")
TEST_CGPU_MATCH("TEST")
TEST_CGPU_NOMATCH("TSF")
TEST_CGPU_NOMATCH("TS")
TEST_CGPU_NOMATCH("TSTF")
TEST_CGPU_MATCH("TST1")
TEST_CGPU_MATCH("test1")
TEST_CGPU_MATCH("TST0-1")
TEST_CGPU_MATCH("TST 1")
TEST_CGPU_MATCH("TST 1-2")
TEST_CGPU_MATCH("TEST 1-2")
TEST_CGPU_NOMATCH("TST2")
TEST_CGPU_NOMATCH("TST2-3")
TEST_CGPU_NOMATCH("TST0-0")
TEST_CGPU_MATCH("TST1b")
TEST_CGPU_MATCH("tst1b")
TEST_CGPU_NOMATCH("TST1c")
TEST_CGPU_NOMATCH("TST1bb")
TEST_CGPU_MATCH("TST0-1b")
TEST_CGPU_NOMATCH("TST0-1c")
TEST_CGPU_MATCH("TST1a-d")
TEST_CGPU_NOMATCH("TST1a-a")
TEST_CGPU_NOMATCH("TST1-a")
TEST_CGPU_NOMATCH("TST1c-z")
TEST_CGPU_NOMATCH("TST1c-")
TEST_CGPU_MATCH("@")
TEST_CGPU_NOMATCH("@abc")
TEST_CGPU_MATCH("@@b")
TEST_CGPU_NOMATCH("@@c")
TEST_CGPU_MATCH("TST@")
TEST_CGPU_NOMATCH("TST@abc")
TEST_CGPU_MATCH("TST@@b")
TEST_CGPU_NOMATCH("TST@@c")
TEST_CGPU_MATCH("TST@@b-f")
TEST_CGPU_NOMATCH("TST@@c-f")
TEST_CGPU_NOMATCH("TST@@-a")
cgpu.device_path = "/dev/test";
cgpu.dev_serial = "testy";
TEST_CGPU_MATCH("TST@/dev/test")
TEST_CGPU_MATCH("TST@testy")
TEST_CGPU_NOMATCH("TST@")
TEST_CGPU_NOMATCH("TST@/dev/test5@b")
TEST_CGPU_NOMATCH("TST@testy3@b")
TEST_CGPU_MATCH("TST@/dev/test@b")
TEST_CGPU_MATCH("TST@testy@b")
TEST_CGPU_NOMATCH("TST@/dev/test@c")
TEST_CGPU_NOMATCH("TST@testy@c")
cgpu.device_path = "usb:000:999";
TEST_CGPU_MATCH("TST@usb:000:999")
drv.dname = "test7";
TEST_CGPU_MATCH("test7")
TEST_CGPU_MATCH("TEST7")
TEST_CGPU_NOMATCH("test&")
TEST_CGPU_MATCH("test7 1-2")
TEST_CGPU_MATCH("test7@testy@b")
}
static
int cgpu_search(const char * const pattern, const int first)
{
int i;
struct cgpu_info *cgpu;
#define CHECK_CGPU_SEARCH do{ \
cgpu = get_devices(i); \
if (cgpu_match(pattern, cgpu)) \
return i; \
}while(0)
for (i = first; i < total_devices; ++i)
CHECK_CGPU_SEARCH;
for (i = 0; i < first; ++i)
CHECK_CGPU_SEARCH;
#undef CHECK_CGPU_SEARCH
return -1;
}
static pthread_mutex_t sharelog_lock;
static FILE *sharelog_file = NULL;
struct thr_info *get_thread(int thr_id)
{
struct thr_info *thr;
rd_lock(&mining_thr_lock);
thr = mining_thr[thr_id];
rd_unlock(&mining_thr_lock);
return thr;
}
static struct cgpu_info *get_thr_cgpu(int thr_id)
{
struct thr_info *thr = get_thread(thr_id);
return thr->cgpu;
}
struct cgpu_info *get_devices(int id)
{
struct cgpu_info *cgpu;
rd_lock(&devices_lock);
cgpu = devices[id];
rd_unlock(&devices_lock);
return cgpu;
}
static pthread_mutex_t noncelog_lock = PTHREAD_MUTEX_INITIALIZER;
static FILE *noncelog_file = NULL;
static
void noncelog(const struct work * const work)
{
const int thr_id = work->thr_id;
const struct cgpu_info *proc = get_thr_cgpu(thr_id);
char buf[0x200], hash[65], data[161], midstate[65];
int rv;
size_t ret;
bin2hex(hash, work->hash, 32);
bin2hex(data, work->data, 80);
bin2hex(midstate, work->midstate, 32);
// timestamp,proc,hash,data,midstate
rv = snprintf(buf, sizeof(buf), "%lu,%s,%s,%s,%s\n",
(unsigned long)time(NULL), proc->proc_repr_ns,
hash, data, midstate);
if (unlikely(rv < 1))
{
applog(LOG_ERR, "noncelog printf error");
return;
}
mutex_lock(&noncelog_lock);
ret = fwrite(buf, rv, 1, noncelog_file);
fflush(noncelog_file);
mutex_unlock(&noncelog_lock);
if (ret != 1)
applog(LOG_ERR, "noncelog fwrite error");
}
static void sharelog(const char*disposition, const struct work*work)
{
char target[(sizeof(work->target) * 2) + 1];
char hash[(sizeof(work->hash) * 2) + 1];
char data[(sizeof(work->data) * 2) + 1];
struct cgpu_info *cgpu;
unsigned long int t;
struct pool *pool;
int thr_id, rv;
char s[1024];
size_t ret;
if (!sharelog_file)
return;
thr_id = work->thr_id;
cgpu = get_thr_cgpu(thr_id);
pool = work->pool;
t = work->ts_getwork + timer_elapsed(&work->tv_getwork, &work->tv_work_found);
bin2hex(target, work->target, sizeof(work->target));
bin2hex(hash, work->hash, sizeof(work->hash));
bin2hex(data, work->data, sizeof(work->data));
// timestamp,disposition,target,pool,dev,thr,sharehash,sharedata
rv = snprintf(s, sizeof(s), "%lu,%s,%s,%s,%s,%u,%s,%s\n", t, disposition, target, pool->rpc_url, cgpu->proc_repr_ns, thr_id, hash, data);
if (rv >= (int)(sizeof(s)))
s[sizeof(s) - 1] = '\0';
else if (rv < 0) {
applog(LOG_ERR, "sharelog printf error");
return;
}
mutex_lock(&sharelog_lock);
ret = fwrite(s, rv, 1, sharelog_file);
fflush(sharelog_file);
mutex_unlock(&sharelog_lock);
if (ret != 1)
applog(LOG_ERR, "sharelog fwrite error");
}
#ifdef HAVE_CURSES