Commit e76f2d03c992c3afe301951022da2a80a8bc746c

kanoi 2014-06-26T20:36:12

minion - add an ioseq number for each ioctl to simplify work ordering

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
diff --git a/driver-minion.c b/driver-minion.c
index 61e7a33..94b6e53 100644
--- a/driver-minion.c
+++ b/driver-minion.c
@@ -499,7 +499,7 @@ typedef struct witem {
 	bool rolled;
 	int errors; // uncertain since the error could mean task_id is wrong
 	struct timeval created; // when work was generated
-	struct timeval txrx; // when work was sent to the chip
+	uint64_t ioseq;
 } WITEM;
 
 #define ALLOC_TITEMS 256
@@ -522,6 +522,7 @@ typedef struct titem {
 	uint8_t work_state;
 	struct work *work;
 	K_ITEM *witem;
+	uint64_t ioseq;
 } TITEM;
 
 #define ALLOC_RITEMS 256
@@ -704,6 +705,7 @@ struct minion_info {
 	uint8_t chipid[MINION_CHIPS]; // Chip Number
 	int chip_pin[MINION_CHIPS];
 
+	uint64_t ioseq;
 	uint32_t next_task_id;
 
 	// Stats
@@ -1077,17 +1079,13 @@ static void init_pins(struct minion_info *minioninfo)
 }
 
 #define EXTRA_LOG_IO 0
-#if EXTRA_LOG_IO
-static int ioc = 0;
-#endif
 
-static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
+static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf,
+		      uint32_t osiz, uint8_t *rbuf, uint32_t rsiz,
+		      uint64_t *ioseq, MINION_FFL_ARGS)
 {
 	struct spi_ioc_transfer tran;
 	int ret;
-#if EXTRA_LOG_IO
-	int myioc;
-#endif
 #if MINION_SHOW_IO
 	char dataw[DATA_ALL], datar[DATA_ALL];
 #endif
@@ -1152,9 +1150,7 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 	}
 	IO_STAT_NOW(&sta);
 	ret = ioctl(minioninfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran);
-#if EXTRA_LOG_IO
-	myioc = ioc++;
-#endif
+	*ioseq = minioninfo->ioseq++;
 	IO_STAT_NOW(&fin);
 	if (usepins) {
 		MINION_PIN_AFTER;
@@ -1200,16 +1196,16 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 				c = 'F';
 				break;
 		}
-		applog(LOG_WARNING, "*** seq %d cmd %c %s rep %.8s %s",
-				    myioc, c, uf1, uf2, uf2+8);
+		applog(LOG_WARNING, "*** ioseq %"PRIu64" cmd %c %s rep %.8s %s",
+				    *ioseq, c, uf1, uf2, uf2+8);
 		free(uf2);
 		free(uf1);
 	}
 	if (obuf[1] == WRITE_ADDR(MINION_QUE_0)) {
 		char *uf;
 		uf = bin2hex(obuf, osiz);
-		applog(LOG_WARNING, "*** seq %d work %s",
-				    myioc, uf);
+		applog(LOG_WARNING, "*** ioseq %"PRIu64" work %s",
+				    *ioseq, uf);
 		free(uf);
 	}
 #endif
@@ -1217,11 +1213,15 @@ static int __do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, ui
 }
 
 #if 1
-#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz) __do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, _rsiz, MINION_FFL_HERE)
+#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz, _ioseq) \
+		__do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, \
+			   _rsiz, _ioseq, MINION_FFL_HERE)
 #else
-#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz) _do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, _rsiz, MINION_FFL_HERE)
+#define do_ioctl(_pin, _obuf, _osiz, _rbuf, _rsiz, _ioseq) \
+		_do_ioctl(minioninfo, _pin, _obuf, _osiz, _rbuf, \
+			  _rsiz, _ioseq, MINION_FFL_HERE)
 // This sends an expected to work, SPI command before each SPI command
-static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, MINION_FFL_ARGS)
+static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uint32_t osiz, uint8_t *rbuf, uint32_t rsiz, uint64_t *ioseq, MINION_FFL_ARGS)
 {
 	struct minion_header *head;
 	uint8_t buf1[MINION_BUFSIZ];
@@ -1233,9 +1233,9 @@ static int _do_ioctl(struct minion_info *minioninfo, int pin, uint8_t *obuf, uin
 	head->reg = READ_ADDR(MINION_SYS_FIFO_STA);
 	SET_HEAD_SIZ(head, DATA_SIZ);
 	siz = HSIZE() + DATA_SIZ;
-	__do_ioctl(minioninfo, pin, buf1, siz, buf2, MINION_CORE_SIZ, MINION_FFL_PASS);
+	__do_ioctl(minioninfo, pin, buf1, siz, buf2, MINION_CORE_SIZ, ioseq, MINION_FFL_PASS);
 
-	return __do_ioctl(minioninfo, pin, obuf, osiz, rbuf, rsiz, MINION_FFL_PASS);
+	return __do_ioctl(minioninfo, pin, obuf, osiz, rbuf, rsiz, ioseq, MINION_FFL_PASS);
 }
 #endif
 
@@ -1256,7 +1256,8 @@ static bool _minion_txrx(struct cgpu_info *minioncgpu, struct minion_info *minio
 		memcpy(&(head->data[0]), task->wbuf, task->wsiz);
 	task->osiz = HSIZE() + task->wsiz + task->rsiz;
 
-	task->reply = do_ioctl(CHIP_PIN(task->chip), task->obuf, task->osiz, task->rbuf, task->rsiz);
+	task->reply = do_ioctl(CHIP_PIN(task->chip), task->obuf, task->osiz, task->rbuf, task->rsiz,
+			       &(task->ioseq));
 	if (task->reply < 0) {
 		applog(LOG_ERR, "%s%d: chip=%d ioctl failed reply=%d err=%d" MINION_FFL,
 				minioncgpu->drv->name, minioncgpu->device_id,
@@ -1276,6 +1277,7 @@ static int build_cmd(struct cgpu_info *minioncgpu, struct minion_info *minioninf
 	struct minion_header *head;
 	uint8_t wbuf[MINION_BUFSIZ];
 	uint32_t wsiz;
+	uint64_t ioseq;
 	int reply;
 
 	head = (struct minion_header *)wbuf;
@@ -1289,7 +1291,7 @@ static int build_cmd(struct cgpu_info *minioncgpu, struct minion_info *minioninf
 	head->data[3] = data[3];
 
 	wsiz = HSIZE() + DATA_SIZ;
-	reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz);
+	reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz, &ioseq);
 
 	if (reply != (int)wsiz) {
 		applog(LOG_ERR, "%s: chip %d %s returned %d (should be %d)",
@@ -1551,6 +1553,7 @@ static void minion_detect_one(struct cgpu_info *minioncgpu, struct minion_info *
 	uint8_t rbuf[MINION_BUFSIZ];
 	uint32_t wsiz, rsiz;
 	int reply, tries, newchip;
+	uint64_t ioseq;
 	bool ok;
 
 	head = (struct minion_header *)wbuf;
@@ -1563,7 +1566,7 @@ static void minion_detect_one(struct cgpu_info *minioncgpu, struct minion_info *
 	tries = 0;
 	ok = false;
 	do {
-		reply = do_ioctl(pin, wbuf, wsiz, rbuf, rsiz);
+		reply = do_ioctl(pin, wbuf, wsiz, rbuf, rsiz, &ioseq);
 
 		if (reply == (int)(wsiz)) {
 			uint32_t sig = u8tou32(rbuf, wsiz - rsiz);
@@ -2322,22 +2325,24 @@ static void *minion_spi_write(void *userdata)
 			}
 
 			if (do_txrx) {
-				cgtime(&now);
-				howlong = tdiff(&now, &(DATAW(titem->witem)->created));
-				minioninfo->wt_work++;
-				minioninfo->wt_time += howlong;
-				if (minioninfo->wt_min == 0 || minioninfo->wt_min > howlong)
-					minioninfo->wt_min = howlong;
-				else if (minioninfo->wt_max < howlong)
-					minioninfo->wt_max = howlong;
-				for (i = 0; i < TIME_BANDS; i++) {
-					if (howlong < time_bands[i]) {
-						minioninfo->wt_bands[i]++;
-						break;
+				if (titem->witem) {
+					cgtime(&now);
+					howlong = tdiff(&now, &(DATAW(titem->witem)->created));
+					minioninfo->wt_work++;
+					minioninfo->wt_time += howlong;
+					if (minioninfo->wt_min == 0 || minioninfo->wt_min > howlong)
+						minioninfo->wt_min = howlong;
+					else if (minioninfo->wt_max < howlong)
+						minioninfo->wt_max = howlong;
+					for (i = 0; i < TIME_BANDS; i++) {
+						if (howlong < time_bands[i]) {
+							minioninfo->wt_bands[i]++;
+							break;
+						}
 					}
+					if (i >= TIME_BANDS)
+						minioninfo->wt_bands[TIME_BANDS]++;
 				}
-				if (i >= TIME_BANDS)
-					minioninfo->wt_bands[TIME_BANDS]++;
 
 				minion_txrx(titem);
 
@@ -2434,7 +2439,7 @@ static void *minion_spi_write(void *userdata)
 						K_WLOCK(minioninfo->wchip_list[chip]);
 						k_unlink_item(minioninfo->wque_list[chip], titem->witem);
 						k_add_head(minioninfo->wchip_list[chip], titem->witem);
-						cgtime(&(DATAW(titem->witem)->txrx));
+						DATAW(titem->witem)->ioseq = titem->ioseq;
 						minioninfo->chip_status[chip].quework--;
 						minioninfo->chip_status[chip].chipwork++;
 #if MINION_SHOW_IO
@@ -2445,6 +2450,9 @@ static void *minion_spi_write(void *userdata)
 								   minioninfo->chip_status[chip].quework);
 #endif
 						K_WUNLOCK(minioninfo->wchip_list[chip]);
+						applog(LOG_DEBUG, "%s%d: task 0x%04x sent to chip %d",
+								  minioncgpu->drv->name, minioncgpu->device_id,
+								  titem->task_id, chip);
 						break;
 					case READ_ADDR(MINION_CORE_ENA0_31):
 					case READ_ADDR(MINION_CORE_ENA32_63):
@@ -2507,6 +2515,7 @@ static void *minion_spi_reply(void *userdata)
 	struct timeval now;
 
 #if ENABLE_INT_NONO
+	uint64_t ioseq;
 	TITEM clr_task;
 	struct pollfd pfd;
 	struct minion_header *head;
@@ -2759,6 +2768,14 @@ repeek:
 									DATAR(item)->nonce = RES_NONCE(use1);
 									DATAR(item)->no_nonce = !RES_GOLD(use1);
 									memcpy(&(DATAR(item)->when), &now, sizeof(now));
+									applog(LOG_DEBUG, "%s%i: reply task_id 0x%04x"
+											  " - chip %d - gold %d",
+											  minioncgpu->drv->name,
+											  minioncgpu->device_id,
+											  RES_TASK(use1),
+											  (int)RES_CHIPID(use1),
+											  (int)RES_GOLD(use1));
+
 									if (!use2)
 										DATAR(item)->another = false;
 									else {
@@ -2766,8 +2783,6 @@ repeek:
 										DATAR(item)->task_id2 = RES_TASK(use2);
 										DATAR(item)->nonce2 = RES_NONCE(use2);
 									}
-//applog(MINTASK_LOG, "%s%i: ZZZ reply task_id 0x%04x - chip %d - gold %d", minioncgpu->drv->name, minioncgpu->device_id, RES_TASK(use1), (int)RES_CHIPID(use1), (int)RES_GOLD(use1));
-
 //if (RES_GOLD(use1))
 //applog(MINTASK_LOG, "%s%i: found a result chip %d core %d task 0x%04x nonce 0x%08x gold=%d", minioncgpu->drv->name, minioncgpu->device_id, DATAR(item)->chip, DATAR(item)->core, DATAR(item)->task_id, DATAR(item)->nonce, (int)RES_GOLD(use1));
 
@@ -2831,7 +2846,7 @@ repeek:
 				if (minioninfo->has_chip[chip]) {
 					SET_HEAD_READ(head, MINION_SYS_INT_STA);
 					head->chipid = minioninfo->chipid[chip];
-					reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz);
+					reply = do_ioctl(CHIP_PIN(chip), wbuf, wsiz, rbuf, rsiz, &ioseq);
 					if (reply != (int)wsiz) {
 						applog(LOG_ERR, "%s: chip %d int status returned %d"
 								" (should be %d)",
@@ -2910,10 +2925,10 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 	K_ITEM *tail;
 //	bool errs;
 
-	/* remove older (txrx date) work items
+	/* remove older ioseq work items
 	   no_nonce means this 'item' has finished also */
 	tail = minioninfo->wchip_list[chip]->tail;
-	while (tail && (tdiff(&(DATAW(tail)->txrx), &(DATAW(item)->txrx)) > 0)) {
+	while (tail && (DATAW(tail)->ioseq < DATAW(item)->ioseq)) {
 		k_unlink_item(minioninfo->wchip_list[chip], tail);
 		if (!(DATAW(tail)->stale)) {
 			minioninfo->chip_status[chip].chipwork--;
@@ -2965,6 +2980,20 @@ static void cleanup_older(struct cgpu_info *minioncgpu, int chip, K_ITEM *item, 
 	}
 }
 
+// Need to put it back in the list where it was - according to ioseq
+static void restorework(struct minion_info *minioninfo, int chip, K_ITEM *item)
+{
+	K_ITEM *look;
+
+	look = minioninfo->wchip_list[chip]->tail;
+	while (look && DATAW(look)->ioseq < DATAW(item)->ioseq)
+		look = look->prev;
+	if (!look)
+		k_add_head(minioninfo->wchip_list[chip], item);
+	else
+		k_insert_after(minioninfo->wchip_list[chip], item, look);
+}
+
 static enum nonce_state oknonce(struct thr_info *thr, struct cgpu_info *minioncgpu, int chip, int core,
 				uint32_t task_id, uint32_t nonce, bool no_nonce, struct timeval *when,
 				bool another, uint32_t task_id2, uint32_t nonce2)
@@ -3027,11 +3056,9 @@ retry:
 		item = item->prev;
 	}
 	max_task_id = DATAW(minioninfo->wchip_list[chip]->head)->task_id;
-	if (item)
-		k_unlink_item(minioninfo->wchip_list[chip], item);
-	K_WUNLOCK(minioninfo->wchip_list[chip]);
 
 	if (!item) {
+		K_WUNLOCK(minioninfo->wchip_list[chip]);
 		if (another && task_id != task_id2) {
 			minioninfo->tasks_failed[chip]++;
 			task_id = task_id2;
@@ -3042,7 +3069,8 @@ retry:
 		minioninfo->spi_errors++;
 		minioninfo->res_spi_errors[chip]++;
 		minioninfo->res_err_count[chip]++;
-		applog(MINTASK_LOG, "%s%i: chip %d core %d unknown task 0x%04x (min=0x%04x max=0x%04x no_nonce=%d)",
+		applog(MINTASK_LOG, "%s%i: chip %d core %d unknown task 0x%04x "
+				    "(min=0x%04x max=0x%04x no_nonce=%d)",
 				    minioncgpu->drv->name, minioncgpu->device_id,
 				    chip, core, (int)task_id, (int)min_task_id,
 				    (int)max_task_id, no_nonce);
@@ -3055,13 +3083,14 @@ retry:
 	if (redo)
 		minioninfo->tasks_recovered[chip]++;
 
+	k_unlink_item(minioninfo->wchip_list[chip], item);
 	if (no_nonce) {
-		K_WLOCK(minioninfo->wchip_list[chip]);
 		cleanup_older(minioncgpu, chip, item, no_nonce);
 		k_free_head(minioninfo->wfree_list, item);
 		K_WUNLOCK(minioninfo->wchip_list[chip]);
 		return NONCE_NO_NONCE;
 	}
+	K_WUNLOCK(minioninfo->wchip_list[chip]);
 
 	minioninfo->tested_nonces++;
 
@@ -3074,6 +3103,9 @@ retest:
 					    (nonce & 0xff), ((nonce >> 8) & 0xff),
 					    ((nonce >> 16) & 0xff), ((nonce >> 24) & 0xff),
 					    chip, core, task_id);
+			K_WLOCK(minioninfo->wchip_list[chip]);
+			restorework(minioninfo, chip, item);
+			K_WUNLOCK(minioninfo->wchip_list[chip]);
 			return NONCE_DUP_NONCE;
 		}
 
@@ -3095,6 +3127,7 @@ retest:
 
 		K_WLOCK(minioninfo->wchip_list[chip]);
 		cleanup_older(minioncgpu, chip, item, no_nonce);
+		restorework(minioninfo, chip, item);
 		K_WUNLOCK(minioninfo->wchip_list[chip]);
 
 		// add to history and remove old history
@@ -3125,6 +3158,10 @@ retest:
 	}
 
 	DATAW(item)->errors++;
+	K_WLOCK(minioninfo->wchip_list[chip]);
+	restorework(minioninfo, chip, item);
+	K_WUNLOCK(minioninfo->wchip_list[chip]);
+
 	minioninfo->chip_bad[chip]++;
 	minioninfo->core_bad[chip][core]++;
 	inc_hw_errors(thr);
@@ -3750,6 +3787,10 @@ static bool minion_queue_full(struct cgpu_info *minioncgpu)
 		ret = true;
 	else {
 		need = totneed - count;
+		/* Ensure we do enough rolling to reduce CPU
+		   but dont roll too much to have them end up stale */
+		if (need < 16)
+			need = 16;
 		work = get_queued(minioncgpu);
 		if (work) {
 			roll_limit = work->drv_rolllimit;
diff --git a/klist.c b/klist.c
index 8a3098d..a552017 100644
--- a/klist.c
+++ b/klist.c
@@ -255,6 +255,56 @@ void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
 	list->count_up++;
 }
 
+void _k_insert_before(K_LIST *list, K_ITEM *item, K_ITEM *before, KLIST_FFL_ARGS)
+{
+	if (item->name != list->name) {
+		quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
+				list->name, __func__, item->name, KLIST_FFL_PASS);
+	}
+
+	if (!before) {
+		quithere(1, "%s() (%s) can't before a null item" KLIST_FFL,
+				__func__, list->name, KLIST_FFL_PASS);
+	}
+
+	item->next = before;
+	item->prev = before->prev;
+	if (before->prev)
+		before->prev->next = item;
+	else
+		list->head = item;
+	before->prev = item;
+
+	list->count++;
+	list->count_up++;
+}
+
+void _k_insert_after(K_LIST *list, K_ITEM *item, K_ITEM *after, KLIST_FFL_ARGS)
+{
+	if (item->name != list->name) {
+		quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
+				list->name, __func__, item->name, KLIST_FFL_PASS);
+	}
+
+	if (!after) {
+		quithere(1, "%s() (%s) can't after a null item" KLIST_FFL,
+				__func__, list->name, KLIST_FFL_PASS);
+	}
+
+	item->prev = after;
+	item->next = after->next;
+	if (after->next)
+		after->next->prev = item;
+	else {
+		if (list->do_tail)
+			list->tail = item;
+	}
+	after->next = item;
+
+	list->count++;
+	list->count_up++;
+}
+
 void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
 {
 	if (item->name != list->name) {
diff --git a/klist.h b/klist.h
index dfcc41d..a79a4ed 100644
--- a/klist.h
+++ b/klist.h
@@ -76,6 +76,10 @@ extern void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
 #define k_free_head(__list, __item) _k_add_head(__list, __item, KLIST_FFL_HERE)
 extern void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
 #define k_add_tail(_list, _item) _k_add_tail(_list, _item, KLIST_FFL_HERE)
+extern void _k_insert_before(K_LIST *list, K_ITEM *item, K_ITEM *before, KLIST_FFL_ARGS);
+#define k_insert_before(_list, _item, _before) _k_insert_before(_list, _item, _before, KLIST_FFL_HERE)
+extern void _k_insert_after(K_LIST *list, K_ITEM *item, K_ITEM *after, KLIST_FFL_ARGS);
+#define k_insert_after(_list, _item, _after) _k_insert_after(_list, _item, _after, KLIST_FFL_HERE)
 extern void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
 #define k_unlink_item(_list, _item) _k_unlink_item(_list, _item, KLIST_FFL_HERE)
 void _k_list_transfer_to_head(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS);