Skip to content

Commit

Permalink
Fix buggy hashrate. Reduce work being sent + optimizations. More reli…
Browse files Browse the repository at this point in the history
…able ltc reset commands.
  • Loading branch information
siklon committed May 3, 2014
1 parent 026462a commit d07b9ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
26 changes: 13 additions & 13 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ struct work {
uint32_t data[32];
uint32_t *target;
char *job_id;
uint32_t *work_id;
uint32_t work_id;
unsigned char xnonce2[4];
unsigned short thr_id;
};
Expand All @@ -227,8 +227,8 @@ static uint32_t g_prev_target[8];
static uint32_t g_curr_target[8];
static char g_prev_job_id[128];
static char g_curr_job_id[128];
static uint32_t g_prev_work_id;
static uint32_t g_curr_work_id;
static uint32_t g_prev_work_id = 0;
static uint32_t g_curr_work_id = 0;
static char can_work = 0x1;

static struct work *g_works;
Expand Down Expand Up @@ -859,7 +859,7 @@ static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
work->data[31] = 0x00000280;

work->target = g_curr_target;
work->work_id = &g_curr_work_id;
work->work_id = g_curr_work_id;

free(coinbase);
}
Expand Down Expand Up @@ -911,12 +911,13 @@ static void *stratum_thread(void *userdata)
goto out;
applog(LOG_INFO, "Starting Stratum on %s", stratum.url);

g_works = malloc(opt_n_threads * sizeof(struct work));
g_works = calloc(opt_n_threads, sizeof(struct work));
for(i = 0; i < opt_n_threads; i++)
{
memset(g_works[i].xnonce2, 0, 4);
g_works[i].thr_id = i;
}
gettimeofday(&timestr, NULL);
g_curr_work_id = (timestr.tv_sec & 0xffff) << 16 | timestr.tv_usec & 0xffff;

while (1) {
int failures = 0;
Expand Down Expand Up @@ -946,26 +947,25 @@ static void *stratum_thread(void *userdata)
(strcmp(stratum.job.job_id, g_curr_job_id) || !g_work_time)) {
pthread_mutex_lock(&g_work_lock);
pthread_mutex_lock(&stratum.work_lock);
restart_threads();
applog(LOG_INFO, "New job_id: %s Diff: %d", stratum.job.job_id, (int) (stratum.job.diff));
g_prev_work_id = g_curr_work_id;
if (stratum.job.clean)
{
applog(LOG_INFO, "Stratum detected new block");
gettimeofday(&timestr, NULL);
g_curr_work_id = (timestr.tv_sec & 0xffff) << 16 | timestr.tv_usec & 0xffff;
restart_threads();
}
strcpy(g_prev_job_id, g_curr_job_id);
for(i = 0; i < 8; i++) g_prev_target[i] = g_curr_target[i];
g_prev_work_id = g_curr_work_id;
for(i = 0; i < opt_n_threads; i++)
{
g_works[i].job_id = g_prev_job_id;
g_works[i].target = g_prev_target;
g_works[i].work_id = &g_prev_work_id;
g_works[i].work_id = g_prev_work_id;
}
gettimeofday(&timestr, NULL);
g_curr_work_id = (timestr.tv_sec & 0xffff) << 16 | timestr.tv_usec & 0xffff;
strcpy(g_curr_job_id, stratum.job.job_id);
diff_to_target(g_curr_target, stratum.job.diff / 65536.0);
applog(LOG_DEBUG, "Dispatching new work to GC3355 threads (0x%x)", g_curr_work_id);
for(i = 0; i < opt_n_threads; i++)
{
stratum_gen_work(&stratum, &g_works[i]);
Expand Down Expand Up @@ -1001,7 +1001,7 @@ static void *stratum_thread(void *userdata)
for(i = 0; i < opt_n_threads; i++)
{
g_works[i].target = g_prev_target;
g_works[i].work_id = &g_prev_work_id;
g_works[i].work_id = g_prev_work_id;
}
gettimeofday(&timestr, NULL);
g_curr_work_id = (timestr.tv_sec & 0xffff) << 16 | timestr.tv_usec & 0xffff;
Expand Down
16 changes: 9 additions & 7 deletions gc3355-commands.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
/* commands for single LTC mode */
static const unsigned char *single_cmd_init[] = {
/* reset gcp */
(unsigned char[]) {0x10, 0x55, 0xaa, 0xc0, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
/* set # of chips to 5 */
(unsigned char[]) {0x10, 0x55, 0xaa, 0xc0, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
/* disable btc cores */
(unsigned char[]) {0x18, 0x55, 0xaa, 0xef, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
/* enable ltc */
(unsigned char[]) {0x08, 0x55, 0xaa, 0xef, 0x30, 0x20, 0x00, 0x00, 0x00},
NULL
};

static const unsigned char *single_cmd_reset[] = {
(unsigned char[]) {0x08, 0x55, 0xaa, 0x1f, 0x28, 0x16, 0x00, 0x00, 0x00},
(unsigned char[]) {0x08, 0x55, 0xaa, 0x1f, 0x28, 0x15, 0x00, 0x00, 0x00},
/* reset ltc report + calculation unit */
(unsigned char[]) {0x08, 0x55, 0xaa, 0x1f, 0x28, 0x14, 0x00, 0x00, 0x00},
/* reset ltc */
(unsigned char[]) {0x10, 0x55, 0xaa, 0xc0, 0x00, 0xe0, 0xe0, 0xe0, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
/* clear reset */
(unsigned char[]) {0x08, 0x55, 0xaa, 0x1f, 0x28, 0x17, 0x00, 0x00, 0x00},
NULL
};

static const unsigned char *disable_fifo[] = {
(unsigned char[]) {0x10, 0x55, 0xaa, 0xc0, 0x00, 0xd0, 0xd0, 0xd0, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
NULL
};

static const unsigned char *firmware_request_cmd[] = {
(unsigned char[]) {0x10, 0x55, 0xaa, 0xc0, 0x00, 0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
NULL
Expand Down
28 changes: 19 additions & 9 deletions gc3355.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,16 @@ static void *gc3355_thread(void *userdata)
struct thr_info *mythr = userdata;
int thr_id = mythr->id;
struct gc3355_dev *gc3355;
struct work work;
struct work work = {0};
unsigned char *scratchbuf = NULL;
int i, chips, rc;
struct timeval timestr;
struct dev_freq *dev_freq_curr;
struct chip_freq *chip_freq_curr;
unsigned char rptbuf[12];

work.job_id = malloc(1);
work.thr_id = thr_id;
gettimeofday(&timestr, NULL);
gc3355 = &gc3355_devs[thr_id];
gc3355->id = thr_id;
Expand Down Expand Up @@ -452,7 +454,14 @@ static void *gc3355_thread(void *userdata)
if (work_restart[thr_id].restart || memcmp(work.data, g_works[thr_id].data, 76))
{
pthread_mutex_lock(&g_work_lock);
memcpy(&work, &g_works[thr_id], sizeof(struct work));
for(i = 0; i < 32; i++)
work.data[i] = g_works[thr_id].data[i];
work.target = g_works[thr_id].target;
free(work.job_id);
work.job_id = strdup(g_works[thr_id].job_id);
work.work_id = g_works[thr_id].work_id;
for(i = 0; i < 4; i++)
work.xnonce2[i] = g_works[thr_id].xnonce2[i];
pthread_mutex_unlock(&g_work_lock);
sha256_init(midstate);
sha256_transform(midstate, work.data, 0);
Expand Down Expand Up @@ -489,6 +498,7 @@ static int gc3355_scanhash(struct gc3355_dev *gc3355, struct work *work, unsigne

if (gc3355->resend)
{
applog(LOG_DEBUG, "%d: Dispatching new work to GC3355 cores (0x%x)", gc3355->id, work->work_id);
unsigned char bin[156];
// swab for big endian
uint32_t midstate2[8];
Expand All @@ -508,7 +518,7 @@ static int gc3355_scanhash(struct gc3355_dev *gc3355, struct work *work, unsigne
memcpy(bin+36, (unsigned char *)midstate2, 32);
memcpy(bin+68, (unsigned char *)data2, 80);
memcpy(bin+148, "\xff\xff\xff\xff", 4);
memcpy(bin+152, (unsigned char[]){*work->work_id >> 24, *work->work_id >> 16, *work->work_id >> 8, *work->work_id}, 4);
memcpy(bin+152, (unsigned char[]){work->work_id >> 24, work->work_id >> 16, work->work_id >> 8, work->work_id}, 4);
// clear read buffer
read(gc3355->dev_fd, rptbuf, 12);
memset(rptbuf, 0, 12);
Expand Down Expand Up @@ -555,18 +565,17 @@ static int gc3355_scanhash(struct gc3355_dev *gc3355, struct work *work, unsigne

stop = 1;
chip_id = nonce / (0xffffffff / gc3355->chips);
if(work_id != g_curr_work_id)
{
applog(LOG_DEBUG, "%d@%d: Work_id differs (%08x != %08x)", gc3355->id, chip_id, work_id, g_curr_work_id);
continue;
}
if(work_restart[thr_id].restart || !can_work)
{
applog(LOG_DEBUG, "%d@%d: Scanhash restart requested", gc3355->id, chip_id);
gc3355->last_nonce[chip_id] = nonce;
break;
}
if(work_id != g_curr_work_id)
{
applog(LOG_DEBUG, "%d@%d: Work_id differs (%08x != %08x)", gc3355->id, chip_id, work_id, g_curr_work_id);
gc3355->last_nonce[chip_id] = nonce;
continue;
}
gettimeofday(&timestr, NULL);
time_now = timestr.tv_sec + timestr.tv_usec / 1000000.0;
freq = gc3355->freq[chip_id];
Expand Down Expand Up @@ -659,6 +668,7 @@ static int gc3355_scanhash(struct gc3355_dev *gc3355, struct work *work, unsigne
gc3355->hwe[chip_id] = 0;
gc3355->steps[chip_id] = 0;
gc3355->autotune_accepted[chip_id] = 0;
work_restart[thr_id].restart = 1;
}
}
pthread_mutex_unlock(&stats_lock);
Expand Down

0 comments on commit d07b9ab

Please sign in to comment.