Skip to content

Commit

Permalink
solve memory leak issues
Browse files Browse the repository at this point in the history
  • Loading branch information
djm34 committed Feb 2, 2019
1 parent 996ec49 commit fd494ce
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 56 deletions.
38 changes: 19 additions & 19 deletions algorithm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1219,9 +1219,7 @@ static cl_int queue_mtp_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unus
// printf("coming in queue mtp kernel prev_job_id %s job_id %s\n", blk->work->prev_job_id, blk->work->job_id);

uint32_t test = 1;
if (pool->swork.prev_job_id!=NULL) {
test = strcmp(pool->swork.prev_job_id, pool->swork.job_id);
}

if (buffer->prev_job_id != NULL) {
test = strcmp(buffer->prev_job_id, pool->swork.job_id);
}
Expand Down Expand Up @@ -1436,8 +1434,7 @@ static cl_int queue_mtp_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unus
// mtp->ordered_tree = new MerkleTree(mtp->dx, true);
mtp->ordered_tree = call_new_MerkleTree(mtp->dx, true);

blk->work->prev_job_id = blk->work->job_id;
pool->swork.prev_job_id = pool->swork.job_id;

buffer->prev_job_id = pool->swork.job_id;

call_MerkleTree_getRoot(mtp->ordered_tree, mtp->TheMerkleRoot);
Expand All @@ -1454,14 +1451,14 @@ static cl_int queue_mtp_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unus
/////////////////////////////////////////////////////////////////////////////////////////////////////
//// hashing here
// DO NOT flip80.

status |= clEnqueueWriteBuffer(clState->commandQueue, buffer->root, CL_TRUE, 0, 4 * sizeof(uint32_t), mtp->TheMerkleRoot, 0, NULL, NULL);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d with writing to root buffer.", status);
cl_int status1 = 0;
status1 = clEnqueueWriteBuffer(clState->commandQueue, buffer->root, CL_TRUE, 0, 4 * sizeof(uint32_t), mtp->TheMerkleRoot, 0, NULL, NULL);
if (status1 != CL_SUCCESS) {
applog(LOG_ERR, "Error %d with writing to root buffer.", status1);
}
status |= clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 20 * sizeof(uint32_t), (unsigned char*)endiandata, 0, NULL, NULL);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d with writing to CLbuffer0.", status);
status1 = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 20 * sizeof(uint32_t), (unsigned char*)endiandata, 0, NULL, NULL);
if (status1 != CL_SUCCESS) {
applog(LOG_ERR, "Error %d with writing to CLbuffer0.", status1);
}

size_t p_global_work_offset = buffer->StartNonce;
Expand All @@ -1477,14 +1474,17 @@ static cl_int queue_mtp_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unus
CL_SET_ARG(buffer->root);
CL_SET_ARG(clState->outputBuffer);
CL_SET_ARG(le_target);
uint32_t *Solution = (uint32_t*)malloc(1024);

status |= clEnqueueNDRangeKernel(clState->commandQueue, clState->mtp_yloop, 1, &p_global_work_offset, &Global2, &Local2, 0, NULL, NULL);
if (status != CL_SUCCESS) {
applog(LOG_ERR, "Error %d with kernel mtp_yloop.", status);
uint32_t Solution[256];
status1 = clEnqueueNDRangeKernel(clState->commandQueue, clState->mtp_yloop, 1, &p_global_work_offset, &Global2, &Local2, 0, NULL, NULL);
if (status1 != CL_SUCCESS) {
applog(LOG_ERR, "Error %d with kernel mtp_yloop.", status1);
}

status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0, buffersize, Solution, 0, NULL, NULL);
status1 = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0, buffersize, Solution, 0, NULL, NULL);
if (status1 != CL_SUCCESS) {
applog(LOG_ERR, "Error reading Solution.", status1);
}
buffer->StartNonce += rawint;
if (Solution[0xff]) {
// uint256 TheUint256Target[1];
Expand All @@ -1511,7 +1511,7 @@ static cl_int queue_mtp_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unus
}
else {
Solution[0xff]=0;
status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0, buffersize, Solution, 0, NULL, NULL);
status1 = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0, buffersize, Solution, 0, NULL, NULL);
printf("*************************************************************************************Not a solution\n");
}
}
Expand Down
3 changes: 0 additions & 3 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -1591,9 +1591,6 @@ struct work {

uint32_t XMRNonce;

// mtp mtpPOW;


unsigned char equihash_data[1487];

int rolls;
Expand Down
48 changes: 28 additions & 20 deletions sgminer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6072,12 +6072,12 @@ static void *stratum_rthread_bos(void *userdata)

snprintf(threadname, sizeof(threadname), "%d/RStratum", pool->pool_no);
RenameThread(threadname);

json_t *s = NULL; //json_object();
while (42) {
struct timeval timeout;
int sel_ret;
fd_set rd;
json_t *s = json_object();


if (unlikely(pool->removed))
break;
Expand Down Expand Up @@ -6116,7 +6116,7 @@ static void *stratum_rthread_bos(void *userdata)
if (!sock_full(pool) && (sel_ret = select(pool->sock + 1, &rd, NULL, NULL, &timeout)) < 1) {
applog(LOG_DEBUG, "Stratum select failed on %s with value %d", get_pool_name(pool), sel_ret);
//s = NULL;
json_decref(s);
s=NULL;
}
else
s = recv_line_bos2(pool);
Expand Down Expand Up @@ -6185,7 +6185,7 @@ static void *stratum_rthread_bos(void *userdata)
test_work_current(work);
free_work(work);
}
// json_decref(s);
json_decref(s);
}

out:
Expand Down Expand Up @@ -6439,14 +6439,18 @@ static void *stratum_sthread_bos(void *userdata)


bos_t *serialized;
json_t *MyObject = json_object();
// json_t *MyObject = json_object();
uint32_t SizeMerkleRoot = 16;
uint32_t SizeReserved = 64;
uint32_t SizeMtpHash = 32;
uint32_t SizeBlockMTP = MTP_L * 2 * 128 * 8;
uint32_t SizeProofMTP = MTP_L * 3 * 353;
uint32_t ntime;



while (42) {
json_t *MyObject = json_object();
char noncehex[12], nonce2hex[20];
struct stratum_share *sshare;
uint32_t *hash32, nonce;
Expand Down Expand Up @@ -6500,33 +6504,36 @@ static void *stratum_sthread_bos(void *userdata)

unsigned char* hexjob_id = (unsigned char*)malloc(4);
hex2bin(hexjob_id, work->job_id, 8);

json_t *json_arr = json_array();


json_t *json_arr =json_array();

json_object_set_new(MyObject, "id", json_integer(4));
json_object_set_new(MyObject, "method", json_string("mining.submit"));
json_object_set_new(MyObject, "params", json_arr);

json_array_append(json_arr, json_string(pool->rpc_user));

json_t *TheString = json_stringn(pool->rpc_user, strlen(pool->rpc_user));

json_array_append(json_arr, TheString);
json_array_append(json_arr, json_bytes((unsigned char*)hexjob_id, 4));
json_array_append(json_arr, json_bytes((unsigned char*)&work->nonce2, sizeof(uint64_t*)));
json_array_append(json_arr, json_bytes((unsigned char*)&ntime, sizeof(uint32_t)));
json_array_append(json_arr, json_bytes((unsigned char*)&nonce, sizeof(uint32_t)));
json_array_append(json_arr, json_bytes(pool->mtp_cache.mtpPOW.MerkleRoot, SizeMerkleRoot));
json_array_append(json_arr, json_bytes((unsigned char*)pool->mtp_cache.mtpPOW.nBlockMTP, SizeBlockMTP));
json_array_append(json_arr, json_bytes(pool->mtp_cache.mtpPOW.nProofMTP, SizeProofMTP));
json_object_set(MyObject, "params", json_arr);

json_error_t boserror;
bos_t *serialized = bos_serialize(MyObject, &boserror);

if (json_arr->refcount!=0)
json_decref(json_arr);

json_decref(json_arr);

// json_arr = NULL;

if (TheString->refcount!=0)
json_decref(TheString);

// applog(LOG_INFO, "Serialized size %d\n",serialized->size);
// stratum.sharediff = work->sharediff[0];
if (MyObject->refcount!=0)
json_decref(MyObject);


applog(LOG_INFO, "Submitting share %08lx to %s", (long unsigned int)htole32(hash32[6]), get_pool_name(pool));

Expand All @@ -6550,7 +6557,7 @@ static void *stratum_sthread_bos(void *userdata)
applog(LOG_INFO, "Pool %d stratum share submission lag time %d seconds",
pool->pool_no, ssdiff);
}

HASH_ADD_INT(stratum_shares, id, sshare);
pool->sshares++;
mutex_unlock(&sshare_lock);
Expand Down Expand Up @@ -6592,14 +6599,15 @@ static void *stratum_sthread_bos(void *userdata)
pool->stale_shares++;
total_stale++;
}

}

/* Freeze the work queue but don't free up its memory in case there is
* work still trying to be submitted to the removed pool. */
tq_freeze(pool->stratum_q);

if(MyObject!=NULL)
json_decref(MyObject);
// if(MyObject!=NULL)
// json_decref(MyObject);


return NULL;
Expand Down
5 changes: 3 additions & 2 deletions submodules/jansson/src/bos_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,10 @@ bos_t *bos_serialize(json_t *value, json_error_t *error) {
/*(unsigned char*)*/buffer->pos += 4;
buffer->size = 4;

if (!write_value(value, buffer, error))
if (!write_value(value, buffer, error)) {
jsonp_free(buffer);
return NULL;

}
size = (uint32_t)buffer->size;
/*
buffer->pos = buffer->data;
Expand Down
5 changes: 4 additions & 1 deletion submodules/jansson/src/jansson_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#define container_of(ptr_, type_, member_) \
((type_ *)((char *)ptr_ - offsetof(type_, member_)))

#define bytes_container_of(ptr_, type_, member_) \
((type_ *)((unsigned char *)ptr_ - offsetof(type_, member_)))

/* On some platforms, max() may already be defined */
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
Expand Down Expand Up @@ -93,7 +96,7 @@ typedef struct {
#define json_to_string(json_) container_of(json_, json_string_t, json)
#define json_to_real(json_) container_of(json_, json_real_t, json)
#define json_to_integer(json_) container_of(json_, json_integer_t, json)
#define json_to_bytes(json_) container_of(json_, json_bytes_t, json)
#define json_to_bytes(json_) bytes_container_of(json_, json_bytes_t, json)

/* Create a string by taking ownership of an existing buffer */
json_t *jsonp_stringn_nocheck_own(const char *value, size_t len);
Expand Down
1 change: 0 additions & 1 deletion submodules/jansson/src/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,6 @@ json_t *json_bytes(void *value, size_t size)
if(!bytes)
return NULL;
json_init(&bytes->json, JSON_BYTES);

bytes->value = value;
bytes->size = size;
return &bytes->json;
Expand Down
Loading

0 comments on commit fd494ce

Please sign in to comment.