All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Subject: [PATCH 10/23] libtracefs: Add tracefs_instance_file_write_number()
Date: Thu, 28 Dec 2023 15:35:32 -0500	[thread overview]
Message-ID: <20231228203714.53294-11-rostedt@goodmis.org> (raw)
In-Reply-To: <20231228203714.53294-1-rostedt@goodmis.org>

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Add the helper function to write a number into a tracefs file. As tracefs
files require writing strings and not binary numbers, this is a helper
function that does the conversion to string followed by a
tracefs_instance_file_write() on that string.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 .../libtracefs-instances-file-manip.txt       |  9 +++++-
 Documentation/libtracefs.txt                  |  1 +
 include/tracefs.h                             |  2 ++
 src/tracefs-instance.c                        | 21 +++++++++++++
 utest/tracefs-utest.c                         | 30 ++++++++++++++++++-
 5 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/Documentation/libtracefs-instances-file-manip.txt b/Documentation/libtracefs-instances-file-manip.txt
index 8c04240523d3..bb1b36e36c3e 100644
--- a/Documentation/libtracefs-instances-file-manip.txt
+++ b/Documentation/libtracefs-instances-file-manip.txt
@@ -5,7 +5,7 @@ NAME
 ----
 
 tracefs_instance_file_open,
-tracefs_instance_file_write, tracefs_instance_file_append, tracefs_instance_file_clear,
+tracefs_instance_file_write, tracefs_instance_file_write_number, tracefs_instance_file_append, tracefs_instance_file_clear,
 tracefs_instance_file_read, tracefs_instance_file_read_number - Work with files in tracing instances.
 
 SYNOPSIS
@@ -16,6 +16,7 @@ SYNOPSIS
 
 int *tracefs_instance_file_open*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int _mode_);
 int *tracefs_instance_file_write*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_);
+int *tracefs_instance_file_write_number*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, size_t _val_);
 int *tracefs_instance_file_append*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_);
 int *tracefs_instance_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_);
 char pass:[*]*tracefs_instance_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int pass:[*]_psize_);
@@ -38,6 +39,10 @@ The *tracefs_instance_file_write()* function writes a string _str_ in a _file_ f
 the given _instance_, without the terminating NULL character. When opening the file, this function
 tries to truncates the size of the file to zero, which clears all previously existing settings.
 
+The *tracefs_instance_file_write_number()* function converts _val_ into a string
+and then writes it to the given file. This is a helper function that does the number
+conversion to string and then calls *tracefs_instance_file_write()*.
+
 The *tracefs_instance_file_append()* function writes a string _str_ in a _file_ from
 the given _instance_, without the terminating NULL character.  This function is similar to
 *tracefs_instance_file_write()*, but the existing content of the is not cleared. Thus the
@@ -61,6 +66,8 @@ closed with *close*(3). In case of an error, -1 is returned.
 The *tracefs_instance_file_write()* function returns the number of written bytes,
 or -1 in case of an error.
 
+The *tracefs_instance_file_write_number()* function returns 0 on success and -1 on error.
+
 The *tracefs_instance_file_append()* function returns the number of written bytes,
 or -1 in case of an error.
 
diff --git a/Documentation/libtracefs.txt b/Documentation/libtracefs.txt
index 850118302ab2..11ff576e826c 100644
--- a/Documentation/libtracefs.txt
+++ b/Documentation/libtracefs.txt
@@ -33,6 +33,7 @@ Trace instances:
 	char pass:[*]*tracefs_instance_get_dir*(struct tracefs_instance pass:[*]_instance_);
 	int *tracefs_instance_file_open*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int _mode_);
 	int *tracefs_instance_file_write*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_);
+	int *tracefs_instance_file_write_number*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, size_t _val_);
 	int *tracefs_instance_file_append*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_);
 	int *tracefs_instance_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_);
 	char pass:[*]*tracefs_instance_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int pass:[*]_psize_);
diff --git a/include/tracefs.h b/include/tracefs.h
index 25429a30c028..0971d54dd7c7 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -36,6 +36,8 @@ tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);
 char *tracefs_instance_get_dir(struct tracefs_instance *instance);
 int tracefs_instance_file_write(struct tracefs_instance *instance,
 				const char *file, const char *str);
+int tracefs_instance_file_write_number(struct tracefs_instance *instance,
+				       const char *file, size_t val);
 int tracefs_instance_file_append(struct tracefs_instance *instance,
 				 const char *file, const char *str);
 int tracefs_instance_file_clear(struct tracefs_instance *instance,
diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c
index 2efcc75dfd98..b019836333a3 100644
--- a/src/tracefs-instance.c
+++ b/src/tracefs-instance.c
@@ -493,6 +493,27 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,
 	return instance_file_write(instance, file, str, O_WRONLY | O_TRUNC);
 }
 
+/**
+ * tracefs_instance_file_write_number - Write integer from a trace file.
+ * @instance: ftrace instance, can be NULL for the top instance
+ * @file: name of the file
+ * @res: The integer to write to @file
+ *
+ * Returns 0 if the write succeeds, -1 on error.
+ */
+int tracefs_instance_file_write_number(struct tracefs_instance *instance,
+				       const char *file, size_t val)
+{
+	char buf[64];
+	int ret;
+
+	snprintf(buf, 64, "%zd\n", val);
+
+	ret = tracefs_instance_file_write(instance, file, buf);
+
+	return ret > 1 ? 0 : -1;
+}
+
 /**
  * tracefs_instance_file_append - Append to a trace file of specific instance.
  * @instance: ftrace instance, can be NULL for the top instance.
diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 492e5c05551f..98cfd322b171 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -488,7 +488,7 @@ static int setup_trace_cpu(struct tracefs_instance *instance, struct test_cpu_da
 	if (!data->buf)
 		goto fail;
 
-	data->kbuf = kbuffer_alloc(sizeof(long) == 8, !tep_is_bigendian());
+	data->kbuf = tep_kbuffer(data->tep);
 	CU_TEST(data->kbuf != NULL);
 	if (!data->kbuf)
 		goto fail;
@@ -2127,6 +2127,7 @@ static void test_instance_file_fd(struct tracefs_instance *instance)
 	const char *name = get_rand_str();
 	const char *tdir = tracefs_instance_get_trace_dir(instance);
 	long long res = -1;
+	long long res2;
 	char rd[2];
 	int fd;
 
@@ -2146,7 +2147,34 @@ static void test_instance_file_fd(struct tracefs_instance *instance)
 	CU_TEST(read(fd, &rd, 1) == 1);
 	rd[1] = 0;
 	CU_TEST(res == atoi(rd));
+	close(fd);
+
+	/* Inverse tracing_on and test changing it with write_number */
+	res ^= 1;
 
+	CU_TEST(tracefs_instance_file_write_number(instance, TRACE_ON, (size_t)res) == 0);
+
+	CU_TEST(tracefs_instance_file_read_number(instance, TRACE_ON, &res2) == 0);
+	CU_TEST(res2 == res);
+	fd = tracefs_instance_file_open(instance, TRACE_ON, O_RDONLY);
+	CU_TEST(fd >= 0);
+	CU_TEST(read(fd, &rd, 1) == 1);
+	rd[1] = 0;
+	CU_TEST(res2 == atoi(rd));
+	close(fd);
+
+	/* Put back the result of tracing_on */
+	res ^= 1;
+
+	CU_TEST(tracefs_instance_file_write_number(instance, TRACE_ON, (size_t)res) == 0);
+
+	CU_TEST(tracefs_instance_file_read_number(instance, TRACE_ON, &res2) == 0);
+	CU_TEST(res2 == res);
+	fd = tracefs_instance_file_open(instance, TRACE_ON, O_RDONLY);
+	CU_TEST(fd >= 0);
+	CU_TEST(read(fd, &rd, 1) == 1);
+	rd[1] = 0;
+	CU_TEST(res2 == atoi(rd));
 	close(fd);
 }
 
-- 
2.42.0


  parent reply	other threads:[~2023-12-28 20:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-28 20:35 [PATCH 00/23] libtracefs: Several updates Steven Rostedt
2023-12-28 20:35 ` [PATCH 01/23] libtracefs Documentation: Fix tracefs_event_file_exists() issues Steven Rostedt
2023-12-28 20:35 ` [PATCH 02/23] libtracefs testing: Use one tep handle for most tests Steven Rostedt
2023-12-28 20:35 ` [PATCH 03/23] libtracefs: Free "missed_followers" of instance Steven Rostedt
2023-12-28 20:35 ` [PATCH 04/23] libtracefs: Free buf in clear_func_filter() Steven Rostedt
2023-12-28 20:35 ` [PATCH 05/23] libtracefs: Free tracing_dir in case of remount Steven Rostedt
2023-12-28 20:35 ` [PATCH 06/23] libtracefs: Free dynamic event list in utest Steven Rostedt
2023-12-28 20:35 ` [PATCH 07/23] libtracefs: Reset tracing before and after unit tests Steven Rostedt
2023-12-28 20:35 ` [PATCH 08/23] libtracefs: Add API to remove followers from an instance or toplevel Steven Rostedt
2023-12-28 20:35 ` [PATCH 09/23] libtracefs: Increase splice to use pipe max size Steven Rostedt
2023-12-28 20:35 ` Steven Rostedt [this message]
2023-12-28 20:35 ` [PATCH 11/23] libtracefs: Add API to read tracefs_cpu and return a kbuffer Steven Rostedt
2023-12-28 20:35 ` [PATCH 12/23] libtracefs: Add tracefs_instance_get/set_buffer_percent() Steven Rostedt
2023-12-28 20:35 ` [PATCH 13/23] libtracefs: Add tracefs_instance_clear() API Steven Rostedt
2023-12-28 20:35 ` [PATCH 14/23] libtracefs utest: Add test to test tracefs_instance_set/get_buffer_percent() Steven Rostedt
2023-12-28 20:35 ` [PATCH 15/23] libtracefs: Add kerneldoc comments to tracefs_instance_set_buffer_size() Steven Rostedt
2023-12-28 20:35 ` [PATCH 16/23] libtracefs: Add tracefs_load_headers() API Steven Rostedt
2023-12-28 20:35 ` [PATCH 17/23] libtracefs: Add API to extract ring buffer statistics Steven Rostedt
2023-12-28 20:35 ` [PATCH 18/23] libtracefs: Add tracefs_instance_set/get_subbuf_size() Steven Rostedt
2023-12-28 20:35 ` [PATCH 19/23] libtracefs: Add ring buffer memory mapping APIs Steven Rostedt
2023-12-28 20:35 ` [PATCH 20/23] libtracefs: Add TIMESTAMP_USECS_DELTA to simplify SQL timestamp compares Steven Rostedt
2023-12-28 20:35 ` [PATCH 21/23] libtracefs: Also clear max_graph_depth on reset Steven Rostedt
2023-12-28 20:35 ` [PATCH 22/23] libtracefs: Add PID filtering API Steven Rostedt
2023-12-28 20:35 ` [PATCH 23/23] libtracefs: Add updating and reading snapshot buffers Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231228203714.53294-11-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.