[PATCH 2/3] trace-cmd record: refactor set_mask()

From: Luiz Capitulino
Date: Fri Oct 07 2016 - 12:48:20 EST


This commit does the following:

1. Move the code that treats "-M -1" out of set_mask()
2. Drop uneeded checks from set_mask()
3. Make instance->cpumask point to dynamic memory

This is a preparation for supporting the "--cpu-list"
option in the next commit.

Signed-off-by: Luiz Capitulino <lcapitulino@xxxxxxxxxx>
---
trace-local.h | 2 +-
trace-record.c | 53 +++++++++++++++++++++++++++++++++--------------------
2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/trace-local.h b/trace-local.h
index 62363d0..0ac39bf 100644
--- a/trace-local.h
+++ b/trace-local.h
@@ -143,7 +143,7 @@ struct func_list {
struct buffer_instance {
struct buffer_instance *next;
const char *name;
- const char *cpumask;
+ char *cpumask;
struct event_list *events;
struct event_list **event_next;

diff --git a/trace-record.c b/trace-record.c
index 22b6835..b042993 100644
--- a/trace-record.c
+++ b/trace-record.c
@@ -2078,27 +2078,25 @@ static void update_pid_event_filters(struct buffer_instance *instance)
update_event_filters(instance);
}

-static void set_mask(struct buffer_instance *instance)
-{
- const char *mask = instance->cpumask;
- struct stat st;
- char cpumask[4096]; /* Don't expect more than 32768 CPUS */
- char *path;
- int fd;
- int ret;
+#define MASK_STR_MAX 4096 /* Don't expect more than 32768 CPUS */

- if (!mask)
- return;
+static char *alloc_mask_from_hex(const char *str)
+{
+ char *cpumask;

- if (strcmp(mask, "-1") == 0) {
+ if (strcmp(str, "-1") == 0) {
/* set all CPUs */
int bytes = (cpu_count + 7) / 8;
int last = cpu_count % 8;
int i;

- if (bytes > 4095) {
+ cpumask = malloc(MASK_STR_MAX);
+ if (!cpumask)
+ die("can't allocate cpumask");
+
+ if (bytes > (MASK_STR_MAX-1)) {
warning("cpumask can't handle more than 32768 CPUS!");
- bytes = 4095;
+ bytes = MASK_STR_MAX-1;
}

sprintf(cpumask, "%x", (1 << last) - 1);
@@ -2107,18 +2105,32 @@ static void set_mask(struct buffer_instance *instance)
cpumask[i] = 'f';

cpumask[i+1] = 0;
-
- mask = cpumask;
+ } else {
+ cpumask = strdup(str);
+ if (!cpumask)
+ die("can't allocate cpumask");
}

+ return cpumask;
+}
+
+static void set_mask(struct buffer_instance *instance)
+{
+ struct stat st;
+ char *path;
+ int fd;
+ int ret;
+
+ if (!instance->cpumask)
+ return;
+
path = get_instance_file(instance, "tracing_cpumask");
if (!path)
die("could not allocate path");

ret = stat(path, &st);
if (ret < 0) {
- if (mask)
- warning("%s not found", path);
+ warning("%s not found", path);
goto out;
}

@@ -2126,12 +2138,13 @@ static void set_mask(struct buffer_instance *instance)
if (fd < 0)
die("could not open %s\n", path);

- if (mask)
- write(fd, mask, strlen(mask));
+ write(fd, instance->cpumask, strlen(instance->cpumask));

close(fd);
out:
tracecmd_put_tracing_file(path);
+ free(instance->cpumask);
+ instance->cpumask = NULL;
}

static void enable_events(struct buffer_instance *instance)
@@ -4530,7 +4543,7 @@ void trace_record (int argc, char **argv)
max_kb = atoi(optarg);
break;
case 'M':
- instance->cpumask = optarg;
+ instance->cpumask = alloc_mask_from_hex(optarg);
break;
case 't':
if (extract)
--
2.5.5