MemoryMap Class Reference

There are a pair of related data structures in the operating system, and also a few simple algorithms that explain why your processes are waiting forever. More...

#include <MemoryMap.h>

Inheritance diagram for MemoryMap:
Inheritance graph
[legend]

List of all members.

Public Member Functions

void debug_print ()
void constructor_clear ()
void destructor_clear ()
virtual bool open (const char *file, int flags=O_RDONLY)
 open a previously created mapped vector
virtual bool create (const char *file, size_t size)
 create the memory mapped file on disk
virtual bool create (size_t size)
 store in allocated memory (malloc), not mmap:
bool close ()
void test ()
size_t length ()
char operator[] (unsigned int index)
int prefetch ()
void useMemoryMap (bool flag=true)

Public Attributes

void * data

Detailed Description

There are a pair of related data structures in the operating system, and also a few simple algorithms that explain why your processes are waiting forever.

The symptom you have is that they are getting little or no CPU time, as shown in the command 'top'. The machine will appear to have available CPU time (look at the Cpu(s): parameter - if less than 100%, you have available CPU). The real key, however, is to look at the 'top' column with the label 'S' - that is the status of the process, and crucial to understanding what is going on.

In your instance, the 'S' column for your karma jobs is 'D', which means it is waiting for data. This is because the process is doing something that is waiting for the filesystem to return data to it. Usually, this is because of a C call like read() or write(), but it also happens in large processes where memory was copied to disk and re-used for other purposes (this is called paging).

So, a bit of background on the operating system... there is a CPU secheduler that takes a list of waiting processes, and picks one to run - if the job is waiting for the disk, there is no point in picking it to run, since it is blocked, waiting for the disk to return data. The scheduler marks the process with 'D' and moves on to the next process to schedule.

In terms of data structures that we care about for this example, there are two that we care about. First is a linear list of disk buffers that are stored in RAM and controlled by the operating system. This is usually called the disk buffer pool. Usually, when a program asks for data from the disk, this list can be scanned quickly to see if the data is already in RAM - if so, no disk operation needs to take place.

Now in the case of the normal Unix read() and write() calls, when the operating system is done finding the page, it copies the data into a buffer to be used by the process that requested it (in the case of a read() - a write() is the opposite). This copy operation is slow and inefficient, but gets the job done.

So overall, you gain some efficiency in a large memory system by having this disk buffer pool data structure, since you aren't re-reading the disk over and over to get the same data that you already have in RAM. However, it is less efficient than it might be because of the extra buffer copying.

Now we come to memory mapped files, and karma. The underlying system call of interest to us is mmap(), and is in MemoryMap.cpp. What it does and how it works are important to understanding the benefits of it, and frankly, most people don't care about it because it is seemingly complex.

Two things are important to know: firstly, there is a data structure in the CPU called the page table, which is mostly contained in the CPU hardware itself. All memory accesses for normal user processes like karma go through this hardware page table. Secondly, it is very fast for the operating system to put together a page table that 'connects' a bunch of memory locations in your user programs address space to the disk buffer pool pages.

The combination of those two facts mean that you can implement a 'zero copy' approach to reading data, which means that the data that is in the disk buffer pool is directly readable by the program without the operating system ever having to actually copy the data, like it does for read() or write().

So the benefit of mmap() is that when the underlying disk pages are already in the disk buffer pool, a hardware data structure gets built, then the program returns, and the data is available at full processor speed with no intervening copy of the data, or waiting for disk or anything else. It is as near to instantaneous as you can possibly get. This works whether it is 100 bytes or 100 gigabytes.

So, the last part of the puzzle is why your program winds up in 'D' (data wait), and what to do about it.

The disk buffer pool is a linear list of blocks ordered by the time and date of access. A process runs every once in awhile to take the oldest of those pages, and free them, during which it also has to update the hardware page tables of any processes referencing them.

So on wonderland, most file access (wget, copy, md5sum, anything else) is constantly putting new fresh pages at the front of the list, and karma index files, having been opened awhile ago, are prime candidates for being paged out. The reason they get paged out as far as I know is that in any given second of execution, nowhere near the entire index is getting accessed... so at some point, at least one page gets sent back to disk (well, flushed from RAM). Once that happens, a cascading effect happens, where the longer it waits, the older the other pages get, then the more that get reclaimed, and the slower it gets, until karma is at a standstill, waiting for pages to be brought back into RAM.

Now in an ideal world, karma would rapidly recover, and it can... sometimes. The problem is that your karma job is accessing data all over that index, and it is essentially looking like a pure random I/O to the underlying filesystem. There is about a 10 to 1 performance difference between accessing the disk sequentially as compared to randomly.

So to make karma work better, the first thing I do when starting karma is force it to read all of the disk pages in order. This causes the entire index to be forced into memory in order, so it is forcing sequential reads, which is the best case possible. There are problems, for example if three karma jobs start at once, the disk I/O is no longer as purely sequential as we would like. Also, if the filesystem is busy taking care of other programs, even if karma thinks it is forcing sequential I/O, the net result looks more random. This happens when the system is starting to break down (thrashing) and it will certainly stall, or look very very slow, or crash.

The upshot of all of this is that when a single reference is shared, it is more likely that all the pages will be in the disk buffer pool to begin with, and thereby reduce startup time to nearly zero. It is also the ideal situation in terms of sharing the same reference among say 24 copies of karma on wonderland - the only cost is the hardware page table that gets set up to point to all of the disk buffers.

As I mentioned a paragraph back, the pages can still get swapped out, even with dozens of karma jobs running. A workaround I created is a program in utilities called mapfile - it simply repeatedly accesses the data in sequential order to help ensure that all of the pages are at the head of the disk buffer pool, and therefore less likely to get swapped out.

The benefit of such a program (mapfile) is greater on wonderland, where a lot of processes are competing for memory and disk buffers.

Definition at line 155 of file MemoryMap.h.


Member Function Documentation

bool MemoryMap::create ( size_t  size  )  [virtual]

store in allocated memory (malloc), not mmap:

This is for code that needs to more flexibly the case when an mmap() file _might_ be available, but if it is not, we want to load it as a convenience to the user. GenomeSequence::populateDBSNP does exactly this.

Definition at line 411 of file MemoryMap.cpp.

References create().

00412 {
00413     return create(NULL, size);
00414 }

bool MemoryMap::create ( const char *  file,
size_t  size 
) [virtual]

create the memory mapped file on disk

a file will be created on disk with the header filled in. The caller must now populate elements using (*this).set(index, value).

Definition at line 361 of file MemoryMap.cpp.

References open().

Referenced by create().

00362 {
00363 
00364     if (file==NULL)
00365     {
00366         data = calloc(size, 1);
00367         if (data==NULL) return true;
00368     }
00369     else
00370     {
00371         int mmap_prot_flag = PROT_READ | PROT_WRITE;
00372 
00373         fd = ::open(file, O_RDWR|O_CREAT|O_TRUNC, 0666);
00374         if (fd==-1)
00375         {
00376             fprintf(stderr, "MemoryMap::open: can't create file '%s'\n",(const char *) file);
00377             constructor_clear();
00378             return true;
00379         }
00380 
00381         lseek(fd, (off_t) size - 1, SEEK_SET);
00382         char ch = 0;
00383         if(write(fd, &ch, 1)!=1) {
00384             perror("MemoryMap::create:");
00385             throw std::logic_error("unable to write at end of file");
00386         }
00387 
00388         data = ::mmap(
00389                    NULL,           // start
00390                    size,
00391                    mmap_prot_flag, // protection flags
00392                    MAP_SHARED,     // share/execute/etc flags
00393                    fd,
00394                    offset
00395                );
00396         if (data == MAP_FAILED)
00397         {
00398             ::close(fd);
00399             unlink(file);
00400             perror("MemoryMap::open");
00401             constructor_clear();
00402             return true;
00403         }
00404         mapped_length = size;
00405         total_length = size;
00406     }
00407     return false;
00408 }

bool MemoryMap::open ( const char *  file,
int  flags = O_RDONLY 
) [virtual]

open a previously created mapped vector

useMemoryMapFlag will determine whether it uses mmap() or malloc()/read() to populate the memory

Reimplemented in GenomeSequence, and MemoryMapArray< elementT, indexT, cookieVal, versionVal, accessorFunc, setterFunc, elementCount2BytesFunc, arrayHeaderClass >.

Definition at line 249 of file MemoryMap.cpp.

Referenced by create().

00250 {
00251 
00252     struct stat buf;
00253 
00254     int mmap_prot_flag = PROT_READ;
00255     if (flags != O_RDONLY) mmap_prot_flag = PROT_WRITE;
00256 
00257     fd = ::open(file, flags);
00258     if (fd==-1)
00259     {
00260         fprintf(stderr, "MemoryMap::open: file %s not found\n", (const char *) file);
00261         constructor_clear();
00262         return true;
00263     }
00264     if (fstat(fd, &buf))
00265     {
00266         perror("MemoryMap::open");
00267         constructor_clear();
00268         return true;
00269     }
00270     mapped_length = buf.st_size;
00271     total_length = mapped_length;
00272 
00273     if (useMemoryMapFlag)
00274     {
00275 
00276         int additionalFlags = 0;
00277 
00278         // try this for amusement ... not portable:
00279 //        additionalFlags |= MAP_HUGETLB;
00280 // #define USE_LOCKED_MMAP
00281 #if defined(USE_LOCKED_MMAP)
00282         // MAP_POPULATE only makes sense if we are reading
00283         // the data
00284         if (flags != O_RDONLY)
00285         {
00286             // furthermore, according to Linux mmap page, populate only
00287             // works if the map is private.
00288             additionalFlags |= MAP_POPULATE;
00289             additionalFlags |= MAP_PRIVATE;
00290         }
00291         else
00292         {
00293             additionalFlags |= MAP_SHARED;
00294         }
00295 #else
00296 additionalFlags |= MAP_SHARED;
00297 #endif
00298 
00299         data = ::mmap(
00300                    NULL,           // start
00301                    mapped_length,
00302                    mmap_prot_flag, // protection flags
00303                    additionalFlags,
00304                    fd,
00305                    offset
00306                );
00307         if (data == MAP_FAILED)
00308         {
00309             ::close(fd);
00310             std::cerr << "Error: Attempting to map " << mapped_length << " bytes of file "
00311                       << file << ":" << std::endl;
00312             perror("MemoryMap::open");
00313             constructor_clear();
00314             return true;
00315         }
00316 
00317 #if defined(USE_LOCKED_MMAP)
00318         //
00319         // non-POSIX, so non portable.
00320         // This call is limited by the RLIMIT_MEMLOCK resource.
00321         //
00322         // In bash, "ulimit -l" shows the limit.
00323         //
00324         if (mlock(data, mapped_length))
00325         {
00326             std::cerr << "Warning: Attempting to lock " << mapped_length << " bytes of file " << file << ":" << std::endl;
00327             perror("unable to lock memory");
00328             // not a fatal error, so continue
00329         }
00330 #endif
00331 
00332         // these really don't appear to have any greatly useful effect on
00333         // Linux.  Last time I checked, the effect on Solaris and AIX was
00334         // exactly what was documented and was significant.
00335         //
00336         madvise(data, mapped_length, MADV_WILLNEED);   // warning, this could hose the system
00337 
00338     }
00339     else
00340     {
00341         data = (void *) malloc(mapped_length);
00342         if (data==NULL)
00343         {
00344             ::close(fd);
00345             perror("MemoryMap::open");
00346             constructor_clear();
00347             return true;
00348         }
00349         ssize_t resultSize = read(fd, data, mapped_length);
00350         if (resultSize!=(ssize_t) mapped_length)
00351         {
00352             ::close(fd);
00353             perror("MemoryMap::open");
00354             constructor_clear();
00355             return true;
00356         }
00357     }
00358     return false;
00359 }


The documentation for this class was generated from the following files:
Generated on Mon Feb 11 13:45:22 2013 for libStatGen Software by  doxygen 1.6.3