forked from kanoi/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgminer.c
11184 lines (9682 loc) · 303 KB
/
cgminer.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-2022 Andrew Smith
* Copyright 2011-2018 Con Kolivas
* Copyright 2011-2012 Luke Dashjr
* 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 __GNUC__
#if __GNUC__ >= 7
#pragma GCC diagnostic ignored "-Wcast-function-type"
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
#endif
#ifdef HAVE_CURSES
#include <curses.h>
#endif
#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 <limits.h>
#ifdef USE_USBUTILS
#include <semaphore.h>
#endif
#ifdef USE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#ifndef WIN32
#include <sys/resource.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <ccan/opt/opt.h>
#include <jansson.h>
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#else
char *curly = ":D";
#endif
#include <libgen.h>
#include <sha2.h>
#include "compat.h"
#include "miner.h"
#include "bench_block.h"
#ifdef USE_USBUTILS
#include "usbutils.h"
#endif
#if defined(unix) || defined(__APPLE__)
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#endif
#ifdef USE_AVALON
#include "driver-avalon.h"
#endif
#ifdef USE_AVALON2
#include "driver-avalon2.h"
#endif
#ifdef USE_AVALON4
#include "driver-avalon4.h"
#endif
#ifdef USE_AVALON7
#include "driver-avalon7.h"
#endif
#ifdef USE_AVALON8
#include "driver-avalon8.h"
#endif
#ifdef USE_AVALON_MINER
#include "driver-avalon-miner.h"
#endif
#ifdef USE_BFLSC
#include "driver-bflsc.h"
#endif
#ifdef USE_SP10
#include "driver-spondoolies-sp10.h"
#endif
#ifdef USE_SP30
#include "driver-spondoolies-sp30.h"
#endif
#ifdef USE_BLOCK_ERUPTER
#include "driver-blockerupter.h"
#endif
#ifdef USE_BITFURY
#include "driver-bitfury.h"
#endif
#ifdef USE_BITFURY16
#include "driver-bitfury16.h"
#endif
#ifdef USE_BITMAIN_SOC
#include <sys/sysinfo.h>
#include "driver-btm-soc.h"
#endif
#ifdef USE_COINTERRA
#include "driver-cointerra.h"
#endif
#ifdef USE_GEKKO
#include "driver-gekko.h"
#endif
#ifdef USE_HASHFAST
#include "driver-hashfast.h"
#endif
#if defined(USE_ANT_S1) || defined(USE_ANT_S2) || defined(USE_ANT_S3)
#include "driver-bitmain.h"
#endif
#if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_AVALON) || defined(USE_AVALON2) || defined(USE_MODMINER)
# define USE_FPGA
#endif
struct strategies strategies[] = {
{ "Failover" },
{ "Round Robin" },
{ "Rotate" },
{ "Load Balance" },
{ "Balance" },
};
static char packagename[256];
bool opt_work_update;
bool opt_protocol;
static struct benchfile_layout {
int length;
char *name;
} benchfile_data[] = {
{ 1, "Version" },
{ 64, "MerkleRoot" },
{ 64, "PrevHash" },
{ 8, "DifficultyBits" },
{ 10, "NonceTime" } // 10 digits
};
enum benchwork {
BENCHWORK_VERSION = 0,
BENCHWORK_MERKLEROOT,
BENCHWORK_PREVHASH,
BENCHWORK_DIFFBITS,
BENCHWORK_NONCETIME,
BENCHWORK_COUNT
};
#ifdef HAVE_LIBCURL
static char *opt_btc_address;
static char *opt_btc_sig;
#endif
struct pool *opt_btcd;
static char *opt_benchfile;
static bool opt_benchfile_display;
static FILE *benchfile_in;
static int benchfile_line;
static int benchfile_work;
static bool opt_benchmark;
static bool opt_blockcheck;
bool have_longpoll;
bool want_per_device_stats;
bool use_syslog;
bool opt_quiet;
bool opt_realquiet;
bool opt_loginput;
bool opt_compact;
bool opt_decode;
const int opt_cutofftemp = 95;
int opt_log_interval = 5;
static const int max_queue = 1;
const int max_scantime = 60;
const int max_expiry = 600;
uint64_t global_hashrate;
unsigned long global_quota_gcd = 1;
time_t last_getwork;
int opt_pool_fallback = 120;
#if defined(USE_USBUTILS)
int nDevs;
#endif
bool opt_restart = true;
bool opt_nogpu;
struct list_head scan_devices;
static bool opt_display_devs;
int total_devices;
int zombie_devs;
static int most_devices;
struct cgpu_info **devices;
int mining_threads;
int num_processors;
#ifdef HAVE_CURSES
bool use_curses = true;
#else
bool use_curses;
#endif
bool opt_mac_yield;
bool opt_widescreen;
static bool alt_status;
static bool switch_status;
static bool opt_submit_stale = true;
static int opt_shares;
static bool opt_fix_protocol;
bool opt_lowmem;
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;
char *opt_api_host = API_LISTEN_ADDR;
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;
static bool no_work;
#ifdef USE_ICARUS
char *opt_icarus_options = NULL;
char *opt_icarus_timing = NULL;
float opt_anu_freq = 250;
float opt_au3_freq = 225;
int opt_au3_volt = 775;
float opt_rock_freq = 270;
#endif
bool opt_worktime;
#ifdef USE_AVALON
char *opt_avalon_options;
char *opt_bitburner_fury_options;
static char *opt_set_avalon_fan;
static char *opt_set_avalon_freq;
#endif
#ifdef USE_AVALON2
static char *opt_set_avalon2_freq;
static char *opt_set_avalon2_fan;
static char *opt_set_avalon2_voltage;
#endif
#ifdef USE_AVALON4
static char *opt_set_avalon4_fan;
static char *opt_set_avalon4_voltage;
static char *opt_set_avalon4_freq;
#endif
#ifdef USE_AVALON7
static char *opt_set_avalon7_fan;
static char *opt_set_avalon7_voltage;
static char *opt_set_avalon7_voltage_level;
static char *opt_set_avalon7_voltage_offset;
static char *opt_set_avalon7_freq;
#endif
#ifdef USE_AVALON8
static char *opt_set_avalon8_fan;
static char *opt_set_avalon8_voltage_level;
static char *opt_set_avalon8_voltage_level_offset;
static char *opt_set_avalon8_freq;
static char *opt_set_avalon8_asic_otp;
#endif
#ifdef USE_AVALON_MINER
static char *opt_set_avalonm_voltage;
static char *opt_set_avalonm_freq;
#endif
#ifdef USE_BLOCKERUPTER
int opt_bet_clk = 0;
#endif
#ifdef USE_GEKKO
char *opt_gekko_serial = NULL;
bool opt_gekko_noboost = 0;
bool opt_gekko_lowboost = 0;
bool opt_gekko_gsc_detect = 0;
bool opt_gekko_gsd_detect = 0;
bool opt_gekko_gse_detect = 0;
bool opt_gekko_gsh_detect = 0;
bool opt_gekko_gsi_detect = 0;
bool opt_gekko_gsf_detect = 0;
bool opt_gekko_r909_detect = 0;
float opt_gekko_gsc_freq = 150;
float opt_gekko_gsd_freq = 100;
float opt_gekko_gse_freq = 150;
float opt_gekko_tune_up = 97;
float opt_gekko_tune_down = 95;
#if defined(__APPLE__)
float opt_gekko_wait_factor = 0.3;
#elif defined (WIN32)
float opt_gekko_wait_factor = 0.4;
#else
float opt_gekko_wait_factor = 0.5;
#endif
float opt_gekko_step_freq = 6.25;
int opt_gekko_gsh_freq = 100;
int opt_gekko_gsi_freq = 550;
int opt_gekko_gsf_freq = 200;
int opt_gekko_r909_freq = 450;
int opt_gekko_bauddiv = 0;
int opt_gekko_gsh_vcore = 400;
int opt_gekko_start_freq = 100;
int opt_gekko_step_delay = 15;
bool opt_gekko_mine2 = false; // gekko code ignores it
int opt_gekko_tune2 = 0;
#endif
#ifdef USE_HASHRATIO
#include "driver-hashratio.h"
#endif
#ifdef USE_KLONDIKE
char *opt_klondike_options = NULL;
#endif
#ifdef USE_DRILLBIT
char *opt_drillbit_options = NULL;
char *opt_drillbit_auto = NULL;
#endif
char *opt_bab_options = NULL;
#ifdef USE_BITMINE_A1
char *opt_bitmine_a1_options = NULL;
#endif
#ifdef USE_DRAGONMINT_T1
#include "dragonmint_t1.h"
char *opt_dragonmint_t1_options = NULL;
int opt_T1Pll[MCOMPAT_CONFIG_MAX_CHAIN_NUM] = {
DEFAULT_PLL, DEFAULT_PLL, DEFAULT_PLL, DEFAULT_PLL,
DEFAULT_PLL, DEFAULT_PLL, DEFAULT_PLL, DEFAULT_PLL
};
int opt_T1Vol[MCOMPAT_CONFIG_MAX_CHAIN_NUM] = {
DEFAULT_VOLT, DEFAULT_VOLT, DEFAULT_VOLT, DEFAULT_VOLT,
DEFAULT_VOLT, DEFAULT_VOLT, DEFAULT_VOLT, DEFAULT_VOLT
};
int opt_T1VID[MCOMPAT_CONFIG_MAX_CHAIN_NUM] = {};
bool opt_T1auto = true;
bool opt_T1_efficient;
bool opt_T1_performance;
int opt_T1_target = 100;
#endif
#if defined(USE_ANT_S1) || defined(USE_ANT_S2)
char *opt_bitmain_options;
char *opt_set_bitmain_fan;
char *opt_bitmain_freq;
// Ignored
bool opt_bitmain_nobeeper;
bool opt_bitmain_notempoverctrl;
bool opt_bitmain_homemode;
#endif
#ifdef USE_ANT_S2
#ifndef USE_ANT_S3
char *opt_bitmain_dev;
#endif
char *opt_bitmain_voltage = BITMAIN_VOLTAGE_DEF;
#endif
#ifdef USE_HASHFAST
static char *opt_set_hfa_fan;
#endif
static char *opt_set_null;
#ifdef USE_MINION
int opt_minion_chipreport;
char *opt_minion_cores;
bool opt_minion_extra;
char *opt_minion_freq;
int opt_minion_freqchange = 1000;
int opt_minion_freqpercent = 70;
bool opt_minion_idlecount;
int opt_minion_ledcount;
int opt_minion_ledlimit = 98;
bool opt_minion_noautofreq;
bool opt_minion_overheat;
int opt_minion_spidelay;
char *opt_minion_spireset;
int opt_minion_spisleep = 200;
int opt_minion_spiusec;
char *opt_minion_temp;
#endif
#ifdef USE_USBUTILS
char *opt_usb_select = NULL;
int opt_usbdump = -1;
bool opt_usb_list_all;
cgsem_t usb_resource_sem;
static pthread_t usb_poll_thread;
static bool usb_polling;
static bool polling_usb;
static bool usb_reinit;
#endif
char *opt_kernel_path;
char *cgminer_path;
bool opt_gen_stratum_work;
#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 gwsched_thr_id;
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;
#ifdef USE_USBUTILS
static int usbres_thr_id;
static int hotplug_thr_id;
#endif
static int total_control_threads;
bool hotplug_mode;
static int new_devices;
static int new_threads;
int hotplug_time = 5;
#if LOCK_TRACKING
pthread_mutex_t lockstat_lock;
#endif
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_mutex_t restart_lock;
pthread_cond_t restart_cond;
pthread_cond_t gws_cond;
double rolling1, rolling5, rolling15;
double total_rolling;
double total_mhashes_done;
#ifdef USE_BITMAIN_SOC
char *opt_version_path = NULL;
char displayed_hash_rate[16] = {0};
char nonce_num10_string[NONCE_BUFF];
char nonce_num30_string[NONCE_BUFF];
char nonce_num60_string[NONCE_BUFF];
char g_miner_version[256] = {0};
char g_miner_compiletime[256] = {0};
char g_miner_type[256] = {0};
double new_total_mhashes_done;
double new_total_secs = 1.0;
// only used for total_secs, because we need use system info time, instead of real data time.
time_t total_tv_start_sys;
time_t total_tv_end_sys;
#endif
static struct timeval total_tv_start, total_tv_end;
static struct timeval restart_tv_start, update_tv_start;
cglock_t control_lock;
pthread_mutex_t stats_lock;
int hw_errors;
int64_t total_accepted, total_rejected, total_diff1;
int64_t total_getworks, total_stale, total_discarded;
double total_diff_accepted, total_diff_rejected, total_diff_stale;
static int staged_rollable;
unsigned int new_blocks;
static unsigned int work_block;
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, total_userpasses;
static
#ifndef HAVE_CURSES
const
#endif
bool curses_active;
/* Protected by ch_lock */
char current_hash[68];
static char prev_block[12];
static char current_block[32];
static char datestamp[40];
static char blocktime[32];
struct timeval block_timeval;
static char best_share[8] = "0";
double current_diff = 0xFFFFFFFFFFFFFFFFULL;
static char block_diff[8];
uint64_t best_diff = 0;
struct block {
char hash[68];
UT_hash_handle hh;
int block_no;
};
static struct block *blocks = NULL;
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;
time_t sshare_time;
time_t sshare_sent;
};
static struct stratum_share *stratum_shares = NULL;
char *opt_socks_proxy = NULL;
int opt_suggest_diff;
static const char def_conf[] = "cgminer.conf";
static char *default_config;
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"
#define JSON_WEB_ERROR "WEB config err"
#if defined(unix) || defined(__APPLE__)
static char *opt_stderr_cmd = NULL;
static int forkpid;
#endif // defined(unix)
struct sigaction termhandler, inthandler, abrthandler;
struct thread_q *getq;
static uint32_t total_work;
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 timeval tv;
struct tm *tm;
if (!schedstart.enable && !schedstop.enable)
return true;
cgtime(&tv);
const time_t tmp_time = tv.tv_sec;
tm = localtime(&tmp_time);
if (schedstart.enable) {
if (!schedstop.enable) {
if (time_before(tm, &schedstart.tm))
return false;
/* This is a once off event with no stop time set */
schedstart.enable = false;
return true;
}
if (time_before(&schedstart.tm, &schedstop.tm)) {
if (time_before(tm, &schedstop.tm) && !time_before(tm, &schedstart.tm))
return true;
return false;
} /* Times are reversed */
if (time_before(tm, &schedstart.tm)) {
if (time_before(tm, &schedstop.tm))
return true;
return false;
}
return true;
}
/* only schedstop.enable == true */
if (!time_before(tm, &schedstop.tm))
return false;
return true;
}
void get_datestamp(char *f, size_t fsiz, struct timeval *tv)
{
struct tm *tm;
const time_t tmp_time = tv->tv_sec;
int ms = (int)(tv->tv_usec / 1000);
tm = localtime(&tmp_time);
snprintf(f, fsiz, "[%d-%02d-%02d %02d:%02d:%02d.%03d]",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec, ms);
}
static void get_timestamp(char *f, size_t fsiz, struct timeval *tv)
{
struct tm *tm;
const time_t tmp_time = tv->tv_sec;
int ms = (int)(tv->tv_usec / 1000);
tm = localtime(&tmp_time);
snprintf(f, fsiz, "[%02d:%02d:%02d.%03d]",
tm->tm_hour,
tm->tm_min,
tm->tm_sec, ms);
}
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, true);
exit(1);
}
static pthread_mutex_t sharelog_lock;
static FILE *sharelog_file = NULL;
static struct thr_info *__get_thread(int thr_id)
{
return mining_thr[thr_id];
}
struct thr_info *get_thread(int thr_id)
{
struct thr_info *thr;
rd_lock(&mining_thr_lock);
thr = __get_thread(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 void sharelog(const char*disposition, const struct work*work)
{
char *target, *hash, *data;
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 = (unsigned long int)(work->tv_work_found.tv_sec);
target = bin2hex(work->target, sizeof(work->target));
hash = bin2hex(work->hash, sizeof(work->hash));
data = bin2hex(work->data, sizeof(work->data));
// timestamp,disposition,target,pool,dev,thr,sharehash,sharedata
rv = snprintf(s, sizeof(s), "%lu,%s,%s,%s,%s%u,%u,%s,%s\n", t, disposition, target, pool->rpc_url, cgpu->drv->name, cgpu->device_id, thr_id, hash, data);
free(target);
free(hash);
free(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");
}
static char *gbt_req = "{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": [{\"capabilities\": [\"coinbasetxn\", \"workid\", \"coinbase/append\"]}]}\n";
static char *gbt_solo_req = "{\"id\": 0, \"method\": \"getblocktemplate\", \"params\": [{\"rules\" : [\"segwit\"]}]}\n";
static const char *gbt_understood_rules[1] = { NULL };
static const char *gbt_solo_understood_rules[2] = {"segwit", NULL};
static bool gbt_check_required_rule(const char* rule, const char** understood_rules)
{
const char *understood_rule;
if (!understood_rules || !rule)
return false;
while ((understood_rule = *understood_rules++)) {
if (strcmp(understood_rule, rule) == 0)
return true;
}
return false;
}
static bool gbt_check_rules(json_t* rules_arr, const char** understood_rules)
{
int i, rule_count;
const char *rule;
if (!rules_arr)
return true;
rule_count = json_array_size(rules_arr);
for (i = 0; i < rule_count; i++) {
rule = json_string_value(json_array_get(rules_arr, i));
if (rule && *rule++ == '!' && !gbt_check_required_rule(rule, understood_rules))
return false;
}
return true;
}
/* Adjust all the pools' quota to the greatest common denominator after a pool
* has been added or the quotas changed. */
void adjust_quota_gcd(void)
{
unsigned long gcd, lowest_quota = ~0UL, quota;
struct pool *pool;
int i;
for (i = 0; i < total_pools; i++) {
pool = pools[i];
quota = pool->quota;
if (!quota)
continue;
if (quota < lowest_quota)
lowest_quota = quota;
}
if (likely(lowest_quota < ~0UL)) {
gcd = lowest_quota;
for (i = 0; i < total_pools; i++) {
pool = pools[i];
quota = pool->quota;
if (!quota)
continue;
while (quota % gcd)
gcd--;
}
} else
gcd = 1;
for (i = 0; i < total_pools; i++) {
pool = pools[i];
pool->quota_used *= global_quota_gcd;
pool->quota_used /= gcd;
pool->quota_gcd = pool->quota / gcd;
}
global_quota_gcd = gcd;
applog(LOG_DEBUG, "Global quota greatest common denominator set to %lu", gcd);
}
/* Return value is ignored if not called from input_pool */
struct pool *add_pool(void)
{
struct pool *pool;
pool = cgcalloc(sizeof(struct pool), 1);
pool->pool_no = pool->prio = total_pools;
pools = cgrealloc(pools, sizeof(struct pool *) * (total_pools + 2));
pools[total_pools++] = pool;
mutex_init(&pool->pool_lock);
if (unlikely(pthread_cond_init(&pool->cr_cond, NULL)))
quit(1, "Failed to pthread_cond_init in add_pool");
cglock_init(&pool->data_lock);
mutex_init(&pool->stratum_lock);
cglock_init(&pool->gbt_lock);
INIT_LIST_HEAD(&pool->curlring);
/* Make sure the pool doesn't think we've been idle since time 0 */
pool->tv_idle.tv_sec = ~0UL;
pool->rpc_req = gbt_req;
pool->rpc_proxy = NULL;
pool->quota = 1;
adjust_quota_gcd();
return pool;
}
/* Pool variant of test and set */
static bool pool_tset(struct pool *pool, bool *var)
{
bool ret;
mutex_lock(&pool->pool_lock);
ret = *var;
*var = true;
mutex_unlock(&pool->pool_lock);
return ret;
}
bool pool_tclear(struct pool *pool, bool *var)
{
bool ret;
mutex_lock(&pool->pool_lock);
ret = *var;
*var = false;
mutex_unlock(&pool->pool_lock);
return ret;
}
struct pool *current_pool(void)
{
struct pool *pool;
cg_rlock(&control_lock);
pool = currentpool;
cg_runlock(&control_lock);
return pool;
}
char *set_int_range(const char *arg, int *i, int min, int max)
{
char *err = opt_set_intval(arg, i);
if (err)
return err;
if (*i < min || *i > max)
return "Value out of range";
return NULL;
}
static char *set_int_0_to_65535(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 65535);
}
static char *set_int_0_to_9999(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 9999);
}
static char *set_int_1_to_65535(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 65535);
}
#ifdef USE_AVALON8
static char *set_int_0_to_1(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 1);
}
static char *set_int_0_to_7(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 7);
}
#endif
static char *set_int_0_to_5(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 5);
}
static char *set_int_0_to_10(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 10);
}
#ifdef USE_DRAGONMINT_T1
static char *set_int_voltage(const char *arg, int *i)
{
return set_int_range(arg, i, CHIP_VOLT_MIN, CHIP_VOLT_MAX);
}
/* Intentionally does NOT accept zero so that zero means the value is NOT set
* and has no effect. */
static char *set_int_1_to_31(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 31);
}
#endif
static char *set_int_0_to_100(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 100);
}
static char *set_int_0_to_255(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 255);
}
static char *set_int_1_to_255(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 255);
}
static char *set_int_0_to_7680(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 7680);
}
static char *set_int_1_to_60(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 60);
}
static char *set_int_0_to_200(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 200);
}
static char *set_int_32_to_63(const char *arg, int *i)