Skip to content

Commit

Permalink
Hugepages for linux optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-andersen committed Jun 22, 2014
1 parent 1ada156 commit 1732617
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/miner/simpleminer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "currency_core/account.h"
#include "currency_core/currency_format_utils.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include <sys/mman.h>

using namespace epee;
namespace po = boost::program_options;
Expand Down Expand Up @@ -101,6 +102,8 @@ namespace mining
m_pass = command_line::get_arg(vm, arg_pass);
m_hi = AUTO_VAL_INIT(m_hi);
m_last_job_ticks = 0;
m_fast_scratchpad_pages = 0;
m_fast_scratchpad = NULL;

return true;
}
Expand Down Expand Up @@ -142,7 +145,7 @@ namespace mining
crypto::hash h = currency::null_hash;
currency::get_blob_longhash(blob, h, m_job.prev_hi.height+1, [&](uint64_t index) -> crypto::hash&
{
return m_scratchpad[index%m_scratchpad.size()];
return m_fast_scratchpad[index%m_scratchpad.size()];
});

if( currency::check_hash(h, m_job.difficulty))
Expand Down Expand Up @@ -237,6 +240,7 @@ namespace mining

uint64_t get_job_start_time = epee::misc_utils::get_tick_count();
get_job(); /* Next version: Handle this asynchronously */
update_fast_scratchpad();
uint64_t get_job_end_time = epee::misc_utils::get_tick_count();
if ((get_job_end_time - get_job_start_time) > 1000)
{
Expand Down Expand Up @@ -388,6 +392,40 @@ namespace mining
LOG_PRINT_L0("Scratchpad received ok, size: " << (m_scratchpad.size()*32)/1024 << "Kb, heigh=" << m_hi.height);
return true;
}
//----------------------------------------------------------------------------------------------------------------------------------
void simpleminer::update_fast_scratchpad()
{

/* Check size - reallocate fast scratch if necessary */
size_t cur_scratchpad_size = m_scratchpad.size() * sizeof(crypto::hash);
size_t cur_scratchpad_pages = (cur_scratchpad_size / 4096) + 1;
if (cur_scratchpad_pages > m_fast_scratchpad_pages)
{
if (m_fast_scratchpad)
{
if (m_fast_mmapped)
{
munmap(m_fast_scratchpad, m_fast_scratchpad_pages*4096);
} else {
free(m_fast_scratchpad);
}
}
void *addr = MAP_FAILED;
size_t mapsize = cur_scratchpad_pages * 4096;
#ifdef MAP_HUGETLB
addr = mmap(0, mapsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|MAP_POPULATE, 0, 0);
#endif
if (addr == MAP_FAILED) {
addr = malloc(mapsize);
} else {
m_fast_mmapped = true;
}
m_fast_scratchpad = (crypto::hash *)addr;
}

memcpy(m_fast_scratchpad, &m_scratchpad[0], m_scratchpad.size() * sizeof(crypto::hash));
}

//----------------------------------------------------------------------------------------------------------------------------------
bool simpleminer::pop_addendum(const addendum& add)
{
Expand Down
4 changes: 4 additions & 0 deletions src/miner/simpleminer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ namespace mining
bool pop_addendum(const addendum& add);
bool push_addendum(const addendum& add);
void worker_thread(uint64_t start_nonce, uint32_t nonce_offset, std::atomic<uint32_t> *result, std::atomic<bool> *do_reset, std::atomic<bool> *done);
void update_fast_scratchpad();

std::vector<mining::addendum> m_blocks_addendums; //need to handle splits without re-downloading whole scratchpad
height_info_native m_hi;
std::vector<crypto::hash> m_scratchpad;
crypto::hash *m_fast_scratchpad;
uint32_t m_fast_scratchpad_pages;
uint64_t m_last_job_ticks;
bool m_fast_mmapped;
uint32_t m_threads_total;
std::atomic<uint64_t> m_hashes_done;
std::string m_pool_session_id;
Expand Down

0 comments on commit 1732617

Please sign in to comment.