On Feb 6, 2012 10:14 PM, "Jidong Xiao" <jidong.xiao@xxxxxxxxx> wrote:
>
> On Mon, Feb 6, 2012 at 10:35 PM, Michael Roth <mdroth@xxxxxxxxxxxxxxxxxx> wrote:
> > My guess is you end up with 2 copies of each page on the guest: the copy in
> > the guest's page cache, and the copy in the buffer you allocated. From the
> > perspective of the host this all looks like anonymous memory, so ksm merges
> > the pages.
>
> Yes, the result definitely shows that there two copies. But I don't
> understand why there would be two copies. So whenever you allocate
> memory in a guest OS, you will always create two copies of the same
> memory?
Well, not just guests, hosts as well. Most operating systems will, by default, cache the data read from disks in memory to speed up subsequent access. In your case you're also creating a copy by allocating a second buffer and storing the data there as well.
Ksm only merges anonymous pages, not disk/page cache, but since your guest's pagecache looks like anonymous memory to the host, ksm is able to merge the dupes.
>
> An interesting thing is, if I replace the posix_memalign() function
> with the malloc() function (See the original program, the commented
> line.) there would be only one copy, i.e., no merging happens,
> however, since I need to have some page-aligned memory, that's why I
> use posix_memalign().
Yup, ksm can only detect duplicate pages, so if your buffer isn't page aligned it's unable to merge with the copy in the guest's page cache
>
> Regards
> Jidong
>