From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48BB1C3DA6E for ; Wed, 10 Jan 2024 10:23:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CBD016B0081; Wed, 10 Jan 2024 05:23:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C68756B0085; Wed, 10 Jan 2024 05:23:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE2946B0087; Wed, 10 Jan 2024 05:23:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 967856B0081 for ; Wed, 10 Jan 2024 05:23:56 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 61AB01A0A44 for ; Wed, 10 Jan 2024 10:23:56 +0000 (UTC) X-FDA: 81663015672.13.F29BD68 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf23.hostedemail.com (Postfix) with ESMTP id 8D8C3140014 for ; Wed, 10 Jan 2024 10:23:54 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf23.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704882234; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GB+qnhxGWs+B1v/m8J5Z2XZhrvOakTkh0cqCoFEBn/4=; b=UpRELhvRly+4ewA3W88oWFXU0hn1jmLd1XWQnPRhhZWm6zr/nquN5pmlyRnKhVCPG3A3lj 9MIrBBfVkE9Yy/C1LSgHesJJyHTQxKUXSEcSPi8cUxJ+OnuqSGtW9PMS/37yU/yevG90Mq tRzJS4xItnc6E2irgolOXYiYXGRYei8= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf23.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704882234; a=rsa-sha256; cv=none; b=6bSaECDVvjXxFlBkyghngL60U9L/cXa8z1yQ4EWMGndm8k8Fbmc0lfNmAi5gcHLUP8Wjt1 vGuSwH42Dy/oxNN0yoc0lbSzIlDjsRlVYtle/SxcynKx7R4Wgz72JjIgD3h115Ap4PHn7P 7x6/mcREG0AKbDUuilCQjESmI0FUjVg= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0E5802F4; Wed, 10 Jan 2024 02:24:39 -0800 (PST) Received: from [10.57.76.12] (unknown [10.57.76.12]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E88913F73F; Wed, 10 Jan 2024 02:23:51 -0800 (PST) Message-ID: <94ebe62b-5f55-4be9-b464-4105b4692496@arm.com> Date: Wed, 10 Jan 2024 10:23:50 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v1] tools/mm: Add thpmaps script to dump THP usage info Content-Language: en-GB To: Barry Song <21cnbao@gmail.com> Cc: John Hubbard , Andrew Morton , Zenghui Yu , Matthew Wilcox , David Hildenbrand , Kefeng Wang , Zi Yan , Alistair Popple , linux-mm@kvack.org References: <20240102153828.1002295-1-ryan.roberts@arm.com> <4e7445a0-acc9-487f-999f-a2b6d03d265e@nvidia.com> <3bd5e4a3-9f67-4483-9a0e-9abb5eb783cd@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 8D8C3140014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: uszjabnuj43ca8qz6naftb1ip69x5z38 X-HE-Tag: 1704882234-903729 X-HE-Meta: U2FsdGVkX1+SJb+/9XmRSS36g+qotzDhRgH8uXnVu6Yry9kxz1EXwpHXGURIOtJDYTQZOagBwAWLD1MUViMx4ROAzhHtFy0nwmdJGNf33CBBVKnQblXCHahHrqWpMYqE5+o4FUi8fLoLZveMY8+7CaUMUBNke8oy9BSlzV9wT87zXHhiebBZVxIRjWYwPUPep9WZ97Eje16EON6LhvzkMOzUj4IeKQhbaNxMwfc7u/mNqQ+Tg1+aaWbWCVt9e3t1G4HC3DqxI2KWi0j0qgcrzWwrtDAuIFsksazMT3oWtCQ70S/8u/U8jK+bYnBzkvuh9u/DVgApO5jatVPK0lUiFHk81zCOuE6OCDpNZ64DXh8SiRNr94r6+CBV7ilIScySudefU0mqHALCIZoAihmHpui075/pHE11qwhM0eIIG1tHykhNYDX394W2/zJihlOP+noyA2I9UGFIkIJbHspScpKw/JM6eOOYKk9LbIuqrzAEAsl0rlCVtgoTfRdxkR9Y/9IHfkJgfdEPHqTR3PIuv0YakjGMc/pQ4PeYtrRstovo0TKCxssyNE1FmF5AViShtkpc/u48omJPsK33cFpCu45+k9xmSF2lFBRnEkSKClWxCjHceUFYOeg6W1Kzla//5m1gWFSRXbE73uWANFMT2O+xyF9hiVBJpvMvESMC0RhJXvB5iFmg2yAm7qpH+OxnG0irun7xgf3MJDvg+ZrhRf8/nKV7ia9zikMJS4cEZeC8jlLT8xbqKi4CRFFnBuMtBVP8rk8ss+i+S4P6Q8EQStea4wchCgMghVfGOTIFi2qRr7PNT8KJ2Y5wT0qghKoIMu+U0NNMRl4tsaFV5Bl1ukAkwb0CM107Dwgl42+UpIsJqGfsxUKru6lccsF2eV1Wq3SIesh1lsBhusXfIaMKJzvMVwNPqMvyr+vHuzxhcb8EEppiVYXi3rLNFNDVrv8PsDGyQFzIhpQ+yQspHTV JfOi3m9d Y8/l6IKDFVDLRDfYsoNfWpFX6ba8yRAjEuJOJgeR+4cl7jodn0Q8d+Z4KY2uNCktDIJ2W1I8ToQ2PtfRAOe6fBzkE2l0Fov13dY1FGc4n9157vQbwvw+eP1/Kovt6rPCclG7Y97bfDFw+RnMK80YDkDeRW7WIN+Rv+tjhRJVBxAm/+UoQCVN6Iq6CbaLrbQbHT/Kfc/+p0L1LqIH6WxJm00v7akax6Csj71mq X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 10/01/2024 09:09, Barry Song wrote: > On Wed, Jan 10, 2024 at 4:58 PM Ryan Roberts wrote: >> >> On 10/01/2024 08:02, Barry Song wrote: >>> On Wed, Jan 10, 2024 at 12:16 PM John Hubbard wrote: >>>> >>>> On 1/9/24 19:51, Barry Song wrote: >>>>> On Wed, Jan 10, 2024 at 11:35 AM John Hubbard wrote: >>>> ... >>>>>> Hi Ryan, >>>>>> >>>>>> One thing that immediately came up during some recent testing of mTHP >>>>>> on arm64: the pid requirement is sometimes a little awkward. I'm running >>>>>> tests on a machine at a time for now, inside various containers and >>>>>> such, and it would be nice if there were an easy way to get some numbers >>>>>> for the mTHPs across the whole machine. >> >> Just to confirm, you're expecting these "global" stats be truely global and not >> per-container? (asking because you exploicitly mentioned being in a container). >> If you want per-container, then you can probably just create the container in a >> cgroup? >> >>>>>> >>>>>> I'm not sure if that changes anything about thpmaps here. Probably >>>>>> this is fine as-is. But I wanted to give some initial reactions from >>>>>> just some quick runs: the global state would be convenient. >> >> Thanks for taking this for a spin! Appreciate the feedback. >> >>>>> >>>>> +1. but this seems to be impossible by scanning pagemap? >>>>> so may we add this statistics information in kernel just like >>>>> /proc/meminfo or a separate /proc/mthp_info? >>>>> >>>> >>>> Yes. From my perspective, it looks like the global stats are more useful >>>> initially, and the more detailed per-pid or per-cgroup stats are the >>>> next level of investigation. So feels odd to start with the more >>>> detailed stats. >>>> >>> >>> probably because this can be done without the modification of the kernel. >> >> Yes indeed, as John said in an earlier thread, my previous attempts to add stats >> directly in the kernel got pushback; DavidH was concerned that we don't really >> know exectly how to account mTHPs yet >> (whole/partial/aligned/unaligned/per-size/etc) so didn't want to end up adding >> the wrong ABI and having to maintain it forever. There has also been some >> pushback regarding adding more values to multi-value files in sysfs, so David >> was suggesting coming up with a whole new scheme at some point (I know >> /proc/meminfo isn't sysfs, but the equivalent files for NUMA nodes and cgroups >> do live in sysfs). >> >> Anyway, this script was my attempt to 1) provide a short term solution to the >> "we need some stats" request and 2) provide a context in which to explore what >> the right stats are - this script can evolve without the ABI problem. >> >>> The detailed per-pid or per-cgroup is still quite useful to my case in which >>> we set mTHP enabled/disabled and allowed sizes according to vma types, >>> eg. libc_malloc, java heaps etc. >>> >>> Different vma types can have different anon_name. So I can use the detailed >>> info to find out if specific VMAs have gotten mTHP properly and how many >>> they have gotten. >>> >>>> However, Ryan did clearly say, above, "In future we may wish to >>>> introduce stats directly into the kernel (e.g. smaps or similar)". And >>>> earlier he ran into some pushback on trying to set up /proc or /sys >>>> values because this is still such an early feature. >>>> >>>> I wonder if we could put the global stats in debugfs for now? That's >>>> specifically supposed to be a "we promise *not* to keep this ABI stable" >>>> location. >> >> Now that I think about it, I wonder if we can add a --global mode to the script >> (or just infer global when neither --pid nor --cgroup are provided). I think I >> should be able to determine all the physical memory ranges from /proc/iomem, >> then grab all the info we need from /proc/kpageflags. We should then be able to >> process it all in much the same way as for --pid/--cgroup and provide the same >> stats, but it will apply globally. What do you think? Having now thought about this for a few mins (in the shower, if anyone wants the complete picture :) ), this won't quite work. This approach doesn't have the virtual mapping information so the best it can do is tell us "how many of each size of THP are allocated?" - it doesn't tell us anything about whether they are fully or partially mapped or what their alignment is (all necessary if we want to know if they are contpte-mapped). So I don't think this approach is going to be particularly useful. And this is also the big problem if we want to gather stats inside the kernel; if we want something equivalant to /proc/meminfo's AnonHugePages/ShmemPmdMapped/FilePmdMapped, we need to consider not just the allocation of the THP but also whether it is mapped. That's easy for PMD-mappings, because there is only one entry to consider - when you set it, you increment the number of PMD-mapped THPs, when you clear it, you decrement. But for PTE-mappings it's harder; you know the size when you are mapping so its easy to increment, but you can do a partial unmap, so you would need to scan the PTEs to figure out if we are unmapping the first page of a previously fully-PTE-mapped THP, which is expensive. We would need a cheap mechanism to determine "is this folio fully and contiguously mapped in at least one process?". So depending on what global stats you actually need, the route to getting them cheaply may not be easy. (My previous attempt to add stats cheated and didn't try to track "fully mapped" vs "partially mapped" - instead it just counted the number of pages belonging to a THP (of any size) that were mapped. If you need the global mapping state, then the short term way to do this would be to provide the root cgroup, then have the script recurse through all child cgroups; That would pick up all the processes and iterate through them: $ thpmaps --cgroup /sys/fs/cgroup --summary ... This won't quite work with the current version because it doesn't recurse through the cgroup children currently, but that would be easy to add. > > for debug purposes, it should be good. imaging there is a health > monitor which needs > to sample the stats of large folios online and periodically, this > might be too expensive. > >> >> If we can possibly avoid sysfs/debugfs I would prefer to keep it all in a script >> for now. >> >>> >>> +1. >>> >>>> >>>> >>>> thanks, >>>> -- >>>> John Hubbard >>>> NVIDIA >>>> >>> > > Thanks > Barry