Browse Source

Re-use CURL object, thereby caching DNS and HTTP connections where possible.

nfactor-troky
Jeff Garzik 14 years ago committed by Jeff Garzik
parent
commit
c0935a9489
  1. 18
      cpu-miner.c
  2. 3
      miner.h
  3. 14
      util.c

18
cpu-miner.c

@ -23,6 +23,7 @@
#include <pthread.h> #include <pthread.h>
#include <getopt.h> #include <getopt.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h>
#include "compat.h" #include "compat.h"
#include "miner.h" #include "miner.h"
@ -199,7 +200,7 @@ err_out:
return false; return false;
} }
static void submit_work(struct work *work) static void submit_work(CURL *curl, struct work *work)
{ {
char *hexstr = NULL; char *hexstr = NULL;
json_t *val, *res; json_t *val, *res;
@ -221,7 +222,7 @@ static void submit_work(struct work *work)
fprintf(stderr, "DBG: sending RPC call:\n%s", s); fprintf(stderr, "DBG: sending RPC call:\n%s", s);
/* issue JSON-RPC request */ /* issue JSON-RPC request */
val = json_rpc_call(rpc_url, userpass, s); val = json_rpc_call(curl, rpc_url, userpass, s);
if (!val) { if (!val) {
fprintf(stderr, "submit_work json_rpc_call failed\n"); fprintf(stderr, "submit_work json_rpc_call failed\n");
goto out; goto out;
@ -259,6 +260,13 @@ static void *miner_thread(void *thr_id_int)
static const char *rpc_req = static const char *rpc_req =
"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n"; "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
uint32_t max_nonce = 0xffffff; uint32_t max_nonce = 0xffffff;
CURL *curl;
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "CURL initialization failed\n");
return NULL;
}
while (1) { while (1) {
struct work work __attribute__((aligned(128))); struct work work __attribute__((aligned(128)));
@ -268,7 +276,7 @@ static void *miner_thread(void *thr_id_int)
bool rc; bool rc;
/* obtain new work from bitcoin */ /* obtain new work from bitcoin */
val = json_rpc_call(rpc_url, userpass, rpc_req); val = json_rpc_call(curl, rpc_url, userpass, rpc_req);
if (!val) { if (!val) {
fprintf(stderr, "json_rpc_call failed, "); fprintf(stderr, "json_rpc_call failed, ");
@ -369,11 +377,13 @@ static void *miner_thread(void *thr_id_int)
/* if nonce found, submit work */ /* if nonce found, submit work */
if (rc) if (rc)
submit_work(&work); submit_work(curl, &work);
failures = 0; failures = 0;
} }
curl_easy_cleanup(curl);
return NULL; return NULL;
} }

3
miner.h

@ -5,6 +5,7 @@
#include <stdint.h> #include <stdint.h>
#include <sys/time.h> #include <sys/time.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h>
#ifdef __SSE2__ #ifdef __SSE2__
#define WANT_SSE2_4WAY 1 #define WANT_SSE2_4WAY 1
@ -45,7 +46,7 @@ static inline void swap256(void *dest_p, const void *src_p)
extern bool opt_debug; extern bool opt_debug;
extern bool opt_protocol; extern bool opt_protocol;
extern const uint32_t sha256_init_state[]; extern const uint32_t sha256_init_state[];
extern json_t *json_rpc_call(const char *url, const char *userpass, extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
const char *rpc_req); const char *rpc_req);
extern char *bin2hex(unsigned char *p, size_t len); extern char *bin2hex(unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len); extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);

14
util.c

@ -80,9 +80,9 @@ static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
return len; return len;
} }
json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req) json_t *json_rpc_call(CURL *curl, const char *url,
const char *userpass, const char *rpc_req)
{ {
CURL *curl;
json_t *val, *err_val, *res_val; json_t *val, *err_val, *res_val;
int rc; int rc;
struct data_buffer all_data = { }; struct data_buffer all_data = { };
@ -92,11 +92,7 @@ json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req
char len_hdr[64]; char len_hdr[64];
char curl_err_str[CURL_ERROR_SIZE]; char curl_err_str[CURL_ERROR_SIZE];
curl = curl_easy_init(); /* it is assumed that 'curl' is freshly [re]initialized at this pt */
if (!curl) {
fprintf(stderr, "CURL initialization failed, aborting JSON-RPC call\n");
return NULL;
}
if (opt_protocol) if (opt_protocol)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
@ -172,13 +168,13 @@ json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req
databuf_free(&all_data); databuf_free(&all_data);
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_reset(curl);
return val; return val;
err_out: err_out:
databuf_free(&all_data); databuf_free(&all_data);
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_reset(curl);
return NULL; return NULL;
} }

Loading…
Cancel
Save