[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Performance Management Guide


Code-Optimization Techniques

The degradation from inefficient use of memory is much greater than that from inefficient use of the caches, because the difference in speed between memory and disk is much higher than between cache and memory. Code-optimization techniques include the following:

Mapped Files

The use of mapped files is another code-optimization technique. Applications can use the shmat() or mmap() system calls to access files by address, instead of using multiple read and write system calls. Because there is always overhead associated with system calls, the fewer calls used, the better. The shmat() or mmap() calls can enhance performance up to 50 times compared with traditional read() or write() system calls. To use the shmat() subroutine, a file is opened and a file descriptor (fd) returned, just as if read or write system calls are being used. A shmat() call then returns the address of the mapped file. Setting elements equal to subsequent addresses in a file, instead of using multiple read system calls, does read from a file to a matrix.

The mmap() call allows mapping of memory to cross segment boundaries. A user can have more than 10 areas mapped into memory. The mmap() functions provide page-level protection for areas of memory. Individual pages can have their own read or write, or they can have no-access permissions set. The mmap() call allows the mapping of only one page of a file.

The shmat() call also allows mapping of more than one segment, when a file being mapped is greater than a segment.

The following example program reads from a file using read statements:

fd = open("myfile", O_RDONLY);
for (i=0;i<cols;i++) {
        for (j=0;j<rows;j++) {
                read(fd,&n,sizeof(char));
                *p++ = n;
        }
}

Using the shmat() subroutine, the same result is accomplished without read statements:

fd = open("myfile", O_RDONLY);
nptr = (signed char *) shmat(fd,0,SHM_MAP | SHM_RDONLY);
for (i=0;i<cols;i++) {
        for (j=0;j<rows;j++) {
                *p++ = *nptr++;
        }
}

The only drawback to using explicitly mapped files is on the writes. The system write-behind feature, that periodically writes modified pages to a file in an orderly fashion using sequential blocks, does not apply when an application uses the shmat() or mmap() subroutine. Modified pages can collect in memory and will only be written randomly when the Virtual Memory Manager (VMM) needs the space. This situation often results in many small writes to the disk, causing inefficiencies in CPU and disk usage.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]