]> git.sesse.net Git - stockfish/commitdiff
Turn on MADV_RANDOM for Syzygy mmaps (on Unix-like builds)
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 24 Nov 2018 10:17:12 +0000 (11:17 +0100)
committerStéphane Nicolet <cassio@free.fr>
Tue, 27 Nov 2018 07:39:23 +0000 (08:39 +0100)
When running on a cloud VM (n1-highcpu-96) with several NVMe SSDs and
some non-SSDs for tablebases, I noticed that the average SSD request size was
more than 256 kB. This doesn't make a lot of sense for Syzygy tablebases,
which have a block size of 32 bytes and very low locality.

Seemingly, the tablebase access patterns during probing make the OS,
at least Linux, think that readahead is advantageous; normally, it
gives up doing readahead if there are too many misses, but it doesn't,
perhaps due to the fairly high overall hit rates. (It seems the kernel cannot
distinguish between reading a block that was paged in because the userspace
wanted it explicitly, and one that was read as part of readahead.)

Setting MADV_RANDOM effectively turns off readahead, which causes
the request size to drop to 4 kB. In the aforemented cloud VM test,
this roughly tripled the amount of I/O requests that were able to go
through, while reducing the total traffic from 2.8 GB/sec to 56 MB/sec
(moving the bottleneck to the non-SSDs; it seems the SSDs could have
sustained many more requests).

Closes https://github.com/official-stockfish/Stockfish/pull/1829

No functional change.

src/syzygy/tbprobe.cpp

index 235fe1953bf97505a8be256b617ef124d9647278..8f1ac15f9431b735615d44db5300911ae9fc34b2 100644 (file)
@@ -216,6 +216,7 @@ public:
         fstat(fd, &statbuf);
         *mapping = statbuf.st_size;
         *baseAddress = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
         fstat(fd, &statbuf);
         *mapping = statbuf.st_size;
         *baseAddress = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
+        madvise(*baseAddress, statbuf.st_size, MADV_RANDOM);
         ::close(fd);
 
         if (*baseAddress == MAP_FAILED) {
         ::close(fd);
 
         if (*baseAddress == MAP_FAILED) {