From patchwork Wed Dec 14 22:51:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuanchu Xie X-Patchwork-Id: 33395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp14242wrn; Wed, 14 Dec 2022 15:06:19 -0800 (PST) X-Google-Smtp-Source: AA0mqf4HDbYqn3w5CkoZQGe9giKlr+s3x5UUDjrsGsRFpltEs+bsU78cdysa+Ru6EyOiaXSyb003 X-Received: by 2002:a05:6402:444b:b0:461:68e4:15cc with SMTP id o11-20020a056402444b00b0046168e415ccmr21300881edb.9.1671059179083; Wed, 14 Dec 2022 15:06:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671059179; cv=none; d=google.com; s=arc-20160816; b=Og3Ape5jHF0a98X18+SCS6hlAODrnbh8KKCQZ6w+kIXk0Ugg8PRUHCgWr7mmSQRRJn YPSawRlWZ5xLHx+EAa0koAx0TbZWXJjJ7nKLECDXaNgZFciLLC9zMzE8ltX8eooHVzWE /kVRDk4IRmI6pJf5yXX9ij0bbXP9psZKIDMxmWVrrAvyUtT5mhjnUOMXIFgO8SDpaVbD LqDx8ZbmVQD26lPRU3vnn79ilM4QZ7vT0M0+yHnTL04HLpSS7J1Rd+J/n87+Asp1dtd9 Iu5NqwYCnEMKjAW3eeIu8x7kUdELCk0hGGiMmAMZXKP6Q+0meXqTm4hF2AENpnL47BJK 4F5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=pP1HscS3NBizlWxdjalcxiOCxdTFFXWst9Um78lFYew=; b=cNY+ZOUThwHuHaCIu+yI1Vi5o0USWYLdnbIltdl8TdJexzye4OGWuFvjk+RS2+q7kb PSzcYiHUfU21uSnM5DQw1bkjkoCdL69NtbB4brkGb3rUYPAY7oiHh03B5KxOzjhvcDka D5xnemKPzIjjU+2RDhUmHlFMVGC85Z6A3FFBAQ/BG+FgjifFB+Bsb6zcpr/3AznR3CaQ EmYaPsBB67g9IzhRENCo3tAxCPybytHs7koyubd4BH9fCAU63rGUHDFMQjaMjlqsu/l/ e3KSvSnddWs90g+aTl7rQ9qr71q+FP1SPYKCC0vr1AGyeFFIuSRLAttOQ66ONOFkd+LD /KPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=T1vg6Npx; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u7-20020aa7d887000000b004608b4f28b7si11766814edq.106.2022.12.14.15.05.17; Wed, 14 Dec 2022 15:06:19 -0800 (PST) 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; dkim=pass header.i=@google.com header.s=20210112 header.b=T1vg6Npx; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229655AbiLNWwF (ORCPT + 99 others); Wed, 14 Dec 2022 17:52:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229680AbiLNWvy (ORCPT ); Wed, 14 Dec 2022 17:51:54 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09399396FF for ; Wed, 14 Dec 2022 14:51:52 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id n197-20020a25d6ce000000b00702558fba96so1595255ybg.0 for ; Wed, 14 Dec 2022 14:51:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pP1HscS3NBizlWxdjalcxiOCxdTFFXWst9Um78lFYew=; b=T1vg6NpxLo6/eIwoy3Qed3mwYZrRcOaXUysYifLHaPi+vYhazUnDs6UDkTZlfRIw7V Y5hy6nvWH1kyT8kmze9eEa6nJSZiNVpW3N3UXMgfEqMuN0e25TVhuhO9D7VqOL/r6JiD 44j5nrRtBPJjeCrY/eBcV76Ko9qHQL7Uf12YDUe97/+MP0nkOYsl4soDy9ktZnmEK6x+ JG4eyDx3Gz2L0H7brEud6mnsIZbHYNBVEQ+rFXpJSg7KtZwCH9Hw29QGY6vvNpzSWtha wOUPLTllmlPcjgFW+I/5/U+W1WIJ6SDoiC88ehNVmoHhh1WhKqr8mBzhsap235RbUrOI kVPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pP1HscS3NBizlWxdjalcxiOCxdTFFXWst9Um78lFYew=; b=IdQAkwpWGjMCsGDXWgQCVK7DNfwmE4aFYCZZReCd6KIxCPjg2734u0E0u3XjU9gdMJ hb3mkCzGRIu02arbukxrVxcxXpCPkQl7HW1fI+laV8UtgB1Y/z3HIOJsIHTFDtU3YkxQ fvBgArWT9YZEsvLUr9HjyDLFSR6xEtd8N9OY7o5uyDAKPrEvmF2SpdntTk6sz4n+cS0D sgXcmtSxSRUKOI0r2NcEV4lVIDv+ljrozVlXgw1IdIAcOstW1LYxCkmW4Cd9ibI13qNF i3NLeZ+d15CxWY/m9tGeB/KPtpmNkqGLVbW8Xk8Og3AL6M8wDXcEXbx9y81Kg+OEHdei Sf0A== X-Gm-Message-State: ANoB5plpRAPFeN5AqL2sc54AM6Y2Td+lncIsm/dM6EBzW1MnHzxbgded n7jKAFDxMiTPlrcv0csUtkg2GzIEjklA X-Received: from yuanchu.svl.corp.google.com ([2620:15c:2d4:203:1311:60bc:9e2a:ab1]) (user=yuanchu job=sendgmr) by 2002:a25:dfc3:0:b0:6f3:2748:7469 with SMTP id w186-20020a25dfc3000000b006f327487469mr55315847ybg.564.1671058311228; Wed, 14 Dec 2022 14:51:51 -0800 (PST) Date: Wed, 14 Dec 2022 14:51:23 -0800 In-Reply-To: <20221214225123.2770216-1-yuanchu@google.com> Mime-Version: 1.0 References: <20221214225123.2770216-1-yuanchu@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20221214225123.2770216-3-yuanchu@google.com> Subject: [RFC PATCH 2/2] mm: multi-gen LRU: cgroup working set stats From: Yuanchu Xie To: Johannes Weiner , Michal Hocko , Roman Gushchin , Yu Zhao Cc: Andrew Morton , Shakeel Butt , Muchun Song , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, Yuanchu Xie X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752232549938585580?= X-GMAIL-MSGID: =?utf-8?q?1752232549938585580?= Expose MGLRU generations as working set stats in cgroupfs as memory.page_idle_age, where we group pages into idle age ranges, and present the number of pages per node per pagetype in each range. This aggregates the time information from MGLRU generations hierarchically. Signed-off-by: Yuanchu Xie --- mm/memcontrol.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7d2fb3fc4580..86554e17be58 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1655,6 +1655,130 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) pr_info("%s", buf); } +#ifdef CONFIG_LRU_GEN +static const unsigned long page_idle_age_ranges[] = { + 1, 2, 5, 10, 20, 30, 45, 60, 90, 120, 180, + 240, 360, 480, 720, 960, 1440, 1920, 2880, 3840, -1 +}; + +#define PAGE_IDLE_AGE_NR_RANGES ARRAY_SIZE(page_idle_age_ranges) + +static unsigned int lru_gen_time_to_page_idle_age_range(unsigned long timestamp) +{ + unsigned int i; + unsigned long gen_age = jiffies_to_msecs(jiffies - timestamp) / MSEC_PER_SEC; + + for (i = 0; i < PAGE_IDLE_AGE_NR_RANGES - 1; ++i) + if (gen_age <= page_idle_age_ranges[i]) + return i; + + return PAGE_IDLE_AGE_NR_RANGES - 1; +} + +static void lru_gen_fill_page_idle_age_table(unsigned long *table, + struct lru_gen_struct *lrugen, + int nid) +{ + unsigned long max_seq = READ_ONCE(lrugen->max_seq); + unsigned long min_seq[ANON_AND_FILE] = { + READ_ONCE(lrugen->min_seq[LRU_GEN_ANON]), + READ_ONCE(lrugen->min_seq[LRU_GEN_FILE]), + }; + unsigned long seq; + unsigned int pagetype; + + /* + * what do we want to do here? + * iterate over all the generations, for each anon and file + */ + + for (pagetype = LRU_GEN_ANON; pagetype < ANON_AND_FILE; ++pagetype) { + for (seq = min_seq[pagetype]; seq <= max_seq; ++seq) { + unsigned int zone; + unsigned int gen = lru_gen_from_seq(seq); + unsigned int idle_age = lru_gen_time_to_page_idle_age_range( + READ_ONCE(lrugen->timestamps[gen])); + unsigned long page_count = 0; + + for (zone = 0; zone < MAX_NR_ZONES; ++zone) { + page_count += READ_ONCE( + lrugen->nr_pages[gen][pagetype][zone]); + } + table[pagetype * PAGE_IDLE_AGE_NR_RANGES * + nr_node_ids + + PAGE_IDLE_AGE_NR_RANGES * nid + idle_age] += + page_count; + } + } +} + +static void memory_page_idle_age_print(struct seq_file *m, unsigned long *table) +{ + static const char *type_str[ANON_AND_FILE] = { "anon", "file" }; + unsigned int i, nid, pagetype; + unsigned int lower = 0; + + for (i = 0; i < PAGE_IDLE_AGE_NR_RANGES; ++i) { + unsigned int upper = page_idle_age_ranges[i]; + + for (pagetype = LRU_GEN_ANON; pagetype < ANON_AND_FILE; + ++pagetype) { + if (upper == -1) + seq_printf(m, "%u-inf %s", lower, + type_str[pagetype]); + else + seq_printf(m, "%u-%u %s", lower, upper, + type_str[pagetype]); + for_each_node_state(nid, N_MEMORY) { + unsigned long page_count = table + [pagetype * + PAGE_IDLE_AGE_NR_RANGES * + nr_node_ids + + PAGE_IDLE_AGE_NR_RANGES * nid + + i]; + seq_printf(m, " N%u=%lu", nid, page_count); + } + seq_puts(m, "\n"); + } + + lower = upper; + } +} + +static int memory_page_idle_age_format(struct mem_cgroup *root, + struct seq_file *m) +{ + struct mem_cgroup *memcg; + unsigned long *table; + + /* + * table contains PAGE_IDLE_AGE_NR_RANGES entries + * per node per pagetype + */ + table = kmalloc_array(PAGE_IDLE_AGE_NR_RANGES * nr_node_ids * + ANON_AND_FILE, + sizeof(*table), __GFP_ZERO | GFP_KERNEL); + + if (!table) + return -ENOMEM; + + memcg = mem_cgroup_iter(root, NULL, NULL); + do { + int nid; + + for_each_node_state(nid, N_MEMORY) { + struct lru_gen_struct *lrugen = + &memcg->nodeinfo[nid]->lruvec.lrugen; + + lru_gen_fill_page_idle_age_table(table, lrugen, nid); + } + } while ((memcg = mem_cgroup_iter(root, memcg, NULL))); + + memory_page_idle_age_print(m, table); + return 0; +} +#endif /* CONFIG_LRU_GEN */ + /* * Return the memory (and swap, if configured) limit for a memcg. */ @@ -6571,6 +6695,13 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of, } #ifdef CONFIG_LRU_GEN +static int memory_page_idle_age_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_seq(m); + + return memory_page_idle_age_format(memcg, m); +} + static int memory_periodic_aging_show(struct seq_file *m, void *v) { unsigned int interval = kold_get_interval(); @@ -6724,6 +6855,11 @@ static struct cftype memory_files[] = { .write = memory_reclaim, }, #ifdef CONFIG_LRU_GEN + { + .name = "page_idle_age", + .flags = CFTYPE_NS_DELEGATABLE, + .seq_show = memory_page_idle_age_show, + }, { .name = "periodic_aging", .flags = CFTYPE_ONLY_ON_ROOT,