Browse Source

Reuse most recent GBT job if in get_work_thread if it isn't stale

Luke Dashjr 13 years ago
parent
commit
ff18c04523
2 changed files with 34 additions and 1 deletions
  1. 31 1
      miner.c
  2. 3 0
      miner.h

+ 31 - 1
miner.c

@@ -449,6 +449,7 @@ struct pool *add_pool(void)
 	if (!pool)
 		quit(1, "Failed to malloc pool in add_pool");
 	pool->pool_no = pool->prio = total_pools;
+	mutex_init(&pool->last_work_lock);
 	mutex_init(&pool->pool_lock);
 	if (unlikely(pthread_cond_init(&pool->cr_cond, NULL)))
 		quit(1, "Failed to pthread_cond_init in add_pool");
@@ -1822,6 +1823,15 @@ static bool work_decode(struct pool *pool, struct work *work, json_t *val)
 
 	gettimeofday(&work->tv_staged, NULL);
 
+	if (work->tmpl) {
+		// Only save GBT jobs, since rollntime isn't coordinated well yet
+		mutex_lock(&pool->last_work_lock);
+		if (pool->last_work_copy)
+			free_work(pool->last_work_copy);
+		pool->last_work_copy = copy_work(work);
+		mutex_unlock(&pool->last_work_lock);
+	}
+
 	ret = true;
 
 out:
@@ -3464,7 +3474,27 @@ retry:
 		goto out;
 	}
 
-	// TODO: Stage work from latest GBT template, if any
+	if (pool->last_work_copy) {
+	  mutex_lock(&pool->last_work_lock);
+	  struct work *last_work = pool->last_work_copy;
+	  if (last_work && can_roll(last_work) && should_roll(last_work)) {
+		ret_work = copy_work(pool->last_work_copy);
+		mutex_unlock(&pool->last_work_lock);
+		roll_work(ret_work);
+		applog(LOG_DEBUG, "Staging work from latest GBT job in get_work_thread with %d seconds left", (int)blkmk_time_left(ret_work->tmpl, time(NULL)));
+		if (unlikely(!stage_work(ret_work))) {
+			applog(LOG_ERR, "Failed to stage gbt work in get_work_thread");
+			kill_work();
+			free_work(ret_work);
+		}
+		dec_queued(pool);
+		goto out;
+	  } else if (last_work) {
+		free_work(last_work);
+		pool->last_work_copy = NULL;
+	  }
+	  mutex_unlock(&pool->last_work_lock);
+	}
 
 	if (clone_available()) {
 		dec_queued(pool);

+ 3 - 0
miner.h

@@ -1005,6 +1005,9 @@ struct pool {
 	struct stratum_work swork;
 	pthread_t stratum_thread;
 	pthread_mutex_t stratum_lock;
+
+	pthread_mutex_t last_work_lock;
+	struct work *last_work_copy;
 };
 
 #define GETWORK_MODE_TESTPOOL 'T'