Linux, xmr-stak and huge pages

After xmr-stak is successfully compiled, with enabling huge pages Monero mining can be improved for around 20%. In this post will be shown how to set huge pages on Linux and how to set memlock too. And lastly, you will find Bash script for starting Monero miner.

The Monero scratchpad requires a minimum of 2MB memory for Cryptonight algorithm. With enabling huge/large pages, scratchpad “will live” permanently in CPU cache (if L2/L3 cache is big enough) without needing to dump it out every computing cycle. More details can be read on Reddit page:

https://www.reddit.com/r/NiceHash/comments/7n0p6y/cache_considerations_for_cryptonight_mining/

To immediate enable huge pages on Linux (until next reboot) just run the following command:

root> sysctl -w vm.nr_hugepages=128

However, for permanent huge pages settings, add vm.nr_hugepages=128 line to the /etc/sysctl.conf configuration file:

# enable hugh/large pages for Monero mining
vm.nr_hugepages=128

With enabled huge pages, miner will work better and it will give more hash rate. For getting best CPU results, the number of miner threads should be calculated with bearing in mind the amount of CPU L2/L3 cache. This way each thread will get the minimum of needed memory (2MB) as is explained before.

thread_number = L2/L3 cache [MB] / 2 [MB]

The next issue I had with xmr-stak was error message printed during start up:

MEMORY ALLOC FAILED: mlock failed

Despite this error message, hash rate was OK and miner didn’t show any problem later. It is possible to recompile xmr-stak with -DHWLOC_ENABLE=OFF settings but better option is to increase memlock. With enabled hwlock option, system can much better be analyzed and dual/quad/octa socket systems will be supported. Here can be found more details about hwlock:

https://github.com/fireice-uk/xmr-stak-cpu/issues/376

So, to increase memlock, just create file /etc/security/limits.d/60-memlock.conf and add the following two lines:

*    - memlock 262144
root - memlock 262144

After 60-memlock.conf file is created, re-login is needed for loading new settings. To display current memlock size, use ulimit command:

[dbunic@pc-dbunic ~]$ ulimit -l 
262144

And here is how to start xmr-stak from command line. It is actually small Bash script named monero1.sh with option to run miner after logout and to redirect all output to the log file:

#!/bin/bash

#
# CPU & GUI 480 H/s
#

# create timestamp
ts=$(date +"%Y%m%d_%H%M")

# kill all xmr-stak processes
killall xmr-stak > /dev/null 2>&1

# pause 4 seconds
sleep 4

# cd to directory
cd /home/dbunic/monero/config1

# start xmr-stak
nohup ../bin/xmr-stak > /tmp/monero_$ts.log 2>&1 &

# print message
#echo "Monero1 started ..."

In my case, I have several Monero configurations which are started from crontab. Light config (config2) is used when I work on PC and the other when PC is turned to full Monero rig. Here is how crontab looks:

MAILTO=""

# every workday in 07:15 use config2
15 07 * * 1-5 /home/dbunic/Documents/bin/monero2.sh

# every workday in 17:01 use config1
01 17 * * 1-5 /home/dbunic/Documents/bin/monero1.sh

# on saturday and sunday restart Monero miner in 06:01 using config1
01 06 * * 6,7 /home/dbunic/Documents/bin/monero1.sh

Hope you’ll find useful information on this post and wish you happy mining.
Cheers!