Add memory to cryptonight_ctx.

This commit is contained in:
XMRig 2017-04-21 11:56:11 +03:00
parent f29d05bdde
commit 8ab4c1c8bd
13 changed files with 85 additions and 81 deletions

View File

@ -40,6 +40,6 @@ extern void (* const extra_hashes[4])(const void *, size_t, char *);
void cryptonight_init(int variant);
void cryptonight_hash(void* output, const void* input, size_t input_len);
int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict pdata, const uint32_t *restrict ptarget, uint32_t max_nonce, unsigned long *restrict hashes_done, const char *memory, struct cryptonight_ctx *persistentctx);
int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict pdata, const uint32_t *restrict ptarget, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx);
#endif /* __CRYPTONIGHT_H__ */

View File

@ -222,13 +222,13 @@ static inline void cn_implode_scratchpad(const __m128i* input, __m128i* output)
}
void cryptonight_av1_aesni(void *restrict output, const void *restrict input, char *restrict memory, struct cryptonight_ctx *restrict ctx)
void cryptonight_av1_aesni(void *restrict output, const void *restrict input, struct cryptonight_ctx *restrict ctx)
{
keccak((const uint8_t *) input, 76, ctx->state, 200);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) memory);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) ctx->memory);
const uint8_t* l0 = memory;
const uint8_t* l0 = ctx->memory;
uint64_t* h0 = (uint64_t*) ctx->state;
uint64_t al0 = h0[0] ^ h0[4];
@ -239,16 +239,16 @@ void cryptonight_av1_aesni(void *restrict output, const void *restrict input, ch
for (size_t i = 0; __builtin_expect(i < 0x80000, 1); i++) {
__m128i cx;
cx = _mm_load_si128((__m128i *)&l0[idx0 & 0x1FFFF0]);
cx = _mm_load_si128((__m128i *) &l0[idx0 & 0x1FFFF0]);
cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0));
_mm_store_si128((__m128i *)&l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx));
_mm_store_si128((__m128i *) &l0[idx0 & 0x1FFFF0], _mm_xor_si128(bx0, cx));
idx0 = _mm_cvtsi128_si64(cx);
bx0 = cx;
uint64_t hi, lo, cl, ch;
cl = ((uint64_t*)&l0[idx0 & 0x1FFFF0])[0];
ch = ((uint64_t*)&l0[idx0 & 0x1FFFF0])[1];
cl = ((uint64_t*) &l0[idx0 & 0x1FFFF0])[0];
ch = ((uint64_t*) &l0[idx0 & 0x1FFFF0])[1];
lo = _umul128(idx0, cl, &hi);
al0 += hi;
@ -262,7 +262,7 @@ void cryptonight_av1_aesni(void *restrict output, const void *restrict input, ch
idx0 = al0;
}
cn_implode_scratchpad((__m128i*) memory, (__m128i*) ctx->state);
cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state);
keccakf(h0, 24);
extra_hashes[ctx->state[0] & 3](ctx->state, 200, output);

View File

@ -222,13 +222,13 @@ static inline void cn_implode_scratchpad(const __m128i* input, __m128i* output)
}
void cryptonight_av2_aesni_stak(void *restrict output, const void *restrict input, char *restrict memory, struct cryptonight_ctx *restrict ctx)
void cryptonight_av2_aesni_stak(void *restrict output, const void *restrict input, struct cryptonight_ctx *restrict ctx)
{
keccak((const uint8_t *) input, 76, ctx->state, 200);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) memory);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) ctx->memory);
const uint8_t* l0 = memory;
const uint8_t* l0 = ctx->memory;
uint64_t* h0 = (uint64_t*) ctx->state;
uint64_t al0 = h0[0] ^ h0[4];
@ -266,7 +266,7 @@ void cryptonight_av2_aesni_stak(void *restrict output, const void *restrict inpu
_mm_prefetch((const char*)&l0[idx0 & 0x1FFFF0], _MM_HINT_T0);
}
cn_implode_scratchpad((__m128i*) memory, (__m128i*) ctx->state);
cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state);
keccakf(h0, 24);
extra_hashes[ctx->state[0] & 3](ctx->state, 200, output);

View File

@ -222,13 +222,13 @@ static inline void cn_implode_scratchpad(const __m128i* input, __m128i* output)
}
void cryptonight_av3_aesni_bmi2(void *restrict output, const void *restrict input, char *restrict memory, struct cryptonight_ctx *restrict ctx)
void cryptonight_av3_aesni_bmi2(void *restrict output, const void *restrict input, struct cryptonight_ctx *restrict ctx)
{
keccak((const uint8_t *) input, 76, ctx->state, 200);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) memory);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) ctx->memory);
const uint8_t* l0 = memory;
const uint8_t* l0 = ctx->memory;
uint64_t* h0 = (uint64_t*) ctx->state;
uint64_t al0 = h0[0] ^ h0[4];
@ -262,7 +262,7 @@ void cryptonight_av3_aesni_bmi2(void *restrict output, const void *restrict inpu
idx0 = al0;
}
cn_implode_scratchpad((__m128i*) memory, (__m128i*) ctx->state);
cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state);
keccakf(h0, 24);
extra_hashes[ctx->state[0] & 3](ctx->state, 200, output);

View File

@ -203,13 +203,13 @@ static inline void cn_implode_scratchpad(const __m128i* input, __m128i* output)
}
void cryptonight_av4_softaes(void *restrict output, const void *restrict input, char *restrict memory, struct cryptonight_ctx *restrict ctx)
void cryptonight_av4_softaes(void *restrict output, const void *restrict input, struct cryptonight_ctx *restrict ctx)
{
keccak((const uint8_t *) input, 76, ctx->state, 200);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) memory);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) ctx->memory);
const uint8_t* l0 = memory;
const uint8_t* l0 = ctx->memory;
uint64_t* h0 = (uint64_t*) ctx->state;
uint64_t al0 = h0[0] ^ h0[4];
@ -243,7 +243,7 @@ void cryptonight_av4_softaes(void *restrict output, const void *restrict input,
idx0 = al0;
}
cn_implode_scratchpad((__m128i*) memory, (__m128i*) ctx->state);
cn_implode_scratchpad((__m128i*) ctx->memory, (__m128i*) ctx->state);
keccakf(h0, 24);
extra_hashes[ctx->state[0] & 3](ctx->state, 200, output);

View File

@ -213,8 +213,10 @@ static inline void cn_implode_scratchpad(const __m128i* input, __m128i* output)
}
void cryptonight_av5_aesni_experimental(void *restrict output, const void *restrict input, char *restrict memory, struct cryptonight_ctx *restrict ctx)
void cryptonight_av5_aesni_experimental(void *restrict output, const void *restrict input, struct cryptonight_ctx *restrict ctx)
{
const uint8_t* memory = ctx->memory;
keccak((const uint8_t *) input, 76, ctx->state, 200);
cn_explode_scratchpad((__m128i*) ctx->state, (__m128i*) memory);

View File

@ -37,18 +37,18 @@
#if defined(__x86_64__)
void cryptonight_av1_aesni(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av2_aesni_stak(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av3_aesni_bmi2(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av4_softaes(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av5_aesni_experimental(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av1_aesni(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av2_aesni_stak(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av3_aesni_bmi2(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av4_softaes(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av5_aesni_experimental(void* output, const void* input, struct cryptonight_ctx* ctx);
#elif defined(__i386__)
void cryptonight_av1_aesni32(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
#endif
void cryptonight_av4_softaes(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av4_softaes(void* output, const void* input, struct cryptonight_ctx* ctx);
void (*cryptonight_hash_ctx)(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx) = NULL;
void (*cryptonight_hash_ctx)(void* output, const void* input, struct cryptonight_ctx* ctx) = NULL;
void cryptonight_init(int variant)
@ -111,26 +111,15 @@ static inline void do_skein_hash(const void* input, size_t len, char* output) {
void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash};
void cryptonight_hash(void* output, const void* input, size_t len) {
uint8_t *memory __attribute((aligned(16))) = (uint8_t *) malloc(MEMORY);
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx));
cryptonight_hash_ctx(output, input, memory, ctx);
free(memory);
free(ctx);
}
#ifndef BUILD_TEST
int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict pdata, const uint32_t *restrict ptarget, uint32_t max_nonce, unsigned long *restrict hashes_done, const char *restrict memory, struct cryptonight_ctx *persistentctx) {
int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict pdata, const uint32_t *restrict ptarget, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx) {
uint32_t *nonceptr = (uint32_t*) (((char*)pdata) + 39);
uint32_t n = *nonceptr - 1;
const uint32_t first_nonce = n + 1;
do {
*nonceptr = ++n;
cryptonight_hash_ctx(hash, pdata, memory, persistentctx);
cryptonight_hash_ctx(hash, pdata, ctx);
if (unlikely(hash[7] < ptarget[7])) {
*hashes_done = n - first_nonce + 1;

View File

@ -24,6 +24,7 @@
#include <string.h>
#include "persistent_memory.h"
#include "algo/cryptonight/cryptonight.h"
static size_t offset = 0;
@ -36,3 +37,11 @@ void * persistent_calloc(size_t num, size_t size) {
return mem;
}
void * create_persistent_ctx(int thr_id) {
struct cryptonight_ctx *ctx = (struct cryptonight_ctx *) &persistent_memory[MEMORY - sizeof(struct cryptonight_ctx) * (thr_id + 1)];
ctx->memory = &persistent_memory[MEMORY * (thr_id + 1)];
return ctx;
}

View File

@ -34,7 +34,7 @@ enum memory_flags {
};
#define TWO_MB_PAGE 2097152
#define MEMORY 2097152
extern char *persistent_memory;
@ -44,6 +44,7 @@ extern int persistent_memory_flags;
const char * persistent_memory_allocate();
void persistent_memory_free();
void * persistent_calloc(size_t num, size_t size);
void * create_persistent_ctx(int thr_id);
#endif /* __PERSISTENT_MEMORY_H__ */

View File

@ -4,11 +4,11 @@
#include <algo/cryptonight/cryptonight.h>
void cryptonight_av1_aesni(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av2_aesni_stak(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av3_aesni_bmi2(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av4_softaes(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av5_aesni_experimental(void* output, const void* input, const char *memory, struct cryptonight_ctx* ctx);
void cryptonight_av1_aesni(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av2_aesni_stak(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av3_aesni_bmi2(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av4_softaes(void* output, const void* input, struct cryptonight_ctx* ctx);
void cryptonight_av5_aesni_experimental(void* output, const void* input, struct cryptonight_ctx* ctx);
char hash[32];
@ -17,7 +17,7 @@ char data[76];
#define RESULT "1a3ffbee909b420d91f7be6e5fb56db71b3110d886011e877ee5786afd080100"
char *bin2hex(const unsigned char *p, size_t len)
static char *bin2hex(const unsigned char *p, size_t len)
{
char *s = malloc((len * 2) + 1);
if (!s) {
@ -31,7 +31,7 @@ char *bin2hex(const unsigned char *p, size_t len)
return s;
}
bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
static bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
{
char hex_byte[3];
char *ep;
@ -57,14 +57,26 @@ bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
}
void test_cryptonight_av1_should_CalcHash(void) {
uint8_t *memory = (uint8_t *) malloc(MEMORY);
static void * create_ctx() {
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx));
ctx->memory = (uint8_t *) malloc(MEMORY);
cryptonight_av1_aesni(&hash, data, memory, ctx);
return ctx;
}
free(memory);
static void free_ctx(struct cryptonight_ctx *ctx) {
free(ctx->memory);
free(ctx);
}
void test_cryptonight_av1_should_CalcHash(void) {
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx();
cryptonight_av1_aesni(&hash, data, ctx);
free_ctx(ctx);
TEST_ASSERT_EQUAL_STRING(RESULT, bin2hex(hash, 32));
}
@ -72,13 +84,11 @@ void test_cryptonight_av1_should_CalcHash(void) {
void test_cryptonight_av2_should_CalcHash(void)
{
uint8_t *memory = (uint8_t *) malloc(MEMORY);
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx));
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx();
cryptonight_av2_aesni_stak(&hash, data, memory, ctx);
cryptonight_av2_aesni_stak(&hash, data, ctx);
free(memory);
free(ctx);
free_ctx(ctx);
TEST_ASSERT_EQUAL_STRING(RESULT, bin2hex(hash, 32));
}
@ -86,13 +96,11 @@ void test_cryptonight_av2_should_CalcHash(void)
void test_cryptonight_av3_should_CalcHash(void)
{
uint8_t *memory = (uint8_t *) malloc(MEMORY);
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx));
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx();
cryptonight_av3_aesni_bmi2(&hash, data, memory, ctx);
cryptonight_av3_aesni_bmi2(&hash, data, ctx);
free(memory);
free(ctx);
free_ctx(ctx);
TEST_ASSERT_EQUAL_STRING(RESULT, bin2hex(hash, 32));
}
@ -100,13 +108,11 @@ void test_cryptonight_av3_should_CalcHash(void)
void test_cryptonight_av4_should_CalcHash(void)
{
uint8_t *memory = (uint8_t *) malloc(MEMORY);
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx));
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx();
cryptonight_av4_softaes(&hash, data, memory, ctx);
cryptonight_av4_softaes(&hash, data, ctx);
free(memory);
free(ctx);
free_ctx(ctx);
TEST_ASSERT_EQUAL_STRING(RESULT, bin2hex(hash, 32));
}
@ -114,13 +120,11 @@ void test_cryptonight_av4_should_CalcHash(void)
void test_cryptonight_av5_should_CalcHash(void)
{
uint8_t *memory = (uint8_t *) malloc(MEMORY);
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx));
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) create_ctx();
cryptonight_av5_aesni_experimental(&hash, data, memory, ctx);
cryptonight_av5_aesni_experimental(&hash, data, ctx);
free(memory);
free(ctx);
free_ctx(ctx);
TEST_ASSERT_EQUAL_STRING(RESULT, bin2hex(hash, 32));
}

View File

@ -38,7 +38,7 @@ int persistent_memory_flags = 0;
const char * persistent_memory_allocate() {
const int size = TWO_MB_PAGE * (opt_n_threads + 1);
const int size = MEMORY * (opt_n_threads + 1);
persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE;
persistent_memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0);
@ -63,7 +63,7 @@ const char * persistent_memory_allocate() {
void persistent_memory_free() {
const int size = TWO_MB_PAGE * (opt_n_threads + 1);
const int size = MEMORY * (opt_n_threads + 1);
if (persistent_memory_flags & MEMORY_HUGEPAGES_ENABLED) {
if (persistent_memory_flags & MEMORY_LOCK) {

View File

@ -58,7 +58,6 @@ static BOOL SetLockPagesPrivilege(HANDLE hProcess, BOOL bEnable) {
} Info;
HANDLE Token;
BOOL result;
if (OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &Token) != TRUE) {
return FALSE;
@ -86,7 +85,7 @@ static BOOL SetLockPagesPrivilege(HANDLE hProcess, BOOL bEnable) {
const char * persistent_memory_allocate() {
const int size = TWO_MB_PAGE * (opt_n_threads + 1);
const int size = MEMORY * (opt_n_threads + 1);
if (SetLockPagesPrivilege(GetCurrentProcess(), TRUE)) {
persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE;

View File

@ -284,7 +284,7 @@ static void *miner_thread(void *userdata) {
applog(LOG_BLUE, "%d", sizeof(struct cryptonight_ctx));
struct cryptonight_ctx *persistentctx = (struct cryptonight_ctx *) &persistent_memory[MEMORY - sizeof(struct cryptonight_ctx) * (thr_id + 1)];
struct cryptonight_ctx *persistentctx = (struct cryptonight_ctx *) create_persistent_ctx(thr_id);
if (cpu_info.count > 1 && opt_n_threads > 1 && opt_affinity != -1L) {
affine_to_cpu_mask(thr_id, (unsigned long) opt_affinity);
@ -337,7 +337,7 @@ static void *miner_thread(void *userdata) {
gettimeofday(&tv_start, NULL );
/* scan nonces for a proof-of-work hash */
rc = scanhash_cryptonight(thr_id, hash, work.data, work.target, max_nonce, &hashes_done, &persistent_memory[MEMORY * (thr_id + 1)], persistentctx);
rc = scanhash_cryptonight(thr_id, hash, work.data, work.target, max_nonce, &hashes_done, persistentctx);
stats_add_hashes(thr_id, &tv_start, hashes_done);
memcpy(work.hash, hash, 32);