<div dir="ltr"><div><div>Now I&#39;m befuddled.  I modified the source so that it was easy to try different combinations.  If I sudo it, I can map a 1GiB (1&lt;&lt;30) private anonymous region if I sudo the program.  I cannot find any combination with MAP_HUGETLB that works at all.<br>

<br></div>Here it is in  working form, with an anonymous map and no backing file.  The mmap request is for a single byte, so it works for ordinary users  Try uncommenting the MAP_HUGETLB line, and I&#39;d love to hear about a version that works, even if it&#39;s only for root.<br>

<br></div><div>Since I&#39;m aiming for a huge memory region, I figured HUGETLB would make sense to limit the overhead.  <br></div><div><br></div>============================ CUT HERE ================<br>/**<br> * @file<br>

 * &lt;pre&gt;&quot;Find out the limits on locked memory&quot;<br> * Last Modified: Mon Aug 18 11:21:23 PDT 2014<br> * @author Kevin O&#39;Gorman<br> */<br><br>#define _GNU_SOURCE /* enable some of the mmap flags */<br>#include &lt;unistd.h&gt;<br>

#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;sys/mman.h&gt;<br><br>/* for getrlimit(2) */<br>#include &lt;sys/time.h&gt;<br>#include &lt;sys/resource.h&gt;<br><br>/* for open(2) */<br>

#include &lt;sys/types.h&gt;<br>#include &lt;sys/stat.h&gt;<br>#include &lt;sys/fcntl.h&gt;<br><br>int<br>main(int argc, char *argv[])<br>{<br>  void *where;<br>  size_t length = 1;<br>  struct rlimit rlim;<br>  int flags = 0;<br>

  int fd = -1;<br><br>//  fd = open(&quot;solvedtaq&quot;, O_RDWR);  if (fd == -1) perror(&quot;open&quot;);<br>  printf(&quot;fd is %d\n&quot;, fd);<br><br>//  flags |= MAP_HUGETLB;<br>  flags |= MAP_PRIVATE;<br>  flags |= MAP_LOCKED;<br>

  flags |= MAP_ANONYMOUS;<br><br>  if (getrlimit(RLIMIT_MEMLOCK, &amp;rlim) == 0) {<br>    printf(&quot;RLIMIT_MEMLOCK: hard: %lld, soft: %lld\n&quot;, (long long)rlim.rlim_max, (long long)rlim.rlim_cur);<br>  } else {<br>

    perror(&quot;getrlimit failed&quot;);<br>  }<br><br>  where = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, 0);<br>  if (where != MAP_FAILED) {<br>    printf(&quot;Mapped at %p\n&quot;, where);<br>  } else {<br>

    perror(&quot;Mapping failed&quot;);<br>  }<br>  <br>  return EXIT_SUCCESS;<br>}<br>============================ CUT HERE ================<br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Aug 18, 2014 at 10:24 AM, Kevin O&#39;Gorman <span dir="ltr">&lt;<a href="mailto:kogorman@gmail.com" target="_blank">kogorman@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote"><div class="">On Mon, Aug 18, 2014 at 9:46 AM,  <span dir="ltr">&lt;<a href="mailto:Valdis.Kletnieks@vt.edu" target="_blank">Valdis.Kletnieks@vt.edu</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On Mon, 18 Aug 2014 08:15:12 -0700, &quot;Kevin O&#39;Gorman&quot; said:<br>
<br>
&gt; maybe someone here can help me figure out how to map a big (really big)<br>
&gt; work area and lock it in memory.<br>
<br>
</div>What problem are you trying to solve by doing this?  It usually doesn&#39;t make<br>
sense to lock more than a small chunk in memory - that&#39;s mostly for crypto<br>
programs like PGP that are paranoid about keys being written to swap spaces,<br>
and a *very* few highly performance oriented programs.  99.8% of the time,<br>
the kernel will do a reasonable job of making sure the right pages are in<br>
memory.<br>
<br>
Plus, programmers almost always over-estimate what they *really* need to lock<br>
down.  The end result is that performance ends up being *worse*, because they<br>
lock out more than needed, leaving all the rest of the processes on the system<br>
fighting over a too-small pool of pages.  Really sucks when you actually have<br>
to read  from disk every I/O because you no longer have an in-core file cache :)<br>
<div><br></div></blockquote></div><div>I&#39;m solving a huge alpha-beta search problem, and want a huge cache table tocheck permutations.  That&#39;s why I increased RAM from 8 to 32 GB, and would<br>be happy to use the extra 24GB all for the cache.  (Cache table is like a hash table but with collisions causing replacement rather than chaining).<br>

<br></div><div>Even in my small tests, the cache is giving me speedups in the neighborhood of<br></div><div>a million, so I&#39;m going big. Because this is about speed, I do not *ever* want it to<br></div><div>swap.<br>
</div><div class="">
<div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
&gt; I wrote a little test program to try this out, and it fails.  As a regular<br>
&gt; user, perror() tells me some &quot;resource is temporarily unavailable&quot;.  As<br>
&gt; root, it says it &quot;cannot allocate memory&quot;.  I&#39;m only asking for 1 byte.<br>
<br>
</div><div>&gt;   where = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE |<br>
&gt; MAP_ANONYMOUS | MAP_HUGETLB | MAP_LOCKED | MAP_POPULATE, -1, 0);<br>
<br>
</div>Remember that no matter what &quot;length&quot; you pass, even if it&#39;s 1 byte, the<br>
kernel has to allocate at least enough integer pages to cover the request.<br>
You ask for 4100 bytes, it has to allocate 2 pages because 4096 isn&#39;t<br>
quite big enough.<br>
<br>
And if you ask for MAP_HUGETLB, you&#39;re going to get at least 1 page.  Which<br>
is probably 2M in size.  And that can be painful if the default max lockable<br>
memory is less than 2M (at least on my Fedora Rawhide box, it&#39;s all of<br>
64K).  There&#39;s also some interaction danger with MAP_POPULATE and memory<br>
fragmentation.  But both POPULATE and HUGETLB have a high chance of<br>
not playing well with LOCKED.<br>
</blockquote></div></div><br></div><div class="gmail_extra">Good catch.  Indeed, removing MAP_HUGETLB allows the program to succeed.  I&#39;ll try increasing the resource limit, but may have to have to sudo it, do the mmap() and then chusr to my real ID.<br>

</div><div class="gmail_extra"><br></div><div class="gmail_extra">The fd of -1 that some folks thought might be a problem is suggested in the man page for use with MAP_ANONYMOUS.  That flag means I&#39;m not using a backing file, after all.<br clear="all">

</div><div class=""><div class="gmail_extra"><br>-- <br><div dir="ltr">Kevin O&#39;Gorman<br><br>programmer, n. an organism that transmutes caffeine into software.<br><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"></span></span><table cellpadding="0" cellspacing="0" border="0" width="448">

<tbody><tr><td width="25"><img src="cid:XVHDKDFDBURW.IMAGE_60.gif" height="21" width="25"></td>
<td width="423"><span style="FONT-FAMILY:Verdana,Geneva,sans-serif;COLOR:rgb(0,153,0);MARGIN-LEFT:5px;FONT-SIZE:10px">Please consider the environment before printing this email.</span></td></tr></tbody></table><br></div>


</div></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr">Kevin O&#39;Gorman<br><br>programmer, n. an organism that transmutes caffeine into software.<br><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"></span></span><table border="0" cellpadding="0" cellspacing="0" width="448">
<tbody><tr><td width="25"><img src="cid:XVHDKDFDBURW.IMAGE_60.gif" height="21" width="25"></td>
<td width="423"><span style="FONT-FAMILY:Verdana,Geneva,sans-serif;COLOR:rgb(0,153,0);MARGIN-LEFT:5px;FONT-SIZE:10px">Please consider the environment before printing this email.</span></td></tr></tbody></table><br></div>

</div>