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 829E3C433F5 for ; Fri, 25 Mar 2022 01:09:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1420C6B0071; Thu, 24 Mar 2022 21:09:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F2828D0001; Thu, 24 Mar 2022 21:09:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFD446B0075; Thu, 24 Mar 2022 21:09:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id E0BAB6B0071 for ; Thu, 24 Mar 2022 21:09:38 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AC3D424F09 for ; Fri, 25 Mar 2022 01:09:38 +0000 (UTC) X-FDA: 79281126036.04.8719402 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf09.hostedemail.com (Postfix) with ESMTP id 0C9BA140012 for ; Fri, 25 Mar 2022 01:09:37 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E8433B81DE2; Fri, 25 Mar 2022 01:09:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 877B6C340EE; Fri, 25 Mar 2022 01:09:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1648170575; bh=B8NIBq0NakOB1oQvrAszd5FdAg2a0WRU4gOU+wg2Q3Q=; h=Date:To:From:In-Reply-To:Subject:From; b=jjaNdUGACAkvI85jajmG4bMky3MBE2GSY/VYkK+F9sM1xO09w9CMQGKEeZDag+OUf 08bgospYk4C9+cO+p2NddNBI1rQzP8u40nbvP2zHTLprKmQRrvgO3EMPWUkJt1yp7i 1VM4E/NIEcFA6JMIt3uudesNk89cyy3abM8obayc= Date: Thu, 24 Mar 2022 18:09:34 -0700 To: weizhenliang@huawei.com,sfr@canb.auug.org.au,seanga2@gmail.com,yejiajian2018@email.szu.edu.cn,akpm@linux-foundation.org,patches@lists.linux.dev,linux-mm@kvack.org,mm-commits@vger.kernel.org,torvalds@linux-foundation.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20220324180758.96b1ac7e17675d6bc474485e@linux-foundation.org> Subject: [patch 020/114] tools/vm/page_owner_sort.c: support for selecting by PID, TGID or task command name Message-Id: <20220325010935.877B6C340EE@smtp.kernel.org> X-Stat-Signature: 5zybm3uegh6pc6feukjzzrjxipbjk9sh Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=jjaNdUGA; spf=pass (imf09.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 0C9BA140012 X-HE-Tag: 1648170577-111223 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: From: Jiajian Ye Subject: tools/vm/page_owner_sort.c: support for selecting by PID, TGID or task command name When viewing page owner information, we may also need to select the blocks by PID, TGID or task command name, which helps to get more accurate page allocation information as needed. Therefore, following adjustments are made: 1. Add three new options, including --pid, --tgid and --name, to support the selection of information blocks by a specific pid, tgid and task command name. In addtion, multiple options are allowed to be used at the same time. ./page_owner_sort [input] [output] --pid ./page_owner_sort [input] [output] --tgid ./page_owner_sort [input] [output] --name Assuming a scenario when a multi-threaded program, ./demo (PID = 5280), is running, and ./demo creates a child process (PID = 5281). $ps PID TTY TIME CMD 5215 pts/0 00:00:00 bash 5280 pts/0 00:00:00 ./demo 5281 pts/0 00:00:00 ./demo 5282 pts/0 00:00:00 ps It would be better to filter out the records with tgid=5280 and the task name "demo" when debugging the parent process, and the specific usage is ./page_owner_sort [input] [output] --tgid 5280 --name demo 2. Add explanations of three new options, including --pid, --tgid and --name, to the document. This work is coauthored by Shenghong Han , Yixuan Cao , Yinan Zhang , Chongxi Zhao , Yuhong Feng . Link: https://lkml.kernel.org/r/1646835223-7584-1-git-send-email-yejiajian2018@email.szu.edu.cn Signed-off-by: Jiajian Ye Cc: Sean Anderson Cc: Stephen Rothwell Cc: Zhenliang Wei Signed-off-by: Andrew Morton --- Documentation/vm/page_owner.rst | 6 + tools/vm/page_owner_sort.c | 118 +++++++++++++++++++++++------- 2 files changed, 98 insertions(+), 26 deletions(-) --- a/Documentation/vm/page_owner.rst~tools-vm-page_owner_sortc-support-for-selecting-by-pid-tgid-or-task-command-name +++ a/Documentation/vm/page_owner.rst @@ -129,3 +129,9 @@ Usage Filter: -f Filter out the information of blocks whose memory has been released. + + Select: + --pid Select by pid. + --tgid Select by tgid. + --name Select by task command name. + --- a/tools/vm/page_owner_sort.c~tools-vm-page_owner_sortc-support-for-selecting-by-pid-tgid-or-task-command-name +++ a/tools/vm/page_owner_sort.c @@ -21,6 +21,12 @@ #include #include #include +#include + +#define bool int +#define true 1 +#define false 0 +#define TASK_COMM_LEN 16 struct block_list { char *txt; @@ -34,7 +40,18 @@ struct block_list { pid_t pid; pid_t tgid; }; - +enum FILTER_BIT { + FILTER_UNRELEASE = 1<<1, + FILTER_PID = 1<<2, + FILTER_TGID = 1<<3, + FILTER_TASK_COMM_NAME = 1<<4 +}; +struct filter_condition { + pid_t tgid; + pid_t pid; + char comm[TASK_COMM_LEN]; +}; +static struct filter_condition fc; static regex_t order_pattern; static regex_t pid_pattern; static regex_t tgid_pattern; @@ -154,7 +171,6 @@ static void check_regcomp(regex_t *patte } # define FIELD_BUFF 25 -# define TASK_COMM_LEN 16 static int get_page_num(char *buf) { @@ -259,11 +275,30 @@ static char *get_comm(char *buf) return comm_str; } +static bool is_need(char *buf) +{ + if ((filter & FILTER_UNRELEASE) != 0 && get_free_ts_nsec(buf) != 0) + return false; + if ((filter & FILTER_PID) != 0 && get_pid(buf) != fc.pid) + return false; + if ((filter & FILTER_TGID) != 0 && get_tgid(buf) != fc.tgid) + return false; + + char *comm = get_comm(buf); + + if ((filter & FILTER_TASK_COMM_NAME) != 0 && + strncmp(comm, fc.comm, TASK_COMM_LEN) != 0) { + free(comm); + return false; + } + return true; +} + static void add_list(char *buf, int len) { if (list_size != 0 && - len == list[list_size-1].len && - memcmp(buf, list[list_size-1].txt, len) == 0) { + len == list[list_size-1].len && + memcmp(buf, list[list_size-1].txt, len) == 0) { list[list_size-1].num++; list[list_size-1].page_num += get_page_num(buf); return; @@ -272,28 +307,27 @@ static void add_list(char *buf, int len) printf("max_size too small??\n"); exit(1); } - - list[list_size].free_ts_nsec = get_free_ts_nsec(buf); - if (filter == 1 && list[list_size].free_ts_nsec != 0) + if (!is_need(buf)) return; + list[list_size].pid = get_pid(buf); + list[list_size].tgid = get_tgid(buf); + list[list_size].comm = get_comm(buf); list[list_size].txt = malloc(len+1); if (!list[list_size].txt) { printf("Out of memory\n"); exit(1); } - + memcpy(list[list_size].txt, buf, len); + list[list_size].txt[len] = 0; list[list_size].len = len; list[list_size].num = 1; list[list_size].page_num = get_page_num(buf); - memcpy(list[list_size].txt, buf, len); - list[list_size].txt[len] = 0; + list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: ""; if (*list[list_size].stacktrace == '\n') list[list_size].stacktrace++; - list[list_size].pid = get_pid(buf); - list[list_size].tgid = get_tgid(buf); - list[list_size].comm = get_comm(buf); list[list_size].ts_nsec = get_ts_nsec(buf); + list[list_size].free_ts_nsec = get_free_ts_nsec(buf); list_size++; if (list_size % 1000 == 0) { printf("loaded %d\r", list_size); @@ -306,16 +340,19 @@ static void add_list(char *buf, int len) static void usage(void) { printf("Usage: ./page_owner_sort [OPTIONS] \n" - "-m Sort by total memory.\n" - "-s Sort by the stack trace.\n" - "-t Sort by times (default).\n" - "-p Sort by pid.\n" - "-P Sort by tgid.\n" - "-n Sort by task command name.\n" - "-a Sort by memory allocate time.\n" - "-r Sort by memory release time.\n" - "-c Cull by comparing stacktrace instead of total block.\n" - "-f Filter out the information of blocks whose memory has been released.\n" + "-m\t\tSort by total memory.\n" + "-s\t\tSort by the stack trace.\n" + "-t\t\tSort by times (default).\n" + "-p\t\tSort by pid.\n" + "-P\t\tSort by tgid.\n" + "-n\t\tSort by task command name.\n" + "-a\t\tSort by memory allocate time.\n" + "-r\t\tSort by memory release time.\n" + "-c\t\tCull by comparing stacktrace instead of total block.\n" + "-f\t\tFilter out the information of blocks whose memory has been released.\n" + "--pid \tSelect by pid. This selects the information of blocks whose process ID number equals to .\n" + "--tgid \tSelect by tgid. This selects the information of blocks whose Thread Group ID number equals to .\n" + "--name \n\t\tSelect by command name. This selects the information of blocks whose command name identical to .\n" ); } @@ -323,12 +360,18 @@ int main(int argc, char **argv) { int (*cmp)(const void *, const void *) = compare_num; FILE *fin, *fout; - char *buf; + char *buf, *endptr; int ret, i, count; struct stat st; int opt; + struct option longopts[] = { + { "pid", required_argument, NULL, 1 }, + { "tgid", required_argument, NULL, 2 }, + { "name", required_argument, NULL, 3 }, + { 0, 0, 0, 0}, + }; - while ((opt = getopt(argc, argv, "acfmnprstP")) != -1) + while ((opt = getopt_long(argc, argv, "acfmnprstP", longopts, NULL)) != -1) switch (opt) { case 'a': cmp = compare_ts; @@ -337,7 +380,7 @@ int main(int argc, char **argv) cull_st = 1; break; case 'f': - filter = 1; + filter = filter | FILTER_UNRELEASE; break; case 'm': cmp = compare_page_num; @@ -360,6 +403,29 @@ int main(int argc, char **argv) case 'n': cmp = compare_comm; break; + case 1: + filter = filter | FILTER_PID; + errno = 0; + fc.pid = strtol(optarg, &endptr, 10); + if (errno != 0 || endptr == optarg || *endptr != '\0') { + printf("wrong/invalid pid in from the command line:%s\n", optarg); + exit(1); + } + break; + case 2: + filter = filter | FILTER_TGID; + errno = 0; + fc.tgid = strtol(optarg, &endptr, 10); + if (errno != 0 || endptr == optarg || *endptr != '\0') { + printf("wrong/invalid tgid in from the command line:%s\n", optarg); + exit(1); + } + break; + case 3: + filter = filter | FILTER_TASK_COMM_NAME; + strncpy(fc.comm, optarg, TASK_COMM_LEN); + fc.comm[TASK_COMM_LEN-1] = '\0'; + break; default: usage(); exit(1); _