From patchwork Thu Aug 17 16:23:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 136256 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp2478462vqi; Sun, 20 Aug 2023 06:25:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEfrbbwdxW1KQdtqjrU9+g9YSKQlqJDT6JC14LGqCdR9VvUGEw4sFIAT7sQVw9DhAHuPdVC X-Received: by 2002:a17:902:680e:b0:1bc:28a8:f856 with SMTP id h14-20020a170902680e00b001bc28a8f856mr3609489plk.47.1692537926299; Sun, 20 Aug 2023 06:25:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692537926; cv=none; d=google.com; s=arc-20160816; b=l4Fuh2oDga+Qc/fNTOcCdUjmuLEGTGXmsTzZPAPa8UDytLJqTr4OdFwpk5LTlVe8pP x0mxnI3OKIH0V6ZKShFpALLtFjdPOp/MDEFAv7wJFgtzqnnK/yYv3Wx0FANLFv1VSFdP qtLhxUtoiJfWccxy7pwyoC4nTTxrfAF0QZRgP7D/An3kFOoGUOHWmsvnhRgGTbZGkORE 5ISSMWBpB10FXPrZy9tYZMesXVJqWWIyxT1E3Ld9RGlZdWAGxo2NKqh3E8mK2iI3a7oZ HvSLOkSrn7zOOLlTmkAyp7PaRbIG2+0jO23f/KAgFdnIKk3995hg1vTtj+apIM5DlqlC EWqQ== 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:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d5-20020a170902f14500b001bdad5c05f7si4890883plb.546.2023.08.20.06.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Aug 2023 06:25:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C792430886C; Sat, 19 Aug 2023 11:56:45 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350578AbjHQQZE (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 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774754648463923409 X-GMAIL-MSGID: 1774754648463923409 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(+) base-commit: f4a280e5bb4a764a75d3215b61bc0f02b4c26417 diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/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_PRIVATE and a page is modified, the file page is replaced by a private anonymous copy. +"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_FREE). 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 might 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 += size; } + if (PageKsm(page)) + mss->ksm += size; + mss->resident += 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 struct 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);