This commit makes set_mask() more specialized: all
it does now is to write the cpumask to tracing_cpumask.
The handling of "-M -1" is now done by the newly
added alloc_mask_from_hex().

Also, uneeded checks are dropped and
buffer_instance->cpumask points to dynamic memory.

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

Signed-off-by: Luiz Capitulino <[email protected]>
---
 trace-local.h  |  2 +-
 trace-record.c | 54 ++++++++++++++++++++++++++++++++++--------------------
 2 files changed, 35 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..0f1f2c4 100644
--- a/trace-record.c
+++ b/trace-record.c
@@ -69,6 +69,8 @@ enum trace_type {
        TRACE_TYPE_PROFILE      = (1 << 4) | TRACE_TYPE_STREAM,
 };
 
+#define CPUMASK_STR_MAX 4096 /* Don't expect more than 32768 CPUS */
+
 static int rt_prio;
 
 static int keep;
@@ -2078,27 +2080,24 @@ 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;
 
-       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(CPUMASK_STR_MAX);
+               if (!cpumask)
+                       die("can't allocate cpumask");
+
+               if (bytes > (CPUMASK_STR_MAX-1)) {
                        warning("cpumask can't handle more than 32768 CPUS!");
-                       bytes = 4095;
+                       bytes = CPUMASK_STR_MAX-1;
                }
 
                sprintf(cpumask, "%x", (1 << last) - 1);
@@ -2107,18 +2106,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 +2139,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 +4544,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

Reply via email to