<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Slyfox &#187; shm</title>
	<atom:link href="http://slyfox.za.net/tag/shm/feed/" rel="self" type="application/rss+xml" />
	<link>http://slyfox.za.net</link>
	<description>Concentrated awesome</description>
	<lastBuildDate>Sun, 30 Aug 2009 18:29:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HP-UX, shared memory, and you</title>
		<link>http://slyfox.za.net/2008/12/hp-ux-shared-memory-and-you/</link>
		<comments>http://slyfox.za.net/2008/12/hp-ux-shared-memory-and-you/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 17:21:07 +0000</pubDate>
		<dc:creator>slyfox</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[hp-ux]]></category>
		<category><![CDATA[shm]]></category>

		<guid isPermaLink="false">http://slyfox.za.net/?p=37</guid>
		<description><![CDATA[My current project at work involves doing some investigation into creating a daemon to allocate memory to processes using a pool of shared memory (SHM).
The reason for this is that the implementation of the C runtime (CRT) library&#8217;s memory management routines on HP-UX does not shrink the heap of a process. This effectively means that [...]]]></description>
			<content:encoded><![CDATA[<p>My current project at work involves doing some investigation into creating a <a href="http://en.wikipedia.org/wiki/Daemon_(computer_software)">daemon</a> to allocate memory to processes using a pool of <a href="http://en.wikipedia.org/wiki/Shared_memory">shared memory</a> (SHM).</p>
<p>The reason for this is that the implementation of the C runtime (CRT) library&#8217;s memory management routines on <a href="http://en.wikipedia.org/wiki/HP-UX">HP-UX</a> does not shrink the heap of a process. This effectively means that if you were to allocate a 512MB block of memory and then release that memory again, the operating system would still report your process as using 512MB of memory. Because of this, long-lived processes (such as services) that may use large amounts of memory to do processing at some point eventually dominate the memory usage of the system, even though they are actually using only a small part of this expanded heap on average. More specifically, the OS is basically reporting that there are 20 instances of a single service each using 1GB of RAM! Shared memory, on the other hand, obviously uses memory on the system, but since SHM isn&#8217;t tied to any specific process, it won&#8217;t cause the participating processes to display a huge amount of memory usage.</p>
<p>The other alternative is of course to change the way the service does processing, but this involves code changes in both the front and back-end parts of the software, so a shared memory allocator would be the path of least resistance in this case.</p>
<p>&#8220;But wait!&#8221;, you say, &#8220;Wouldn&#8217;t that involve changing the back-end code to make use of the new allocator anyway?&#8221; The answer to this is that, yes, it could, but there&#8217;s an easier option. The dynamic loader on UNIX uses a neat little environment variable called <code>LD_PRELOAD</code> which causes the shared libraries specified in it to be loaded before any other shared libraries. This allows you to easily override the CRT&#8217;s memory management routines, since yours get loaded first. This means that the amount of code that needs to be changed go from every memory allocation call to, effectively, none at all.</p>
<p>So we can have the manager allocate a big block of shared memory, and have the services communicate their requests for memory to the manager using <a href="http://en.wikipedia.org/wiki/Unix_domain_socket"></a>UNIX domain sockets</a>. The manager then attempts to service the request by reserving a block of shared memory for that process and passing the address back to the requesting process. The service can then map the shared memory segment into it&#8217;s own address space, use the memory for whatever it needs it for, and then tells the manager to release the memory again. Easy off bang!</p>
<p>Not quite, though.</p>
<p>HP-UX has support for both 32-bit and 64-bit operation. In 32-bit mode, the maximum size of a shared memory segment is 1GB, while in 64-bit mode, a shared memory segment may be up to 4TB in size. The systems we&#8217;re working on run a 64-bit kernel, so the maximum size of a segment isn&#8217;t the problem. The problem is that the processes themselves are 32-bit executables, and to understand why, I&#8217;ll need to talk about how HP-UX defines the memory space for a 32-bit executable.</p>
<p>32-bit mode implies a maximum addressable range of 2<sup>32</sup> bits, or 4GB of memory. In HP-UX, this 4GB <a href="http://en.wikipedia.org/wiki/Virtual_address_space">virtual address space</a> is divided into four quadrants of 1GB each. The first two quadrants are used for the process&#8217; <a href="http://en.wikipedia.org/wiki/Code_segment">text</a> and <a href="http://en.wikipedia.org/wiki/Data_segment">data</a> segments. The third and fourth quadrants are used as a global shared memory space, with the final 250MB of the fourth quadrant being reserved for system I/O (my guess is that this is used for buffers when doing I/O operations). The global adjective means that any shared memory in the system will be (potentially) visible in any other process. Since shared memory does utilise access control when a process requests to map a segment into its address space, there aren&#8217;t any security concerns regarding that, though. What this does mean, though, is that the total amount of shared memory available in 32-bit processes is limited to 1.75GB. And by &#8220;32-bit processes&#8221; I mean <em>all</em> 32-bit processes currently running in the system.</p>
<p>Luckily, HP-UX has a feature known as <strong>memory windows</strong>. To use memory windows, the process to be executed is run using a wrapper called <code>setmemwindow</code>, which will execute the given command in a given memory window. The effect of this is that instead of the process having the normal 1GB global Quadrant 3, it receives a separate 1GB Quadrant 3 which is then shared by all processes belonging to the same memory window.</p>
<p>The idea I had is to use this feature by partitioning the services that require the shared memory pool into groups, and having them and their respective shared memory manager all belong to the same memory window. This frees up the global Q3 for other processes running on the system that might also use SHM, and allows multiple shared memory managers to execute in parallel with their own set of managed services.</p>
<p>The current status of my investigation, barring finding out about the above technical details, is that I&#8217;ve completed a basic manager process capable of reserving and releasing blocks of the shared memory segment to requesting processes, as well as a small test program that communicates requests to the manager. The next step is to write two programs that will perform random memory allocations, one using the SHM manager and another using the standard CRT, to get an idea of what the performance impact of the additional management layer will be.</p>
<p>Hopefully someone will find this useful, since I had to do quite a bit of Google-trawling to find out about all of this. If anyone wants a few of the references I used, feel free to leave a comment, and I&#8217;ll see if I can set you up.</p>
]]></content:encoded>
			<wfw:commentRss>http://slyfox.za.net/2008/12/hp-ux-shared-memory-and-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
