Message ID | 20240212180941.379c419b@gandalf.local.home |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-62520-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp215682dyb; Mon, 12 Feb 2024 15:09:13 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVPntoJEDpjDjHwyz/28DsIKHdVgXOmJwuzRPRD4DtBSHNuR0P/QNcUCdzZkAR5MniO9EGld6p1Obz6HW4htPjDmNxO7w== X-Google-Smtp-Source: AGHT+IEQXLy4SOmr9f/I87/RfDW33FcRAE6FX7l7TP+GAS1rn7OK3QJR0x0xmy+7OeFiTfg5u8gi X-Received: by 2002:a05:6a21:3949:b0:19e:4ab2:c362 with SMTP id ac9-20020a056a21394900b0019e4ab2c362mr1360629pzc.14.1707779352815; Mon, 12 Feb 2024 15:09:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707779352; cv=pass; d=google.com; s=arc-20160816; b=ASEu8m384D25a03E41znydf5dlygKdE8sIfJAfOuBWnyZfsteWIciGrVFHKXN8RixP C6wmiBT3Hb38AIibTg7OMSfcRIZgvqYKm5aUFzBXSoPK2CBlYJbEkd1HmQQwFMXCwXx0 UsljHVk4cw6+YW00DmKEQVNG6MRoQpw60pu/YkcvfxF+62rjx84HhX8/mSiAJPqq3yDE 6iLxyu2NmubnXDMuvCVg0m8HH6lZdyzZXvbY/rVz6rQ921y4jxagANfSfJ3ShnvUmX2S DcpexR0Aung74GQHOzSGSLcQef8xBrTF9b/6Smnn7Jeggd/mhJt96j+MLLiqdvOvR7gN FOzQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:subject:cc:to:from :date; bh=ah0Czp+s0eKq660ZRgL4nqC3xmu8vBz4foHTsa/CE2Q=; fh=GF6ctq3dfjtlVilVHqhs4cszNsOzX2Hltffyo9a4wtY=; b=JF9kQdCox6VXPzwj23oBX9gg+TuiTbhYK1wTL12x1jXCwlPZIq6hT2WS/wOH0dCEux /0keJmHpL0ieJJHsGdQdBHNi7gz0UO1/4Gu143jF2EdylNkeEp4UaSGOIgT7YIkSLeep if4Jq8XI2AO4lNMzA1xVUIPiTu4G1hnmK0UIgKkVgvTy6udM6se15NGzE/3HJmImSXsW fi26F6UyVQdvZr4epwoX/zaHg7r5ZDctIcyCNGDMrM2sTW+Jp7S/XS2uKLJxGtnc0m6I Aj0GAQdX/VjfuFmK9iVq/gNVdiCf0cLsmwf4xjf4Kf/7dwo/TdzchLA1woYB28uFFxIN 615g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-62520-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62520-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCXfZ55c5gQr7EE/zqPBoLdsiHC22eG7rVfJUiY5KTRhiHvZrgH0DK8L0DcXD2GsZf7TA2xUMfYBsiFSqrmzIObltbFqog== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m130-20020a633f88000000b005cf60fc5fc9si888958pga.274.2024.02.12.15.09.12 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:09:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62520-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-62520-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62520-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 5B81F285369 for <ouuuleilei@gmail.com>; Mon, 12 Feb 2024 23:09:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C91174F88E; Mon, 12 Feb 2024 23:09:01 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46FB74EB28; Mon, 12 Feb 2024 23:08:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707779340; cv=none; b=XAFKARQNbqpQyK+pc5uRvh8MeRlsDwNfgEIUUg+tz+ueJK0Jx9t1DOWWHN01TL3gzSu6WVXiOaIMmgxrcIyt/WCcY+xUcBqBszAC4G8Q9pzL1gy3Xgw0KvL+Y5AQwxWPgkb2u/cYdrFnUVVO5F9KL/nwU3drQP3ea9c1/Kd9C3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707779340; c=relaxed/simple; bh=hhDd4Zg33Gyyd3pXps6UXldwu1puK288MZOmI4zYWyY=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=RQLZUpnbZzj7xFZCjhXbW/fL8rcnwhqZRNMO5xwu1h2H7fNKd2avfylGoQ4njVwRYCoNOm9CUOPdqE9HO4ykv3DM13o9DGFADQBZMMVGw4WgD+4z7co/7mv6kQX40BLdWIdzwOnAQtDMeHwyGkCHX/k2W2imV/wkYYpQdomRL5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC65EC433C7; Mon, 12 Feb 2024 23:08:58 +0000 (UTC) Date: Mon, 12 Feb 2024 18:09:41 -0500 From: Steven Rostedt <rostedt@goodmis.org> To: LKML <linux-kernel@vger.kernel.org>, Linux Trace Kernel <linux-trace-kernel@vger.kernel.org> Cc: Masami Hiramatsu <mhiramat@kernel.org>, Mathieu Desnoyers <mathieu.desnoyers@efficios.com>, Mark Rutland <mark.rutland@arm.com>, Tim Chen <tim.c.chen@linux.intel.com>, Vincent Donnefort <vdonnefort@google.com>, Sven Schnelle <svens@linux.ibm.com>, Mete Durlu <meted@linux.ibm.com> Subject: [PATCH] tracing: Have saved_cmdlines arrays all in one allocation Message-ID: <20240212180941.379c419b@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790736442788760521 X-GMAIL-MSGID: 1790736442788760521 |
Series |
tracing: Have saved_cmdlines arrays all in one allocation
|
|
Commit Message
Steven Rostedt
Feb. 12, 2024, 11:09 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org> The saved_cmdlines have three arrays for mapping PIDs to COMMs: - map_pid_to_cmdline[] - map_cmdline_to_pid[] - saved_cmdlines The map_pid_to_cmdline[] is PID_MAX_DEFAULT in size and holds the index into the other arrays. The map_cmdline_to_pid[] is a mapping back to the full pid as it can be larger than PID_MAX_DEFAULT. And the saved_cmdlines[] just holds the COMMs associated to the pids. Currently the map_pid_to_cmdline[] and saved_cmdlines[] are allocated together (in reality the saved_cmdlines is just in the memory of the rounding of the allocation of the structure as it is always allocated in powers of two). The map_cmdline_to_pid[] array is allocated separately. Since the rounding to a power of two is rather large (it allows for 8000 elements in saved_cmdlines), also include the map_cmdline_to_pid[] array. (This drops it to 6000 by default, which is still plenty for most use cases). This saves even more memory as the map_cmdline_to_pid[] array doesn't need to be allocated. Link: https://lore.kernel.org/linux-trace-kernel/20240212174011.068211d9@gandalf.local.home/ Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> --- kernel/trace/trace_sched_switch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
Comments
On Mon, 12 Feb 2024 18:09:41 -0500 Steven Rostedt <rostedt@goodmis.org> wrote: > From: "Steven Rostedt (Google)" <rostedt@goodmis.org> > > The saved_cmdlines have three arrays for mapping PIDs to COMMs: > > - map_pid_to_cmdline[] > - map_cmdline_to_pid[] > - saved_cmdlines > > The map_pid_to_cmdline[] is PID_MAX_DEFAULT in size and holds the index > into the other arrays. The map_cmdline_to_pid[] is a mapping back to the > full pid as it can be larger than PID_MAX_DEFAULT. And the > saved_cmdlines[] just holds the COMMs associated to the pids. > > Currently the map_pid_to_cmdline[] and saved_cmdlines[] are allocated > together (in reality the saved_cmdlines is just in the memory of the > rounding of the allocation of the structure as it is always allocated in > powers of two). The map_cmdline_to_pid[] array is allocated separately. > > Since the rounding to a power of two is rather large (it allows for 8000 > elements in saved_cmdlines), also include the map_cmdline_to_pid[] array. > (This drops it to 6000 by default, which is still plenty for most use > cases). This saves even more memory as the map_cmdline_to_pid[] array > doesn't need to be allocated. > > Link: https://lore.kernel.org/linux-trace-kernel/20240212174011.068211d9@gandalf.local.home/ > Looks good to me. Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Thank you, > Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> > --- > kernel/trace/trace_sched_switch.c | 13 ++++--------- > 1 file changed, 4 insertions(+), 9 deletions(-) > > diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c > index e4fbcc3bede5..210c74dcd016 100644 > --- a/kernel/trace/trace_sched_switch.c > +++ b/kernel/trace/trace_sched_switch.c > @@ -201,7 +201,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > int order; > > /* Figure out how much is needed to hold the given number of cmdlines */ > - orig_size = sizeof(*s) + val * TASK_COMM_LEN; > + orig_size = sizeof(*s) + val * (TASK_COMM_LEN + sizeof(int)); > order = get_order(orig_size); > size = 1 << (order + PAGE_SHIFT); > page = alloc_pages(GFP_KERNEL, order); > @@ -212,16 +212,11 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > memset(s, 0, sizeof(*s)); > > /* Round up to actual allocation */ > - val = (size - sizeof(*s)) / TASK_COMM_LEN; > + val = (size - sizeof(*s)) / (TASK_COMM_LEN + sizeof(int)); > s->cmdline_num = val; > > - s->map_cmdline_to_pid = kmalloc_array(val, > - sizeof(*s->map_cmdline_to_pid), > - GFP_KERNEL); > - if (!s->map_cmdline_to_pid) { > - free_saved_cmdlines_buffer(s); > - return NULL; > - } > + /* Place map_cmdline_to_pid array right after saved_cmdlines */ > + s->map_cmdline_to_pid = (unsigned *)&s->saved_cmdlines[val * TASK_COMM_LEN]; > > s->cmdline_idx = 0; > memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP, > -- > 2.43.0 >
On Mon, 2024-02-12 at 18:09 -0500, Steven Rostedt wrote: > From: "Steven Rostedt (Google)" <rostedt@goodmis.org> > > The saved_cmdlines have three arrays for mapping PIDs to COMMs: > > - map_pid_to_cmdline[] > - map_cmdline_to_pid[] > - saved_cmdlines > > The map_pid_to_cmdline[] is PID_MAX_DEFAULT in size and holds the index > into the other arrays. The map_cmdline_to_pid[] is a mapping back to the > full pid as it can be larger than PID_MAX_DEFAULT. And the > saved_cmdlines[] just holds the COMMs associated to the pids. > > Currently the map_pid_to_cmdline[] and saved_cmdlines[] are allocated > together (in reality the saved_cmdlines is just in the memory of the > rounding of the allocation of the structure as it is always allocated in > powers of two). The map_cmdline_to_pid[] array is allocated separately. > > Since the rounding to a power of two is rather large (it allows for 8000 > elements in saved_cmdlines), also include the map_cmdline_to_pid[] array. > (This drops it to 6000 by default, which is still plenty for most use > cases). This saves even more memory as the map_cmdline_to_pid[] array > doesn't need to be allocated. This patch does make better use of the extra space and make the previous change better. Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com> > > Link: https://lore.kernel.org/linux-trace-kernel/20240212174011.068211d9@gandalf.local.home/ > > Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> > --- > kernel/trace/trace_sched_switch.c | 13 ++++--------- > 1 file changed, 4 insertions(+), 9 deletions(-) > > diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c > index e4fbcc3bede5..210c74dcd016 100644 > --- a/kernel/trace/trace_sched_switch.c > +++ b/kernel/trace/trace_sched_switch.c > @@ -201,7 +201,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > int order; > > /* Figure out how much is needed to hold the given number of cmdlines */ > - orig_size = sizeof(*s) + val * TASK_COMM_LEN; > + orig_size = sizeof(*s) + val * (TASK_COMM_LEN + sizeof(int)); Strictly speaking, *map_cmdline_to_pid is unsigned int so it is more consistent to use sizeof(unsigned) in line above. But I'm nitpicking and I'm fine to leave it as is. > order = get_order(orig_size); > size = 1 << (order + PAGE_SHIFT); > page = alloc_pages(GFP_KERNEL, order); > @@ -212,16 +212,11 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > memset(s, 0, sizeof(*s)); > > /* Round up to actual allocation */ > - val = (size - sizeof(*s)) / TASK_COMM_LEN; > + val = (size - sizeof(*s)) / (TASK_COMM_LEN + sizeof(int)); > s->cmdline_num = val; > > - s->map_cmdline_to_pid = kmalloc_array(val, > - sizeof(*s->map_cmdline_to_pid), > - GFP_KERNEL); > - if (!s->map_cmdline_to_pid) { > - free_saved_cmdlines_buffer(s); > - return NULL; > - } > + /* Place map_cmdline_to_pid array right after saved_cmdlines */ > + s->map_cmdline_to_pid = (unsigned *)&s->saved_cmdlines[val * TASK_COMM_LEN]; > > s->cmdline_idx = 0; > memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
On Mon, 12 Feb 2024 15:39:03 -0800 Tim Chen <tim.c.chen@linux.intel.com> wrote: > > diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c > > index e4fbcc3bede5..210c74dcd016 100644 > > --- a/kernel/trace/trace_sched_switch.c > > +++ b/kernel/trace/trace_sched_switch.c > > @@ -201,7 +201,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > > int order; > > > > /* Figure out how much is needed to hold the given number of cmdlines */ > > - orig_size = sizeof(*s) + val * TASK_COMM_LEN; > > + orig_size = sizeof(*s) + val * (TASK_COMM_LEN + sizeof(int)); > > Strictly speaking, *map_cmdline_to_pid is unsigned int so it is more consistent > to use sizeof(unsigned) in line above. But I'm nitpicking and I'm fine to > leave it as is. I was thinking about making that into a macro as it is used in two places. /* Holds the size of a cmdline and pid element */ #define SAVED_CMDLINE_MAP_ELEMENT_SIZE(s) \ (TASK_COMM_LEN + sizeof((s)->map_cmdline_to_pid[0])) orig_size = sizeof(*s) + val * SAVED_CMDLINE_MAP_ELEMENT_SIZE(s); > > > order = get_order(orig_size); > > size = 1 << (order + PAGE_SHIFT); > > page = alloc_pages(GFP_KERNEL, order); > > @@ -212,16 +212,11 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > > memset(s, 0, sizeof(*s)); > > > > /* Round up to actual allocation */ > > - val = (size - sizeof(*s)) / TASK_COMM_LEN; > > + val = (size - sizeof(*s)) / (TASK_COMM_LEN + sizeof(int)); val = (size - sizeof(*s)) / SAVED_CMDLINE_MAP_ELEMENT_SIZE(s); -- Steve > > s->cmdline_num = val; > > > > - s->map_cmdline_to_pid = kmalloc_array(val, > > - sizeof(*s->map_cmdline_to_pid), > > - GFP_KERNEL); > > - if (!s->map_cmdline_to_pid) { > > - free_saved_cmdlines_buffer(s); > > - return NULL; > > - } > > + /* Place map_cmdline_to_pid array right after saved_cmdlines */ > > + s->map_cmdline_to_pid = (unsigned *)&s->saved_cmdlines[val * TASK_COMM_LEN]; > > > > s->cmdline_idx = 0; > > memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
On Mon, 2024-02-12 at 19:13 -0500, Steven Rostedt wrote: > On Mon, 12 Feb 2024 15:39:03 -0800 > Tim Chen <tim.c.chen@linux.intel.com> wrote: > > > > diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c > > > index e4fbcc3bede5..210c74dcd016 100644 > > > --- a/kernel/trace/trace_sched_switch.c > > > +++ b/kernel/trace/trace_sched_switch.c > > > @@ -201,7 +201,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) > > > int order; > > > > > > /* Figure out how much is needed to hold the given number of cmdlines */ > > > - orig_size = sizeof(*s) + val * TASK_COMM_LEN; > > > + orig_size = sizeof(*s) + val * (TASK_COMM_LEN + sizeof(int)); > > > > Strictly speaking, *map_cmdline_to_pid is unsigned int so it is more consistent > > to use sizeof(unsigned) in line above. But I'm nitpicking and I'm fine to > > leave it as is. > > I was thinking about making that into a macro as it is used in two places. > > /* Holds the size of a cmdline and pid element */ > #define SAVED_CMDLINE_MAP_ELEMENT_SIZE(s) \ > (TASK_COMM_LEN + sizeof((s)->map_cmdline_to_pid[0])) > > orig_size = sizeof(*s) + val * SAVED_CMDLINE_MAP_ELEMENT_SIZE(s); > > Looks good. This makes the code more readable. Tim
diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c index e4fbcc3bede5..210c74dcd016 100644 --- a/kernel/trace/trace_sched_switch.c +++ b/kernel/trace/trace_sched_switch.c @@ -201,7 +201,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) int order; /* Figure out how much is needed to hold the given number of cmdlines */ - orig_size = sizeof(*s) + val * TASK_COMM_LEN; + orig_size = sizeof(*s) + val * (TASK_COMM_LEN + sizeof(int)); order = get_order(orig_size); size = 1 << (order + PAGE_SHIFT); page = alloc_pages(GFP_KERNEL, order); @@ -212,16 +212,11 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) memset(s, 0, sizeof(*s)); /* Round up to actual allocation */ - val = (size - sizeof(*s)) / TASK_COMM_LEN; + val = (size - sizeof(*s)) / (TASK_COMM_LEN + sizeof(int)); s->cmdline_num = val; - s->map_cmdline_to_pid = kmalloc_array(val, - sizeof(*s->map_cmdline_to_pid), - GFP_KERNEL); - if (!s->map_cmdline_to_pid) { - free_saved_cmdlines_buffer(s); - return NULL; - } + /* Place map_cmdline_to_pid array right after saved_cmdlines */ + s->map_cmdline_to_pid = (unsigned *)&s->saved_cmdlines[val * TASK_COMM_LEN]; s->cmdline_idx = 0; memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,