bab - tune the chip speed based on error rates
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-bab.c b/driver-bab.c
index 902e51f..9fc3357 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -140,9 +140,17 @@ static const uint32_t bab_test_data[BAB_TEST_DATA] = {
* 56/ 101/ 125/ 99
*/
#define BAB_MAXSPEED 57
+#define BAB_DEFMAXSPEED 55
#define BAB_DEFSPEED 54
#define BAB_MINSPEED 52
+/*
+ * % of errors to tune the speed up or down
+ * 1.0 to 10.0 should average around 5.5% errors
+ */
+#define BAB_TUNEUP 1.0
+#define BAB_TUNEDOWN 10.0
+
#define MIDSTATE_BYTES 32
#define MERKLE_OFFSET 64
#define MERKLE_BYTES 12
@@ -313,6 +321,12 @@ static double chip_speed_ranges[BAB_CHIP_SPEEDS - 1] =
static char *chip_speed_names[BAB_CHIP_SPEEDS] =
{ "Dead", "V.Slow", "Slow", "OK", "Good", "Fast" };
+/*
+ * This is required to do chip tuning
+ * If disabled, it will simply run the chips at default speed
+ */
+#define UPDATE_HISTORY 1
+
struct bab_info {
struct thr_info spi_thr;
struct thr_info res_thr;
@@ -342,6 +356,13 @@ struct bab_info {
struct bab_work_reply chip_results[BAB_MAXCHIPS];
struct bab_work_reply chip_prev[BAB_MAXCHIPS];
+ uint8_t max_speed;
+ uint8_t def_speed;
+ uint8_t min_speed;
+
+ float tune_up;
+ float tune_down;
+
uint8_t chip_fast[BAB_MAXCHIPS];
uint8_t chip_conf[BAB_MAXCHIPS];
uint8_t old_fast[BAB_MAXCHIPS];
@@ -350,9 +371,6 @@ struct bab_info {
uint8_t osc[BAB_OSC];
- // TODO: performance tuning
- int fixchip;
-
/*
* Ignore errors in the first work reply since
* they may be from a previous run or random junk
@@ -435,6 +453,13 @@ struct bab_info {
K_LIST *nfree_list;
K_STORE *good_nonces[BAB_MAXCHIPS];
K_STORE *bad_nonces[BAB_MAXCHIPS];
+#if UPDATE_HISTORY
+ struct timeval first_work[BAB_MAXCHIPS];
+ uint32_t work_count[BAB_MAXCHIPS];
+ struct timeval last_tune[BAB_MAXCHIPS];
+ uint8_t bad_fast[BAB_MAXCHIPS];
+ bool dead_msg[BAB_MAXCHIPS];
+#endif
struct timeval last_did;
@@ -790,9 +815,7 @@ static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
bank = babinfo->chip_bank[i];
BAB_ADD_BREAK(sitem);
}
- if (i == babinfo->fixchip &&
- (BAB_CFGD_SET(babinfo->chip_conf[i]) ||
- !babinfo->chip_conf[i])) {
+ if (BAB_CFGD_SET(babinfo->chip_conf[i]) || !babinfo->chip_conf[i]) {
bab_set_osc(babinfo, i);
bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc, sizeof(babinfo->osc));
bab_config_reg(sitem, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo->chip_conf[i]));
@@ -843,7 +866,7 @@ static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
}
DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz + 3;
chip_input = &(DATAW(DATAS(sitem)->witems[i])->chip_input);
- // Would no-ops need to be added if a chip gets disabled?
+
if (babinfo->chip_conf[i])
bab_add_data(sitem, BAB_INP_ADDR, (uint8_t *)chip_input, sizeof(*chip_input));
@@ -861,8 +884,6 @@ static void bab_put(struct bab_info *babinfo, K_ITEM *sitem)
K_WUNLOCK(babinfo->spi_list);
cgsem_post(&(babinfo->spi_work));
-
- babinfo->fixchip = (babinfo->fixchip + 1) % babinfo->chips;
}
static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info *babinfo, struct timeval *when)
@@ -1172,9 +1193,21 @@ static void bab_detect(bool hotplug)
quithere(1, "Failed to calloc babinfo");
babcgpu->device_data = (void *)babinfo;
+ babinfo->max_speed = BAB_DEFMAXSPEED;
+ babinfo->def_speed = BAB_DEFSPEED;
+ babinfo->min_speed = BAB_MINSPEED;
+
+ babinfo->tune_up = BAB_TUNEUP;
+ babinfo->tune_down = BAB_TUNEDOWN;
+
+ // TODO: get --bab-options
+
for (i = 0; i < BAB_MAXCHIPS; i++) {
babinfo->chip_conf[i] = BAB_DEFCONF;
- babinfo->chip_fast[i] = BAB_DEFSPEED;
+ babinfo->chip_fast[i] = babinfo->def_speed;
+#if UPDATE_HISTORY
+ babinfo->bad_fast[i] = babinfo->max_speed + 1;
+#endif
}
if (!bab_init_gpio(babcgpu, babinfo, BAB_SPI_BUS, BAB_SPI_CHIP))
@@ -1319,6 +1352,11 @@ static void *bab_spi(void *userdata)
memcpy(&(DATAW(witem)->work_start), &(DATAS(sitem)->work_start),
sizeof(DATAW(witem)->work_start));
k_add_head(babinfo->chip_work[chip], witem);
+#if UPDATE_HISTORY
+ babinfo->work_count[chip]++;
+ if (babinfo->first_work[chip].tv_sec == 0)
+ memcpy(&(babinfo->first_work[chip]), &send, sizeof(send));
+#endif
}
}
K_WUNLOCK(babinfo->wfree_list);
@@ -1385,13 +1423,14 @@ static void bab_flush_work(struct cgpu_info *babcgpu)
#define WORK_BITS (18*4)
#define WORK_NONCE (19*4)
-#define UPDATE_HISTORY 1
-
#if UPDATE_HISTORY
-static void process_history(struct bab_info *babinfo, int chip, struct timeval *when, bool good, struct timeval *now)
+static void process_history(struct cgpu_info *babcgpu, int chip, struct timeval *when, bool good, struct timeval *now)
{
+ struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
+ uint64_t good_nonces, bad_nonces;
+ uint8_t chip_fast;
+ float tune;
K_ITEM *item;
- double diff;
int i;
K_WLOCK(babinfo->nfree_list);
@@ -1406,8 +1445,7 @@ static void process_history(struct bab_info *babinfo, int chip, struct timeval *
for (i = 0; i < babinfo->chips; i++) {
item = babinfo->good_nonces[i]->tail;
while (item) {
- diff = tdiff(now, &(DATAN(item)->found));
- if (diff < HISTORY_TIME_S)
+ if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
break;
k_unlink_item(babinfo->good_nonces[i], item);
@@ -1418,8 +1456,7 @@ static void process_history(struct bab_info *babinfo, int chip, struct timeval *
item = babinfo->bad_nonces[i]->tail;
while (item) {
- diff = tdiff(now, &(DATAN(item)->found));
- if (diff < HISTORY_TIME_S)
+ if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
break;
k_unlink_item(babinfo->bad_nonces[i], item);
@@ -1429,6 +1466,99 @@ static void process_history(struct bab_info *babinfo, int chip, struct timeval *
}
}
K_WUNLOCK(babinfo->nfree_list);
+
+ // Tuning ...
+ good_nonces = babinfo->good_nonces[chip]->count;
+ bad_nonces = babinfo->bad_nonces[chip]->count;
+
+ if (tdiff(now, &(babinfo->first_work[chip])) >= HISTORY_TIME_S &&
+ tdiff(now, &(babinfo->last_tune[chip])) >= HISTORY_TIME_S &&
+ (good_nonces + bad_nonces) > 0) {
+
+ chip_fast = babinfo->chip_fast[chip];
+
+ /*
+ * If dead then step it down and remember the speed
+ * TODO: does a speed change reset the chip? Or is there a reset?
+ */
+ if (good_nonces == 0) {
+ if (chip_fast > babinfo->min_speed) {
+ if (babinfo->bad_fast[chip] > chip_fast)
+ babinfo->bad_fast[chip] = chip_fast;
+
+ babinfo->chip_fast[chip]--;
+
+ applog(LOG_WARNING, "%s%d: Chip %d DEAD - speed down from %d to %d",
+ babcgpu->drv->name, babcgpu->device_id,
+ chip, (int)chip_fast, (int)chip_fast - 1);
+ } else {
+ /*
+ * Permanently DEAD since we're already at the minumum speed
+ * but only getting bad nonces
+ */
+ if (babinfo->dead_msg[chip] == false) {
+ applog(LOG_WARNING, "%s%d: Chip %d DEAD - at min speed %d",
+ babcgpu->drv->name, babcgpu->device_id,
+ chip, (int)chip_fast);
+
+ babinfo->dead_msg[chip] = true;
+ }
+ }
+ goto tune_over;
+ }
+
+ /*
+ * It 'was' permanently DEAD but a good nonce came back!
+ */
+ if (babinfo->dead_msg[chip]) {
+ applog(LOG_WARNING, "%s%d: Chip %d REVIVED - at speed %d",
+ babcgpu->drv->name, babcgpu->device_id,
+ chip, (int)chip_fast);
+
+ babinfo->dead_msg[chip] = false;
+ }
+
+ /*
+ * Since we have found 'some' nonces -
+ * make sure bad_fast is higher than current chip_fast
+ */
+ if (babinfo->bad_fast[chip] <= chip_fast)
+ babinfo->bad_fast[chip] = chip_fast + 1;
+
+ tune = (float)bad_nonces / (float)(good_nonces + bad_nonces) * 100.0;
+
+ // Tune it down if error rate is too high
+ if (tune >= babinfo->tune_down) {
+ babinfo->chip_fast[chip]--;
+
+ applog(LOG_WARNING, "%s%d: Chip %d High errors %.2f%% - speed down %d to %d",
+ babcgpu->drv->name, babcgpu->device_id,
+ chip, tune, (int)chip_fast, (int)chip_fast - 1);
+
+ goto tune_over;
+ }
+
+ /*
+ * TODO: if we are at bad_fast-1 and tune_up
+ * and bad_fast was set more than some time limit ago
+ * then consider increasing bad_fast by 1?
+ */
+
+ // Tune it up if error rate is low enough
+ if (tune <= babinfo->tune_up &&
+ chip_fast < babinfo->max_speed &&
+ chip_fast < (babinfo->bad_fast[chip] - 1)) {
+ babinfo->chip_fast[chip]++;
+
+ applog(LOG_WARNING, "%s%d: Chip %d Low errors %.2f%% - speed up %d to %d",
+ babcgpu->drv->name, babcgpu->device_id,
+ chip, tune, (int)chip_fast, (int)chip_fast + 1);
+
+ goto tune_over;
+ }
+tune_over:
+ cgtime(&(babinfo->last_tune[chip]));
+ }
}
#endif
@@ -1528,7 +1658,7 @@ static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_
babinfo->chip_cont_bad[chip] = 0;
#if UPDATE_HISTORY
- process_history(babinfo, chip,
+ process_history(babcgpu, chip,
&(DATAR(ritem)->when),
true, &now);
#endif
@@ -1564,7 +1694,7 @@ static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu, K_
babinfo->chip_max_bad[chip] = babinfo->chip_cont_bad[chip];
#if UPDATE_HISTORY
- process_history(babinfo, chip, &(DATAR(ritem)->when), false, &now);
+ process_history(babcgpu, chip, &(DATAR(ritem)->when), false, &now);
#endif
} else {
babinfo->initial_ignored++;
@@ -1896,6 +2026,7 @@ static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
}
#define CHIPS_PER_STAT 16
+#define FMT_RANGE "%d-%d"
static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
{
@@ -1994,7 +2125,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
babinfo->chip_nonces[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Nonces %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Nonces "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2005,7 +2136,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
babinfo->chip_good[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Good %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Good "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2016,7 +2147,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
babinfo->chip_bad[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Bad %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2027,7 +2158,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
(int)(babinfo->chip_conf[j]));
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Conf %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Conf "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2038,7 +2169,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
(int)(babinfo->chip_fast[j]));
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Fast %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Fast "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2049,7 +2180,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
(int)(babinfo->chip_spie[j]));
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Spie %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Spie "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2060,7 +2191,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
(int)(babinfo->chip_miso[j]));
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Miso %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Miso "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2075,7 +2206,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
j == i ? "" : " ", hw);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "HW%% %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "HW%% "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
ghs_sum = 0;
@@ -2093,10 +2224,10 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
strcat(data, buf);
ghs_sum += (float)ghs;
}
- snprintf(buf, sizeof(buf), "GHs %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "GHs "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
- snprintf(buf, sizeof(buf), "Sum GHs %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Sum GHs "FMT_RANGE, i, to);
root = api_add_avg(root, buf, &ghs_sum, true);
data[0] = '\0';
@@ -2107,7 +2238,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
babinfo->chip_cont_bad[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Cont-Bad %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Cont-Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2118,7 +2249,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
babinfo->chip_max_bad[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "Max-Bad %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Max-Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2129,7 +2260,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
history_good[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "History Good %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "History Good "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2140,7 +2271,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
history_bad[j]);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "History Bad %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "History Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
@@ -2155,7 +2286,7 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
j == i ? "" : " ", hw);
strcat(data, buf);
}
- snprintf(buf, sizeof(buf), "History HW%% %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "History HW%% "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
ghs_sum = 0;
@@ -2188,10 +2319,10 @@ static struct api_data *bab_api_stats(struct cgpu_info *babcgpu)
if (sp >= (BAB_CHIP_SPEEDS - 1))
speeds[BAB_CHIP_SPEEDS - 1]++;
}
- snprintf(buf, sizeof(buf), "History GHs %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "History GHs "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
- snprintf(buf, sizeof(buf), "Sum History GHs %d - %d", i, to);
+ snprintf(buf, sizeof(buf), "Sum History GHs "FMT_RANGE, i, to);
root = api_add_avg(root, buf, &ghs_sum, true);
ghs_tot += ghs_sum;