]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
tracing: Do not calculate strlen() twice for __string() fields
authorSteven Rostedt (Google) <rostedt@goodmis.org>
Thu, 22 Feb 2024 21:14:17 +0000 (16:14 -0500)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Mon, 18 Mar 2024 14:32:57 +0000 (10:32 -0400)
The TRACE_EVENT() macro handles dynamic strings by having:

  TP_PROTO(struct some_struct *s),
  TP_ARGS(s),
  TP_STRUCT__entry(
        __string(my_string, s->string)
 ),
 TP_fast_assign(
        __assign_str(my_string, s->string);
 )
 TP_printk("%s", __get_str(my_string))

There's even some code that may call a function helper to find the
s->string value. The problem with the above is that the work to get the
s->string is done twice. Once at the __string() and again in the
__assign_str().

The length of the string is calculated via a strlen(), not once, but
twice. Once during the __string() macro and again in __assign_str(). But
the length is actually already recorded in the data location and here's no
reason to call strlen() again.

Just use the saved length that was saved in the __string() code for the
__assign_str() code.

Link: https://lore.kernel.org/linux-trace-kernel/20240222211442.793074999@goodmis.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
include/trace/stages/stage6_event_callback.h

index b3e2f321e78788a1875a00c35cad20e24ea0c209..c0e5d097324e7436fbf97e1cb9626a798b43d365 100644 (file)
@@ -32,8 +32,9 @@
 
 #undef __assign_str
 #define __assign_str(dst, src)                                         \
-       strcpy(__get_str(dst), __data_offsets.dst##_ptr_ ?              \
-              __data_offsets.dst##_ptr_ : "(null)")
+       memcpy(__get_str(dst), __data_offsets.dst##_ptr_ ?              \
+              __data_offsets.dst##_ptr_ : "(null)",                    \
+              __get_dynamic_array_len(dst))
 
 #undef __assign_str_len
 #define __assign_str_len(dst, src, len)                                        \
@@ -94,8 +95,9 @@
 
 #undef __assign_rel_str
 #define __assign_rel_str(dst, src)                                     \
-       strcpy(__get_rel_str(dst), __data_offsets.dst##_ptr_ ?          \
-              __data_offsets.dst##_ptr_ : "(null)")
+       memcpy(__get_rel_str(dst), __data_offsets.dst##_ptr_ ?          \
+              __data_offsets.dst##_ptr_ : "(null)",                    \
+              __get_rel_dynamic_array_len(dst))
 
 #undef __assign_rel_str_len
 #define __assign_rel_str_len(dst, src, len)                            \