Shared library memory footprints on AIX 5L
https://www.ibm.com/developerworks/aix/library/au-slib_memory/
This article examines how shared libraries occupy memory on 32-bit AIX 5L™ (5.3), demonstrating the following commands:
- ps
- svmon
- slibclean
- procldd
- procmap
- genkld
- genld
The article discusses the virtual address space of processes, as well as the kernel shared-library segment, how to examine them, and how to interpret the output of the various diagnostic utilities mentioned above. The article also discusses how to diagnose situations where the kernel shared segment is full and possible approaches to resolving that situation.
In the examples throughout, we happen to use the processes from the software product Business Objects Enterprise Xir2®. This is arbitrary, as the concepts will apply to all processes running on AIX 5L.
Review
Just so we are all in the same mindframe, let's review a little on 32-bit architecture. In doing so, I'll resort to the employ of the most useful 'bc' command-line calculator.
In a 32-bit processor, the registers are capable of holding 2^32 possible values,
1 2 3 4 5 6 |
|
That is a 4 gigabyte range. This means a program running on the system is able to access any function or data address in the range of 0 and 2^32 - 1.
1 2 3 4 5 6 |
|
Now, as you know, any operating system has potentially hundreds of programs running at the same time. Even though each one of them is capable of accessing a 4GB range of memory, it doesn't mean that they each get their own 4GB allotment of physical RAM. That would be impractical. Rather, the OS implements a sophisticated scheme of swapping code and data between a moderate amount of physical RAM and areas of the file system designated as swap (or paging) space. Moreover, even though each process is capable of accessing 4GB of memory space, many don't even use most of it. So the OS only loads or swaps the required amount of code and data for each particular process.
Figure 1. Conceptual diagram of virtual memory
This mechanism is often referred to as virtual memory and virtual address spaces.
When an executable file is run, the Virtual Memory Manager of the OS looks at the code and data that comprise the file, and decides what parts it will load into RAM, or load into swap, or reference from the file system. At the same time, it establishes some structure to map the physical locations to virtual locations in the 4GB range. This 4GB range represents the process' maximum theoretical extent and (together sometimes with the VMM's structures that represent it), is known as the virtual address space of the process.
On AIX, the 4GB virtual address space is divided into sixteen 256-megabyte segments. The segments have predetermined functions, some of which are described below:
- Segment 0 is for kernel-related data.
- Segment 1 is for code.
- Segment 2 is for stack and dynamic memory allocation.
- Segment 3 is for memory for mapped files, mmap'd memory.
- Segment d is for shared library code.
- Segment f is for shared library data.
On HP-UX® by comparison, the address space is divided into four quadrants. Quadrants three and four are available for shared library mappings if they are designated using the chatr command with the +q3p enable and +q4p enableoptions.
Where shared libraries are loaded
Shared libraries are, naturally, intended to be shared. More specifically, the read-only sections of the binary image, namely the code (also known as "text") and read-only data (const data, and data that can be copy-on-write) may be loaded once into physical memory, and mapped multiple times into any process that requires it.
To demonstrate this, take a running AIX machine and see which shared libraries are presently loaded:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
As an interesting observation, we can see on this machine right away Clearcase and Java™ are running. Let's take any one of these common libraries, say,libpthreads.a
. Browse the library and see which functions it implements:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Hmm, that was cool. Now let's see which currently running processes have it loaded currently on the system:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Cool! Now let's get the same thing, but eliminate the redundancies:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
There, now we have a nice, discrete list of binaries that are currently executing and all load libpthreads.a
. Note that there are many more processes on this system than this at this time:
1 2 |
|
Now, let's see where each process happens to load libpthreads.a
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Notice that each process loads it at the same address each time. Don't be confused by the constituent listings for the .o's in the library. On AIX, you can share archive libraries (.a files, customarily) as well as dynamic shared libraries (.so files, customarily). The purpose of this is to be able to bind symbols at link time, just like traditional archive linking, yet not require the constituent object (.o file in the archive) be copied into the final binary image. No dynamic (or runtime) symbol resolution is performed, however, as is the case with dynamic shared libraries (.so/.sl files).
Also note libpthreads.a
code sections, those marked read/exec, are loaded into segment 0xd. That segment, as mentioned above, is designated on AIX as the segment for shared library code. That is to say, the kernel loads the shareable segments of this shared library into an area that is shared by all processes running on the same kernel.
You might notice that the data sections are also loaded to the same segment: the shared library segment 0xf. That doesn't mean, however, that each process is also sharing the data section of libpthreads.a
. Loosely defined, such an arrangement wouldn't work, as different processes would need to maintain different data values at different times. Segment 0xf is distinct for each process using libpthreads.a
, even though the virtual memory address is the same.
The svmon command can show us the segment IDs in the Virtual Memory Manager (Vsid) for processes. We'll see the shared-library code segments all have the same Vsid, while the shared-library data segments all have distinct Vsids. The Esid, meaning Effective Segment ID, is the segment ID within the scope of the process's address space (just terminology; don't let it confuse you).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Doing the math
Let's see how much is currently in this shared segment 0xd. We'll revert to our bc calculator tool again. So we know we are sane, we'll verify the size of segment 0xd:
1 2 3 4 5 6 7 |
|
That looks good. Like stated above, each segment is 256MB. Ok, now let's see how much is currently being used.
1 2 3 4 5 6 7 8 |
|
That is saying that there is 37MB currently being used. Let's start up XIr2, and compare:
1 2 3 4 5 6 7 8 |
|
Now there is 253MB being used. That is very close to the limit of 256MB. Let's pick a random process, like WIReportServer, and see how many shared libraries made it into shared space, and how many had to be mapped privately. Since we know the shared segment begins at address 0xd000000, we can filter that out of the output from procmap. Remember, only code sections are mapped to segment 0xd, so we'll just look for the lines that are read/exec:
1 2 3 4 5 6 7 8 9 10 |
|
It looks like the above four libraries couldn't be mapped into the shared segment. Consequently, they were mapped to the private segment 0x3, which is used for any general memory allocated by a call to the mmap() routine.
There are a few conditions that force a shared library to be mapped privately on 32-bit AIX:
- It is out of space in the shared segment 0xd (as above).
- The shared library does not have execute permissions for group or other. You can use a permission designation of rwxr-xr-x mto correct this; however, developers would want to use private permissions (eg. rwx------) so they don't have to run slibclean each time they recompile a shared library and deploy it for testing.
- Some documentation says shared libraries are loaded over nfs.
The AIX kernel will even load the same library twice into shared memory, if it comes from a different location:
1 2 3 |
|
When it goes wrong
If we run another instance of XIr2 deployed in a different directory, we see a significant difference in the process footprint:
1 2 3 4 5 6 |
|
The instance for account 'jbrown' was started first, and the instance for account 'sj1xir2a' was started second. If we were to do something obscure and risky like setting at the appropriate place in our bobje/setup/env.sh file,
1 |
|
before starting the second instance, we would see the footprints normalized, (I switch to the process boe_fcprocd, as I couldn't get WIReportServer to start for this LIBPATH test).
1 2 3 4 5 |
|
And we see procmap shows us the files are loaded from ~jbrown as expected:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Clean up
Once applications are shut down, shared libraries may still reside in the shared segment 0xd. In such case, you can use the utility 'slibclean' to unload any shared libraries that are no longer referenced. The utility requires no arguments:
1 |
|
There is also the utility genld, which when passed the -l option, can show you output like procmap, but for all existing process on the system,
1 |
|
Sometimes, after running slibclean, you may still be prohibited from copying a shared library. For example:
1 2 |
|
You may have run slibclean already, and running 'genld -l' doesn't show any process having this library loaded. Yet the system still has this file protected. You can overcome this limitation by first deleting the shared library in the target location, and then copying the new shared library:
1 2 |
|
During shared-library development, if you are making repeated compile, link, execute, and test exercises, you can avoid having to run slibclean in each cycle by making your shared-library executable only by the owner (eg. r_xr__r__). This will cause the process that you use for testing to load and map your shared-library privately. Be sure to make it executable by all, however (e.g. r_xr_xr_x at product release time).
Summary
I hope you've been able to see in more detail how shared libraries occupy memory and the utilities used to examine them. With this you'll be better able to assess the sizing requirements for your applications and analyze the constituents of memory footprints for processes running on AIX systems.
Downloadable resources
Related topics
- Performance Management Guide: Using Shared Memory has some general notes on segments in 32-bit AIX as well as use of EXTSHM, shmat() and mmap() subroutines.
- BIN DIRECTORY FILLING UP AFTER COMPILES OF SHARED LIBRARY a short trouble-shooting tip discusses the slibclean and genkld commands.
- General Programming Concepts: Writing and Debugging Programs Chapter 19 Shared Libraries and Shared Memory provides information about the operating system facilities provided for sharing libraries and memory allocation.
- Solaris Internals: Solaris 10 and OpenSolaris Kernel Architecture, Richard McDougall, Jim Mauro, July2006 Part 4, and Chapter 21 provide great information of virtual memory architecture on a comparative unix implementation. Chapter 14 provides a good demo of analyzing file mappings in active VM segments.
- AIX 5L Porting Guide provides details on the types of problems most likely to be encountered when porting applications from other UNIX-based platforms to the AIX 5L Operating System.