From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from psmtp.com (na3sys010amx201.postini.com [74.125.245.201]) by kanga.kvack.org (Postfix) with SMTP id BB8B86B13F2 for ; Tue, 7 Feb 2012 00:46:43 -0500 (EST) Received: by lbbgg6 with SMTP id gg6so1902838lbb.14 for ; Mon, 06 Feb 2012 21:46:41 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 6 Feb 2012 23:46:41 -0600 Message-ID: Subject: Re: Strange finding about kernel samepage merging From: fluxion Content-Type: multipart/alternative; boundary=e0cb4efe2888adb99304b8594dc0 Sender: owner-linux-mm@kvack.org List-ID: To: Jidong Xiao Cc: virtualization@lists.linux-foundation.org, linux-mm@kvack.org --e0cb4efe2888adb99304b8594dc0 Content-Type: text/plain; charset=ISO-8859-1 On Feb 6, 2012 10:14 PM, "Jidong Xiao" wrote: > > On Mon, Feb 6, 2012 at 10:35 PM, Michael Roth 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 > --e0cb4efe2888adb99304b8594dc0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On Feb 6, 2012 10:14 PM, "Jidong Xiao" <jidong.xiao@gmail.com> wrote:
>
> On Mon, Feb 6, 2012 at 10:35 PM, Michael Roth <mdroth@linux.vnet.ibm.com> wrote:
> > My guess is you end up with 2 copies of each page on the guest: t= he copy in
> > the guest's page cache, and the copy in the buffer you alloca= ted. 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 de= fault, cache the data read from disks in memory to speed up subsequent acce= ss. In your case you're also creating a copy by allocating a second buf= fer and storing the data there as well.

Ksm only merges anonymous pages, not disk/page cache, but since your gue= st'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 pa= ge aligned it's unable to merge with the copy in the guest's page c= ache

>
> Regards
> Jidong
>

--e0cb4efe2888adb99304b8594dc0-- -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: email@kvack.org