Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S941277AbcJGQsL (ORCPT ); Fri, 7 Oct 2016 12:48:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34690 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965076AbcJGQr0 (ORCPT ); Fri, 7 Oct 2016 12:47:26 -0400 From: Luiz Capitulino To: rostedt@goodmis.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 2/3] trace-cmd record: refactor set_mask() Date: Fri, 7 Oct 2016 12:47:10 -0400 Message-Id: <1475858831-32687-3-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1475858831-32687-1-git-send-email-lcapitulino@redhat.com> References: <1475858831-32687-1-git-send-email-lcapitulino@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 07 Oct 2016 16:47:20 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3245 Lines: 135 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 --- 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