[v1,04/32] eal/trace: implement trace operation APIs
diff mbox series

Message ID 20200318190241.3150971-5-jerinj@marvell.com
State Superseded, archived
Delegated to: Thomas Monjalon
Headers show
Series
  • DPDK Trace support
Related show

Checks

Context Check Description
ci/Intel-compilation fail Compilation issues
ci/checkpatch success coding style OK

Commit Message

Jerin Jacob Kollanukkaran March 18, 2020, 7:02 p.m. UTC
From: Jerin Jacob <jerinj@marvell.com>

This patch implements the following public trace APIs.

- rte_trace_global_is_enabled()
- rte_trace_global_is_disabled()
- rte_trace_is_id_invalid()
- rte_trace_global_level_set()
- rte_trace_global_level_get()
- rte_trace_global_mode_set()
- rte_trace_global_mode_get()
- rte_trace_enable()
- rte_trace_disable()
- rte_trace_is_enabled()
- rte_trace_is_disabled()
- rte_trace_level_set()
- rte_trace_level_get()
- rte_trace_mode_set()
- rte_trace_mode_get()
- rte_trace_pattern()
- rte_trace_regexp()
- rte_trace_from_name()

Signed-off-by: Jerin Jacob <jerinj@marvell.com>
Signed-off-by: Sunil Kumar Kori <skori@marvell.com>
---
 lib/librte_eal/common/eal_common_trace.c | 249 +++++++++++++++++++++++
 lib/librte_eal/common/eal_trace.h        |  11 +
 lib/librte_eal/rte_eal_version.map       |  18 ++
 3 files changed, 278 insertions(+)

Comments

Mattias Rönnblom March 19, 2020, 10:16 a.m. UTC | #1
On 2020-03-18 20:02, jerinj@marvell.com wrote:
> From: Jerin Jacob <jerinj@marvell.com>
>
> This patch implements the following public trace APIs.
>
> - rte_trace_global_is_enabled()
> - rte_trace_global_is_disabled()
> - rte_trace_is_id_invalid()
> - rte_trace_global_level_set()
> - rte_trace_global_level_get()
> - rte_trace_global_mode_set()
> - rte_trace_global_mode_get()
> - rte_trace_enable()
> - rte_trace_disable()
> - rte_trace_is_enabled()
> - rte_trace_is_disabled()
> - rte_trace_level_set()
> - rte_trace_level_get()
> - rte_trace_mode_set()
> - rte_trace_mode_get()
> - rte_trace_pattern()
> - rte_trace_regexp()
> - rte_trace_from_name()
>
> Signed-off-by: Jerin Jacob <jerinj@marvell.com>
> Signed-off-by: Sunil Kumar Kori <skori@marvell.com>
> ---
>   lib/librte_eal/common/eal_common_trace.c | 249 +++++++++++++++++++++++
>   lib/librte_eal/common/eal_trace.h        |  11 +
>   lib/librte_eal/rte_eal_version.map       |  18 ++
>   3 files changed, 278 insertions(+)
>
> diff --git a/lib/librte_eal/common/eal_common_trace.c b/lib/librte_eal/common/eal_common_trace.c
> index ddde04de5..f8855e09b 100644
> --- a/lib/librte_eal/common/eal_common_trace.c
> +++ b/lib/librte_eal/common/eal_common_trace.c
> @@ -3,7 +3,9 @@
>    */
>   
>   #include <inttypes.h>
> +#include <fnmatch.h>
>   #include <sys/queue.h>
> +#include <regex.h>
>   
>   #include <rte_common.h>
>   #include <rte_errno.h>
> @@ -20,6 +22,253 @@ RTE_DEFINE_PER_LCORE(int, ctf_count);
>   static struct trace_point_head tp_list = STAILQ_HEAD_INITIALIZER(tp_list);
>   static struct trace trace;
>   
> +bool
> +rte_trace_global_is_enabled(void)
> +{
> +	return trace.global_status;
> +}
> +
> +bool
> +rte_trace_global_is_disabled(void)
> +{
> +	return !trace.global_status;
> +}
> +
> +void
> +rte_trace_global_level_set(uint32_t level)
> +{
> +	struct trace_point *tp;
> +
> +	trace.level = level;
> +
> +	if (rte_trace_global_is_disabled())
> +		return;
> +
> +	STAILQ_FOREACH(tp, &tp_list, next) {
> +		if (level >= rte_trace_level_get(tp->handle))
> +			rte_trace_enable(tp->handle);
> +		else
> +			rte_trace_disable(tp->handle);
> +	}
> +}
> +
> +uint32_t
> +rte_trace_global_level_get(void)
> +{
> +	return trace.level;
> +}
> +
> +void
> +rte_trace_global_mode_set(enum rte_trace_mode_e mode)
> +{
> +	struct trace_point *tp;
> +
> +	if (rte_trace_global_is_disabled())
> +		return;
> +
> +	STAILQ_FOREACH(tp, &tp_list, next)
> +		rte_trace_mode_set(tp->handle, mode);
> +
> +	trace.mode = mode;
> +}
> +
> +enum
> +rte_trace_mode_e rte_trace_global_mode_get(void)
> +{
> +	return trace.mode;
> +}
> +
> +bool
> +rte_trace_is_id_invalid(rte_trace_t t)
> +{
> +	if (trace_id_get(t) >= trace.nb_trace_points)
> +		return true;
> +
> +	return false;
> +}
> +
> +bool
> +rte_trace_is_enabled(rte_trace_t trace)
> +{
> +	uint64_t val;
> +
> +	if (rte_trace_is_id_invalid(trace))
> +		return false;
> +
> +	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
> +	return !!(val & __RTE_TRACE_FIELD_ENABLE_MASK);
The function has a bool return type, so no need to do a double negation.
> +}
> +
> +bool
> +rte_trace_is_disabled(rte_trace_t trace)
> +{
> +	uint64_t val;
> +
> +	if (rte_trace_is_id_invalid(trace))
> +		return true;
> +
> +	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
> +	return !!!(val & __RTE_TRACE_FIELD_ENABLE_MASK);
> +}
> +
Same here.
> +int
> +rte_trace_enable(rte_trace_t trace)
> +{
> +	if (rte_trace_is_id_invalid(trace))
> +		return -ERANGE;
> +
> +	if (rte_trace_level_get(trace) > rte_trace_global_level_get())
> +		return -EACCES;
> +
> +	__atomic_or_fetch(trace, __RTE_TRACE_FIELD_ENABLE_MASK,
> +			  __ATOMIC_RELEASE);
> +	return 0;
> +}
> +
> +int
> +rte_trace_disable(rte_trace_t trace)
> +{
> +	if (rte_trace_is_id_invalid(trace))
> +		return -ERANGE;
> +
> +	__atomic_and_fetch(trace, ~__RTE_TRACE_FIELD_ENABLE_MASK,
> +			   __ATOMIC_RELEASE);
> +	return 0;
> +}
> +
> +uint32_t
> +rte_trace_level_get(rte_trace_t trace)
> +{
> +	uint64_t val;
> +
> +	if (rte_trace_is_id_invalid(trace))
> +		return 0;
> +
> +	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
> +	val &= __RTE_TRACE_FIELD_LEVEL_MASK;
> +	val = val >> __RTE_TRACE_FIELD_LEVEL_SHIFT;
> +
> +	return val;
> +}
> +
> +int
> +rte_trace_level_set(rte_trace_t trace, uint32_t level)
> +{
> +	uint64_t val;
> +
> +	if (rte_trace_is_id_invalid(trace) || level > RTE_LOG_DEBUG)
> +		return -EINVAL;
> +
> +	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
> +	val &= ~__RTE_TRACE_FIELD_LEVEL_MASK;
> +	val |= (uint64_t)level << __RTE_TRACE_FIELD_LEVEL_SHIFT;
> +	__atomic_store_n(trace, val, __ATOMIC_RELEASE);
> +
> +	if (level <= rte_trace_global_level_get())
> +		rte_trace_enable(trace);
> +	else
> +		rte_trace_disable(trace);
> +
> +	return 0;
> +}
> +
> +int
> +rte_trace_mode_get(rte_trace_t trace)
> +{
> +	uint64_t val;
> +
> +	if (rte_trace_is_id_invalid(trace))
> +		return -EINVAL;
> +
> +	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
> +
> +	return !!(val & __RTE_TRACE_FIELD_ENABLE_DISCARD);
> +}
> +
> +int
> +rte_trace_mode_set(rte_trace_t trace, enum rte_trace_mode_e mode)
> +{
> +	if (rte_trace_is_id_invalid(trace) || mode >  RTE_TRACE_MODE_DISCARD)
> +		return -EINVAL;
> +
> +	if (mode == RTE_TRACE_MODE_OVERWRITE)
> +		__atomic_and_fetch(trace, ~__RTE_TRACE_FIELD_ENABLE_DISCARD,
> +				   __ATOMIC_RELEASE);
> +	else
> +		__atomic_or_fetch(trace, __RTE_TRACE_FIELD_ENABLE_DISCARD,
> +				   __ATOMIC_RELEASE);
> +
> +	return 0;
> +}
> +
> +int
> +rte_trace_pattern(const char *pattern, bool enable, bool *found)
> +{
> +	struct trace_point *tp;
> +	int rc;
> +
> +	if (found)
> +		*found = false;
> +
> +	STAILQ_FOREACH(tp, &tp_list, next) {
> +		if (fnmatch(pattern, tp->name, 0) == 0) {
> +			if (found)
> +				*found = true;
> +			if (enable)
> +				rc = rte_trace_enable(tp->handle);
> +			else
> +				rc = rte_trace_disable(tp->handle);
> +		}
> +		if (rc < 0)
> +			return rc;
> +	}
> +	return 0;
> +}
> +
> +int
> +rte_trace_regexp(const char *regex, bool enable, bool *found)
> +{
> +	struct trace_point *tp;
> +	regex_t r;
> +	int rc;
> +
> +	if (regcomp(&r, regex, 0) != 0)
> +		return -EINVAL;
> +
> +	if (found)
> +		*found = false;
> +
> +	STAILQ_FOREACH(tp, &tp_list, next) {
> +		if (regexec(&r, tp->name, 0, NULL, 0) == 0) {
> +			if (found)
> +				*found = true;
> +			if (enable)
> +				rc = rte_trace_enable(tp->handle);
> +			else
> +				rc = rte_trace_disable(tp->handle);
> +		}
> +		if (rc < 0)
> +			return rc;
In case of a non-match, rc may not be initialized.
> +	}
> +	regfree(&r);
> +	return 0;
> +}
> +
> +rte_trace_t
> +rte_trace_from_name(const char *name)
> +{
> +	struct trace_point *tp;
> +
> +	if (name == NULL)
> +		return NULL;
> +
> +	STAILQ_FOREACH(tp, &tp_list, next)
> +		if (strncmp(tp->name, name, TRACE_POINT_NAME_SIZE) == 0)
> +			return tp->handle;
> +
> +	return NULL;
> +}
> +
>   int
>   __rte_trace_point_register(rte_trace_t handle, const char *name, uint32_t level,
>   			 void (*fn)(void))
> diff --git a/lib/librte_eal/common/eal_trace.h b/lib/librte_eal/common/eal_trace.h
> index 9aef536a0..9f90a5d17 100644
> --- a/lib/librte_eal/common/eal_trace.h
> +++ b/lib/librte_eal/common/eal_trace.h
> @@ -27,9 +27,20 @@ struct trace_point {
>   
>   struct trace {
>   	int register_errno;
> +	bool global_status;
> +	enum rte_trace_mode_e mode;
> +	uint32_t level;
>   	uint32_t nb_trace_points;
>   };
>   
> +/* Helper functions */
> +static inline uint16_t
> +trace_id_get(rte_trace_t trace)
> +{
> +	return (*trace & __RTE_TRACE_FIELD_ID_MASK) >>
> +		__RTE_TRACE_FIELD_ID_SHIFT;
> +}
> +
>   /* Trace point list functions */
>   STAILQ_HEAD(trace_point_head, trace_point);
>   
> diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
> index d97d14845..841ebc5f9 100644
> --- a/lib/librte_eal/rte_eal_version.map
> +++ b/lib/librte_eal/rte_eal_version.map
> @@ -339,4 +339,22 @@ EXPERIMENTAL {
>   	# added in 20.05
>   	rte_thread_getname;
>   	__rte_trace_point_register;
> +	rte_trace_global_is_enabled;
> +	rte_trace_global_is_disabled;
> +	rte_trace_is_id_invalid;
> +	rte_trace_global_level_set;
> +	rte_trace_global_level_get;
> +	rte_trace_global_mode_set;
> +	rte_trace_global_mode_get;
> +	rte_trace_enable;
> +	rte_trace_disable;
> +	rte_trace_is_enabled;
> +	rte_trace_is_disabled;
> +	rte_trace_level_set;
> +	rte_trace_level_get;
> +	rte_trace_mode_set;
> +	rte_trace_mode_get;
> +	rte_trace_pattern;
> +	rte_trace_regexp;
> +	rte_trace_from_name;
>   };
Jerin Jacob March 23, 2020, 12:23 p.m. UTC | #2
On Thu, Mar 19, 2020 at 3:46 PM Mattias Rönnblom
<mattias.ronnblom@ericsson.com> wrote:
>
> On 2020-03-18 20:02, jerinj@marvell.com wrote:
> > From: Jerin Jacob <jerinj@marvell.com>
> >
> > This patch implements the following public trace APIs.

All the comments in this patch will be fixed in v2.

Patch
diff mbox series

diff --git a/lib/librte_eal/common/eal_common_trace.c b/lib/librte_eal/common/eal_common_trace.c
index ddde04de5..f8855e09b 100644
--- a/lib/librte_eal/common/eal_common_trace.c
+++ b/lib/librte_eal/common/eal_common_trace.c
@@ -3,7 +3,9 @@ 
  */
 
 #include <inttypes.h>
+#include <fnmatch.h>
 #include <sys/queue.h>
+#include <regex.h>
 
 #include <rte_common.h>
 #include <rte_errno.h>
@@ -20,6 +22,253 @@  RTE_DEFINE_PER_LCORE(int, ctf_count);
 static struct trace_point_head tp_list = STAILQ_HEAD_INITIALIZER(tp_list);
 static struct trace trace;
 
+bool
+rte_trace_global_is_enabled(void)
+{
+	return trace.global_status;
+}
+
+bool
+rte_trace_global_is_disabled(void)
+{
+	return !trace.global_status;
+}
+
+void
+rte_trace_global_level_set(uint32_t level)
+{
+	struct trace_point *tp;
+
+	trace.level = level;
+
+	if (rte_trace_global_is_disabled())
+		return;
+
+	STAILQ_FOREACH(tp, &tp_list, next) {
+		if (level >= rte_trace_level_get(tp->handle))
+			rte_trace_enable(tp->handle);
+		else
+			rte_trace_disable(tp->handle);
+	}
+}
+
+uint32_t
+rte_trace_global_level_get(void)
+{
+	return trace.level;
+}
+
+void
+rte_trace_global_mode_set(enum rte_trace_mode_e mode)
+{
+	struct trace_point *tp;
+
+	if (rte_trace_global_is_disabled())
+		return;
+
+	STAILQ_FOREACH(tp, &tp_list, next)
+		rte_trace_mode_set(tp->handle, mode);
+
+	trace.mode = mode;
+}
+
+enum
+rte_trace_mode_e rte_trace_global_mode_get(void)
+{
+	return trace.mode;
+}
+
+bool
+rte_trace_is_id_invalid(rte_trace_t t)
+{
+	if (trace_id_get(t) >= trace.nb_trace_points)
+		return true;
+
+	return false;
+}
+
+bool
+rte_trace_is_enabled(rte_trace_t trace)
+{
+	uint64_t val;
+
+	if (rte_trace_is_id_invalid(trace))
+		return false;
+
+	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
+	return !!(val & __RTE_TRACE_FIELD_ENABLE_MASK);
+}
+
+bool
+rte_trace_is_disabled(rte_trace_t trace)
+{
+	uint64_t val;
+
+	if (rte_trace_is_id_invalid(trace))
+		return true;
+
+	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
+	return !!!(val & __RTE_TRACE_FIELD_ENABLE_MASK);
+}
+
+int
+rte_trace_enable(rte_trace_t trace)
+{
+	if (rte_trace_is_id_invalid(trace))
+		return -ERANGE;
+
+	if (rte_trace_level_get(trace) > rte_trace_global_level_get())
+		return -EACCES;
+
+	__atomic_or_fetch(trace, __RTE_TRACE_FIELD_ENABLE_MASK,
+			  __ATOMIC_RELEASE);
+	return 0;
+}
+
+int
+rte_trace_disable(rte_trace_t trace)
+{
+	if (rte_trace_is_id_invalid(trace))
+		return -ERANGE;
+
+	__atomic_and_fetch(trace, ~__RTE_TRACE_FIELD_ENABLE_MASK,
+			   __ATOMIC_RELEASE);
+	return 0;
+}
+
+uint32_t
+rte_trace_level_get(rte_trace_t trace)
+{
+	uint64_t val;
+
+	if (rte_trace_is_id_invalid(trace))
+		return 0;
+
+	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
+	val &= __RTE_TRACE_FIELD_LEVEL_MASK;
+	val = val >> __RTE_TRACE_FIELD_LEVEL_SHIFT;
+
+	return val;
+}
+
+int
+rte_trace_level_set(rte_trace_t trace, uint32_t level)
+{
+	uint64_t val;
+
+	if (rte_trace_is_id_invalid(trace) || level > RTE_LOG_DEBUG)
+		return -EINVAL;
+
+	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
+	val &= ~__RTE_TRACE_FIELD_LEVEL_MASK;
+	val |= (uint64_t)level << __RTE_TRACE_FIELD_LEVEL_SHIFT;
+	__atomic_store_n(trace, val, __ATOMIC_RELEASE);
+
+	if (level <= rte_trace_global_level_get())
+		rte_trace_enable(trace);
+	else
+		rte_trace_disable(trace);
+
+	return 0;
+}
+
+int
+rte_trace_mode_get(rte_trace_t trace)
+{
+	uint64_t val;
+
+	if (rte_trace_is_id_invalid(trace))
+		return -EINVAL;
+
+	val = __atomic_load_n(trace, __ATOMIC_ACQUIRE);
+
+	return !!(val & __RTE_TRACE_FIELD_ENABLE_DISCARD);
+}
+
+int
+rte_trace_mode_set(rte_trace_t trace, enum rte_trace_mode_e mode)
+{
+	if (rte_trace_is_id_invalid(trace) || mode >  RTE_TRACE_MODE_DISCARD)
+		return -EINVAL;
+
+	if (mode == RTE_TRACE_MODE_OVERWRITE)
+		__atomic_and_fetch(trace, ~__RTE_TRACE_FIELD_ENABLE_DISCARD,
+				   __ATOMIC_RELEASE);
+	else
+		__atomic_or_fetch(trace, __RTE_TRACE_FIELD_ENABLE_DISCARD,
+				   __ATOMIC_RELEASE);
+
+	return 0;
+}
+
+int
+rte_trace_pattern(const char *pattern, bool enable, bool *found)
+{
+	struct trace_point *tp;
+	int rc;
+
+	if (found)
+		*found = false;
+
+	STAILQ_FOREACH(tp, &tp_list, next) {
+		if (fnmatch(pattern, tp->name, 0) == 0) {
+			if (found)
+				*found = true;
+			if (enable)
+				rc = rte_trace_enable(tp->handle);
+			else
+				rc = rte_trace_disable(tp->handle);
+		}
+		if (rc < 0)
+			return rc;
+	}
+	return 0;
+}
+
+int
+rte_trace_regexp(const char *regex, bool enable, bool *found)
+{
+	struct trace_point *tp;
+	regex_t r;
+	int rc;
+
+	if (regcomp(&r, regex, 0) != 0)
+		return -EINVAL;
+
+	if (found)
+		*found = false;
+
+	STAILQ_FOREACH(tp, &tp_list, next) {
+		if (regexec(&r, tp->name, 0, NULL, 0) == 0) {
+			if (found)
+				*found = true;
+			if (enable)
+				rc = rte_trace_enable(tp->handle);
+			else
+				rc = rte_trace_disable(tp->handle);
+		}
+		if (rc < 0)
+			return rc;
+	}
+	regfree(&r);
+	return 0;
+}
+
+rte_trace_t
+rte_trace_from_name(const char *name)
+{
+	struct trace_point *tp;
+
+	if (name == NULL)
+		return NULL;
+
+	STAILQ_FOREACH(tp, &tp_list, next)
+		if (strncmp(tp->name, name, TRACE_POINT_NAME_SIZE) == 0)
+			return tp->handle;
+
+	return NULL;
+}
+
 int
 __rte_trace_point_register(rte_trace_t handle, const char *name, uint32_t level,
 			 void (*fn)(void))
diff --git a/lib/librte_eal/common/eal_trace.h b/lib/librte_eal/common/eal_trace.h
index 9aef536a0..9f90a5d17 100644
--- a/lib/librte_eal/common/eal_trace.h
+++ b/lib/librte_eal/common/eal_trace.h
@@ -27,9 +27,20 @@  struct trace_point {
 
 struct trace {
 	int register_errno;
+	bool global_status;
+	enum rte_trace_mode_e mode;
+	uint32_t level;
 	uint32_t nb_trace_points;
 };
 
+/* Helper functions */
+static inline uint16_t
+trace_id_get(rte_trace_t trace)
+{
+	return (*trace & __RTE_TRACE_FIELD_ID_MASK) >>
+		__RTE_TRACE_FIELD_ID_SHIFT;
+}
+
 /* Trace point list functions */
 STAILQ_HEAD(trace_point_head, trace_point);
 
diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
index d97d14845..841ebc5f9 100644
--- a/lib/librte_eal/rte_eal_version.map
+++ b/lib/librte_eal/rte_eal_version.map
@@ -339,4 +339,22 @@  EXPERIMENTAL {
 	# added in 20.05
 	rte_thread_getname;
 	__rte_trace_point_register;
+	rte_trace_global_is_enabled;
+	rte_trace_global_is_disabled;
+	rte_trace_is_id_invalid;
+	rte_trace_global_level_set;
+	rte_trace_global_level_get;
+	rte_trace_global_mode_set;
+	rte_trace_global_mode_get;
+	rte_trace_enable;
+	rte_trace_disable;
+	rte_trace_is_enabled;
+	rte_trace_is_disabled;
+	rte_trace_level_set;
+	rte_trace_level_get;
+	rte_trace_mode_set;
+	rte_trace_mode_get;
+	rte_trace_pattern;
+	rte_trace_regexp;
+	rte_trace_from_name;
 };