Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp49188pxb; Tue, 2 Mar 2021 18:43:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZMq6YkWvZ6V+rqFUBw1ej1v/m2FsRYze8fsjjhuasiE7uTv9wWdU+pWtYAx6gqdr76Mlv X-Received: by 2002:a17:907:778b:: with SMTP id ky11mr23764307ejc.333.1614739413052; Tue, 02 Mar 2021 18:43:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614739413; cv=none; d=google.com; s=arc-20160816; b=uXYu6yA37QyXgo7BfcUp2XmnPaUc6vLPqN+00wYR7AgAjc2WV5nBqjRyi6/Ieq3L/g 0Rf1HUpM3w8hrzwbt0wvlxqqJCO14tYGz5PPzBSL2VgcXZoK45vGiZECEPFqEKhAJDmE 5gUhFhmEDj8VDaSjG6jgn6CIJo53tyzV4+KEEC4alb8kfHOnuJD0QjkPZBXCH3Asm9Bf epAe9ASVQmCEZhn0lcUjuJj8P0ABGNn8t2j1kscCD4LmLYxrpT1aoDYeUoxEe8vCKcC8 aK21U9J7hWtxNTwKaeVj7pFQqhwLm+aK6ps4JBGuUP+w2Re406Qw8gIXlKO0QrVHTOR6 BhRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:dmarc-filter :sender:dkim-signature; bh=Z6iFUxoiBsHFARzbI4gTg2kb9vnADIPjoZSG+52+ZEA=; b=h8tQCvy7hbwLqJIRP0ii/4Gc6PtC1nZm9+btTMeMSeADF4c8wZGZWkoziqXjF1W0z5 3KZTlnu7e0uXsu3vrVncCfAFCV7Uxkecp/u4NHmFkKOc+0LIX8SMj0urIa1VGu2xAI8h wLii+dZ8UTANjAowoZvNVyFdt3lBBUe5wtwNZuuZV+icJCNX3NJdsUcE3x4M+mO4sHkC Jore9I89BmyedfgdT+32/NQtOabz+9sx+JXrTWDJTmROTZJryK0rXBb5oJb4fpTF+aai aodGVhO94JqWoEtBsJdJGU2soXPR+G5R2S5fmUffeDfYhSLrMN2y5gTKy+8otp+b3z6H u/nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=fwrsron6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g2si8190877ejs.58.2021.03.02.18.43.10; Tue, 02 Mar 2021 18:43:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=fwrsron6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234794AbhCAKve (ORCPT + 99 others); Mon, 1 Mar 2021 05:51:34 -0500 Received: from m42-2.mailgun.net ([69.72.42.2]:64302 "EHLO m42-2.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234788AbhCAKvZ (ORCPT ); Mon, 1 Mar 2021 05:51:25 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1614595859; h=Message-Id: Date: Subject: Cc: To: From: Sender; bh=Z6iFUxoiBsHFARzbI4gTg2kb9vnADIPjoZSG+52+ZEA=; b=fwrsron6iIWKkBS5MCC1VLErty0QbCiOW1G+A0dlkrOUhnZtj23l2HTLUtCfYkVGZfDrvmYg UwDKcxxVVhPsIWsUh8kq/aYsODmaJgJBBiH99BwWybXFQObYJ1OeR4RxUJvThiaIR3h0ihyX GgodZK6g9o2Ozsrtqls1jnz08pY= X-Mailgun-Sending-Ip: 69.72.42.2 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n06.prod.us-east-1.postgun.com with SMTP id 603cc6f016ba7452010130f8 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Mon, 01 Mar 2021 10:50:24 GMT Sender: pintu=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id 1FB1CC43465; Mon, 1 Mar 2021 10:50:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00,SPF_FAIL autolearn=no autolearn_force=no version=3.4.0 Received: from blr-ubuntu-498.qualcomm.com (blr-bdr-fw-01_GlobalNAT_AllZones-Outside.qualcomm.com [103.229.18.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: pintu) by smtp.codeaurora.org (Postfix) with ESMTPSA id DE0E3C433CA; Mon, 1 Mar 2021 10:50:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org DE0E3C433CA Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=pintu@codeaurora.org From: Pintu Kumar To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, jaewon31.kim@samsung.com, yuzhao@google.com, shakeelb@google.com, guro@fb.com, mchehab+huawei@kernel.org, xi.fengfei@h3c.com, pintu@codeaurora.org, lokeshgidra@google.com, hannes@cmpxchg.org, nigupta@nvidia.com, famzheng@amazon.com, andrew.a.klychkov@gmail.com, bigeasy@linutronix.de, ping.ping@gmail.com, vbabka@suse.cz, yzaikin@google.com, keescook@chromium.org, mcgrof@kernel.org, corbet@lwn.net Cc: pintu.ping@gmail.com Subject: [PATCH] mm: introduce clear all vm events counters Date: Mon, 1 Mar 2021 16:19:26 +0530 Message-Id: <1614595766-7640-1-git-send-email-pintu@codeaurora.org> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At times there is a need to regularly monitor vm counters while we reproduce some issue, or it could be as simple as gathering some system statistics when we run some scenario and every time we like to start from beginning. The current steps are: Dump /proc/vmstat Run some scenario Dump /proc/vmstat again Generate some data or graph reboot and repeat again So, each time we wanted to capture fresh data a reboot is required. Thus, in this patch I introduce a way to clear/reset all vm counters to 0 without rebooting the system. With this patch the steps can be: Dump /proc/vmstat Run some scenario Dump /proc/vmstat again Generate some data or graph Do: echo 1 > /proc/sys/vm/clear_all_vm_events Then repeat again Thus, when 1 is written to this file, it resets all the vm counters present under /proc/vmstat to its default value or zero. The next time when you read /proc/vmstat, each node is filled with default or current value. There could be few other use cases where this can be useful. Signed-off-by: Pintu Kumar Signed-off-by: Pintu Agarwal --- Documentation/admin-guide/sysctl/vm.rst | 8 ++++++++ include/linux/vmstat.h | 7 +++++++ kernel/sysctl.c | 9 +++++++++ mm/vmstat.c | 26 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index e35a3f2..54382ab 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -26,6 +26,7 @@ Currently, these files are in /proc/sys/vm: - admin_reserve_kbytes - block_dump +- clear_all_vm_events - compact_memory - compaction_proactiveness - compact_unevictable_allowed @@ -112,6 +113,13 @@ block_dump block_dump enables block I/O debugging when set to a nonzero value. More information on block I/O debugging is in Documentation/admin-guide/laptops/laptop-mode.rst. +clear_all_vm_events +=================== + +When 1 is written to this file, it resets all the vm counters present under +/proc/vmstat to its default value or zeros. +The next time when you read /proc/vmstat, each node is filled with default or +current value. compact_memory ============== diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h index 773135f..8ab9be5 100644 --- a/include/linux/vmstat.h +++ b/include/linux/vmstat.h @@ -11,6 +11,8 @@ #include extern int sysctl_stat_interval; +extern int sysctl_clearvmevents_handler(struct ctl_table *table, int write, + void __user *buffer, size_t *length, loff_t *ppos); #ifdef CONFIG_NUMA #define ENABLE_NUMA_STAT 1 @@ -83,6 +85,8 @@ static inline void count_vm_events(enum vm_event_item item, long delta) extern void all_vm_events(unsigned long *); +extern void clear_all_vm_events(void); + extern void vm_events_fold_cpu(int cpu); #else @@ -103,6 +107,9 @@ static inline void __count_vm_events(enum vm_event_item item, long delta) static inline void all_vm_events(unsigned long *ret) { } +static inline void clear_all_vm_events(void) +{ +} static inline void vm_events_fold_cpu(int cpu) { } diff --git a/kernel/sysctl.c b/kernel/sysctl.c index c9fbdd8..25299a4 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -200,6 +200,8 @@ static int min_extfrag_threshold; static int max_extfrag_threshold = 1000; #endif +static int sysctl_clear_vm_events; + #endif /* CONFIG_SYSCTL */ #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_SYSCTL) @@ -2891,6 +2893,13 @@ static struct ctl_table vm_table[] = { #endif /* CONFIG_COMPACTION */ { + .procname = "clear_all_vm_events", + .data = &sysctl_clear_vm_events, + .maxlen = sizeof(int), + .mode = 0200, + .proc_handler = sysctl_clearvmevents_handler, + }, + { .procname = "min_free_kbytes", .data = &min_free_kbytes, .maxlen = sizeof(min_free_kbytes), diff --git a/mm/vmstat.c b/mm/vmstat.c index f894216..26b8f81 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -136,6 +136,32 @@ void all_vm_events(unsigned long *ret) } EXPORT_SYMBOL_GPL(all_vm_events); +void clear_all_vm_events(void) +{ + int cpu; + + for_each_online_cpu(cpu) { + struct vm_event_state *this = &per_cpu(vm_event_states, cpu); + int sz = NR_VM_EVENT_ITEMS * sizeof(unsigned long); + + memset(this->event, 0, sz); + } +} +EXPORT_SYMBOL_GPL(clear_all_vm_events); + +/* + * This is the node to reset all vm events in /proc/vmstat + * via /proc/sys/vm/clear_all_vm_events + */ +int sysctl_clearvmevents_handler(struct ctl_table *table, int write, + void __user *buffer, size_t *length, loff_t *ppos) +{ + if (write) + clear_all_vm_events(); + + return 0; +} + /* * Fold the foreign cpu events into our own. * -- Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc., is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.