diff mbox series

[v2,2/2] trace-cmd: Reset CPU mask to its default value with "trace-cmd reset".

Message ID 20191001145740.8866-3-tz.stoyanov@gmail.com (mailing list archive)
State Accepted
Headers show
Series Reset CPU mask | expand

Commit Message

Tzvetomir Stoyanov (VMware) Oct. 1, 2019, 2:57 p.m. UTC
"trace-cmd reset" command should put all ftrace config to its default
state, but trace cpumask was not reseted. The patch sets cpumask to
its default value - all CPUs are enabled for tracing.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 tracecmd/trace-record.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Comments

Steven Rostedt Oct. 1, 2019, 3:32 p.m. UTC | #1
On Tue,  1 Oct 2019 17:57:40 +0300
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:

> "trace-cmd reset" command should put all ftrace config to its default
> state, but trace cpumask was not reseted. The patch sets cpumask to
> its default value - all CPUs are enabled for tracing.
> 
> Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> ---
>  tracecmd/trace-record.c | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> index 69de82a..6f3520c 100644
> --- a/tracecmd/trace-record.c
> +++ b/tracecmd/trace-record.c
> @@ -4096,6 +4096,40 @@ static void reset_clock(void)
>  		write_instance_file(instance, "trace_clock", "local", "clock");
>  }
>  
> +static void reset_cpu_mask(void)
> +{
> +	struct buffer_instance *instance;
> +	int cpus = count_cpus();
> +	int fullwords;
> +	char *buf;
> +	int bits;
> +	int len;
> +
> +	fullwords = cpus / 32;
> +	bits = cpus % 32;
> +	len = (fullwords + 1) * 9;
> +
> +	buf = malloc(len + 1);
> +	if (!buf) {
> +		warning("Unable to allocate %d bytes for cpu mask", len + 1);
> +		return;
> +	}
> +	buf[0] = '\0';
> +	if (bits) {
> +		sprintf(buf, "%x", (1 << bits) - 1);
> +	} else if (fullwords) {
> +		strcat(buf, "ffffffff");
> +		fullwords--;
> +	}
> +	while (fullwords-- > 0)
> +		strcat(buf, ",ffffffff");

I like my way because it's a little more condensed ;-)

But this works too and it's far from a fast path. As it's probably
more readable, I'll all this one.

-- Steve


> +
> +	for_all_instances(instance)
> +		write_instance_file(instance, "tracing_cpumask", buf, "cpumask");
> +
> +	free(buf);
> +}
> +
>  static void reset_event_pid(void)
>  {
>  	add_event_pid("");
> @@ -4808,6 +4842,7 @@ void trace_reset(int argc, char **argv)
>  	reset_clock();
>  	reset_event_pid();
>  	reset_max_latency_instance();
> +	reset_cpu_mask();
>  	tracecmd_remove_instances();
>  	clear_func_filters();
>  	/* restore tracing_on to 1 */
Tzvetomir Stoyanov (VMware) Oct. 1, 2019, 3:37 p.m. UTC | #2
On Tue, Oct 1, 2019 at 6:32 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Tue,  1 Oct 2019 17:57:40 +0300
> "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
>
> > "trace-cmd reset" command should put all ftrace config to its default
> > state, but trace cpumask was not reseted. The patch sets cpumask to
> > its default value - all CPUs are enabled for tracing.
> >
> > Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> > ---
> >  tracecmd/trace-record.c | 35 +++++++++++++++++++++++++++++++++++
> >  1 file changed, 35 insertions(+)
> >
> > diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> > index 69de82a..6f3520c 100644
> > --- a/tracecmd/trace-record.c
> > +++ b/tracecmd/trace-record.c
> > @@ -4096,6 +4096,40 @@ static void reset_clock(void)
> >               write_instance_file(instance, "trace_clock", "local", "clock");
> >  }
> >
> > +static void reset_cpu_mask(void)
> > +{
> > +     struct buffer_instance *instance;
> > +     int cpus = count_cpus();
> > +     int fullwords;
> > +     char *buf;
> > +     int bits;
> > +     int len;
> > +
> > +     fullwords = cpus / 32;
> > +     bits = cpus % 32;
> > +     len = (fullwords + 1) * 9;
> > +
> > +     buf = malloc(len + 1);
> > +     if (!buf) {
> > +             warning("Unable to allocate %d bytes for cpu mask", len + 1);
> > +             return;
> > +     }
> > +     buf[0] = '\0';
> > +     if (bits) {
> > +             sprintf(buf, "%x", (1 << bits) - 1);
> > +     } else if (fullwords) {
> > +             strcat(buf, "ffffffff");
> > +             fullwords--;
> > +     }
> > +     while (fullwords-- > 0)
> > +             strcat(buf, ",ffffffff");
>
> I like my way because it's a little more condensed ;-)
>
> But this works too and it's far from a fast path. As it's probably
> more readable, I'll all this one.
>
> -- Steve
>
Actually the only change here is to handle the case when bits is 0,
not to put the leading ",".
The ftrace complains with "Invalid argument" in that case.

>
> > +
> > +     for_all_instances(instance)
> > +             write_instance_file(instance, "tracing_cpumask", buf, "cpumask");
> > +
> > +     free(buf);
> > +}
> > +
> >  static void reset_event_pid(void)
> >  {
> >       add_event_pid("");
> > @@ -4808,6 +4842,7 @@ void trace_reset(int argc, char **argv)
> >       reset_clock();
> >       reset_event_pid();
> >       reset_max_latency_instance();
> > +     reset_cpu_mask();
> >       tracecmd_remove_instances();
> >       clear_func_filters();
> >       /* restore tracing_on to 1 */
>
Steven Rostedt Oct. 1, 2019, 3:45 p.m. UTC | #3
On Tue, 1 Oct 2019 18:37:54 +0300
Tzvetomir Stoyanov <tz.stoyanov@gmail.com> wrote:

> On Tue, Oct 1, 2019 at 6:32 PM Steven Rostedt <rostedt@goodmis.org> wrote:
> >
> > On Tue,  1 Oct 2019 17:57:40 +0300
> > "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
> >  
> > > "trace-cmd reset" command should put all ftrace config to its default
> > > state, but trace cpumask was not reseted. The patch sets cpumask to
> > > its default value - all CPUs are enabled for tracing.
> > >
> > > Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> > > ---
> > >  tracecmd/trace-record.c | 35 +++++++++++++++++++++++++++++++++++
> > >  1 file changed, 35 insertions(+)
> > >
> > > diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> > > index 69de82a..6f3520c 100644
> > > --- a/tracecmd/trace-record.c
> > > +++ b/tracecmd/trace-record.c
> > > @@ -4096,6 +4096,40 @@ static void reset_clock(void)
> > >               write_instance_file(instance, "trace_clock", "local", "clock");
> > >  }
> > >
> > > +static void reset_cpu_mask(void)
> > > +{
> > > +     struct buffer_instance *instance;
> > > +     int cpus = count_cpus();
> > > +     int fullwords;
> > > +     char *buf;
> > > +     int bits;
> > > +     int len;
> > > +
> > > +     fullwords = cpus / 32;
> > > +     bits = cpus % 32;
> > > +     len = (fullwords + 1) * 9;
> > > +
> > > +     buf = malloc(len + 1);
> > > +     if (!buf) {
> > > +             warning("Unable to allocate %d bytes for cpu mask", len + 1);
> > > +             return;
> > > +     }
> > > +     buf[0] = '\0';
> > > +     if (bits) {
> > > +             sprintf(buf, "%x", (1 << bits) - 1);
> > > +     } else if (fullwords) {
> > > +             strcat(buf, "ffffffff");
> > > +             fullwords--;
> > > +     }
> > > +     while (fullwords-- > 0)
> > > +             strcat(buf, ",ffffffff");  
> >
> > I like my way because it's a little more condensed ;-)
> >
> > But this works too and it's far from a fast path. As it's probably
> > more readable, I'll all this one.
> >
> > -- Steve
> >  
> Actually the only change here is to handle the case when bits is 0,
> not to put the leading ",".
> The ftrace complains with "Invalid argument" in that case.
> 

Mine handled that case too. The difference was the "cpus - 1" part of
my code:

	fullwords = (cpus - 1) / 32;  <--
	bits = (cpus - 1) % 32 + 1;   <--
	len = (fullwords + 1) * 9;

	buf = malloc(len + 1);
	buf[0] = '\0';

	if (bits)
		sprintf(buf, "%x", (unsigned int)((1ULL << bits) - 1));
	while (fullwords-- > 0)
		strcat(buf, ",ffffffff");


The output of yours and mine were identical. When cpus was a power of
32, it would still have one less of "fullwords", and bits would be 32.
Which is why I needed to add "1ULL" to make:

	(1 << 32) - 1 == 0xffffffff

the fullwords was only for non bits.

-- Steve
diff mbox series

Patch

diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index 69de82a..6f3520c 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -4096,6 +4096,40 @@  static void reset_clock(void)
 		write_instance_file(instance, "trace_clock", "local", "clock");
 }
 
+static void reset_cpu_mask(void)
+{
+	struct buffer_instance *instance;
+	int cpus = count_cpus();
+	int fullwords;
+	char *buf;
+	int bits;
+	int len;
+
+	fullwords = cpus / 32;
+	bits = cpus % 32;
+	len = (fullwords + 1) * 9;
+
+	buf = malloc(len + 1);
+	if (!buf) {
+		warning("Unable to allocate %d bytes for cpu mask", len + 1);
+		return;
+	}
+	buf[0] = '\0';
+	if (bits) {
+		sprintf(buf, "%x", (1 << bits) - 1);
+	} else if (fullwords) {
+		strcat(buf, "ffffffff");
+		fullwords--;
+	}
+	while (fullwords-- > 0)
+		strcat(buf, ",ffffffff");
+
+	for_all_instances(instance)
+		write_instance_file(instance, "tracing_cpumask", buf, "cpumask");
+
+	free(buf);
+}
+
 static void reset_event_pid(void)
 {
 	add_event_pid("");
@@ -4808,6 +4842,7 @@  void trace_reset(int argc, char **argv)
 	reset_clock();
 	reset_event_pid();
 	reset_max_latency_instance();
+	reset_cpu_mask();
 	tracecmd_remove_instances();
 	clear_func_filters();
 	/* restore tracing_on to 1 */