From: Steven Rostedt <rostedt@goodmis.org>
To: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
Cc: linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH v3 07/21] trace-cmd library: Do not use local variables when reading CPU stat option
Date: Mon, 4 Oct 2021 11:35:40 -0400 [thread overview]
Message-ID: <20211004113540.7d5c470e@gandalf.local.home> (raw)
In-Reply-To: <20210914131232.3964615-8-tz.stoyanov@gmail.com>
On Tue, 14 Sep 2021 16:12:18 +0300
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
> Using a local variable to read all CPUSTAT options assumes that all of
> them are in a single option section. While this is true for the current
> trace file version format, this assumption limits the design of a more
> flexible format with multiple options sections. Use input handler context
> instead of the local variable.
>
> Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> ---
> lib/trace-cmd/trace-input.c | 20 +++++++++++---------
> 1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
> index 3d7f1c48..d020cfae 100644
> --- a/lib/trace-cmd/trace-input.c
> +++ b/lib/trace-cmd/trace-input.c
> @@ -138,6 +138,7 @@ struct tracecmd_input {
>
> struct host_trace_info host;
> double ts2secs;
> + unsigned int cpustats_size;
> char * cpustats;
The above places cpustats_size (4 bytes) between ts2secs (8 bytes) and
cpustats (8 bytes) on 64 bit machines. Thus, it creates a hole of 4 bytes
of padding.
Best to try to place 4 bytes integers in pairs, where they always take up 8
bytes.
Above this, there's:
// 8 bytes
unsigned long long trace_id;
// pair 1
int fd;
int long_size;
// pair 2
int page_size;
int page_map_size;
// pair 3
int cpus;
int ref;
// pair 4
int nr_buffers; /* buffer instances */
bool use_trace_clock;
// pair 5
bool read_page;
bool use_pipe;
// uneven set of 4 bytes
int file_version;
// 8 bytes
struct cpu_data *cpu_data;
You can move the 4 byte field after file_version to fill in that hole.
> char * uname;
> char * version;
> @@ -2658,7 +2659,6 @@ static int handle_options(struct tracecmd_input *handle)
> unsigned short option;
> unsigned int size;
> char *cpustats = NULL;
> - unsigned int cpustats_size = 0;
> struct input_buffer_instance *buffer;
> struct hook_list *hook;
> char *buf;
> @@ -2738,12 +2738,16 @@ static int handle_options(struct tracecmd_input *handle)
> break;
> case TRACECMD_OPTION_CPUSTAT:
> buf[size-1] = '\n';
> - cpustats = realloc(cpustats, cpustats_size + size + 1);
> - if (!cpustats)
> - return -ENOMEM;
> - memcpy(cpustats + cpustats_size, buf, size);
> - cpustats_size += size;
> - cpustats[cpustats_size] = 0;
> + cpustats = realloc(handle->cpustats,
> + handle->cpustats_size + size + 1);
> + if (!cpustats) {
> + ret = -ENOMEM;
> + return ret;
> + }
Changing:
if (!cpustats)
return -ENOMEM;
to
if (!cpustats) {
ret = -ENOMEM;
return ret;
}
Doesn't make sense, as you are just adding a useless use of a local
variable.
-- Steve
> + memcpy(cpustats + handle->cpustats_size, buf, size);
> + handle->cpustats_size += size;
> + cpustats[handle->cpustats_size] = 0;
> + handle->cpustats = cpustats;
> break;
> case TRACECMD_OPTION_BUFFER:
> /* A buffer instance is saved at the end of the file */
> @@ -2813,8 +2817,6 @@ static int handle_options(struct tracecmd_input *handle)
>
> }
>
> - handle->cpustats = cpustats;
> -
> return 0;
> }
>
next prev parent reply other threads:[~2021-10-04 15:35 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-14 13:12 [PATCH v3 00/21] trace-cmd fixes and clean-ups Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 01/21] trace-cmd library: Read option id with correct endian Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 02/21] trace-cmd report: Fix typos in error messages Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 03/21] trace-cmd library: Fix version string memory leak Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 04/21] trace-cmd library: Fixed a memory leak on input handler close Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 05/21] trace-cmd library: Do not pass guests list to a buffer instance Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 06/21] trace-cmd library: Set long size to the input tep handler when it is read from the file Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 07/21] trace-cmd library: Do not use local variables when reading CPU stat option Tzvetomir Stoyanov (VMware)
2021-10-04 15:35 ` Steven Rostedt [this message]
2021-09-14 13:12 ` [PATCH v3 08/21] trace-cmd library: Reuse within the library the function that checks file state Tzvetomir Stoyanov (VMware)
2021-10-04 15:43 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 09/21] trace-cmd library: Fix possible memory leak in read_ftrace_files() Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 10/21] trace-cmd library: Fix possible memory leak in read_event_files() Tzvetomir Stoyanov (VMware)
2021-10-04 15:47 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 11/21] trace-cmd library: Fix possible memory leak in read_proc_kallsyms() Tzvetomir Stoyanov (VMware)
2021-10-04 15:55 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 12/21] trace-cmd library: Fix possible memory leak in read_ftrace_printk() Tzvetomir Stoyanov (VMware)
2021-10-04 18:43 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 13/21] trace-cmd library: Fix possible memory leak in read_and_parse_cmdlines() Tzvetomir Stoyanov (VMware)
2021-10-04 18:44 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 14/21] trace-cmd library: Set input handler default values in allocation function Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 15/21] trace-cmd library: Track maximum CPUs count in input handler Tzvetomir Stoyanov (VMware)
2021-10-04 18:48 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 16/21] trace-cmd report: Close input file handlers on exit Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 17/21] trace-cmd report: Do not print empty buffer name Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 18/21] trace-cmd library: Set correct CPU to the record, retrieved with tracecmd_peek_data Tzvetomir Stoyanov (VMware)
2021-09-14 13:12 ` [PATCH v3 19/21] trace-cmd library: Refactor APIs for creating output handler Tzvetomir Stoyanov (VMware)
2021-10-04 22:37 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 20/21] trace-cmd library: Refactor the logic for writing trace data in the file Tzvetomir Stoyanov (VMware)
2021-10-05 0:51 ` Steven Rostedt
2021-09-14 13:12 ` [PATCH v3 21/21] trace-cmd report: Init the top trace instance earlier Tzvetomir Stoyanov (VMware)
2021-10-05 1:00 ` [PATCH v3 00/21] trace-cmd fixes and clean-ups Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211004113540.7d5c470e@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=linux-trace-devel@vger.kernel.org \
--cc=tz.stoyanov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).