Linux Page Sharing


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Linux Page Sharing
# 1  
Old 09-17-2011
Linux Page Sharing

Hi,

I have following doubts regarding page sharing in Linux (please also correct me if any of my assumptions are wrong),

1. In recent kernel KSM does the page sharing for user process' anonymous pages.

2. What about pages where the program text is stored ? are they shared between two unrelated processes (by unrelated i mean they have no parent/child/sibling relationship) ?.

3. Parent and child shares these pages when they are forked and didn't execute a new program.


The thing I really wanted to know is, if there are two processes which have no relationship between them but they have some pages with same content, then is it possible for the kernel to identify them and use a single copy.
( pages can be of text area,user's anon pages)

Please help with your inputs.

Thanks
# 2  
Old 09-17-2011
Quote:
Originally Posted by kumaran_5555
2. What about pages where the program text is stored ? are they shared between two unrelated processes (by unrelated i mean they have no parent/child/sibling relationship) ?
No merging is necessary to share program text -- program text is 100% shared already, because it's loaded with memory mapping.

It's easy to share file-backed memory maps because they're not anonymous. No contents need to be checked, just locations. Map the same location, get the same pages.
# 3  
Old 09-18-2011
From .../kernel/Documentation/vm/ksm.txt

Quote:
KSM only operates on those areas of address space which an application has advised to be likely candidates for merging, by using the madvise(2) system call.
# 4  
Old 09-19-2011
Quote:
Originally Posted by Corona688
No merging is necessary to share program text -- program text is 100% shared already, because it's loaded with memory mapping.

It's easy to share file-backed memory maps because they're not anonymous. No contents need to be checked, just locations. Map the same location, get the same pages.
Thanks for all your input. Can anyone explain me little about how program text sharing happens in Linux while loading the program.

Code:
42416000    1568     572       0 r-x--  libc-2.13.90.so
4259e000       8       8       4 r----  libc-2.13.90.so
425a0000       4       4       4 rw---  libc-2.13.90.so

This is what I found in pmap of bash process, suppose if another bash process is started will these areas will be shared, how they are shared while new bash is created. (is there any info that kernel keeps to know that these files are loaded at these parts)

Please show me some light in this area.
# 5  
Old 09-22-2011
Quote:
Originally Posted by kumaran_5555
This is what I found in pmap of bash process, suppose if another bash process is started will these areas will be shared, how they are shared while new bash is created. (is there any info that kernel keeps to know that these files are loaded at these parts)
It's done with memory mapping.
Code:
$ cat owls.c
int main(void)
{
        int fd=open("filename", O_RDWR, 0660);
        // Map the first page of bytes into 'mem'.
        // getpagesize() is 4096 or 8192 bytes on most systems.
        void *mem=mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
        // Make sure the file is long enough.
        // The empty space in the file will be filled with NULLs.
        ftruncate(fd, getpagesize());
        if(mem == MAP_FAILED)
        {
                perror("Couldn't map");
                close(fd);
                return(1);
        }

        printf("Old string was:  '%s'\n", mem);

        strcpy(mem, "THE OWLS ARE NOT WHAT THEY SEEM\n");
        munmap(mem, getpagesize());
        close(fd);
        return(0);
}

$ rm -f filename
$ gcc owls.c -o owls
$ ./owls
Old string was: ''
$ ./owls
Old string was: 'THE OWLS ARE NOT WHAT THEY SEEM
'
$ cat filename
THE OWLS ARE NOT WHAT THEY SEEM
$

Any dynamically-linked code is loaded in this fashion, though it'd be mapped read-only, not read-write.

I suppose the kernel would just need to track the inode number and partition id of memory mapped from files. If someone tries to map the same inode on the same partition, and the area being mapped intersects, some or all of it may be shared.

The memory savings is deeper than just not reloading the same library 23 times. The kernel uses hardware features of the CPU itself to be notified when a process tries to access mapped pages of memory -- like a segmentation fault, except instead of killing the process, the kernel freezes it. Once the memory's loaded, the kernel lets it continue. This allows the kernel to only load memory pages which you're actually using, rather than blindly loading the entire file.

Memory may eventually be paged back out if it falls into disuse, as well. In this manner, mapped segments can operate on things larger than the entire available memory on your system. Many large things like databases use memory mapping to operate on their files.

Last edited by Corona688; 09-22-2011 at 02:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Experience sharing and questions for NIS migration from Solaris 8 to Linux

I'd like to share some experiences and what I found for NIS migration from Solaris 8 NIS to Linux platform. I'm not an expert for both platforms, it's just when I tested both systems and found something really tricky. That might takes a lot of time for you to find the root cause. So, I think I can... (11 Replies)
Discussion started by: bestard
11 Replies

2. UNIX for Dummies Questions & Answers

What is a page from Linux point of view ?

Versions : RHEL 6.xx /OL 6.xx I am trying to understand what a page is in Linux? The concept should be same in Unix as well, I guess The below doc says "A page is a block of virtual memory. A typical block size on Linux operating system is 4KB " ... (4 Replies)
Discussion started by: kraljic
4 Replies

3. AIX

problem on sharing cdrom drive in AIX with linux

HI i have setuped a nfs between a AIX system and a linux os ,keeping AIX as sever i need to share the CDrom in AIX server with the linux operating system. ie. linux os machine does't have a cdrom . will i be able be share ..i tried a lot but it gives a message which resembles NFS access... (3 Replies)
Discussion started by: raguraja
3 Replies

4. Linux

Find Page size in linux

Hi, How do i find Linux kernel page size using command ? Thanks in advance. (1 Reply)
Discussion started by: forumguest
1 Replies

5. UNIX for Advanced & Expert Users

How to create Linux manual page

Hi, Anyone can tell me how do i create Linux manual page. I copied ls manual page from /usr/share/man/man1/ls.1.gz and unziped ls.1.gz got one file ls.1 If i apply man ls.1 it is displaying man page of ls. Similarly i want to create myExe.1 file , man myExe.1 should display... (2 Replies)
Discussion started by: ashokd001
2 Replies

6. Linux

Sharing Printer Linux + XP (cups/samba)

I keep getting a access denied error when I install from the XP wizard my Linux shared printer. CUPS CONFIGURATION FILE <Location /admin> # # You definitely will want to limit access to the administration functions. # The default configuration requires a local connection from a user who #... (2 Replies)
Discussion started by: Alux
2 Replies

7. IP Networking

Printer Sharing on a Mixed(Windows/Linux) Home Network

Sometimes you get the tiger...but sometimes he get you and this latest home network “project” of mine has gnawed on me pretty badly. Perhaps you can offer some technical help. It will be heartily appreciated. I have a small home network initially comprising two computers running Windows... (1 Reply)
Discussion started by: Annatar
1 Replies

8. IP Networking

sharing of IP address for load sharing avoiding virtual server & redirection machine

I have RedHat 9.0 installed on three of my servers (PIII - 233MHz) and want that they share a common IP address so that any request made reaches each of the servers. Can anyone suggest how should I setup my LAN. I'm new to networking in Linux so please elaborate and would be thankful for a timely... (2 Replies)
Discussion started by: Rakesh Ranjan
2 Replies

9. Linux

File Sharing among NTFS Partition & RH Linux 9 Partitions

Well Guys, will anybody solve my problem? I have installed Win XP and RH Linux 9 (Dual Boot) on an Intel x86 Machine. Everything is going fine except that I cannot share files among the two operating systems. For example, if I download a PDF file from internet and save it in my Win XP partition... (1 Reply)
Discussion started by: Jawwad
1 Replies

10. UNIX for Dummies Questions & Answers

I need help to set up internet sharing in linux.

Ok, I'm kind of in between newbie and experienced somewhere. I'm an advanced computer user but only have a little experience with linux and a lot of problems with it. Currently I'm using Linux-Mandrake 8.0 (I know, I know, but let's not go into the many reasons why it's not even close to the best... (2 Replies)
Discussion started by: Nazo
2 Replies
Login or Register to Ask a Question