forked from fancyIX/sgminer-phi2-branch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_parser.c
2323 lines (1927 loc) · 63.4 KB
/
config_parser.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 2013-2014 sgminer developers (see AUTHORS.md)
* Copyright 2011-2013 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"
#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>
#include <sys/stat.h>
#include <sys/types.h>
#include <ccan/opt/opt.h>
#include <bosjansson.h>
#include <libgen.h>
#include <sha2.h>
#include "compat.h"
#include "miner.h"
#include "config_parser.h"
#include "driver-opencl.h"
#include "bench_block.h"
#include "algorithm.h"
#include "pool.h"
#ifdef HAVE_ADL
#include "adl.h"
#endif
#if defined(unix) || defined(__APPLE__)
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#endif
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
char *cnfbuf = NULL; //config file loaded
int fileconf_load; //config file load status
const char def_conf[] = "sgminer.conf";
char *default_config;
bool config_loaded;
//static int include_count;
int json_array_index = -1; //current array index being parsed
char *last_json_error = NULL; //last json_error
/*#define JSON_MAX_DEPTH 10
#define JSON_MAX_DEPTH_ERR "Too many levels of JSON includes (limit 10) or a loop"*/
/*******************************************
* Profile list functions
*******************************************/
struct profile default_profile;
struct profile **profiles;
int total_profiles;
static struct profile *add_profile()
{
struct profile *profile;
char buf[32];
if(!(profile = (struct profile *)calloc(sizeof(struct profile), 1)))
quit(1, "Failed to calloc profile in add_profile");
profile->profile_no = total_profiles;
//default profile name is the profile index
sprintf(buf, "%d", profile->profile_no);
profile->name = strdup(buf);
profile->algorithm.name[0] = '\0';
// intensity set to blank by default
buf[0] = 0;
profile->intensity = strdup(buf);
profile->xintensity = strdup(buf);
profile->rawintensity = strdup(buf);
profiles = (struct profile **)realloc(profiles, sizeof(struct profile *) * (total_profiles + 2));
profiles[total_profiles++] = profile;
return profile;
}
static void remove_profile(struct profile *profile)
{
int i;
int found = 0;
for(i = 0; i < (total_profiles - 1); i++)
{
//look for the profile
if(profiles[i]->profile_no == profile->profile_no)
found = 1;
//once we found the profile, change the current index profile to next
if(found)
{
profiles[i] = profiles[i+1];
profiles[i]->profile_no = i;
}
}
//give the profile an invalid number and remove
profile->profile_no = total_profiles;
profile->removed = true;
total_profiles--;
}
//only used while loading config
static struct profile *get_current_profile()
{
while ((json_array_index + 1) > total_profiles)
add_profile();
if (json_array_index < 0)
{
if (!total_profiles)
add_profile();
return profiles[total_profiles - 1];
}
return profiles[json_array_index];
}
//find a profile by name
static struct profile *get_profile(char *name)
{
int i;
if (empty_string(name)) {
return NULL;
}
for (i=0;i<total_profiles;++i) {
if (!safe_cmp(profiles[i]->name, name)) {
return profiles[i];
}
}
return NULL;
}
struct profile *get_gpu_profile(int gpuid)
{
struct profile *profile;
struct pool *pool = pools[gpus[gpuid].thr[0]->pool_no];
if (!(profile = get_profile(pool->profile))) {
if (!(profile = get_profile(default_profile.name))) {
profile = &default_profile;
}
}
return profile;
}
/******* Default profile functions used during config parsing *****/
char *set_default_algorithm(const char *arg)
{
set_algorithm(&default_profile.algorithm, arg);
applog(LOG_INFO, "Set default algorithm to %s", default_profile.algorithm.name);
return NULL;
}
char *set_default_nfactor(const char *arg)
{
set_algorithm_nfactor(&default_profile.algorithm, (const uint8_t) atoi(arg));
applog(LOG_INFO, "Set algorithm N-factor to %d (N to %d)", default_profile.algorithm.nfactor);
return NULL;
}
char *set_default_devices(const char *arg)
{
default_profile.devices = arg;
return NULL;
}
char *set_default_kernelfile(const char *arg)
{
applog(LOG_INFO, "Set default kernel file to %s", arg);
default_profile.algorithm.kernelfile = arg;
return NULL;
}
char *set_default_lookup_gap(const char *arg)
{
default_profile.lookup_gap = arg;
return NULL;
}
char *set_default_intensity(const char *arg)
{
opt_set_charp(arg, &default_profile.intensity);
return NULL;
}
char *set_default_xintensity(const char *arg)
{
opt_set_charp(arg, &default_profile.xintensity);
return NULL;
}
char *set_default_rawintensity(const char *arg)
{
opt_set_charp(arg, &default_profile.rawintensity);
return NULL;
}
char *set_default_thread_concurrency(const char *arg)
{
default_profile.thread_concurrency = arg;
return NULL;
}
#ifdef HAVE_ADL
char *set_default_gpu_engine(const char *arg)
{
default_profile.gpu_engine = arg;
return NULL;
}
char *set_default_gpu_memclock(const char *arg)
{
default_profile.gpu_memclock = arg;
return NULL;
}
char *set_default_gpu_threads(const char *arg)
{
default_profile.gpu_threads = arg;
return NULL;
}
char *set_default_gpu_fan(const char *arg)
{
default_profile.gpu_fan = arg;
return NULL;
}
char *set_default_gpu_powertune(const char *arg)
{
default_profile.gpu_powertune = arg;
return NULL;
}
char *set_default_gpu_vddc(const char *arg)
{
default_profile.gpu_vddc = arg;
return NULL;
}
#endif
char *set_default_profile(char *arg)
{
default_profile.name = arg;
return NULL;
}
char *set_default_shaders(const char *arg)
{
default_profile.shaders = arg;
return NULL;
}
char *set_default_worksize(const char *arg)
{
default_profile.worksize = arg;
return NULL;
}
/****** Profile functions used in during config parsing ********/
char *set_profile_name(const char *arg)
{
struct profile *profile = get_current_profile();
applog(LOG_DEBUG, "Setting profile %i name to %s", profile->profile_no, arg);
opt_set_charp(arg, &profile->name);
return NULL;
}
char *set_profile_algorithm(const char *arg)
{
struct profile *profile = get_current_profile();
applog(LOG_DEBUG, "Setting profile %s algorithm to %s", profile->name, arg);
set_algorithm(&profile->algorithm, arg);
return NULL;
}
char *set_profile_devices(const char *arg)
{
struct profile *profile = get_current_profile();
profile->devices = arg;
return NULL;
}
char *set_profile_kernelfile(const char *arg)
{
struct profile *profile = get_current_profile();
applog(LOG_DEBUG, "Setting profile %s algorithm kernel file to %s", profile->name, arg);
profile->algorithm.kernelfile = arg;
return NULL;
}
char *set_profile_lookup_gap(const char *arg)
{
struct profile *profile = get_current_profile();
profile->lookup_gap = arg;
return NULL;
}
char *set_profile_intensity(const char *arg)
{
struct profile *profile = get_current_profile();
opt_set_charp(arg, &profile->intensity);
return NULL;
}
char *set_profile_xintensity(const char *arg)
{
struct profile *profile = get_current_profile();
opt_set_charp(arg, &profile->xintensity);
return NULL;
}
char *set_profile_rawintensity(const char *arg)
{
struct profile *profile = get_current_profile();
opt_set_charp(arg, &profile->rawintensity);
return NULL;
}
char *set_profile_thread_concurrency(const char *arg)
{
struct profile *profile = get_current_profile();
profile->thread_concurrency = arg;
return NULL;
}
#ifdef HAVE_ADL
char *set_profile_gpu_engine(const char *arg)
{
struct profile *profile = get_current_profile();
profile->gpu_engine = arg;
return NULL;
}
char *set_profile_gpu_memclock(const char *arg)
{
struct profile *profile = get_current_profile();
profile->gpu_memclock = arg;
return NULL;
}
char *set_profile_gpu_threads(const char *arg)
{
struct profile *profile = get_current_profile();
profile->gpu_threads = arg;
return NULL;
}
char *set_profile_gpu_fan(const char *arg)
{
struct profile *profile = get_current_profile();
profile->gpu_fan = arg;
return NULL;
}
char *set_profile_gpu_powertune(const char *arg)
{
struct profile *profile = get_current_profile();
profile->gpu_powertune = arg;
return NULL;
}
char *set_profile_gpu_vddc(const char *arg)
{
struct profile *profile = get_current_profile();
profile->gpu_vddc = arg;
return NULL;
}
#endif
char *set_profile_nfactor(const char *arg)
{
struct profile *profile = get_current_profile();
applog(LOG_DEBUG, "Setting profile %s N-factor to %s", profile->name, arg);
set_algorithm_nfactor(&profile->algorithm, (const uint8_t) atoi(arg));
return NULL;
}
char *set_profile_shaders(const char *arg)
{
struct profile *profile = get_current_profile();
profile->shaders = arg;
return NULL;
}
char *set_profile_worksize(const char *arg)
{
struct profile *profile = get_current_profile();
profile->worksize = arg;
return NULL;
}
/***************************************
* Helper Functions
****************************************/
json_t *sgminer_json_sprintf(const char *fmt, ...)
{
va_list args;
char *buf;
size_t bufsize;
//build args
va_start(args, fmt);
//get final string buffer size
bufsize = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if(!(buf = (char *)malloc(++bufsize)))
quit(1, "Malloc failure in config_parser::sgminer_json_sprintf().");
//zero out buffer
memset(buf, '\0', bufsize);
//get args again
va_start(args, fmt);
vsnprintf(buf, bufsize, fmt, args);
va_end(args);
//return json string
return json_string(buf);
}
//set last json error
char *set_last_json_error(const char *fmt, ...)
{
va_list args;
size_t bufsize;
//build args
va_start(args, fmt);
//get final string buffer size
bufsize = vsnprintf(NULL, 0, fmt, args);
va_end(args);
//if NULL allocate memory... otherwise reallocate
if(!last_json_error)
{
if(!(last_json_error = (char *)malloc(++bufsize)))
quit(1, "Malloc failure in config_parser::set_last_json_error().");
}
else
{
if(!(last_json_error = (char *)realloc(last_json_error, ++bufsize)))
quit(1, "Realloc failure in config_parser::set_last_json_error().");
}
//zero out buffer
memset(last_json_error, '\0', bufsize);
//get args again
va_start(args, fmt);
vsnprintf(last_json_error, bufsize, fmt, args);
va_end(args);
return last_json_error;
}
//find opt by name in an opt table
static struct opt_table *opt_find(struct opt_table *tbl, char *optname)
{
struct opt_table *opt;
char *p, *name;
for(opt = tbl;opt->type != OPT_END;opt++)
{
/* We don't handle subtables. */
assert(!(opt->type & OPT_SUBTABLE));
if(!opt->names)
continue;
/* Pull apart the option name(s). */
name = strdup(opt->names);
for(p = strtok(name, "|");p;p = strtok(NULL, "|"))
{
/* Ignore short options. */
if(p[1] != '-')
continue;
//if this is the opt we're looking for, return it...
if(!strcasecmp(optname, p))
{
free(name);
return opt;
}
}
free(name);
}
return NULL;
}
/**************************************
* Remote Config Functions (Curl Only)
**************************************/
#ifdef HAVE_LIBCURL
struct remote_config {
const char *filename;
FILE *stream;
};
//curl file data write callback
static size_t fetch_remote_config_cb(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct remote_config *out = (struct remote_config *)stream;
//create file if not created
if(out && !out->stream)
{
if(!(out->stream = fopen(out->filename, "w+")))
return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
//download remote config file - return filename on success or NULL on failure
static char *fetch_remote_config(const char *url)
{
CURL *curl;
CURLcode res;
char *p;
struct remote_config file = { "", NULL };
//get filename out of url
if((p = (char *)strrchr(url, '/')) == NULL)
{
applog(LOG_ERR, "Fetch remote file failed: Invalid URL");
return NULL;
}
file.filename = p+1;
//check for empty filename
if(file.filename[0] == '\0')
{
applog(LOG_ERR, "Fetch remote file failed: Invalid Filename");
return NULL;
}
//init curl
if((curl = curl_easy_init()) == NULL)
{
applog(LOG_ERR, "Fetch remote file failed: curl init failed.");
return NULL;
}
//https stuff - skip verification we just want the data
if(strstr(url, "https") != NULL)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//set url
curl_easy_setopt(curl, CURLOPT_URL, url);
//set write callback and fileinfo
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); // fail on 404 or other 4xx http codes
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); // timeout after 30 secs to prevent being stuck
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file); // stream to write data to
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fetch_remote_config_cb); // callback function to write to config file
if((res = curl_easy_perform(curl)) != CURLE_OK)
applog(LOG_ERR, "Fetch remote file failed: %s", curl_easy_strerror(res));
if(file.stream)
fclose(file.stream);
curl_easy_cleanup(curl);
return (char *)((res == CURLE_OK)?file.filename:NULL);
}
#endif
/***************************************
* Config Parsing Functions
****************************************/
//Handle parsing JSON objects
void parse_config_object(json_t *obj, const char *parentkey, bool fileconf, int parent_iteration)
{
//char *err = NULL;
const char *key;
size_t idx;
json_t *val, *subval;
json_object_foreach(obj, key, val)
{
//process include
if(!strcasecmp(key, "include"))
{
if(val && json_is_string(val))
load_config(json_string_value(val), parentkey, NULL);
}
//process includes - multi include
else if(!strcasecmp(key, "includes"))
{
if(val && json_is_array(val))
{
json_array_foreach(val, idx, subval)
{
if(subval && json_is_string(subval))
load_config(json_string_value(subval), parentkey, NULL);
}
}
}
else
parse_config(val, key, parentkey, fileconf, parent_iteration);
/*
{
if((err = parse_config(val, key, parentkey, fileconf, parent_iteration)))
return err;
}*/
}
}
//Handle parsing JSON arrays
static char *parse_config_array(json_t *obj, char *parentkey, bool fileconf)
{
char *err = NULL;
size_t idx;
json_t *val;
//fix parent key - remove extra "s" to match opt names (e.g. --pool-gpu-memclock not --pools-gpu-memclock)
if(!strcasecmp(parentkey, "pools") || !strcasecmp(parentkey, "profiles") || !strcasecmp(parentkey, "events"))
parentkey[(strlen(parentkey) - 1)] = '\0';
json_array_foreach(obj, idx, val)
{
//abort on error
if((err = parse_config(val, "", parentkey, fileconf, idx)))
return err;
}
return NULL;
}
//Parse JSON values from config file
char *parse_config(json_t *val, const char *key, const char *parentkey, bool fileconf, int parent_iteration)
{
static char err_buf[200];
char *err = NULL;
struct opt_table *opt;
char optname[255];
/*const char *key
json_t *val;*/
json_array_index = parent_iteration;
if (fileconf && !fileconf_load)
fileconf_load = 1;
/*
parse the json config items into opts instead of looking for opts in the json config...
This adds greater flexibility with config files.
*/
switch(json_typeof(val))
{
//json value is an object
case JSON_OBJECT:
parse_config_object(val, parentkey, false, parent_iteration);
break;
//json value is an array
case JSON_ARRAY:
err = parse_config_array(val, (char *)key, fileconf);
break;
//other json types process here
default:
//convert json key to opt name
sprintf(optname, "--%s%s%s", ((!empty_string(parentkey))?parentkey:""), ((!empty_string(parentkey))?"-":""), key);
//look up opt name in config table
if((opt = opt_find(opt_config_table, optname)) != NULL)
{
//strings
if ((opt->type & OPT_HASARG) && json_is_string(val)) {
err = opt->cb_arg(json_string_value(val), opt->u.arg);
}
//boolean values
else if ((opt->type & OPT_NOARG) && json_is_true(val)) {
err = opt->cb(opt->u.arg);
}
else {
err = "Invalid value";
}
}
else {
err = "Invalid option";
}
break;
}
//error processing JSON...
if(err)
{
/* Allow invalid values to be in configuration
* file, just skipping over them provided the
* JSON is still valid after that. */
if(fileconf)
{
applog(LOG_WARNING, "Skipping config option %s: %s", optname+2, err);
fileconf_load = -1;
}
else
{
snprintf(err_buf, sizeof(err_buf), "Error parsing JSON option %s: %s", optname+2, err);
return err_buf;
}
}
return NULL;
}
char *load_config(const char *arg, const char *parentkey, void __maybe_unused *unused)
{
json_error_t err;
json_t *config;
#ifdef HAVE_LIBCURL
int retry = opt_remoteconf_retry;
const char *url;
// if detected as url
if ((strstr(arg, "http://") != NULL) || (strstr(arg, "https://") != NULL) || (strstr(arg, "ftp://") != NULL)) {
url = strdup(arg);
do {
// wait for next retry
if (retry < opt_remoteconf_retry) {
sleep(opt_remoteconf_wait);
}
// download config file locally and reset arg to it so we can parse it
if ((arg = fetch_remote_config(url)) != NULL) {
break;
}
--retry;
} while (retry);
// file not downloaded... abort
if (arg == NULL) {
// if we should use last downloaded copy...
if (opt_remoteconf_usecache) {
char *p;
// extract filename out of url
if ((p = (char *)strrchr(url, '/')) == NULL) {
quit(1, "%s: invalid URL.", url);
}
arg = p+1;
} else {
quit(1, "%s: unable to download config file.", url);
}
}
}
#endif
// most likely useless but leaving it here for now...
if (!cnfbuf) {
cnfbuf = strdup(arg);
}
// no need to restrict the number of includes... if it causes problems, restore it later
/*if(++include_count > JSON_MAX_DEPTH)
return JSON_MAX_DEPTH_ERR;
*/
// check if the file exists
if (access(arg, F_OK) == -1) {
quit(1, "%s: file not found.", arg);
}
#if JANSSON_MAJOR_VERSION > 1
config = json_load_file(arg, 0, &err);
#else
config = json_load_file(arg, &err);
#endif
// if json root is not an object, error out
if (!json_is_object(config)) {
return set_last_json_error("Error: JSON decode of file \"%s\" failed:\n %s", arg, err.text);
}
config_loaded = true;
/* Parse the config now, so we can override it. That can keep pointers
* so don't free config object. */
return parse_config(config, "", parentkey, true, -1);
}
char *set_default_config(const char *arg)
{
opt_set_charp(arg, &default_config);
return NULL;
}
void load_default_config(void)
{
cnfbuf = (char *)malloc(PATH_MAX);
default_save_file(cnfbuf);
if (!access(cnfbuf, R_OK))
load_config(cnfbuf, "", NULL);
else {
free(cnfbuf);
cnfbuf = NULL;
}
}
/*******************************************
* Startup functions
* *****************************************/
void init_default_profile()
{
char buf[32];
buf[0] = 0;
default_profile.name = strdup(buf);
default_profile.algorithm.name[0] = 0;
default_profile.algorithm.kernelfile = strdup(buf);
default_profile.intensity = strdup(buf);
default_profile.xintensity = strdup(buf);
default_profile.rawintensity = strdup(buf);
}
// assign default settings from default profile if set
void load_default_profile()
{
struct profile *profile;
if (empty_string(default_profile.name))
return;
applog(LOG_DEBUG, "default_profile.name is %s", default_profile.name);
// find profile ...
if(!(profile = get_profile(default_profile.name)))
{
applog(LOG_WARNING, "Could not load default profile %s", default_profile.name);
return;
}
// ... and copy settings
if(!empty_string(profile->algorithm.name))
default_profile.algorithm = profile->algorithm;
if(!empty_string(profile->devices))
default_profile.devices = profile->devices;
if(!empty_string(profile->lookup_gap))
default_profile.lookup_gap = profile->lookup_gap;
if(!empty_string(profile->intensity))
default_profile.intensity = profile->intensity;
if(!empty_string(profile->xintensity))
default_profile.xintensity = profile->xintensity;
if(!empty_string(profile->rawintensity))
default_profile.rawintensity = profile->rawintensity;
if(!empty_string(profile->thread_concurrency))
default_profile.thread_concurrency = profile->thread_concurrency;
#ifdef HAVE_ADL
if(!empty_string(profile->gpu_engine))
default_profile.gpu_engine = profile->gpu_engine;
if(!empty_string(profile->gpu_memclock))
default_profile.gpu_memclock = profile->gpu_memclock;
if(!empty_string(profile->gpu_threads))
default_profile.gpu_threads = profile->gpu_threads;
if(!empty_string(profile->gpu_fan))
default_profile.gpu_fan = profile->gpu_fan;
if(!empty_string(profile->gpu_powertune))
default_profile.gpu_powertune = profile->gpu_powertune;
if(!empty_string(profile->gpu_vddc))
default_profile.gpu_vddc = profile->gpu_vddc;
#endif
if(!empty_string(profile->shaders))
default_profile.shaders = profile->shaders;
if(!empty_string(profile->worksize))
default_profile.worksize = profile->worksize;
}
//apply default settings
void apply_defaults()
{
//if no algorithm specified, use scrypt as default
if (empty_string(default_profile.algorithm.name))
set_algorithm(&default_profile.algorithm, "scrypt");
//by default all unless specified
if(empty_string(default_profile.devices))
default_profile.devices = strdup("all");
applog(LOG_DEBUG, "Default Devices = %s", default_profile.devices);
set_devices((char *)default_profile.devices);
//set raw intensity first
if (!empty_string(default_profile.rawintensity))
set_rawintensity(default_profile.rawintensity);
//then try xintensity
else if (!empty_string(default_profile.xintensity))
set_xintensity(default_profile.xintensity);
//then try intensity
else if (!empty_string(default_profile.intensity))
set_intensity(default_profile.intensity);
if (!empty_string(default_profile.lookup_gap))
set_lookup_gap((char *)default_profile.lookup_gap);
if (!empty_string(default_profile.thread_concurrency))
set_thread_concurrency(default_profile.thread_concurrency);
#ifdef HAVE_ADL
if (!empty_string(default_profile.gpu_engine))
set_gpu_engine(default_profile.gpu_engine);
if (!empty_string(default_profile.gpu_memclock))
set_gpu_memclock(default_profile.gpu_memclock);
if (!empty_string(default_profile.gpu_threads))
set_gpu_threads(default_profile.gpu_threads);
if (!empty_string(default_profile.gpu_fan))
set_gpu_fan(default_profile.gpu_fan);
if (!empty_string(default_profile.gpu_powertune))
set_gpu_powertune((char *)default_profile.gpu_powertune);
if (!empty_string(default_profile.gpu_vddc))
set_gpu_vddc((char *)default_profile.gpu_vddc);
#endif
if (!empty_string(default_profile.shaders))
set_shaders((char *)default_profile.shaders);
if (!empty_string(default_profile.worksize))
set_worksize((char *)default_profile.worksize);
}
//apply profile settings to pools
void apply_pool_profiles()
{
int i;