Commit 2384d0fbc6b2040507dd1af8c45cd297ed5526e1

Paul Sheppard 2012-06-12T18:20:18

Removed idle mode... again!? Changed work_reset rate to every 100ms & use defines rather than numerics. Change total sleep time to 4300ms after performance testing/measurement (now good to 916MH/s). Changed timeout to 15s - Throttle time is 15s. Added support for 'Busy' when submitting work. Improved logging text to include device identifier. Added a thread_enable api function, called when a thread is re-enabled. ... and removed useless re-init inside scanhash. Use modminer introduced mutex.

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
diff --git a/api.c b/api.c
index c71b949..6a4d715 100644
--- a/api.c
+++ b/api.c
@@ -465,7 +465,7 @@ struct CODES {
  { SEVERITY_SUCC,  MSG_GPUFAN,	PARAM_BOTH,	"Setting GPU %d fan to (%s) reported succeess" },
  { SEVERITY_ERR,   MSG_MISFN,	PARAM_NONE,	"Missing save filename parameter" },
  { SEVERITY_ERR,   MSG_BADFN,	PARAM_STR,	"Can't open or create save file '%s'" },
- { SEVERITY_ERR,   MSG_SAVED,	PARAM_STR,	"Configuration saved to file '%s'" },
+ { SEVERITY_SUCC,  MSG_SAVED,	PARAM_STR,	"Configuration saved to file '%s'" },
  { SEVERITY_ERR,   MSG_ACCDENY,	PARAM_STR,	"Access denied to '%s' command" },
  { SEVERITY_SUCC,  MSG_ACCOK,	PARAM_NONE,	"Privileged access OK" },
  { SEVERITY_SUCC,  MSG_ENAPOOL,	PARAM_POOL,	"Enabling pool %d:'%s'" },
@@ -879,7 +879,7 @@ static void pgastatus(int pga, bool isjson)
 
 		cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
 
-		if (cgpu->deven == DEV_ENABLED)
+		if (cgpu->deven != DEV_DISABLED)
 			enabled = (char *)YES;
 		else
 			enabled = (char *)NO;
@@ -1092,7 +1092,7 @@ static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
 
 	struct cgpu_info *cgpu = devices[dev];
 
-	if (cgpu->deven == DEV_ENABLED) {
+	if (cgpu->deven != DEV_DISABLED) {
 		strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
 		return;
 	}
@@ -1143,12 +1143,12 @@ static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
 
 	struct cgpu_info *cgpu = devices[dev];
 
-	if (cgpu->deven != DEV_ENABLED) {
+	if (cgpu->deven == DEV_DISABLED) {
 		strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
 		return;
 	}
 
-	cgpu->deven = DEV_IDLE;
+	cgpu->deven = DEV_DISABLED;
 
 	strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
 }
@@ -1979,12 +1979,13 @@ static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, 
 
 void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
 {
+	char filename[PATH_MAX];
 	FILE *fcfg;
 	char *ptr;
 
 	if (param == NULL || *param == '\0') {
-		strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
-		return;
+		default_save_file(filename);
+		param = filename;
 	}
 
 	fcfg = fopen(param, "w");
@@ -2584,4 +2585,3 @@ die:
 
 	mutex_unlock(&quit_restart_lock);
 }
-
diff --git a/cgminer.c b/cgminer.c
index d12a009..ceda7a6 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3959,7 +3959,7 @@ void *miner_thread(void *userdata)
 				tv_lastupdate = tv_end;
 			}
 
-			if (unlikely(mythr->pause || cgpu->deven == DEV_DISABLED || cgpu->deven == DEV_RECOVER)) {
+			if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED)) {
 				applog(LOG_WARNING, "Thread %d being disabled", thr_id);
 disabled:
 				mythr->rolling = mythr->cgpu->rolling = 0;
@@ -3968,6 +3968,7 @@ disabled:
 				tq_pop(mythr->q, NULL); /* Ignore ping that's popped */
 				thread_reportin(mythr);
 				applog(LOG_WARNING, "Thread %d being re-enabled", thr_id);
+				if (api->thread_enable) api->thread_enable(mythr);
 			}
 
 			sdiff.tv_sec = sdiff.tv_usec = 0;
diff --git a/driver-bitforce.c b/driver-bitforce.c
index 8067cca..0a2fc82 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -20,9 +20,13 @@
 #include "fpgautils.h"
 #include "miner.h"
 
-#define BITFORCE_SLEEP_US 4500000
-#define BITFORCE_SLEEP_MS (BITFORCE_SLEEP_US/1000)
-#define BITFORCE_TIMEOUT_MS 30000
+/* @832MH/s wait time is ~4500ms 
+   @864MH/s wait time is ~4430ms 
+-> @896MH/s wait time will be ~4350ms */
+#define BITFORCE_SLEEP_MS 4300
+#define BITFORCE_TIMEOUT_MS 15000
+#define BITFORCE_CHECK_INTERVAL_MS 10
+#define WORK_CHECK_INTERVAL_MS 100
 
 struct device_api bitforce_api;
 
@@ -44,7 +48,7 @@ static ssize_t BFwrite2(int fd, const void *buf, ssize_t bufLen)
 
 #define BFwrite(fd, buf, bufLen)  do {  \
 	if ((bufLen) != BFwrite2(fd, buf, bufLen)) {  \
-		applog(LOG_ERR, "Error writing to BitForce (" #buf ")");  \
+		applog(LOG_ERR, "BFL: Error writing (" #buf ")");  \
 		return 0;  \
 	}  \
 } while(0)
@@ -58,18 +62,18 @@ static bool bitforce_detect_one(const char *devpath)
 
 	int fdDev = BFopen(devpath);
 	if (unlikely(fdDev == -1)) {
-		applog(LOG_ERR, "BitForce Detect: Failed to open %s", devpath);
+		applog(LOG_ERR, "BFL: Failed to open %s", devpath);
 		return false;
 	}
 	BFwrite(fdDev, "ZGX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (ZGX)");
+		applog(LOG_ERR, "BFL: Error reading (ZGX)");
 		return 0;
 	}
 	BFclose(fdDev);
 	if (unlikely(!strstr(pdevbuf, "SHA256"))) {
-		applog(LOG_ERR, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
+		applog(LOG_ERR, "BFL: Didn't recognise BitForce on %s", devpath);
 		return false;
 	}
 
@@ -86,7 +90,7 @@ static bool bitforce_detect_one(const char *devpath)
 		bitforce->name = strdup(pdevbuf + 7);
 	}
 	
-	mutex_init(&bitforce->dev_lock);
+	mutex_init(&bitforce->device_mutex);
 
 	return add_cgpu(bitforce);
 }
@@ -122,13 +126,13 @@ static bool bitforce_thread_prepare(struct thr_info *thr)
 
 	int fdDev = BFopen(bitforce->device_path);
 	if (unlikely(-1 == fdDev)) {
-		applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
+		applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, bitforce->device_path);
 		return false;
 	}
 
 	bitforce->device_fd = fdDev;
 
-	applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
+	applog(LOG_INFO, "BFL%i: Opened %s", bitforce->device_id, bitforce->device_path);
 	gettimeofday(&now, NULL);
 	get_datestamp(bitforce->init, &now);
 
@@ -142,30 +146,33 @@ static bool bitforce_init(struct cgpu_info *bitforce)
 	char pdevbuf[0x100];
 	char *s;
 
-	applog(LOG_DEBUG, "BFL%i: Re-initalizing", bitforce->device_id);
+	applog(LOG_INFO, "BFL%i: Re-initalizing", bitforce->device_id);
 
-	BFclose(fdDev);
+	if (fdDev) {
+		BFclose(fdDev);
+		bitforce->device_fd = 0;
+	}
 
 	fdDev = BFopen(devpath);
 	if (unlikely(fdDev == -1)) {
-		applog(LOG_ERR, "BitForce init: Failed to open %s", devpath);
+		applog(LOG_ERR, "BFL%i: Failed to open %s", bitforce->device_id, devpath);
 		return false;
 	}
 
 	bitforce->device_fd = fdDev;
 
-	mutex_lock(&bitforce->dev_lock);
+	mutex_lock(&bitforce->device_mutex);
 	BFwrite(fdDev, "ZGX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
-	mutex_unlock(&bitforce->dev_lock);
+	mutex_unlock(&bitforce->device_mutex);
 	
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (ZGX)");
+		applog(LOG_ERR, "BFL%i: Error reading (ZGX)", bitforce->device_id);
 		return false;
 	}
 
 	if (unlikely(!strstr(pdevbuf, "SHA256"))) {
-		applog(LOG_ERR, "BitForce init: Didn't recognise BitForce on %s", devpath);
+		applog(LOG_ERR, "BFL%i: Didn't recognise BitForce on %s", bitforce->device_id, devpath);
 		return false;
 	}
 	
@@ -184,13 +191,13 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
 	char pdevbuf[0x100];
 	char *s;
 
-	mutex_lock(&bitforce->dev_lock);
+	mutex_lock(&bitforce->device_mutex);
 	BFwrite(fdDev, "ZLX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
-	mutex_unlock(&bitforce->dev_lock);
+	mutex_unlock(&bitforce->device_mutex);
 	
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading temp from BitForce (ZLX)");
+		applog(LOG_ERR, "BFL%i: Error reading (ZLX)", bitforce->device_id);
 		return false;
 	}
 	if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
@@ -198,7 +205,7 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
 		if (temp > 0) {
 			bitforce->temp = temp;
 			if (temp > bitforce->cutofftemp) {
-				applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
+				applog(LOG_WARNING, "BFL%i: Hit thermal cutoff limit, disabling!", bitforce->device_id);
 				bitforce->deven = DEV_RECOVER;
 
 				bitforce->device_last_not_well = time(NULL);
@@ -218,17 +225,22 @@ static bool bitforce_send_work(struct thr_info *thr, struct work *work)
 	unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
 	char *s;
 
-	mutex_lock(&bitforce->dev_lock);
+	mutex_lock(&bitforce->device_mutex);
 	BFwrite(fdDev, "ZDX", 3);
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (ZDX)");
-		mutex_unlock(&bitforce->dev_lock);
+		applog(LOG_ERR, "BFL%i: Error reading (ZDX)", bitforce->device_id);
+		mutex_unlock(&bitforce->device_mutex);
 		return false;
 	}
-	if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
-		applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
-		mutex_unlock(&bitforce->dev_lock);
+	if (pdevbuf[0] == 'B'){
+		applog(LOG_WARNING, "BFL%i: Throttling", bitforce->device_id);
+		mutex_unlock(&bitforce->device_mutex);
+		return true;
+	}
+	else if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
+		applog(LOG_ERR, "BFL%i: ZDX reports: %s", bitforce->device_id, pdevbuf);
+		mutex_unlock(&bitforce->device_mutex);
 		return false;
 	}
 	memcpy(ob + 8, work->midstate, 32);
@@ -237,17 +249,17 @@ static bool bitforce_send_work(struct thr_info *thr, struct work *work)
 	BFwrite(fdDev, ob, 60);
 	if (opt_debug) {
 		s = bin2hex(ob + 8, 44);
-		applog(LOG_DEBUG, "BitForce block data: %s", s);
+		applog(LOG_DEBUG, "BFL%i: block data: %s", bitforce->device_id, s);
 		free(s);
 	}
 	BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
-	mutex_unlock(&bitforce->dev_lock);
+	mutex_unlock(&bitforce->device_mutex);
 	if (unlikely(!pdevbuf[0])) {
-		applog(LOG_ERR, "Error reading from BitForce (block data)");
+		applog(LOG_ERR, "BFL%i: Error reading (block data)", bitforce->device_id);
 		return false;
 	}
 	if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
-		applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
+		applog(LOG_ERR, "BFL%i: block data reports: %s", bitforce->device_id, pdevbuf);
 		return false;
 	}
 	return true;
@@ -261,41 +273,39 @@ static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 	char pdevbuf[0x100];
 	char *pnoncebuf;
 	uint32_t nonce;
-	int i;
+	unsigned int wait_ms = BITFORCE_SLEEP_MS;
 
-	i = BITFORCE_SLEEP_MS;
-	while (i < BITFORCE_TIMEOUT_MS) {
-    	mutex_lock(&bitforce->dev_lock);
+	while (wait_ms < BITFORCE_TIMEOUT_MS) {
+		mutex_lock(&bitforce->device_mutex);
 		BFwrite(fdDev, "ZFX", 3);
 		BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
-	    mutex_unlock(&bitforce->dev_lock);
+		mutex_unlock(&bitforce->device_mutex);
 		if (unlikely(!pdevbuf[0])) {
-			applog(LOG_ERR, "Error reading from BitForce (ZFX)");
-	    	mutex_unlock(&bitforce->dev_lock);
+			applog(LOG_ERR, "BFL%i: Error reading (ZFX)", bitforce->device_id);
+			mutex_unlock(&bitforce->device_mutex);
 			return 0;
 		}
 		if (pdevbuf[0] != 'B')
 			break;
-		usleep(10000);
-		i += 10;
+		usleep(BITFORCE_CHECK_INTERVAL_MS*1000);
+		wait_ms += BITFORCE_CHECK_INTERVAL_MS;
 	}
-
-	if (i >= BITFORCE_TIMEOUT_MS) {
-		applog(LOG_ERR, "BitForce took longer than 30s");
+	if (wait_ms >= BITFORCE_TIMEOUT_MS) {
+		applog(LOG_ERR, "BFL%i took longer than 15s");
 		bitforce->device_last_not_well = time(NULL);
 		bitforce->device_not_well_reason = REASON_THREAD_ZERO_HASH;
 		bitforce->thread_zero_hash_count++;
 		return 1;
 	}
 
-	applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
+	applog(LOG_DEBUG, "BFL%i: waited %dms until %s\n", bitforce->device_id, wait_ms, pdevbuf);
 	work->blk.nonce = 0xffffffff;
 	if (pdevbuf[2] == '-') 
 		return 0xffffffff;   /* No valid nonce found */
 	else if (pdevbuf[0] == 'I') 
 		return 1;          /* Device idle */
 	else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
-		applog(LOG_WARNING, "BitForce result reports: %s", pdevbuf);
+		applog(LOG_WARNING, "BFL%i: result reports: %s", bitforce->device_id, pdevbuf);
 		return 1;
 	}
 
@@ -319,42 +329,34 @@ static uint64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 static void bitforce_shutdown(struct thr_info *thr)
 {
 	struct cgpu_info *bitforce = thr->cgpu;
-	int fdDev = bitforce->device_fd;
 
-	BFclose(fdDev);
+	BFclose(bitforce->device_fd);
+	bitforce->device_fd = 0;
 }
 
-#define CHECK_INTERVAL_MS 200
+static void biforce_thread_enable(struct thr_info *thr)
+{
+	struct cgpu_info *bitforce = thr->cgpu;
+
+	bitforce_init(bitforce);
+}
 
 static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
 {
 	struct cgpu_info *bitforce = thr->cgpu;
-	bool dev_enabled = (bitforce->deven == DEV_ENABLED);
-	static enum dev_enable last_dev_state = DEV_ENABLED;
-	int wait_ms = 0;
-	
-	if (bitforce->deven == DEV_DISABLED) {
-		bitforce_shutdown(thr);
-		return 1;
-	}
-	
-	// if device has just gone from disabled to enabled, re-initialise it
-	if (last_dev_state == DEV_DISABLED && dev_enabled)
-		bitforce_init(bitforce);
-	last_dev_state = bitforce->deven;
+	unsigned int wait_ms = 0;
 
 	if (!bitforce_send_work(thr, work))
 		return 0;
-
+	
 	while (wait_ms < BITFORCE_SLEEP_MS) {
-		usleep(CHECK_INTERVAL_MS * 1000);
-		wait_ms += CHECK_INTERVAL_MS;
+		usleep(WORK_CHECK_INTERVAL_MS*1000);
+		wait_ms += WORK_CHECK_INTERVAL_MS;
 		if (work_restart[thr->id].restart) {
-			applog(LOG_DEBUG, "BFL%i: New work detected, discarding current job", bitforce->device_id);
-		    return 1; //we have discard all work; equivilent to 0 hashes done.
+			applog(LOG_DEBUG, "BFL%i: Work restart, discarding after %dms", bitforce->device_id, wait_ms);
+			return 1; //we have discarded all work; equivilent to 0 hashes done.
 		}
 	}
-
 	return bitforce_get_result(thr, work);
 }
 
@@ -367,9 +369,11 @@ struct device_api bitforce_api = {
 	.dname = "bitforce",
 	.name = "BFL",
 	.api_detect = bitforce_detect,
+	.reinit_device = bitforce_init,
 	.get_statline_before = get_bitforce_statline_before,
 	.get_stats = bitforce_get_stats,
 	.thread_prepare = bitforce_thread_prepare,
 	.scanhash = bitforce_scanhash,
-	.thread_shutdown = bitforce_shutdown
+	.thread_shutdown = bitforce_shutdown,
+	.thread_enable = biforce_thread_enable
 };
diff --git a/miner.h b/miner.h
index 6e9ea70..a71447b 100644
--- a/miner.h
+++ b/miner.h
@@ -245,13 +245,13 @@ struct device_api {
 	bool (*prepare_work)(struct thr_info*, struct work*);
 	uint64_t (*scanhash)(struct thr_info*, struct work*, uint64_t);
 	void (*thread_shutdown)(struct thr_info*);
+	void (*thread_enable)(struct thr_info*);
 };
 
 enum dev_enable {
 	DEV_ENABLED,
 	DEV_DISABLED,
 	DEV_RECOVER,
-	DEV_IDLE,
 };
 
 enum cl_kernels {
@@ -377,8 +377,6 @@ struct cgpu_info {
 	int dev_thermal_cutoff_count;
 
 	struct cgminer_stats cgminer_stats;
-	
-	pthread_mutex_t dev_lock;
 };
 
 extern bool add_cgpu(struct cgpu_info*);