Make the next work item queue 2/3 of the way through the work on the current work item. Load the first queued extra work in the main function to avoid having a once-off variable in get_work(). Load an extra set of work for each function in advance once a longpoll is detected since every thread will need to get new work. Discard requests with a separate function to ensure the right number is always queued.
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
diff --git a/cpu-miner.c b/cpu-miner.c
index 13ee61b..c1a748d 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -713,10 +713,20 @@ static bool queue_request(void)
return true;
}
-static void flush_requests(void)
+static bool discard_request(void)
{
struct thr_info *thr = &thr_info[0];
struct work *work_heap;
+
+ work_heap = tq_pop(thr->q, NULL);
+ if (unlikely(!work_heap))
+ return false;
+ free(work_heap);
+ return true;
+}
+
+static void flush_requests(void)
+{
unsigned int i;
/* Queue a whole batch of new requests */
@@ -731,25 +741,21 @@ static void flush_requests(void)
/* Pop off the old requests. Cancelling the requests would be better
* but is tricky */
for (i = 0; i < opt_queue; i++) {
- work_heap = tq_pop(thr->q, NULL);
- if (unlikely(!work_heap)) {
- applog(LOG_ERR, "Failed to pop requests in flush_requests");
+ if (unlikely(!discard_request())) {
+ applog(LOG_ERR, "Failed to discard requests in flush_requests");
kill_work();
return;
}
- free(work_heap);
}
}
-static bool get_work(struct work *work)
+static bool get_work(struct work *work, bool queued)
{
struct thr_info *thr = &thr_info[0];
- static bool first_work = true;
struct work *work_heap;
bool ret = false;
- unsigned int i;
- if (unlikely(!queue_request()))
+ if (unlikely(!queued && !queue_request()))
goto out;
/* wait for 1st response, or get cached response */
@@ -757,20 +763,9 @@ static bool get_work(struct work *work)
if (unlikely(!work_heap))
goto out;
- if (unlikely(first_work)) {
- first_work = false;
- /* send for extra work requests for the next time get_work
- * is called. */
- for (i = 1; i < opt_queue; i++) {
- if (unlikely(!queue_request()))
- goto out_free;
- }
- }
-
memcpy(work, work_heap, sizeof(*work));
memcpy(current_block, work->data, 36);
ret = true;
-out_free:
free(work_heap);
out:
return ret;
@@ -831,6 +826,10 @@ static void *miner_thread(void *userdata)
bool needs_work = true;
/* Try to cycle approximately 5 times before each log update */
const unsigned long cycle = opt_log_interval / 5 ? : 1;
+ /* Request the next work item at 2/3 of the scantime */
+ unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
+ unsigned const long request_nonce = MAXTHREADS / 3 * 2;
+ bool requested = false;
/* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
* and if that fails, then SCHED_BATCH. No need for this to be an
@@ -851,15 +850,25 @@ static void *miner_thread(void *userdata)
bool rc;
if (needs_work) {
+ if (work_restart[thr_id].restart) {
+ if (requested) {
+ /* We have one extra request than desired now */
+ if (unlikely(!discard_request())) {
+ applog(LOG_ERR, "Failed to discard request in uminer thread");
+ goto out;
+ }
+ } else
+ requested = true;
+ }
gettimeofday(&tv_workstart, NULL);
/* obtain new work from internal workio thread */
- if (unlikely(!get_work(&work))) {
+ if (unlikely(!get_work(&work, requested))) {
applog(LOG_ERR, "work retrieval failed, exiting "
"mining thread %d", mythr->id);
goto out;
}
work.thr_id = thr_id;
- needs_work = false;
+ needs_work = requested = false;
work.blk.nonce = 0;
}
hashes_done = 0;
@@ -958,6 +967,12 @@ static void *miner_thread(void *userdata)
}
timeval_subtract(&diff, &tv_end, &tv_workstart);
+ if (!requested && (diff.tv_sec > request_interval || work.blk.nonce > request_nonce)) {
+ if (unlikely(!queue_request()))
+ goto out;
+ requested = true;
+ }
+
if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
work.blk.nonce >= MAXTHREADS - hashes_done)
needs_work = true;
@@ -1031,8 +1046,8 @@ static inline int gpu_from_thr_id(int thr_id)
static void *gpuminer_thread(void *userdata)
{
const unsigned long cycle = opt_log_interval / 5 ? : 1;
+ struct timeval tv_start, tv_end, diff;
struct thr_info *mythr = userdata;
- struct timeval tv_start, diff;
const int thr_id = mythr->id;
uint32_t *res, *blank_res;
@@ -1050,6 +1065,11 @@ static void *gpuminer_thread(void *userdata)
unsigned const int hashes = threads * vectors;
unsigned int hashes_done = 0;
+ /* Request the next work item at 2/3 of the scantime */
+ unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
+ unsigned const long request_nonce = MAXTHREADS / 3 * 2;
+ bool requested = false;
+
res = calloc(BUFFERSIZE, 1);
blank_res = calloc(BUFFERSIZE, 1);
@@ -1061,11 +1081,11 @@ static void *gpuminer_thread(void *userdata)
gettimeofday(&tv_start, NULL);
globalThreads[0] = threads;
localThreads[0] = clState->work_size;
- work_restart[thr_id].restart = 1;
- diff.tv_sec = 0;
+ diff.tv_sec = ~0UL;
+ gettimeofday(&tv_end, NULL);
while (1) {
- struct timeval tv_end, tv_workstart;
+ struct timeval tv_workstart;
/* This finish flushes the readbuffer set with CL_FALSE later */
clFinish(clState->commandQueue);
@@ -1078,13 +1098,24 @@ static void *gpuminer_thread(void *userdata)
memset(res, 0, BUFFERSIZE);
gettimeofday(&tv_workstart, NULL);
+ if (work_restart[thr_id].restart) {
+ if (requested) {
+ /* We have one extra request than desired now */
+ if (unlikely(!discard_request())) {
+ applog(LOG_ERR, "Failed to discard request in gpuminer thread");
+ goto out;
+ }
+ } else
+ requested = true;
+ }
/* obtain new work from internal workio thread */
- if (unlikely(!get_work(work))) {
+ if (unlikely(!get_work(work, requested))) {
applog(LOG_ERR, "work retrieval failed, exiting "
"gpu mining thread %d", mythr->id);
goto out;
}
work->thr_id = thr_id;
+ requested = false;
precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
work->blk.nonce = 0;
@@ -1136,6 +1167,11 @@ static void *gpuminer_thread(void *userdata)
}
timeval_subtract(&diff, &tv_end, &tv_workstart);
+ if (!requested && (diff.tv_sec > request_interval || work->blk.nonce > request_nonce)) {
+ if (unlikely(!queue_request()))
+ goto out;
+ requested = true;
+ }
}
out:
tq_freeze(mythr->q);
@@ -1149,8 +1185,18 @@ static void restart_threads(void)
/* Discard old queued requests and get new ones */
flush_requests();
- for (i = 0; i < opt_n_threads + gpu_threads; i++)
+
+ /* Queue extra requests for each worker thread since they'll all need
+ * new work. Each worker will set their "requested" flag to true
+ * should they receive a .restart */
+ for (i = 0; i < opt_n_threads + gpu_threads; i++) {
+ if (unlikely(!queue_request())) {
+ applog(LOG_ERR, "Failed to queue requests in flush_requests");
+ kill_work();
+ return;
+ }
work_restart[i].restart = 1;
+ }
}
static void *longpoll_thread(void *userdata)
@@ -1545,6 +1591,14 @@ int main (int argc, char *argv[])
}
}
+ /* Put the extra work in the queue */
+ for (i = 1; i < opt_queue; i++) {
+ if (unlikely(!queue_request())) {
+ applog(LOG_ERR, "Failed to queue_request in main");
+ return 1;
+ }
+ }
+
/* start GPU mining threads */
for (i = 0; i < gpu_threads; i++) {
int gpu = gpu_from_thr_id(i);