From patchwork Mon Nov 14 20:45:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13042847 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3307EC4321E for ; Mon, 14 Nov 2022 20:44:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237525AbiKNUos (ORCPT ); Mon, 14 Nov 2022 15:44:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236281AbiKNUoo (ORCPT ); Mon, 14 Nov 2022 15:44:44 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2AFC12606 for ; Mon, 14 Nov 2022 12:44:42 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 594CD61469 for ; Mon, 14 Nov 2022 20:44:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C41AC433D7; Mon, 14 Nov 2022 20:44:41 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1ougKa-00AD5o-05; Mon, 14 Nov 2022 15:45:24 -0500 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH v2 1/4] libtracefs: Add tracefs_event_is_enabled() API Date: Mon, 14 Nov 2022 15:45:19 -0500 Message-Id: <20221114204522.2433500-2-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221114204522.2433500-1-rostedt@goodmis.org> References: <20221114204522.2433500-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" Add a function that checks if an event (or events) are enabled or not. Returns the enums: TRACEFS_ERROR = -1 TRACEFS_ALL_DISABLED = 0 TRACEFS_ALL_ENABLED = 1 TRACEFS_SOME_ENABLED = 2 Signed-off-by: Steven Rostedt (Google) --- Documentation/libtracefs-events.txt | 33 ++++++- Documentation/libtracefs.txt | 2 + include/tracefs.h | 9 ++ src/tracefs-events.c | 128 ++++++++++++++++++++++++++-- 4 files changed, 162 insertions(+), 10 deletions(-) diff --git a/Documentation/libtracefs-events.txt b/Documentation/libtracefs-events.txt index f998c79b04ec..d5bd779273e2 100644 --- a/Documentation/libtracefs-events.txt +++ b/Documentation/libtracefs-events.txt @@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_event_systems, tracefs_system_events, tracefs_event_enable, tracefs_event_disable, -tracefs_iterate_raw_events, tracefs_iterate_stop - Work with trace systems and events. +tracefs_event_is_enabled, tracefs_iterate_raw_events, tracefs_iterate_stop - Work with trace systems and events. SYNOPSIS -------- @@ -12,12 +12,22 @@ SYNOPSIS -- *#include * +enum tracefs_event_state { + TRACEFS_ERROR = -1, + TRACEFS_ALL_DISABLED = 0, + TRACEFS_ALL_ENABLED = 1, + TRACEFS_SOME_ENABLED = 2, +}; + char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_); char pass:[*]pass:[*]*tracefs_system_events*(const char pass:[*]_tracing_dir_, const char pass:[*]_system_); int *tracefs_event_enable*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_); int *tracefs_event_disable*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_); +enum tracefs_enable_state *tracefs_event_is_enabled*(struct tracefs_instance pass:[*]_instance_, + const char pass:[*]_system_, const char pass:[*]_event_); + int *tracefs_iterate_raw_events*(struct tep_handle pass:[*]_tep_, struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_cpus_, int _cpu_size_, int (pass:[*]_callback_)(struct tep_event pass:[*], struct tep_record pass:[*], int, void pass:[*]), @@ -61,6 +71,24 @@ events. That is, if _instance_ is NULL, then the top level tracing directory is used. If both _system_ and _event_ are NULL then all events are disabled for the given _instance_, and so on. +The *tracefs_event_is_enabled()* returns if an event is enabled, a set of +events are enabled, a system is enabled, or all events are enabled. If both +_system_ and _event_ are NULL, then it returns the enable state of all events. +If _system_ is not NULL and _event_ is NULL, then it will check if all the events +in all the systems that _system_ and return the enable state of those events. +If _system_ is NULL and _event_ is not NULL, then it will match all the events +in all systems that match _event_ and return their enabled state. If both _system_ +and _event_ are not NULL, then it will return the enabled state of all matching +events. The enabled state is defined as: + +*TRACEFS_ERROR* - An error occurred including no event were matched. + +*TRACEFS_ALL_DISABLED* - All matching events are disabled. + +*TRACEFS_ALL_ENABLED* - All matching events are enabled. + +*TRACEFS_SOME_ENABLED* - Some matching events were enabled while others were not. + The *tracefs_iterate_raw_events()* function will read the tracefs raw data buffers and call the specified _callback_ function for every event it encounters. Events are iterated in sorted order: oldest first. An initialized @@ -95,6 +123,9 @@ found, it will return -1 and errno will be set. If no errors occur, but no event are found that match the _system_ and _event_ parameters, then -1 is returned and errno is not set. +The *tracefs_event_is_enabled()* returns the enabled status of the matching events +or TRACEFS_ERROR on error. + The *tracefs_iterate_raw_events()* function returns -1 in case of an error or 0 otherwise. diff --git a/Documentation/libtracefs.txt b/Documentation/libtracefs.txt index 0081210f8951..e02974004ef8 100644 --- a/Documentation/libtracefs.txt +++ b/Documentation/libtracefs.txt @@ -53,6 +53,8 @@ Trace events: const char pass:[*]_event_); int *tracefs_event_disable*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_); + enum tracefs_enable_state *tracefs_event_is_enabled*(struct tracefs_instance pass:[*]_instance_, + const char pass:[*]_system_, const char pass:[*]_event_); int *tracefs_iterate_raw_events*(struct tep_handle pass:[*]_tep_, struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_cpus_, int _cpu_size_, int (pass:[*]_callback_)(struct tep_event pass:[*], struct tep_record pass:[*], int, void pass:[*]), void pass:[*]_callback_context_); void *tracefs_iterate_stop*(struct tracefs_instance pass:[*]_instance_); struct tep_handle pass:[*]*tracefs_local_events*(const char pass:[*]_tracing_dir_); diff --git a/include/tracefs.h b/include/tracefs.h index 9f0bdc62836a..f2524b07501d 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -67,8 +67,17 @@ int tracefs_trace_off(struct tracefs_instance *instance); int tracefs_trace_on_fd(int fd); int tracefs_trace_off_fd(int fd); +enum tracefs_enable_state { + TRACEFS_ERROR = -1, + TRACEFS_ALL_DISABLED = 0, + TRACEFS_ALL_ENABLED = 1, + TRACEFS_SOME_ENABLED = 2, +}; + int tracefs_event_enable(struct tracefs_instance *instance, const char *system, const char *event); int tracefs_event_disable(struct tracefs_instance *instance, const char *system, const char *event); +enum tracefs_enable_state tracefs_event_is_enabled(struct tracefs_instance *instance, + const char *system, const char *event); char *tracefs_error_last(struct tracefs_instance *instance); char *tracefs_error_all(struct tracefs_instance *instance); diff --git a/src/tracefs-events.c b/src/tracefs-events.c index d870241e127f..57b22964f893 100644 --- a/src/tracefs-events.c +++ b/src/tracefs-events.c @@ -1044,9 +1044,68 @@ static bool match(const char *str, regex_t *re) return regexec(re, str, 0, NULL, 0) == 0; } +enum event_state { + STATE_INIT, + STATE_ENABLED, + STATE_DISABLED, + STATE_MIXED, + STATE_ERROR, +}; + +static int read_event_state(struct tracefs_instance *instance, const char *file, + enum event_state *state) +{ + char *val; + int ret = 0; + + if (*state == STATE_ERROR) + return -1; + + val = tracefs_instance_file_read(instance, file, NULL); + if (!val) + return -1; + + switch (val[0]) { + case '0': + switch (*state) { + case STATE_INIT: + *state = STATE_DISABLED; + break; + case STATE_ENABLED: + *state = STATE_MIXED; + break; + default: + break; + } + break; + case '1': + switch (*state) { + case STATE_INIT: + *state = STATE_ENABLED; + break; + case STATE_DISABLED: + *state = STATE_MIXED; + break; + default: + break; + } + break; + case 'X': + *state = STATE_MIXED; + break; + default: + *state = TRACEFS_ERROR; + ret = -1; + break; + } + free(val); + + return ret; +} + static int enable_disable_event(struct tracefs_instance *instance, const char *system, const char *event, - bool enable) + bool enable, enum event_state *state) { const char *str = enable ? "1" : "0"; char *system_event; @@ -1056,14 +1115,18 @@ static int enable_disable_event(struct tracefs_instance *instance, if (ret < 0) return ret; - ret = tracefs_instance_file_write(instance, system_event, str); + if (state) + ret = read_event_state(instance, system_event, state); + else + ret = tracefs_instance_file_write(instance, system_event, str); free(system_event); return ret; } static int enable_disable_system(struct tracefs_instance *instance, - const char *system, bool enable) + const char *system, bool enable, + enum event_state *state) { const char *str = enable ? "1" : "0"; char *system_path; @@ -1073,7 +1136,10 @@ static int enable_disable_system(struct tracefs_instance *instance, if (ret < 0) return ret; - ret = tracefs_instance_file_write(instance, system_path, str); + if (state) + ret = read_event_state(instance, system_path, state); + else + ret = tracefs_instance_file_write(instance, system_path, str); free(system_path); return ret; @@ -1111,7 +1177,7 @@ static int make_regex(regex_t *re, const char *match) static int event_enable_disable(struct tracefs_instance *instance, const char *system, const char *event, - bool enable) + bool enable, enum event_state *state) { regex_t system_re, event_re; char **systems; @@ -1148,7 +1214,7 @@ static int event_enable_disable(struct tracefs_instance *instance, /* Check for the short cut first */ if (!event) { - ret = enable_disable_system(instance, systems[s], enable); + ret = enable_disable_system(instance, systems[s], enable, state); if (ret < 0) break; ret = 0; @@ -1163,7 +1229,7 @@ static int event_enable_disable(struct tracefs_instance *instance, if (!match(events[e], &event_re)) continue; ret = enable_disable_event(instance, systems[s], - events[e], enable); + events[e], enable, state); if (ret < 0) break; ret = 0; @@ -1202,11 +1268,55 @@ static int event_enable_disable(struct tracefs_instance *instance, int tracefs_event_enable(struct tracefs_instance *instance, const char *system, const char *event) { - return event_enable_disable(instance, system, event, true); + return event_enable_disable(instance, system, event, true, NULL); } int tracefs_event_disable(struct tracefs_instance *instance, const char *system, const char *event) { - return event_enable_disable(instance, system, event, false); + return event_enable_disable(instance, system, event, false, NULL); +} + +/** + * tracefs_event_is_enabled - return if the event is enabled or not + * @instance: ftrace instance, can be NULL for the top instance + * @system: The name of the system to check + * @event: The name of the event to check + * + * Checks is an event or multiple events are enabled. + * + * If @system is NULL, then it will check all the systems where @event is + * a match. + * + * If @event is NULL, then it will check all events where @system is a match. + * + * If both @system and @event are NULL, then it will check all events + * + * Returns TRACEFS_ALL_ENABLED if all matching are enabled. + * Returns TRACEFS_SOME_ENABLED if some are enabled and some are not + * Returns TRACEFS_ALL_DISABLED if none of the events are enabled. + * Returns TRACEFS_ERROR if there is an error reading the events. + */ +enum tracefs_enable_state +tracefs_event_is_enabled(struct tracefs_instance *instance, + const char *system, const char *event) +{ + enum event_state state = STATE_INIT; + int ret; + + ret = event_enable_disable(instance, system, event, false, &state); + + if (ret < 0) + return TRACEFS_ERROR; + + switch (state) { + case STATE_ENABLED: + return TRACEFS_ALL_ENABLED; + case STATE_DISABLED: + return TRACEFS_ALL_DISABLED; + case STATE_MIXED: + return TRACEFS_SOME_ENABLED; + default: + return TRACEFS_ERROR; + } } From patchwork Mon Nov 14 20:45:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13042845 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A9A0C433FE for ; Mon, 14 Nov 2022 20:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237281AbiKNUoq (ORCPT ); Mon, 14 Nov 2022 15:44:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236990AbiKNUon (ORCPT ); Mon, 14 Nov 2022 15:44:43 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2E6A60DD for ; Mon, 14 Nov 2022 12:44:42 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 381566144C for ; Mon, 14 Nov 2022 20:44:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F5D6C433B5; Mon, 14 Nov 2022 20:44:41 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1ougKa-00AD5r-09; Mon, 14 Nov 2022 15:45:24 -0500 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH v2 2/4] libtracefs: Add tracefs_instance_get_buffer_size() API Date: Mon, 14 Nov 2022 15:45:20 -0500 Message-Id: <20221114204522.2433500-3-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221114204522.2433500-1-rostedt@goodmis.org> References: <20221114204522.2433500-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" Add the function tracefs_instance_get_buffer_size() to return the size of the ring buffer. Signed-off-by: Steven Rostedt (Google) --- Documentation/libtracefs-instances-utils.txt | 14 +++++--- Documentation/libtracefs.txt | 1 + include/tracefs.h | 1 + src/tracefs-instance.c | 37 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Documentation/libtracefs-instances-utils.txt b/Documentation/libtracefs-instances-utils.txt index a79cc3447d80..9bec8a9163cd 100644 --- a/Documentation/libtracefs-instances-utils.txt +++ b/Documentation/libtracefs-instances-utils.txt @@ -3,9 +3,8 @@ libtracefs(3) NAME ---- -tracefs_instance_get_name, tracefs_instance_get_trace_dir, -tracefs_instances_walk, tracefs_instance_exists - -Helper functions for working with tracing instances. +tracefs_instance_get_name, tracefs_instance_get_trace_dir, tracefs_instances_walk, tracefs_instance_exists, +tracefs_instance_get_buffer_size - Helper functions for working with tracing instances. SYNOPSIS -------- @@ -17,7 +16,7 @@ const char pass:[*]*tracefs_instance_get_name*(struct tracefs_instance pass:[*]_ const char pass:[*]*tracefs_instance_get_trace_dir*(struct tracefs_instance pass:[*]_instance_); int *tracefs_instances_walk*(int (pass:[*]_callback_)(const char pass:[*], void pass:[*]), void pass:[*]_context)_; bool *tracefs_instance_exists*(const char pass:[*]_name_); - +size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); -- DESCRIPTION @@ -39,6 +38,10 @@ called for the top top instance. The *tracefs_instance_exists()* function checks if an instance with the given _name_ exists in the system. +The *tracefs_instace_get_buffer_size()* returns the size of the ring buffer. If _cpu_ +is negative, it returns the total size of all the per CPU ring buffers, otherwise +it returns the size of the per CPU ring buffer for _cpu_. + RETURN VALUE ------------ The *tracefs_instance_get_name()* returns a string or NULL in case of the top @@ -53,6 +56,9 @@ if the iteration was stopped by the _callback_, or -1 in case of an error. The *tracefs_instance_exists()* returns true if an instance with the given _name_ exists in the system or false otherwise. +The *tracefs_instance_get_buffer_size()* returns the size of the ring buffer depending on +the _cpu_ value passed in, or -1 on error. + EXAMPLE ------- [source,c] diff --git a/Documentation/libtracefs.txt b/Documentation/libtracefs.txt index e02974004ef8..3b485a018ed4 100644 --- a/Documentation/libtracefs.txt +++ b/Documentation/libtracefs.txt @@ -45,6 +45,7 @@ Trace instances: char pass:[*]*tracefs_instance_get_affinity*(struct tracefs_instance pass:[*]_instance_); int *tracefs_instance_get_affinity_set*(struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_set_, size_t _set_size_); char pass:[*]*tracefs_instance_get_affinity_raw*(struct tracefs_instance pass:[*]_instance_); + size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); Trace events: char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_); diff --git a/include/tracefs.h b/include/tracefs.h index f2524b07501d..3391debccc80 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -55,6 +55,7 @@ char *tracefs_instance_get_affinity(struct tracefs_instance *instance); char *tracefs_instance_get_affinity_raw(struct tracefs_instance *instance); int tracefs_instance_get_affinity_set(struct tracefs_instance *instance, cpu_set_t *set, size_t set_size); +ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, int cpu); char **tracefs_instances(const char *regex); bool tracefs_instance_exists(const char *name); diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c index 249a5c9a3abe..206f8c99cf36 100644 --- a/src/tracefs-instance.c +++ b/src/tracefs-instance.c @@ -363,6 +363,43 @@ const char *tracefs_instance_get_name(struct tracefs_instance *instance) return NULL; } +/** + * tracefs_instance_get_buffer_size - return the buffer size of the ring buffer + * @instance: The instance to get the buffer size from + * @cpu: if less that zero, will return the total size, otherwise the cpu size + * + * Returns the buffer size. If @cpu is less than zero, it returns the total size + * of the ring buffer otherwise it returs the size of the buffer for the given + * CPU. + * + * Returns -1 on error. + */ +ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, int cpu) +{ + unsigned long long size; + char *path; + char *val; + int ret; + + if (cpu < 0) { + val = tracefs_instance_file_read(instance, "buffer_total_size_kb", NULL); + } else { + ret = asprintf(&path, "per_cpu/cpu%d/buffer_size_kb", cpu); + if (ret < 0) + return ret; + + val = tracefs_instance_file_read(instance, path, NULL); + free(path); + } + + if (!val) + return -1; + + size = strtoull(val, NULL, 0); + free(val); + return size; +} + /** * tracefs_instance_get_trace_dir - return the top trace directory, where the instance is confuigred * @instance: ftrace instance From patchwork Mon Nov 14 20:45:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13042848 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CAF2C4332F for ; Mon, 14 Nov 2022 20:44:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237295AbiKNUot (ORCPT ); Mon, 14 Nov 2022 15:44:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237426AbiKNUoq (ORCPT ); Mon, 14 Nov 2022 15:44:46 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76BE160CD for ; Mon, 14 Nov 2022 12:44:44 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 1B444B81260 for ; Mon, 14 Nov 2022 20:44:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2829C433C1; Mon, 14 Nov 2022 20:44:41 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1ougKa-00AD5u-0D; Mon, 14 Nov 2022 15:45:24 -0500 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH v2 3/4] libtracefs: Add tracefs_instance_set_buffer_size() API Date: Mon, 14 Nov 2022 15:45:21 -0500 Message-Id: <20221114204522.2433500-4-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221114204522.2433500-1-rostedt@goodmis.org> References: <20221114204522.2433500-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" Add functionality that sets the ring buffer size. Signed-off-by: Steven Rostedt (Google) --- Documentation/libtracefs-instances-utils.txt | 10 +++++++- Documentation/libtracefs.txt | 1 + include/tracefs.h | 1 + src/tracefs-instance.c | 27 ++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Documentation/libtracefs-instances-utils.txt b/Documentation/libtracefs-instances-utils.txt index 9bec8a9163cd..bc8c9a7bf8e9 100644 --- a/Documentation/libtracefs-instances-utils.txt +++ b/Documentation/libtracefs-instances-utils.txt @@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_instance_get_name, tracefs_instance_get_trace_dir, tracefs_instances_walk, tracefs_instance_exists, -tracefs_instance_get_buffer_size - Helper functions for working with tracing instances. +tracefs_instance_get_buffer_size, tracefs_instance_set_buffer_size - Helper functions for working with tracing instances. SYNOPSIS -------- @@ -17,6 +17,7 @@ const char pass:[*]*tracefs_instance_get_trace_dir*(struct tracefs_instance pass int *tracefs_instances_walk*(int (pass:[*]_callback_)(const char pass:[*], void pass:[*]), void pass:[*]_context)_; bool *tracefs_instance_exists*(const char pass:[*]_name_); size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); +int *tracefs_instance_set_buffer_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_, int _cpu_); -- DESCRIPTION @@ -42,6 +43,11 @@ The *tracefs_instace_get_buffer_size()* returns the size of the ring buffer. If is negative, it returns the total size of all the per CPU ring buffers, otherwise it returns the size of the per CPU ring buffer for _cpu_. +The *tracefs_instance_set_buffer_size()* function sets the size of the ring buffer. +If _cpu_ is negative, then it sets all the per CPU ring buffers to _size_ (note +the total size is the number of CPUs * _size_). If _cpu_ is specified, then it only +sets the size of the per CPU ring buffer. + RETURN VALUE ------------ The *tracefs_instance_get_name()* returns a string or NULL in case of the top @@ -59,6 +65,8 @@ exists in the system or false otherwise. The *tracefs_instance_get_buffer_size()* returns the size of the ring buffer depending on the _cpu_ value passed in, or -1 on error. +The *tracefs_instance_set_buffer_size()* returns zero on success and -1 on error. + EXAMPLE ------- [source,c] diff --git a/Documentation/libtracefs.txt b/Documentation/libtracefs.txt index 3b485a018ed4..73c4997efeef 100644 --- a/Documentation/libtracefs.txt +++ b/Documentation/libtracefs.txt @@ -46,6 +46,7 @@ Trace instances: int *tracefs_instance_get_affinity_set*(struct tracefs_instance pass:[*]_instance_, cpu_set_t pass:[*]_set_, size_t _set_size_); char pass:[*]*tracefs_instance_get_affinity_raw*(struct tracefs_instance pass:[*]_instance_); size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); + int *tracefs_instance_set_buffer_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_, int _cpu_); Trace events: char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_); diff --git a/include/tracefs.h b/include/tracefs.h index 3391debccc80..5ff0c6f134d3 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -56,6 +56,7 @@ char *tracefs_instance_get_affinity_raw(struct tracefs_instance *instance); int tracefs_instance_get_affinity_set(struct tracefs_instance *instance, cpu_set_t *set, size_t set_size); ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, int cpu); +int tracefs_instance_set_buffer_size(struct tracefs_instance *instance, size_t size, int cpu); char **tracefs_instances(const char *regex); bool tracefs_instance_exists(const char *name); diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c index 206f8c99cf36..6905f61ff929 100644 --- a/src/tracefs-instance.c +++ b/src/tracefs-instance.c @@ -400,6 +400,33 @@ ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, int return size; } +int tracefs_instance_set_buffer_size(struct tracefs_instance *instance, size_t size, int cpu) +{ + char *path; + char *val; + int ret; + + ret = asprintf(&val, "%zd", size); + if (ret < 0) + return ret; + + if (cpu < 0) { + ret = tracefs_instance_file_write(instance, "buffer_size_kb", val); + } else { + ret = asprintf(&path, "per_cpu/cpu%d/buffer_size_kb", cpu); + if (ret < 0) { + free(val); + return ret; + } + + ret = tracefs_instance_file_write(instance, path, "val"); + free(path); + } + free(val); + + return ret < 0 ? -1 : 0; +} + /** * tracefs_instance_get_trace_dir - return the top trace directory, where the instance is confuigred * @instance: ftrace instance From patchwork Mon Nov 14 20:45:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13042846 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3FADC43217 for ; Mon, 14 Nov 2022 20:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236990AbiKNUor (ORCPT ); Mon, 14 Nov 2022 15:44:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237343AbiKNUoo (ORCPT ); Mon, 14 Nov 2022 15:44:44 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCE00F007 for ; Mon, 14 Nov 2022 12:44:42 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4BE6261467 for ; Mon, 14 Nov 2022 20:44:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A82A0C43144; Mon, 14 Nov 2022 20:44:41 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.96) (envelope-from ) id 1ougKa-00AD5x-0H; Mon, 14 Nov 2022 15:45:24 -0500 From: Steven Rostedt To: linux-trace-devel@vger.kernel.org Cc: "Steven Rostedt (Google)" Subject: [PATCH v2 4/4] libtracefs: Add tracefs_tracing_dir_is_mounted() API Date: Mon, 14 Nov 2022 15:45:22 -0500 Message-Id: <20221114204522.2433500-5-rostedt@goodmis.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221114204522.2433500-1-rostedt@goodmis.org> References: <20221114204522.2433500-1-rostedt@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: "Steven Rostedt (Google)" Add the tracefs_tracing_dir_is_mounted() API that passes in a mount flag and an address to a char pointer (or NULL). It returns 1 if the tracing_dir was already mounted and 0 if not. If the mount flag is set, then it will try to mount it and return -1 if it fails. If path points to the address of a char*, then it will be set to the path that was mounted, or was already mounted. This is useful for wanting to unmount the tracing_dir if the tool was the one that mounted it. Signed-off-by: Steven Rostedt (Google) --- Documentation/libtracefs-files.txt | 14 ++++++- Documentation/libtracefs.txt | 1 + include/tracefs.h | 1 + src/tracefs-utils.c | 62 ++++++++++++++++++++++++------ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/Documentation/libtracefs-files.txt b/Documentation/libtracefs-files.txt index 2a3f5449839d..d22e75900098 100644 --- a/Documentation/libtracefs-files.txt +++ b/Documentation/libtracefs-files.txt @@ -3,8 +3,8 @@ libtracefs(3) NAME ---- -tracefs_get_tracing_file, tracefs_put_tracing_file, tracefs_tracing_dir, tracefs_debug_dir, tracefs_set_tracing_dir - -Find and set locations of trace directory and files. +tracefs_get_tracing_file, tracefs_put_tracing_file, tracefs_tracing_dir, tracefs_debug_dir, tracefs_set_tracing_dir, +tracefs_tracing_dir_is_mounted - Find and set locations of trace directory and files. SYNOPSIS -------- @@ -17,6 +17,7 @@ void *tracefs_put_tracing_file*(char pass:[*]_name_); const char pass:[*]*tracefs_tracing_dir*(void); const char pass:[*]*tracefs_debug_dir*(void); int *tracefs_set_tracing_dir*(char pass:[*]_tracing_dir_) +int *tracefs_tracing_dir_is_mounted*(bool _mount_, const char pass:[**]_path_); -- DESCRIPTION @@ -52,6 +53,12 @@ that it will return where the debugfs file system is mounted. If it is not mounted it will try to mount it. The return string must _not_ be freed. +*tracefs_tracing_dir_is_mounted()* returns 1 if the tracing directory is +already mounted and 0 if it is not. If _mount_ is true, it will try to +mount it if it is not, and returns 0 if it succesfully mounted it and -1 +if it did not. If _path_ is set, it will be assigned to the path where it +mounted it. _path_ is internal and should not be freed. + RETURN VALUE ------------ The *tracefs_set_tracing_dir()* function returns 0 on success, -1 otherwise. @@ -65,6 +72,9 @@ in case of an error. The returned string must _not_ be freed. The *tracefs_debug_dir()* function returns a constant string or NULL in case of an error. The returned string must _not_ be freed. +The *tracefs_tracing_dir_is_mounted()* returns 1 if the tracing directory +is already mounted, 0 if it is not, and -1 on error. + EXAMPLE ------- [source,c] diff --git a/Documentation/libtracefs.txt b/Documentation/libtracefs.txt index 73c4997efeef..cc53388438e5 100644 --- a/Documentation/libtracefs.txt +++ b/Documentation/libtracefs.txt @@ -17,6 +17,7 @@ Locations of tracing files and directories: const char pass:[*]*tracefs_tracing_dir*(void); const char pass:[*]*tracefs_debug_dir*(void); int *tracefs_set_tracing_dir*(char pass:[*]_tracing_dir_) + int *tracefs_tracing_dir_is_mounted*(bool _mount_, const char pass:[**]_path_); Trace instances: struct tracefs_instance pass:[*]*tracefs_instance_create*(const char pass:[*]_name_); diff --git a/include/tracefs.h b/include/tracefs.h index 5ff0c6f134d3..10f84a25c31e 100644 --- a/include/tracefs.h +++ b/include/tracefs.h @@ -17,6 +17,7 @@ void tracefs_put_tracing_file(char *name); const char *tracefs_tracing_dir(void); const char *tracefs_debug_dir(void); int tracefs_set_tracing_dir(char *tracing_dir); +int tracefs_tracing_dir_is_mounted(bool mount, const char **path); /* ftrace instances */ struct tracefs_instance; diff --git a/src/tracefs-utils.c b/src/tracefs-utils.c index a38da6616498..777912e46821 100644 --- a/src/tracefs-utils.c +++ b/src/tracefs-utils.c @@ -85,14 +85,7 @@ static int mount_debugfs(void) return ret; } -/** - * trace_find_tracing_dir - Find tracing directory - * @debugfs: Boolean to just return the debugfs directory - * - * Returns string containing the full path to the system's tracing directory. - * The string must be freed by free() - */ -__hidden char *trace_find_tracing_dir(bool debugfs) +static char *find_tracing_dir(bool debugfs, bool mount) { char *debug_str = NULL; char fspath[PATH_MAX+1]; @@ -127,18 +120,19 @@ __hidden char *trace_find_tracing_dir(bool debugfs) if (debugfs) { if (strcmp(type, "debugfs") != 0) { - if (mount_debugfs() < 0) + if (!mount || mount_debugfs() < 0) return NULL; strcpy(fspath, DEBUGFS_PATH); } } else if (strcmp(type, "tracefs") != 0) { - if (mount_tracefs() < 0) { + if (!mount || mount_tracefs() < 0) { if (debug_str) { strncpy(fspath, debug_str, PATH_MAX); fspath[PATH_MAX] = 0; } else { - if (mount_debugfs() < 0) { - tracefs_warning("debugfs not mounted, please mount"); + if (!mount || mount_debugfs() < 0) { + if (mount) + tracefs_warning("debugfs not mounted, please mount"); free(debug_str); return NULL; } @@ -165,6 +159,50 @@ __hidden char *trace_find_tracing_dir(bool debugfs) return tracing_dir; } +/** + * tracefs_tracing_dir_is_mounted - test if the tracing dir is already mounted + * @mount: Mount it if it is not already mounted + * @path: the path to the tracing directory if mounted or was mounted + * + * Returns 1 if the tracing directory is already mounted and 0 if it is not. + * If @mount is set and it fails to mount, it returns -1. + * + * If path is not NULL, and the tracing directory is or was mounted, it holds + * the path to the tracing directory. It must not be freed. + */ +int tracefs_tracing_dir_is_mounted(bool mount, const char **path) +{ + const char *dir; + + dir = find_tracing_dir(false, false); + if (dir) { + if (path) + *path = dir; + return 1; + } + if (!mount) + return 0; + + dir = find_tracing_dir(false, mount); + if (!dir) + return -1; + if (path) + *path = dir; + return 0; +} + +/** + * trace_find_tracing_dir - Find tracing directory + * @debugfs: Boolean to just return the debugfs directory + * + * Returns string containing the full path to the system's tracing directory. + * The string must be freed by free() + */ +__hidden char *trace_find_tracing_dir(bool debugfs) +{ + return find_tracing_dir(debugfs, false); +} + /** * tracefs_set_tracing_dir - Set location of the tracing directory * @tracing_dir: full path to the system's tracing directory mount point.