From patchwork Wed May 8 09:45:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Malat X-Patchwork-Id: 13658313 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 9DA4BC04FFE for ; Wed, 8 May 2024 09:45:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 353A36B00D0; Wed, 8 May 2024 05:45:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 303F76B00D1; Wed, 8 May 2024 05:45:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CCD56B00D2; Wed, 8 May 2024 05:45:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0317E6B00D0 for ; Wed, 8 May 2024 05:45:40 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6F6C8160FF2 for ; Wed, 8 May 2024 09:45:40 +0000 (UTC) X-FDA: 82094746440.12.C62B55F Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by imf22.hostedemail.com (Postfix) with ESMTP id 9132DC000A for ; Wed, 8 May 2024 09:45:38 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=malat-biz.20230601.gappssmtp.com header.s=20230601 header.b=dOko166B; dmarc=none; spf=none (imf22.hostedemail.com: domain of oss@malat.biz has no SPF policy when checking 209.85.218.41) smtp.mailfrom=oss@malat.biz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715161538; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=LQ3sOWx7qxGjQvK9Peow6eEouUa9BiyZbG57An/gdpg=; b=3T4+ANEx3rGtjZxOJh1cvIrcJj7vrnRnP095FhMH1xbyVfRhEdH/JIpCdWlQKkiig9TjeT FNBDLHb/CqBEYT9Dz+XKRa/LaUBqiaFm7sInJKl+lgzf6xG9oujZ+ATsbl7FCzuLQ/ODbP AThbr3YFTaxEtmAoW9sxoJfcaCaFF04= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715161538; a=rsa-sha256; cv=none; b=gSymGxqqmw9aJIFHkypPefD/T7O+83V5/pej6onP1iiA+E4LpLZvKk7IuIdl7tO2xHmSsV GBZprV9nTrn2qLtYPB3Uu2tzLI1Z8LbnC1CFTkT9MjUuCquoen4DgHqNQCWYdbBMQg8mut HdGCMfdKKJgjpzub5TUvfOYZMlBZXr4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=malat-biz.20230601.gappssmtp.com header.s=20230601 header.b=dOko166B; dmarc=none; spf=none (imf22.hostedemail.com: domain of oss@malat.biz has no SPF policy when checking 209.85.218.41) smtp.mailfrom=oss@malat.biz Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a59cc765c29so763261466b.3 for ; Wed, 08 May 2024 02:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=malat-biz.20230601.gappssmtp.com; s=20230601; t=1715161537; x=1715766337; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=LQ3sOWx7qxGjQvK9Peow6eEouUa9BiyZbG57An/gdpg=; b=dOko166ByRv8ZMN8Gb2kaouZPVBx1sk93uLYoM8jJBRgN4onH66UGSDYSGn0x1IjRe gY8uGU+uUfZMO1nQs0W7tEeDoJmx4OMsRXxUW7WDdeMSzeuLI5IcVtwGiINRsqLRKaba MMdIyX1jSs+P9stzkDuDuq6BajPH2A44YTAaHH28ovHtqm5+L2umU+KEs010T0PEBNz0 F8RKmNGMSbfPHb/JIIEe8g+tqq00JBtlTwsNLO3TGE2KlzttSJGhzmrHguLTStUxOFT1 F1XRQbiw/mk5vJOB9ufx//BXiHoIDCbhsvi2nDTXxl/2LRN4Gath0HRzm68mM1zXByJe wn2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715161537; x=1715766337; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LQ3sOWx7qxGjQvK9Peow6eEouUa9BiyZbG57An/gdpg=; b=AHkMzBjfWsci4kqZHqY2H5iZ//iXYsy+KnCPwHpy2BAnr6EORVd0A/CE+KR6kP37JO wQ3PofqtfXRajzsIktEZoPaNSTW+jPd5lNl7mBAtAOhW8rtjjFHbI02JWrD0MHCb7w4f NmmP71+iWOx1Y8iBDY9psIZWl93V1CXQR6J0Pd3Q/gG150zF5Rtrm48IG/c83MpNAYvU swFSZKBKNrzZlw1avVGFZiMjzBg2J86kEhC00GVTKhO1J1KwNjjdAUdBbc9nN24MG5UK uSKpXQ7gzzaRCptrQmYIQ1aLXkohS2e8XaIJECFVZX+NQCBok0gc/iyzzvnOu0dh51iy 3d/Q== X-Gm-Message-State: AOJu0YzsvD8xJrALQqX5aakmW2FQSliDPXN6Db8TPEdD4M3TNqLI/iZr t7/NkCU+4jjIYZKy8AlG62dq3SMMukHY6QAt8heTmzWEW4WD3sQBqT4dtxYxzphoahEjxb0QiL9 M2Q== X-Google-Smtp-Source: AGHT+IEc8gB+AMrV8TO5V/Ufe7ac4tKEuzfr2zGJJ2BdK6CceaEQ2Pk3VsclsNGBaN9TJJMapLmO+w== X-Received: by 2002:a50:bb41:0:b0:572:719f:b44f with SMTP id 4fb4d7f45d1cf-5731d9ce2a9mr1474610a12.9.1715161536527; Wed, 08 May 2024 02:45:36 -0700 (PDT) Received: from ntb.emea.nsn-net.net ([193.86.118.65]) by smtp.googlemail.com with ESMTPSA id q11-20020a056402248b00b005726b83071esm7488240eda.4.2024.05.08.02.45.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 02:45:35 -0700 (PDT) From: Petr Malat To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, Petr Malat Subject: [PATCH] tools/mm: allow filtering and culling by module in page_owner_sort Date: Wed, 8 May 2024 11:45:07 +0200 Message-Id: <20240508094507.685475-1-oss@malat.biz> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: 9132DC000A X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: wptjcrzrwdc6oz84igbtxfd7by17uuh9 X-HE-Tag: 1715161538-730588 X-HE-Meta: U2FsdGVkX18FzTtMjvJ+54gmjkiBMwYbuJpYSOfmWFVm8nPRyb7yqZseeri3hW7glGm9QGXlucFs7v12Y2rYZTvLbcOTV37Csz0poKqWDBWWvihwOnYf6PGnDTdul43jMWWsqBy3rVYFCq2qs6yv3yPh/MmXshwNXXv5h6MyeILT7EvcCjtaqgCpO/4E6vLwbmAYRpyWvL/cYiKOhDiLizne7PB7hssY1T96rTEwdOFsTYW9pje/EmZq/vv5R6sixCPIVonkACbe/yBKard2//janIu45kkmq/8wUtQFC3Z65CfzoKKdWbCC7rXGX3JdoJsNL8jXPPMSHP5H59VDq+0SOOJq9pR6Ik51IY77XOQDhL8hNZRBGp+RDlrsrsabqDWcj5B3I1MWElXD1og+4BtFBXK/2gaX2682MQ803LF0l4hgCSNOcfl6CEeaJUf45gJgqfBH6wSOaLf5t2agoE4BZOjxexC/fZJqxi9DFxks/SpVPJK7jB8PxDBjXMjmLtbLVqsqtLPQPC2DtqI6Wcv+t9/UOaP3VNkMWPyqK8x/t35/m9wC1uwjF9XJFMzlemzdJeZxtmff+xlSGP+7xBYO4qlSVsFqpreLtci6LXBvevwiLWd238BXzxk15QZBwDgxccnNW8rQZsIQhZE29nfc61i4TJsnGnDs+AFXmpmPzgUmJG31YpUYO+3S+0TcoAUfowbIoGtJ/Wi4dxd1Fd8whbYR4D5ZOOE2v1Old7XWzx1pb+82oCYySN7trClEeE4yhcnBjTq/S5a8xpb5q+o1RfmbhHmjn33P6Z6SGwfA7M8j6356rj8F1iqcrEls64OHaDAbvaNOtGI21wJJNTnop3xVpPjXX7UPX+6h8OvYatJOz3vDPmRNaaNsKl6JrWdbCJ/C3F3KMdJkcsGyz3ipbu7mOj4NlAxEBeXORTEo8FWUFBxOoukGGGBVjmizNlBdN1A+x8f+cjk6a5u WOY3xHLb JODa6OooCXUc32jKx1MT/YUJc3XEwMqmzVqkhKRcdV0OmO8mBtiqRZRUCwiI1D/i6REoCSxr6ej+YNIKfgSmvu/yVjHMKoy5Hpl6wHxTC77E579LeL2rgjHm1EJxgVoMV+WZrMpMQCdWTRg4sWXve/DQtI0/mTDsX1WGHJ7oVlBmVckexJ5EIKjX5EhuUzRKXjsBtbnGlC85D20I2rNTMIJ7pSipJDO3jihnZxeeriOgfpPE2r6tOTZs8NpmhNUbRzgQvFIJoRgMcQUwtbf2+hAPbrIlEqHH1naBgRvOJIXtPpv+46odzizGTG9f3CoOBX9fMUAJs9SSAZ4R23MuB978YNzUctmFZRD0vf+IlmlD3YUkttQlXbqRa36JywEC3aqr2VK/+oI6KEhoWC1yUAGfG7MuJNxBTtUjz7BF9CZ1BARZ5TIX8wHznN5pzLx6tKDq3DCeNZ1E8eIVOq4mfZyAnqg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000156, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Extend page_owner_sort filtering and culling features to work with module names as well. The top most module is used. Fix regex error handling, failure labels were one step shifted. Signed-off-by: Petr Malat --- tools/mm/page_owner_sort.c | 96 ++++++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 20 deletions(-) diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c index e1f264444342..a4d4d8528997 100644 --- a/tools/mm/page_owner_sort.c +++ b/tools/mm/page_owner_sort.c @@ -27,11 +27,13 @@ #define true 1 #define false 0 #define TASK_COMM_LEN 16 +#define MODULE_NAME_LEN 64 struct block_list { char *txt; char *comm; // task command name char *stacktrace; + char *module; __u64 ts_nsec; int len; int num; @@ -43,14 +45,16 @@ struct block_list { enum FILTER_BIT { FILTER_PID = 1<<1, FILTER_TGID = 1<<2, - FILTER_COMM = 1<<3 + FILTER_COMM = 1<<3, + FILTER_MODULE = 1<<4, }; enum CULL_BIT { CULL_PID = 1<<1, CULL_TGID = 1<<2, CULL_COMM = 1<<3, CULL_STACKTRACE = 1<<4, - CULL_ALLOCATOR = 1<<5 + CULL_ALLOCATOR = 1<<5, + CULL_MODULE = 1<<6, }; enum ALLOCATOR_BIT { ALLOCATOR_CMA = 1<<1, @@ -60,7 +64,8 @@ enum ALLOCATOR_BIT { }; enum ARG_TYPE { ARG_TXT, ARG_COMM, ARG_STACKTRACE, ARG_ALLOC_TS, ARG_CULL_TIME, - ARG_PAGE_NUM, ARG_PID, ARG_TGID, ARG_UNKNOWN, ARG_ALLOCATOR + ARG_PAGE_NUM, ARG_PID, ARG_TGID, ARG_UNKNOWN, ARG_ALLOCATOR, + ARG_MODULE }; enum SORT_ORDER { SORT_ASC = 1, @@ -80,9 +85,11 @@ struct filter_condition { pid_t *pids; pid_t *tgids; char **comms; + char **modules; int pids_size; int tgids_size; int comms_size; + int modules_size; }; struct sort_condition { int (**cmps)(const void *, const void *); @@ -95,6 +102,7 @@ static regex_t order_pattern; static regex_t pid_pattern; static regex_t tgid_pattern; static regex_t comm_pattern; +static regex_t module_pattern; static regex_t ts_nsec_pattern; static struct block_list *list; static int list_size; @@ -179,6 +187,13 @@ static int compare_comm(const void *p1, const void *p2) return strcmp(l1->comm, l2->comm); } +static int compare_module(const void *p1, const void *p2) +{ + const struct block_list *l1 = p1, *l2 = p2; + + return strcmp(l1->module, l2->module); +} + static int compare_ts(const void *p1, const void *p2) { const struct block_list *l1 = p1, *l2 = p2; @@ -200,6 +215,8 @@ static int compare_cull_condition(const void *p1, const void *p2) return compare_comm(p1, p2); if ((cull & CULL_ALLOCATOR) && compare_allocator(p1, p2)) return compare_allocator(p1, p2); + if ((cull & CULL_MODULE) && compare_module(p1, p2)) + return compare_module(p1, p2); return 0; } @@ -372,9 +389,7 @@ static char *get_comm(char *buf) memset(comm_str, 0, TASK_COMM_LEN); - search_pattern(&comm_pattern, comm_str, buf); - errno = 0; - if (errno != 0) { + if (search_pattern(&comm_pattern, comm_str, buf)) { if (debug_on) fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf); return NULL; @@ -383,6 +398,16 @@ static char *get_comm(char *buf) return comm_str; } +static char *get_module(char *buf) +{ + char tmp[MODULE_NAME_LEN] = { 0 }; + + if (search_pattern(&module_pattern, tmp, buf)) + return strdup("vmlinux"); + + return strdup(tmp); +} + static int get_arg_type(const char *arg) { if (!strcmp(arg, "pid") || !strcmp(arg, "p")) @@ -399,6 +424,8 @@ static int get_arg_type(const char *arg) return ARG_ALLOC_TS; else if (!strcmp(arg, "allocator") || !strcmp(arg, "ator")) return ARG_ALLOCATOR; + else if (!strcmp(arg, "module")) + return ARG_MODULE; else { return ARG_UNKNOWN; } @@ -449,20 +476,30 @@ static bool match_str_list(const char *str, char **list, int list_size) static bool is_need(char *buf) { + bool match; + if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size)) return false; if ((filter & FILTER_TGID) && !match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size)) return false; - char *comm = get_comm(buf); - - if ((filter & FILTER_COMM) && - !match_str_list(comm, fc.comms, fc.comms_size)) { + if (filter & FILTER_COMM) { + char *comm = get_comm(buf); + match = match_str_list(comm, fc.comms, fc.comms_size); free(comm); - return false; + if (!match) + return false; } - free(comm); + + if (filter & FILTER_MODULE) { + char *module = get_module(buf); + match = match_str_list(module, fc.modules, fc.modules_size); + free(module); + if (!match) + return false; + } + return true; } @@ -477,6 +514,7 @@ static bool add_list(char *buf, int len, char *ext_buf) list[list_size].pid = get_pid(buf); list[list_size].tgid = get_tgid(buf); list[list_size].comm = get_comm(buf); + list[list_size].module = get_module(buf); list[list_size].txt = malloc(len+1); if (!list[list_size].txt) { fprintf(stderr, "Out of memory\n"); @@ -522,6 +560,8 @@ static bool parse_cull_args(const char *arg_str) cull |= CULL_STACKTRACE; else if (arg_type == ARG_ALLOCATOR) cull |= CULL_ALLOCATOR; + else if (arg_type == ARG_MODULE) + cull |= CULL_MODULE; else { free_explode(args, size); return false; @@ -649,10 +689,14 @@ static void usage(void) "--name \tSelect by command name. This selects the" " information\n\t\t\tof blocks whose command name appears in" " .\n" + "--module \tSelect by module name. This selects the" + " information\n\t\t\tof blocks whose stacktrace topmost module" + " appears in .\n\t\t\t'vmlinux' is used if there isn't" + " module in the stacktrace\n" "--cull \t\tCull by user-defined rules. is a " "single\n\t\t\targument in the form of a comma-separated list " "with some\n\t\t\tcommon fields predefined (pid, tgid, comm, " - "stacktrace, allocator)\n" + "stacktrace, allocator,\n\t\t\tmodule)\n" "--sort \t\tSpecify sort order as: [+|-]key[,[+|-]key[,...]]\n" ); } @@ -661,7 +705,7 @@ int main(int argc, char **argv) { FILE *fin, *fout; char *buf, *ext_buf; - int i, count, compare_flag; + int i, count, compare_flag, rtn = 1; struct stat st; int opt; struct option longopts[] = { @@ -670,6 +714,7 @@ int main(int argc, char **argv) { "name", required_argument, NULL, 3 }, { "cull", required_argument, NULL, 4 }, { "sort", required_argument, NULL, 5 }, + { "module", required_argument, NULL, 6 }, { 0, 0, 0, 0}, }; @@ -737,6 +782,10 @@ int main(int argc, char **argv) exit(1); } break; + case 6: + filter = filter | FILTER_MODULE; + fc.modules = explode(',', optarg, &fc.modules_size); + break; default: usage(); exit(1); @@ -796,6 +845,8 @@ int main(int argc, char **argv) goto out_tgid; if (!check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts")) goto out_comm; + if (!check_regcomp(&module_pattern, "^ .*\\[(.*)\\]$")) + goto out_module; if (!check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns")) goto out_ts; @@ -858,12 +909,15 @@ int main(int argc, char **argv) fprintf(fout, ", "); print_allocator(fout, list[i].allocator); } + if (cull & CULL_MODULE) + fprintf(fout, ", module: %s", list[i].module); if (cull & CULL_STACKTRACE) fprintf(fout, ":\n%s", list[i].stacktrace); fprintf(fout, "\n"); } } + rtn = 0; out_free: if (ext_buf) free(ext_buf); @@ -871,16 +925,18 @@ int main(int argc, char **argv) free(buf); if (list) free(list); -out_ts: regfree(&ts_nsec_pattern); -out_comm: +out_ts: + regfree(&module_pattern); +out_module: regfree(&comm_pattern); -out_tgid: +out_comm: regfree(&tgid_pattern); -out_pid: +out_tgid: regfree(&pid_pattern); -out_order: +out_pid: regfree(&order_pattern); +out_order: - return 0; + return rtn; }