Received: by 2002:a05:7412:bc1a:b0:d7:7d3a:4fe2 with SMTP id ki26csp127443rdb; Fri, 18 Aug 2023 17:12:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVahodQwBOzS77jVbos0YBrXLNWS0oa7jc//pumGwhxQcNlyZd6AH/Mros0NojG7JsC/41 X-Received: by 2002:a17:906:cc48:b0:993:d8a2:385 with SMTP id mm8-20020a170906cc4800b00993d8a20385mr395104ejb.22.1692403978419; Fri, 18 Aug 2023 17:12:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692403978; cv=none; d=google.com; s=arc-20160816; b=DK6af5aMNM9aFxHRra8aPxjT4TTp1FAMZziaOLfqPCEds4E+2llKgYKfuqEBTmayx1 2cPsxY3DfQvJkWWt8qh/IAe83IiqhlaCfHJC9AYG65S+b/xIcKMF3hqu6g0mhSpEDNOI OzZc5Mb4WKkjK+ZTHu9Zr4+vxJYO6jX5LmxW4M7mDsePYyPGqUt8O4nxCwEgK3yEvZmd WTUpNnvP0ewAr2UGfaIOLb27kMk127BJbkiMX8EKqDwaxdmTHJ64YZTtCCtvbdjRUnSF +MyfL/BBoTlykY2+La5jxEIL4+RJBEEOmKcqysP+cWyIguPibNyZ2f9eoIWi69yaXpfB kOrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=RY5QaPIx9fUNvWSG4Vwd0rnaopGK7Td0a28StUcDrtM=; fh=b5jBeBO35Om38J5KhOUXuzAaPqiHhCpV7eyplgVgWzs=; b=MdpnjsPnHT8dYcnZM5NUdvAkbcV5pccWdNLZAnes71dxfXaDLoSJkRUHwkvl7Dk3Xf Kz4qR42LQWaCc5bmNyJJM6Pt4Oh/nTgqAfbRlxGkhfshkgW2ekNN/ZO+54FALveBKAOb 6KpSPLXfkI4gvRieh+1ZRgsBPAGT3HzUZNp8Ipef3KagmIDtZ23Nxz4dmd0ilwb+dlq9 FIOldPG0h1s2cYcMY1b7d3IQetScxsKbh/JKdJG0at3H8jTys39Ebau6Y9y/U2zF5eo3 tmWDMrUp1bMi0ne5Eu8W1gwR67Gq3CtbymemqG4JhW3ppmIhlVFsl05xXKOOvjRl4QcN 0XJg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l4-20020a17090615c400b00987d26a0998si2094149ejd.455.2023.08.18.17.12.27; Fri, 18 Aug 2023 17:12:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349296AbjHQQZE (ORCPT + 99 others); Thu, 17 Aug 2023 12:25:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352228AbjHQQYg (ORCPT ); Thu, 17 Aug 2023 12:24:36 -0400 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0D563A9A for ; Thu, 17 Aug 2023 09:23:53 -0700 (PDT) Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 375BBA56E3A4; Thu, 17 Aug 2023 09:23:09 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, linux-fsdevel@vger.kernel.org, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3] proc/ksm: add ksm stats to /proc/pid/smaps Date: Thu, 17 Aug 2023 09:23:01 -0700 Message-Id: <20230817162301.3472457-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,RDNS_DYNAMIC, SPF_HELO_PASS,SPF_NEUTRAL,TVD_RCVD_IP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With madvise and prctl KSM can be enabled for different VMA's. Once it is enabled we can query how effective KSM is overall. However we cannot easily query if an individual VMA benefits from KSM. This commit adds a KSM section to the /prod//smaps file. It reports how many of the pages are KSM pages. The returned value for KSM is independent of the use of the shared zeropage. Here is a typical output: 7f420a000000-7f421a000000 rw-p 00000000 00:00 0 Size: 262144 kB KernelPageSize: 4 kB MMUPageSize: 4 kB Rss: 51212 kB Pss: 8276 kB Shared_Clean: 172 kB Shared_Dirty: 42996 kB Private_Clean: 196 kB Private_Dirty: 7848 kB Referenced: 15388 kB Anonymous: 51212 kB KSM: 41376 kB LazyFree: 0 kB AnonHugePages: 0 kB ShmemPmdMapped: 0 kB FilePmdMapped: 0 kB Shared_Hugetlb: 0 kB Private_Hugetlb: 0 kB Swap: 202016 kB SwapPss: 3882 kB Locked: 0 kB THPeligible: 0 ProtectionKey: 0 ksm_state: 0 ksm_skip_base: 0 ksm_skip_count: 0 VmFlags: rd wr mr mw me nr mg anon This information also helps with the following workflow: - First enable KSM for all the VMA's of a process with prctl. - Then analyze with the above smaps report which VMA's benefit the most - Change the application (if possible) to add the corresponding madvise calls for the VMA's that benefit the most Signed-off-by: Stefan Roesch --- Documentation/filesystems/proc.rst | 4 ++++ fs/proc/task_mmu.c | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesyste= ms/proc.rst index 7897a7dafcbc..d5bdfd59f5b0 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -461,6 +461,7 @@ Memory Area, or VMA) there is a series of lines such = as the following:: Private_Dirty: 0 kB Referenced: 892 kB Anonymous: 0 kB + KSM: 0 kB LazyFree: 0 kB AnonHugePages: 0 kB ShmemPmdMapped: 0 kB @@ -501,6 +502,9 @@ accessed. a mapping associated with a file may contain anonymous pages: when MAP_P= RIVATE and a page is modified, the file page is replaced by a private anonymous= copy. =20 +"KSM" shows the amount of anonymous memory that has been de-duplicated. = The +value is independent of the use of shared zeropage. + "LazyFree" shows the amount of memory which is marked by madvise(MADV_FR= EE). The memory isn't freed immediately with madvise(). It's freed in memory pressure if the memory is clean. Please note that the printed value migh= t diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 51315133cdc2..f591c750ffda 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -396,6 +396,7 @@ struct mem_size_stats { unsigned long swap; unsigned long shared_hugetlb; unsigned long private_hugetlb; + unsigned long ksm; u64 pss; u64 pss_anon; u64 pss_file; @@ -452,6 +453,9 @@ static void smaps_account(struct mem_size_stats *mss,= struct page *page, mss->lazyfree +=3D size; } =20 + if (PageKsm(page)) + mss->ksm +=3D size; + mss->resident +=3D size; /* Accumulate the size in pages that have been accessed. */ if (young || page_is_young(page) || PageReferenced(page)) @@ -822,6 +826,7 @@ static void __show_smap(struct seq_file *m, const str= uct mem_size_stats *mss, SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); + SEQ_PUT_DEC(" kB\nKSM: ", mss->ksm); SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp); base-commit: f4a280e5bb4a764a75d3215b61bc0f02b4c26417 --=20 2.39.3