Skip to contentSkip to conversation

libtraceeval: Add warning messages

PATCH New Steven Rostedt 1 participant 3 messages Oct 10, 2023

lore
Share & tools
lore
#0 lore raw

cover libtraceeval: Add warning messages

From: "Steven Rostedt (Google)" <[email protected]>

Change the output to use traceeval_vwarning() and create a log level like libtraceevent and libtracefs have. This lets the application run with warnings enabled when developing, and be able to turn them off for production.

Steven Rostedt (Google) (2):
libtraceeval: Add traceeval_vwaring() API
libtraceeval: Have various errors print warnings

 include/traceeval.h |  11 +++
 src/delta.c         |  40 +++++++--
 src/eval-local.h    |   9 ++
 src/histograms.c    | 200 ++++++++++++++++++++++++++++++++++++--------
 4 files changed, 215 insertions(+), 45 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/2 libtraceeval: Add traceeval_vwaring() API

From: "Steven Rostedt (Google)" <[email protected]>

Add a way to tell the library to print any errors. As passing in wrong size keys and such can cause errors, having a way to tell the library to print output when that happens is useful.

Add a traceeval_set_log_level() and define the following log levels:

TEVAL_NONE - Do not print anything
TEVAL_CRIT - Print anything that is considered a critical error TEVAL_WARN - Print warnings (this includes bad array sizes) TEVAL_INFO - Print any message that might be interesting

---
 include/traceeval.h | 11 +++++++++++
 src/eval-local.h    |  5 +++++
 src/histograms.c    | 48 ++++++++++++++++++++++++++++++++++-----------
 3 files changed, 53 insertions(+), 11 deletions(-)
include/traceeval.h +11 -0
index 2b4082e8dfcb..55d6df67375a 100644
@@ -10,6 +10,7 @@
 
 #include <stdlib.h>
 #include <stddef.h>
+#include <stdarg.h>
 #include <stdbool.h>
 
 /* Data definition interfaces */
@@ -62,6 +63,16 @@ enum traceeval_flags {
 	TRACEEVAL_FL_STAT		= (1 << 4),
 };
 
+enum traceeval_log_level {
+	TEVAL_NONE		= 0,
+	TEVAL_CRIT,
+	TEVAL_WARN,
+	TEVAL_INFO,
+};
+
+extern void traceeval_vwarning(const char *fmt, va_list ap);
+extern void traceeval_set_log_level(enum traceeval_log_level level);
+
 struct traceeval_data_delta {
 	unsigned long long			delta;
 	unsigned long long			timestamp;
src/eval-local.h +5 -0
index b0de30c713b4..252a1c1d2029 100644
@@ -5,6 +5,7 @@
 #include <string.h>
 
 #define __hidden __attribute__((visibility ("hidden")))
+#define __weak __attribute__((weak))
 
 #define offset_of(type, field) ((size_t)(&(((type *)(NULL))->field)))
 #define container_of(ptr, type, field) \
@@ -14,6 +15,10 @@
 #define HASH_SIZE(bits)	(1 << (bits))
 #define HASH_MASK(bits)	(HASH_SIZE(bits) - 1)
 
+
+extern void __attribute__ ((format (printf, 2, 3)))
+ teval_print_err(int level, const char *fmt, ...);
+
 /*
  * Compare two integers of variable length.
  *
src/histograms.c +37 -11

100 lines not on this page · raw message

Signature · 1 line
2.42.0
#2 lore raw

PATCH 2/2 libtraceeval: Have various errors print warnings

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Now that there's a way to print out warning messages, use it to print out most errors that libtraceeval can have.

---
 src/delta.c      |  40 ++++++++++---
 src/eval-local.h |   4 ++
 src/histograms.c | 152 +++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 162 insertions(+), 34 deletions(-)
src/delta.c +31 -9
index 99c86122d83f..544998dff412 100644
@@ -58,12 +58,16 @@ int traceeval_delta_create_data_size(struct traceeval *teval,
 	int i;
 
 	/* Only one can exist */
-	if (teval->tdelta)
+	if (teval->tdelta) {
+		teval_print_err(TEVAL_WARN, "Multiple deltas added to traceeval");
 		return -1;
+	}
 
 	tdelta = calloc(1, sizeof(*tdelta));
-	if (!tdelta)
+	if (!tdelta) {
+		teval_print_err(TEVAL_CRIT, "Failed to allocate delta");
 		return -1;
+	}
 
 	if (vals) {
 		for (i = 0; i < nr_vals && vals[i].type != TRACEEVAL_TYPE_NONE; i++)
@@ -134,8 +138,10 @@ int traceeval_delta_remove_size(struct traceeval *teval,
 				const struct traceeval_data *keys,
 				size_t nr_keys)
 {
-	if (!teval->tdelta)
+	if (!teval->tdelta) {
+		teval_print_err(TEVAL_WARN, "traceeval_delta_remove: traceeval does not have delta to remove");
 		return -1;
+	}
 	return traceeval_remove_size(teval->tdelta->teval, keys, nr_keys);
 }
 
@@ -162,8 +168,10 @@ int traceeval_delta_query_size(struct traceeval *teval,
 			       const struct traceeval_data *keys,
 			       size_t nr_keys, const struct traceeval_data **results)
 {
-	if (!teval->tdelta)
+	if (!teval->tdelta) {
+		teval_print_err(TEVAL_WARN, "traceeval_delta_query: traceeval does not have delta to query");
 		return -1;
+	}
 	return traceeval_query_size(teval->tdelta->teval, keys,
 				    nr_keys, results);
 }
@@ -177,14 +185,24 @@ static int delta_start(struct traceeval *teval,
 	int ret;
 	int i;
 
-	if (!teval->tdelta)
+	if (!teval->tdelta) {
+		teval_print_err(TEVAL_WARN, "traceeval_delta_start/continue: traceeval does not have delta");
 		return -1;
+	}
 
 	teval = teval->tdelta->teval;
 
-	if (nr_keys != teval->nr_key_types ||
-	    nr_vals != teval->nr_val_types - 1)
+	if (nr_keys != teval->nr_key_types) {
+		teval_print_err(TEVAL_WARN, "traceeval_delta_start/continue: Received %zd keys but expected %zd",
+				nr_keys, teval->nr_key_types);
+		return -1;
+	}
+
+	if (nr_vals != teval->nr_val_types - 1) {
+		teval_print_err(TEVAL_WARN, "traceeval_delta_start/continue: Received %zd vals but expected %zd",
+				nr_vals, teval->nr_val_types - 1);
 		return -1;
+	}
 
 	ret = _teval_get_entry(teval, keys, &entry);
 	if (ret < 0)
@@ -309,8 +327,10 @@ int traceeval_delta_stop_size(struct traceeval *teval,
 	struct entry *entry;
 	int ret;
 
-	if (!teval->tdelta)
+	if (!teval->tdelta) {
+		teval_print_err(TEVAL_WARN, "traceeval_delta_stop: traceeval does not have delta");
 		return -1;
+	}
 
 	teval = teval->tdelta->teval;
 
@@ -349,8 +369,10 @@ static int create_delta_iter_array(struct traceeval_iterator *iter)
 
 	iter->nr_entries = hash_nr_items(hist);
 	iter->entries = calloc(iter->nr_entries, sizeof(*iter->entries));
-	if (!iter->entries)
+	if (!iter->entries) {
+		teval_print_err(TEVAL_CRIT, "Failed to allocate entries");
 		return -1;
+	}
 
 	for (i = 0, hiter = hash_iter_start(hist); (item = hash_iter_next(hiter)); i++) {
 		struct entry *entry = container_of(item, struct entry, hash);
src/eval-local.h +4 -0
index 252a1c1d2029..ca8195a2f4cb 100644
@@ -103,6 +103,10 @@ struct traceeval_iterator {
 	bool				no_sort;
 };
 
+extern void teval_print_failed_type(const char *type,
+				    const struct traceeval_type *expect,
+				    const struct traceeval_data *got);
+
 extern int _teval_get_entry(struct traceeval *teval, const struct traceeval_data *keys,
 			    struct entry **result);
 
src/histograms.c +127 -25

309 lines not on this page · raw message

Signature · 1 line
2.42.0