From patchwork Wed Aug 9 17:53:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stevie Alvarez X-Patchwork-Id: 13348276 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 E4894C04A94 for ; Wed, 9 Aug 2023 17:53:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231591AbjHIRxu (ORCPT ); Wed, 9 Aug 2023 13:53:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjHIRxt (ORCPT ); Wed, 9 Aug 2023 13:53:49 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFD3E10F6 for ; Wed, 9 Aug 2023 10:53:48 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-76754b9eac0so11014785a.0 for ; Wed, 09 Aug 2023 10:53:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691603627; x=1692208427; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=irLZzpaAbuT5pEiREaaBz+Xmu3lslzxYJH7JhZcbFus=; b=kn5uJmZh4zGnh7EDdB/aSpH9cBbLTTeTuewk8J7TnP8kxxGkPUYxEaTRhBxAUTgjRA vpdbZLIdreL8Fge5YTgpOo5G3E5Wrx0vAYFgTruBC+FKSvCTlBSVTamAYnwlhkQF7MEY c7nWm7mf5hT5bMJMUBYA7bwJgoiMGTzqzUS4e7wXiVu1JmPCMxsnikEtL4/sZ29jtGGR sSZcJ4999f6Thk4iMdbyyLlqjW90RsgJ0M1lwuEQefWntNfusyBilGU3CwoWccR5Nrq5 oHWK4OGquFH2Uh4yCzqEMGwJyAkBcDXGbLmbzmqX3GxUdysCqtRJ3wlw6IY0mwUDenq7 WafQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691603627; x=1692208427; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=irLZzpaAbuT5pEiREaaBz+Xmu3lslzxYJH7JhZcbFus=; b=feX+SHw2XcIPPMvuUqHvZmCZtHCzPO4H8MEZZpEguGSqVO4hGLIPTKKcWIQzNRtZve 9RI7q5Ru/Xom7IYMGWXnZfKHbVd7NMRl/4xa9OLe5rrYqgb8wWIa5X8VBCR+3hshh9vE hFczagZhmRgBj2rryoyLlethxsf8lJqKsXvfJ/TBygep23HwWZTaGP3NzeC7K/6ZJcKT pFfuxNKwn0klmMYKMOwHsxFRdwlOQfMHCjG+O6mzVzE94Q6V8eqS25Pc39QYMXK2qpzE VGMsWa/tL5IvADClRICSFFEt0Jxfpp0+nOJCaAYWD305NVMLM0/vxpz81HUlvQWwqajS bgrA== X-Gm-Message-State: AOJu0YzYgk2yR+kcouPU8qzfcgeMvJNz1+Cz0l2wx4ZpMl1LlQFRWGtk IVujsTivajKCPLoNRpyzHfLYtV+wqDg= X-Google-Smtp-Source: AGHT+IFz6lDnuS0NU583v7sxOiiG9GuEsJc4wTPKvBdxmW6zdFCHuWqAILqEgh/jAwarze1btvKUUQ== X-Received: by 2002:ad4:4f47:0:b0:63d:2907:46 with SMTP id eu7-20020ad44f47000000b0063d29070046mr2775798qvb.26.1691603627438; Wed, 09 Aug 2023 10:53:47 -0700 (PDT) Received: from 3xKetch.hsd1.ma.comcast.net ([2601:180:8300:500::2409]) by smtp.gmail.com with ESMTPSA id x4-20020a0c8e84000000b00623839cba8csm4633646qvb.44.2023.08.09.10.53.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 10:53:46 -0700 (PDT) From: Stevie Alvarez To: linux-trace-devel@vger.kernel.org Cc: Stevie Alvarez , Steven Rostedt , Ross Zwisler Subject: [PATCH v4 1/5] histograms: Initial histograms interface Date: Wed, 9 Aug 2023 13:53:34 -0400 Message-ID: <20230809175340.3066-2-stevie.6strings@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809175340.3066-1-stevie.6strings@gmail.com> References: <20230809175340.3066-1-stevie.6strings@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Stevie Alvarez (Google) Initial header file for libtraceeval's histogram API. The interface provides a simple way of aggregating trace data and reading through said data. Signed-off-by: Stevie Alvarez (Google) --- include/traceeval-hist.h | 130 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 include/traceeval-hist.h diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h new file mode 100644 index 0000000..3565756 --- /dev/null +++ b/include/traceeval-hist.h @@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: MIT */ +/* + * libtraceeval histogram interface. + * + * Copyright (C) 2023 Google Inc, Steven Rostedt + * Copyright (C) 2023 Google Inc, Stevie Alvarez + */ +#ifndef __LIBTRACEEVAL_HIST_H__ +#define __LIBTRACEEVAL_HIST_H__ + +#include +#include +#include + +/* Data definition interfaces */ + +/* Field name/descriptor for number of hits */ +#define TRACEEVAL_VAL_HITS ((const char *)(-1UL)) + +/* Data type distinguishers */ +enum traceeval_data_type { + TRACEEVAL_TYPE_NONE, + TRACEEVAL_TYPE_NUMBER_8, + TRACEEVAL_TYPE_NUMBER_16, + TRACEEVAL_TYPE_NUMBER_32, + TRACEEVAL_TYPE_NUMBER_64, + TRACEEVAL_TYPE_NUMBER, + TRACEEVAL_TYPE_STRING, + TRACEEVAL_TYPE_DYNAMIC +}; + +/* Statistics specification flags */ +enum traceeval_flags { + TRACEEVAL_FL_SIGNED = (1 << 0), + TRACEEVAL_FL_TIMESTAMP = (1 << 1), +}; + +/* + * struct traceeval_dynamic - Storage for dynamic traceeval_types + * @size: The size of the dynamic type + * @data: The pointer to the data of the dynamic type + */ +struct traceeval_dynamic { + void *data; + size_t size; +}; + +/* + * Trace data entry for a traceeval histogram + * Constitutes keys and values. + */ +union traceeval_data { + struct traceeval_dynamic dyn_data; + char *string; + unsigned long number; + unsigned long long number_64; + unsigned int number_32; + unsigned short number_16; + unsigned char number_8; +}; + +struct traceeval_type; + +/* struct traceeval_dynamic release function signature */ +typedef void (*traceeval_dyn_release_fn)(struct traceeval_type *, + struct traceeval_dynamic); + +/* struct traceeval_dynamic compare function signature */ +typedef int (*traceeval_dyn_cmp_fn)(struct traceeval_dynamic, + struct traceeval_dynamic, + struct traceeval_type *); + +/* + * struct traceeval_type - Describes the type of a traceevent_data instance + * @type: The enum type that describes the traceeval_data + * @name: The string name of the traceeval_data + * @flags: flags to describe the traceeval_data + * @id: User specified identifier + * @dyn_release: For dynamic types called on release (ignored for other types) + * @dyn_cmp: A way to compare dynamic types (ignored for other types) + * + * The traceeval_type structure defines expectations for a corresponding + * traceeval_data instance for a traceeval histogram instance. Used to + * describe both keys and values. + * + * The @id field is an optional value in case the user has multiple struct + * traceeval_type instances with @type fields set to TRACEEVAL_TYPE_DYNAMIC, + * which each relate to distinct user defined struct traceeval_dynamic + * 'sub-types'. + * + * For flexibility, @dyn_cmp() and @dyn_release() take a struct + * traceeval_type instance. This allows the user to distinguish between + * different sub-types of struct traceeval_dynamic within a single + * callback function by examining the @id field. This is not a required + * approach, merely one that is accommodated. + * + * @dyn_cmp() is used to compare two struct traceeval_dynamic instances when a + * corresponding struct traceeval_type is reached with its type field set to + * TRACEEVAL_TYPE_DYNAMIC. It should return 0 on equality, 1 if the first + * argument is greater than the second, -1 for the other way around, and -2 on + * error. + * + * dyn_release() is used during traceeval_release() to release a union + * traceeval_data's struct traceeval_dynamic field when the corresponding + * traceeval_type type is set to TRACEEVAL_TYPE_DYNAMIC. + */ +struct traceeval_type { + char *name; + enum traceeval_data_type type; + size_t flags; + size_t id; + traceeval_dyn_release_fn dyn_release; + traceeval_dyn_cmp_fn dyn_cmp; +}; + +/* Statistics about a given entry element */ +struct traceeval_stat { + unsigned long long max; + unsigned long long min; + unsigned long long total; + unsigned long long avg; + unsigned long long std; +}; + +/* Iterator over aggregated data */ +struct traceeval_iterator; + +struct traceeval; + +#endif /* __LIBTRACEEVAL_HIST_H__ */ From patchwork Wed Aug 9 17:53:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stevie Alvarez X-Patchwork-Id: 13348279 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 630A4C04E69 for ; Wed, 9 Aug 2023 17:53:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229456AbjHIRxw (ORCPT ); Wed, 9 Aug 2023 13:53:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229978AbjHIRxw (ORCPT ); Wed, 9 Aug 2023 13:53:52 -0400 Received: from mail-vk1-xa36.google.com (mail-vk1-xa36.google.com [IPv6:2607:f8b0:4864:20::a36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2168810D2 for ; Wed, 9 Aug 2023 10:53:51 -0700 (PDT) Received: by mail-vk1-xa36.google.com with SMTP id 71dfb90a1353d-4871e8fdcfeso46342e0c.2 for ; Wed, 09 Aug 2023 10:53:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691603630; x=1692208430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4907FKRmX3ceovQo+cWr+kmFWgMDszrcRWKDtwkgTSs=; b=b+inm9Rfs9Z3NBai71ewAOmDs6gPyBLNyPJT7lKUUH187JRIXlqsrQuTmUwodcm++3 y6Gv7tHKkrjrWF6DKBjWrD5n6ZoIiiYxWXwyHE9tz7mmvvvLDQXCrztpLq7ee5Vlucxk bdNdeyejt++q8YjFTFtKxWhvll3Z5XDXk79f0CnQuOHyDVofLjtv+eUwU454kkDCS7AF p9EyGZ2J0nDYYODXrMyheV/Wevkg2KofkphChox38p13vF72gjm3AbDOF2GvjO1Ft+t1 z/NMX59szKw+ntV8mjN0IaUjwH5CIOPMFUJGC61mz2sF8zse09GQYD8+3H6m//lNTpLo 8oew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691603630; x=1692208430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4907FKRmX3ceovQo+cWr+kmFWgMDszrcRWKDtwkgTSs=; b=hXGYsLzCCZmFx0d/ekviY1bC6UpeM0/BCIbCLEZHhWL8uU1d4+EqH45L/JqAOlT5Kw klHRhnBXDzo4Av4Xt+tE7W8ayRMCO7ItmvOwdYbWNm79Ix0mupsBOFbBg7UoGCOpQjit Y1JKTpkZG5jsvtrQEv9N+KMR4xlKUugWwl4IjXlBRoG9G0sYSM44P46p7f3+4in0EPNH Js4KXWv05O/HhJ6IYGqcpd+qS0zkU9W7cYb3KON+ruu97YEaxSomg2irbljoojrhqQ+E axKxC53DubrRPad/L3gf3XShgih1ZRigom+0kIlz4iztn5cBRArPEf+BL7q0/Hua7vkQ ngog== X-Gm-Message-State: AOJu0YxHRygLxlKe8lXbzEnfvYgXaxGhFAhvEXSpg59Yw6c0j5UPL6fL xP/iQz82KfGKD9QNx86/n59blzQJ3dQ= X-Google-Smtp-Source: AGHT+IH3dQQ/9BcHJOWavjnQRgOF0SC1oH8z6pypbmWfUAqAi1AAtl+a3cIkTqPqwRlkOXyopiexvg== X-Received: by 2002:a1f:5c41:0:b0:487:10c9:3f08 with SMTP id q62-20020a1f5c41000000b0048710c93f08mr195143vkb.0.1691603628571; Wed, 09 Aug 2023 10:53:48 -0700 (PDT) Received: from 3xKetch.hsd1.ma.comcast.net ([2601:180:8300:500::2409]) by smtp.gmail.com with ESMTPSA id x4-20020a0c8e84000000b00623839cba8csm4633646qvb.44.2023.08.09.10.53.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 10:53:47 -0700 (PDT) From: Stevie Alvarez To: linux-trace-devel@vger.kernel.org Cc: Stevie Alvarez , Steven Rostedt , Ross Zwisler Subject: [PATCH v4 2/5] histograms: Add traceeval initialize and release Date: Wed, 9 Aug 2023 13:53:35 -0400 Message-ID: <20230809175340.3066-3-stevie.6strings@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809175340.3066-1-stevie.6strings@gmail.com> References: <20230809175340.3066-1-stevie.6strings@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Stevie Alvarez (Google) traceeval_init() creates a new struct traceeval instance with regards to the struct traceeval_type array arguments keys and vals. These arrays define the structure of the histogram, with each describing the expected structure of inserted arrays of union traceeval_data. The keys and vals arguments are copied on the heap to ensure that the struct traceeval instance has access to the definition regardless of how the user initialized keys and vals. traceeval_init() uses traceeval_release() and it's helper functions to clean up on error. traceeval_release() deconstructs a given struct traceeval instance. It frees any data allocated to the heap within the entries to the histogram, and the names allocated for the struct traceeval_type key-value definitions. Signed-off-by: Stevie Alvarez (Google) --- Makefile | 2 +- include/traceeval-hist.h | 7 + src/Makefile | 1 + src/histograms.c | 315 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 src/histograms.c diff --git a/Makefile b/Makefile index 4a24d5a..3ea051c 100644 --- a/Makefile +++ b/Makefile @@ -172,7 +172,7 @@ libs: $(LIBRARY_A) $(LIBRARY_SO) VALGRIND = $(shell which valgrind) UTEST_DIR = utest -UTEST_BINARY = trace-utest +UTEST_BINARY = eval-utest test: force $(LIBRARY_STATIC) ifneq ($(CUNIT_INSTALLED),1) diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h index 3565756..63e8b0e 100644 --- a/include/traceeval-hist.h +++ b/include/traceeval-hist.h @@ -127,4 +127,11 @@ struct traceeval_iterator; struct traceeval; +/* Histogram interfaces */ + +struct traceeval *traceeval_init(const struct traceeval_type *keys, + const struct traceeval_type *vals); + +void traceeval_release(struct traceeval *teval); + #endif /* __LIBTRACEEVAL_HIST_H__ */ diff --git a/src/Makefile b/src/Makefile index b4b6e52..b32a389 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,6 +4,7 @@ include $(src)/scripts/utils.mk OBJS = OBJS += trace-analysis.o +OBJS += histograms.o OBJS := $(OBJS:%.o=$(bdir)/%.o) diff --git a/src/histograms.c b/src/histograms.c new file mode 100644 index 0000000..568c631 --- /dev/null +++ b/src/histograms.c @@ -0,0 +1,315 @@ +/* SPDX-License-Identifier: MIT */ +/* + * libtraceeval histogram interface implementation. + * + * Copyright (C) 2023 Google Inc, Stevie Alvarez + */ + +#include +#include +#include +#include + +#include + +/* A key-value pair */ +struct entry { + union traceeval_data *keys; + union traceeval_data *vals; +}; + +/* A table of key-value entries */ +struct hist_table { + struct entry *map; + size_t nr_entries; +}; + +/* Histogram */ +struct traceeval { + struct traceeval_type *key_types; + struct traceeval_type *val_types; + struct hist_table *hist; + size_t nr_key_types; + size_t nr_val_types; +}; + +/* + * print_err - print an error message + * @fmt: String format + * @...: (optional) Additional arguments to print in conjunction with @format + */ +static void print_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); +} + +/* + * type_release - free a struct traceeval_type array + * @defs: The array to release + * @len: The length of @defs + * + * It is assumed that all elements of @defs, within the length of @len, have + * name fields initialized to valid pointers. + * + * This function was designed to be used on an array allocated by type_alloc(). + * Note that type_alloc() initializes all name fields of elements within the + * returned size. + */ +static void type_release(struct traceeval_type *defs, size_t len) +{ + if (!defs) + return; + + for (size_t i = 0; i < len; i++) { + free(defs[i].name); + } + + free(defs); +} + +/* + * type_alloc - clone a struct traceeval_type array + * @defs: The original array + * @copy: A pointer to where to place the @defs copy + * + * Clone traceeval_type array @defs to the heap, and place in @copy. + * @defs must be terminated with an instance of type TRACEEVAL_TYPE_NONE. + * + * The size of the copy pointed to by @copy is returned. It counts all elements + * in @defs excluding the terminating TRACEEVAL_TYPE_NONE traceeval_type. + * The copy contains everything from @defs excluding the TRACEEVAL_TYPE_NONE + * traceeval_type. + * On error, copy is set to point to NULL. + * + * The name field of each element of @defs (except for the terminating + * TRACEEVAL_TYPE_NONE) must be a NULL-terminated string. The validity of the + * name field is not checked, but errors are returned upon finding unset name + * fields and string duplication failures. It is guaranteed that all elements + * of the copy within the returned size have valid pointers in their name + * fields. + * + * Returns the size of the array pointed to by @copy, or -1 on error. + */ +static size_t type_alloc(const struct traceeval_type *defs, + struct traceeval_type **copy) +{ + struct traceeval_type *new_defs = NULL; + size_t size; + size_t i; + + *copy = NULL; + + if (!defs) + return 0; + + for (size = 0; defs && defs[size].type != TRACEEVAL_TYPE_NONE; size++) + ; + + if (!size) + return 0; + + new_defs = calloc(size, sizeof(*new_defs)); + + for (i = 0; i < size; i++) { + /* copy current def data to new_def */ + new_defs[i] = defs[i]; + + /* copy name to heap, ensures name copied */ + if (!defs[i].name) + goto fail; + new_defs[i].name = strdup(defs[i].name); + + if (!new_defs[i].name) + goto fail; + } + + *copy = new_defs; + return size; + +fail: + if (defs[i].name) + print_err("Failed to allocate traceeval_type %zu", size); + else + print_err("traceeval_type list missing a name"); + + for (; i >=0; i--) + free(new_defs[i].name); + free(new_defs); + return -1; +} + +/* + * traceeval_init - create a traceeval descriptor + * @keys: Defines the keys to differentiate traceeval entries + * @vals: Defines values attached to entries differentiated by @keys. + * + * The @keys and @vals define how the traceeval instance will be populated. + * The @keys will be used by traceeval_query() to find an instance within + * the "histogram". Note, both the @keys and @vals array must end with: + * { .type = TRACEEVAL_TYPE_NONE }. + * + * The @keys and @vals passed in are copied for internal use. + * + * For any member of @keys or @vals that isn't of type TRACEEVAL_TYPE_NONE, + * the name field must be a null-terminated string. Members of type + * TRACEEVAL_TYPE_NONE are used to terminate the array, therefore their other + * fields are ignored. + * + * @vals can be NULL or start with its type field as TRACEEVAL_TYPE_NONE to + * define the values of the histogram to be empty. + * @keys must be populated with at least one element that is not of type + * TRACEEVAL_TYPE_NONE. + * + * Returns the descriptor on success, or NULL on error. + */ +struct traceeval *traceeval_init(const struct traceeval_type *keys, + const struct traceeval_type *vals) +{ + struct traceeval *teval; + char *err_msg; + + if (!keys) + return NULL; + + if (keys->type == TRACEEVAL_TYPE_NONE) { + err_msg = "Keys cannot start with type TRACEEVAL_TYPE_NONE"; + goto fail; + } + + /* alloc teval */ + teval = calloc(1, sizeof(*teval)); + if (!teval) { + err_msg = "Failed to allocate memory for traceeval instance"; + goto fail; + } + + /* alloc key types */ + teval->nr_key_types = type_alloc(keys, &teval->key_types); + if (teval->nr_key_types <= 0) { + err_msg = "Failed to allocate user defined keys"; + goto fail_release; + } + + /* alloc val types */ + teval->nr_val_types = type_alloc(vals, &teval->val_types); + if (teval->nr_val_types < 0) { + err_msg = "Failed to allocate user defined values"; + goto fail_release; + } + + /* alloc hist */ + teval->hist = calloc(1, sizeof(*teval->hist)); + if (!teval->hist) { + err_msg = "Failed to allocate memory for histogram"; + goto fail_release; + } + + return teval; + +fail_release: + traceeval_release(teval); + +fail: + print_err(err_msg); + return NULL; +} + +/* + * Frees dynamic data in @data if @type specifies a dynamic data type. + */ +static void clean_data(union traceeval_data *data, struct traceeval_type *type) +{ + switch (type->type) { + case TRACEEVAL_TYPE_STRING: + free(data->string); + break; + case TRACEEVAL_TYPE_DYNAMIC: + if (type->dyn_release) + type->dyn_release(type, data->dyn_data); + break; + default: + break; + } +} + +/* + * Free any specified dynamic data in @data. + */ +static void clean_data_set(union traceeval_data *data, struct traceeval_type *defs, + size_t size) +{ + size_t i; + + if (!data || !defs) { + if (data) + print_err("Data to be freed without accompanied types!"); + return; + } + + for (i = 0; i < size; i++) { + clean_data(data + i, defs + i); + } + + free(data); +} + +/* + * Free all possible data stored within the entry. + */ +static void clean_entry(struct entry *entry, struct traceeval *teval) +{ + if (!entry) + return; + + /* free dynamic traceeval_data */ + clean_data_set(entry->keys, teval->key_types, teval->nr_key_types); + clean_data_set(entry->vals, teval->val_types, teval->nr_val_types); +} + +/* + * Free the hist_table allocated to a traceeval instance. + */ +static void hist_table_release(struct traceeval *teval) +{ + struct hist_table *hist = teval->hist; + + if (!hist) + return; + + for (size_t i = 0; i < hist->nr_entries; i++) { + clean_entry(hist->map + i, teval); + } + + free(hist->map); + free(hist); + teval->hist = NULL; +} + +/* + * traceeval_release - release a traceeval descriptor + * @teval: An instance of traceeval returned by traceeval_init() + * + * When the caller of traceeval_init() is done with the returned @teval, + * it must call traceeval_release(). + * + * This frees all internally allocated data of @teval and will call the + * corresponding dyn_release() functions registered for keys and values of + * type TRACEEVAL_TYPE_DYNAMIC. + */ +void traceeval_release(struct traceeval *teval) +{ + if (!teval) + return; + + hist_table_release(teval); + type_release(teval->key_types, teval->nr_key_types); + type_release(teval->val_types, teval->nr_val_types); + teval->key_types = NULL; + teval->val_types = NULL; + free(teval); +} From patchwork Wed Aug 9 17:53:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stevie Alvarez X-Patchwork-Id: 13348277 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 37C0AC001B0 for ; Wed, 9 Aug 2023 17:53:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231733AbjHIRxw (ORCPT ); Wed, 9 Aug 2023 13:53:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjHIRxw (ORCPT ); Wed, 9 Aug 2023 13:53:52 -0400 Received: from mail-qv1-xf33.google.com (mail-qv1-xf33.google.com [IPv6:2607:f8b0:4864:20::f33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33A6E10F6 for ; Wed, 9 Aug 2023 10:53:51 -0700 (PDT) Received: by mail-qv1-xf33.google.com with SMTP id 6a1803df08f44-63fba828747so566326d6.0 for ; Wed, 09 Aug 2023 10:53:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691603630; x=1692208430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7E+Hjbc3wWdeXPLd5XqEKGpICnBXALlADWs7V9geozA=; b=o3dCzH8We6Z02+aEArYG6yRszbWvUVvybDr+EFrraYeZuiF/dDsEIHVrFcEqme8HwT S/SC+81G52KrUGl2TDvWJX1e3N+ZRkSHUauy13aCwXy8ZI2o+FGO+6jwGeHxLVH+6yCy u2rk9RjtHE5a0O1hZ7OBaXZwU0UMupqbPedmNiNOFW3ttODDCZJv50sNIO0oXBae8cJh 4pfjU5cUQPa60x+Yj9x6WRKAe2ySHWkcnQc/+d4tTDXKxa4n3vIIFTkwrIV6QkXn4eTS gypZDhrNu61zF3YPmtu/5xRDPTomriBEJ3KMvK08E3jLKeTiwcC2LXkRRrqg0BYLyaEd 8cHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691603630; x=1692208430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7E+Hjbc3wWdeXPLd5XqEKGpICnBXALlADWs7V9geozA=; b=Y1Bp4814a6UHVbzP8NAy/9RkIDFk41Uk7VAe2Bhi8q9UnlOzHcnXfEc9/OXCxF+vZK pck2Gsr5Cq6R2rmosuh1Jy3nCenx25oqSycUizGLx2yYMPSfjWsa/hNwi/tP2LUOKbNA 31ndKytWKu2ox4Cjpoa6WfovZa/9n0xfIhCJ7A/WxpLVvQoD1iEeSNP3nzDl59AYKPL4 JwWZnjJax4iuMBHVNUtOzw/87qemPZyPK3p2vKyWMuP4bJazbbeueAIWIOdnzF5kjIqU cvBdOrQLZf+fKwOoniFVIFWfUdMoMrv4q1j7iI4aamrhBkjX02isKTpoHHvfKtVSXCwS yjHw== X-Gm-Message-State: AOJu0YzzJZOUH70qnLJ2BCAPLXNu+Mook8+ECMecp9Wu2IWDufPwcuTP xXvn5nFnRQBJuiYivAOhF38BPc+2AhQ= X-Google-Smtp-Source: AGHT+IG85/o3BnIinb2N5lJunE0fMhyLxJ+hrz/wfCll3H9qoCii9CXG3NiA2RZsdQbvDVDt9tF0qw== X-Received: by 2002:a05:6214:517:b0:63d:639a:37d4 with SMTP id px23-20020a056214051700b0063d639a37d4mr3129630qvb.45.1691603629840; Wed, 09 Aug 2023 10:53:49 -0700 (PDT) Received: from 3xKetch.hsd1.ma.comcast.net ([2601:180:8300:500::2409]) by smtp.gmail.com with ESMTPSA id x4-20020a0c8e84000000b00623839cba8csm4633646qvb.44.2023.08.09.10.53.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 10:53:49 -0700 (PDT) From: Stevie Alvarez To: linux-trace-devel@vger.kernel.org Cc: Stevie Alvarez , Steven Rostedt , Ross Zwisler Subject: [PATCH v4 3/5] histograms: Add traceeval compare Date: Wed, 9 Aug 2023 13:53:36 -0400 Message-ID: <20230809175340.3066-4-stevie.6strings@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809175340.3066-1-stevie.6strings@gmail.com> References: <20230809175340.3066-1-stevie.6strings@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Stevie Alvarez (Google) traceeval_compare() compares two struct traceeval instances for equality. This suite of comparitors was made for testing purposes. Signed-off-by: Stevie Alvarez (Google) --- include/traceeval-test.h | 16 +++ src/histograms.c | 214 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 include/traceeval-test.h diff --git a/include/traceeval-test.h b/include/traceeval-test.h new file mode 100644 index 0000000..bb8092a --- /dev/null +++ b/include/traceeval-test.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: MIT */ +/* + * libtraceeval interface for unit testing. + * + * Copyright (C) 2023 Google Inc, Steven Rostedt + * Copyright (C) 2023 Google Inc, Stevie Alvarez + */ + +#ifndef __LIBTRACEEVAL_TEST_H__ +#define __LIBTRACEEVAL_TEST_H__ + +#include + +int traceeval_compare(struct traceeval *orig, struct traceeval *copy); + +#endif /* __LIBTRACEEVAL_TEST_H__ */ diff --git a/src/histograms.c b/src/histograms.c index 568c631..ed631b0 100644 --- a/src/histograms.c +++ b/src/histograms.c @@ -12,6 +12,21 @@ #include +#include "traceeval-test.h" + +/* + * Compare two integers of variable length. + * + * Return 0 if @a and @b are the same, 1 if @a is greater than @b, and -1 + * if @b is greater than @a. + */ +#define compare_numbers_return(a, b) \ +do { \ + if ((a) < (b)) \ + return -1; \ + return (a) != (b); \ +} while (0) \ + /* A key-value pair */ struct entry { union traceeval_data *keys; @@ -48,6 +63,205 @@ static void print_err(const char *fmt, ...) fprintf(stderr, "\n"); } +/* + * Compare traceeval_type instances for equality. + * + * Return 1 if @orig and @copy are the same, 0 otherwise. + */ +static int compare_traceeval_type(struct traceeval_type *orig, + struct traceeval_type *copy, + size_t orig_size, size_t copy_size) +{ + size_t i; + + /* same memory/NULL */ + if (orig == copy) + return 1; + if (!!orig != !!copy) + return 0; + + if (orig_size != copy_size) + return 0; + + for (i = 0; i < orig_size; i++) { + if (orig[i].type != copy[i].type) + return 0; + if (orig[i].flags != copy[i].flags) + return 0; + if (orig[i].id != copy[i].id) + return 0; + if (orig[i].dyn_release != copy[i].dyn_release) + return 0; + if (orig[i].dyn_cmp != copy[i].dyn_cmp) + return 0; + + // make sure both names are same type + if (!!orig[i].name != !!copy[i].name) + return 0; + if (!orig[i].name) + continue; + if (strcmp(orig[i].name, copy[i].name) != 0) + return 0; + } + + return 1; +} + +/* + * Compare traceeval_data instances. + * + * Return 0 if @orig and @copy are the same, 1 if @orig is greater than @copy, + * -1 for the other way around, and -2 on error. + */ +static int compare_traceeval_data(union traceeval_data *orig, + const union traceeval_data *copy, + struct traceeval_type *type) +{ + int i; + + if (orig == copy) + return 0; + + if (!orig) + return -1; + + if (!copy) + return 1; + + switch (type->type) { + case TRACEEVAL_TYPE_STRING: + i = strcmp(orig->string, copy->string); + compare_numbers_return(i, 0); + + case TRACEEVAL_TYPE_NUMBER: + compare_numbers_return(orig->number, copy->number); + + case TRACEEVAL_TYPE_NUMBER_64: + compare_numbers_return(orig->number_64, copy->number_64); + + case TRACEEVAL_TYPE_NUMBER_32: + compare_numbers_return(orig->number_32, copy->number_32); + + case TRACEEVAL_TYPE_NUMBER_16: + compare_numbers_return(orig->number_16, copy->number_16); + + case TRACEEVAL_TYPE_NUMBER_8: + compare_numbers_return(orig->number_8, copy->number_8); + + case TRACEEVAL_TYPE_DYNAMIC: + if (type->dyn_cmp) + return type->dyn_cmp(orig->dyn_data, copy->dyn_data, type); + return 0; + + default: + print_err("%d is an invalid enum traceeval_data_type member", + type->type); + return -2; + } +} + +/* + * Compare arrays of union traceeval_data's with respect to @def. + * + * Return 1 if @orig and @copy are the same, 0 if not, and -1 on error. + */ +static int compare_traceeval_data_set(union traceeval_data *orig, + const union traceeval_data *copy, + struct traceeval_type *defs, size_t size) +{ + int check; + size_t i; + + /* compare data arrays */ + for (i = 0; i < size; i++) { + if ((check = compare_traceeval_data(orig + i, copy + i, defs + i))) + return check == -2 ? -1 : 0; + } + + return 1; +} + +/* + * Return 1 if @orig and @copy are the same, 0 if not, and -1 on error. + */ +static int compare_entries(struct entry *orig, struct entry *copy, + struct traceeval *teval) +{ + int check; + + /* compare keys */ + check = compare_traceeval_data_set(orig->keys, copy->keys, + teval->key_types, teval->nr_key_types); + if (check < 1) + return check; + + /* compare values */ + check = compare_traceeval_data_set(orig->vals, copy->vals, + teval->val_types, teval->nr_val_types); + return check; +} + +/* + * Compares the hist fields of @orig and @copy for equality. + * + * Assumes all other aspects of @orig and @copy are the same. + * + * Return 1 if struct hist_table of @orig and @copy are the same, 0 if not, + * and -1 on error. + */ +static int compare_hist(struct traceeval *orig, struct traceeval *copy) +{ + struct hist_table *o_hist; + struct hist_table *c_hist; + int c; + + o_hist = orig->hist; + c_hist = copy->hist; + + if (o_hist->nr_entries != c_hist->nr_entries) + return 0; + + for (size_t i = 0; i < o_hist->nr_entries; i++) { + if ((c = compare_entries(o_hist->map + i, c_hist->map + i, orig)) < 1) + return c; + } + + return 1; +} + +/* + * traceeval_compare - Check equality between two traceeval instances + * @orig: The first traceeval instance + * @copy: The second traceeval instance + * + * This compares the values of the key definitions, value definitions, and + * inserted data between @orig and @copy in order. It does not compare + * by memory address, except for struct traceeval_type's dyn_release() and + * dyn_cmp() fields. + * + * Returns 1 if @orig and @copy are the same, 0 if not, and -1 on error. + */ + int traceeval_compare(struct traceeval *orig, struct traceeval *copy) +{ + int keys; + int vals; + int hists; + + if (!orig || !copy) + return -1; + + keys = compare_traceeval_type(orig->key_types, copy->key_types, + orig->nr_key_types, copy->nr_key_types); + vals = compare_traceeval_type(orig->val_types, copy->val_types, + orig->nr_val_types, copy->nr_val_types); + hists = compare_hist(orig, copy); + + if (hists == -1) + return -1; + + return keys && vals && hists; +} + /* * type_release - free a struct traceeval_type array * @defs: The array to release From patchwork Wed Aug 9 17:53:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stevie Alvarez X-Patchwork-Id: 13348278 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 3F7C8C001E0 for ; Wed, 9 Aug 2023 17:53:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229554AbjHIRxx (ORCPT ); Wed, 9 Aug 2023 13:53:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231743AbjHIRxx (ORCPT ); Wed, 9 Aug 2023 13:53:53 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D57010FB for ; Wed, 9 Aug 2023 10:53:52 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id 6a1803df08f44-63d1238c300so609206d6.0 for ; Wed, 09 Aug 2023 10:53:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691603631; x=1692208431; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VLaMsZyfh3kdnwuZu2mOjfu9FkuAZKI4YATx7fYp+SU=; b=JZi5r2k/JoHKrRav5S0b15oiB4UJ1s9Uao5B+91wwDP7sAYrlo3wS7z4SI9MaInLVp /CmwcfyHItO5Plk7RMw7BxlF6r1pyVzFGwDyxYOPGPOsQ7nC46IGLw0sO42rzFt1QlFZ oU9xkw2ao8GL2qqnteYAbbzY8hHURULW8Yxt1N4HGxzZExBB+dPXfoLoY3yX/BEJq3ac OzAyA3Ye759yQ6iP8etYIeFgKaFss0iwWqLyPlgQ/cZS/vG7W2j/Y5KbDPbXqZCJEAbG GEcZv6Dvp/0YgwDNfcTybVPyh7ypB2i/jP9fAnYUe1tyHr2zFNY4D/EHdU8y5h4b6NQj Sfug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691603631; x=1692208431; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VLaMsZyfh3kdnwuZu2mOjfu9FkuAZKI4YATx7fYp+SU=; b=iKPG7ykYEayF4A65/U/fKtd0EEzMVGkPDqeau4Mex81GJA4tcxtQcbkagmOefUnTQR Dz/tz1+HUITT2MOQdcti7JjWDSI1ow4+CUvPKjXkGda8P90udjlZwO+6chesrJ+cDzQZ WN0lZugAIuxo12lBtW3lOo6UugeSECUdBAi3CUZ2kivEYDD6GeNjKNyFJzjlxKAAblPc a5ULiYh5vUHRZBRxeIj+M4bmx3N1+FMN9mzq25KPWVlF/dtL2Ot1CQfONYBybeFLDmp4 DWX9cLFpikcdhjVtqLB97fNFo4b7gyXYO1Z24Mtvrjw3KZZdaJ+Qe1WmIVjUl86sODRg yEfQ== X-Gm-Message-State: AOJu0Yx+2ytuwBfOwVY0pv0/u6VYL3wKgVRautvKtvmr4vDAv/ld95no qNCaVhjBx6JdhkBwRzuPgJvW9Pb8Tgo= X-Google-Smtp-Source: AGHT+IFEofSb+Ozy/WxneQxEqSJesOB0D0tiJVOEDHjwcoPgXEnpm+BJFVGfBBqE47btk5HxeK2xZg== X-Received: by 2002:a0c:f18e:0:b0:635:e0dd:db4c with SMTP id m14-20020a0cf18e000000b00635e0dddb4cmr3057438qvl.25.1691603630964; Wed, 09 Aug 2023 10:53:50 -0700 (PDT) Received: from 3xKetch.hsd1.ma.comcast.net ([2601:180:8300:500::2409]) by smtp.gmail.com with ESMTPSA id x4-20020a0c8e84000000b00623839cba8csm4633646qvb.44.2023.08.09.10.53.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 10:53:50 -0700 (PDT) From: Stevie Alvarez To: linux-trace-devel@vger.kernel.org Cc: Stevie Alvarez , Steven Rostedt , Ross Zwisler Subject: [PATCH v4 4/5] histograms: Add traceeval query Date: Wed, 9 Aug 2023 13:53:37 -0400 Message-ID: <20230809175340.3066-5-stevie.6strings@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809175340.3066-1-stevie.6strings@gmail.com> References: <20230809175340.3066-1-stevie.6strings@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Stevie Alvarez (Google) traceeval_query() fetches the values (aka results) of an entry with keys that match the array of keys provided. The results of a query can be useful for aggregating data between entries/trace events. traceeval_results_release() frees the results fetched by the query. The user must free the results of a successful query. Signed-off-by: Stevie Alvarez (Google) --- include/traceeval-hist.h | 6 ++ src/histograms.c | 161 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h index 63e8b0e..82cfbb1 100644 --- a/include/traceeval-hist.h +++ b/include/traceeval-hist.h @@ -134,4 +134,10 @@ struct traceeval *traceeval_init(const struct traceeval_type *keys, void traceeval_release(struct traceeval *teval); +int traceeval_query(struct traceeval *teval, const union traceeval_data *keys, + union traceeval_data **results); + +void traceeval_results_release(struct traceeval *teval, + union traceeval_data *results); + #endif /* __LIBTRACEEVAL_HIST_H__ */ diff --git a/src/histograms.c b/src/histograms.c index ed631b0..1b6e3a0 100644 --- a/src/histograms.c +++ b/src/histograms.c @@ -527,3 +527,164 @@ void traceeval_release(struct traceeval *teval) teval->val_types = NULL; free(teval); } + +/* + * Find the entry that @keys corresponds to within @teval. + * + * Returns 1 on success, 0 if no match found, -1 on error. + */ +static int get_entry(struct traceeval *teval, const union traceeval_data *keys, + struct entry **result) +{ + struct hist_table *hist; + struct entry *entry; + int check = 0; + int i; + + if (!teval || !keys) + return -1; + + hist = teval->hist; + for (i = 0, entry = hist->map; i < hist->nr_entries; entry = &hist->map[++i]) { + check = compare_traceeval_data_set(entry->keys, keys, + teval->key_types, teval->nr_key_types); + + if (!check) + continue; + break; + } + + if (check > 0) + *result = entry; + return check; +} + +/* + * Copy @orig to @copy with respect to @type. + * + * Return 0 on success, -1 on error. + */ +static int copy_traceeval_data(struct traceeval_type *type, + const union traceeval_data *orig, + union traceeval_data *copy) +{ + *copy = *orig; + + if (type->type == TRACEEVAL_TYPE_STRING) { + copy->string = NULL; + + if (orig->string) + copy->string = strdup(orig->string); + else + return 0; + + if (!copy->string) + return -1; + } + + return 0; +} + +/* + * Free @data with respect to @size and @type. + * + * Does not call dyn_release on type TRACEEVAL_TYPE_DYNAMIC. + */ +static void data_release(size_t size, union traceeval_data **data, + struct traceeval_type *type) +{ + for (size_t i = 0; i < size; i++) { + if (type[i].type == TRACEEVAL_TYPE_STRING) + free((*data)[i].string); + } + free(*data); + *data = NULL; +} + +/* + * Copy @orig to @copy with respect to @size and @type. + * + * Returns 1 on success, -1 on error. + */ +static int copy_traceeval_data_set(size_t size, struct traceeval_type *type, + const union traceeval_data *orig, + union traceeval_data **copy) +{ + size_t i; + + *copy = NULL; + if (!size) + return 1; + + *copy = calloc(size, sizeof(**copy)); + if (!*copy) + return -1; + + for (i = 0; i < size; i++) { + if (copy_traceeval_data(type + i, orig + i, (*copy) + i)) + goto fail; + } + + return 1; + +fail: + data_release(i, copy, type); + return -1; +} + + +/* + * traceeval_query - find the last instance defined by the keys + * @teval: The descriptor to search from + * @keys: A list of data to look for + * @results: A pointer to where to place the results (if found) + * + * This does a lookup for an instance within the traceeval data. + * The @keys is an array defined by the keys declared in traceeval_init(). + * The @keys will return an item that had the same keys when it was + * inserted by traceeval_insert(). The @keys here follow the same rules + * as the keys for traceeval_insert(). + * + * Note, when the caller is done with @results, it must call + * traceeval_results_release() on it. + * + * Returns 1 if found, 0 if not found, and -1 on error. + */ +int traceeval_query(struct traceeval *teval, const union traceeval_data *keys, + union traceeval_data **results) +{ + struct entry *entry; + int check; + + if (!teval || !keys || !results) + return -1; + + /* find key and copy its corresponding value pair */ + if ((check = get_entry(teval, keys, &entry)) < 1) + return check; + return copy_traceeval_data_set(teval->nr_val_types, teval->val_types, + entry->vals, results); +} + +/* + * traceeval_results_release - release the results return by traceeval_query() + * @teval: The descriptor used in traceeval_query() + * @results: The results returned by traceeval_query() + * + * The @results returned by traceeval_query() is owned by @teval, and + * how it manages it is implementation specific. The caller should not + * worry about it. When the caller of traceeval_query() is done with + * the @results, it must call traceeval_results_release() on it to + * allow traceeval to clean up its references. + */ +void traceeval_results_release(struct traceeval *teval, + union traceeval_data *results) +{ + if (!teval || !results) { + if (!results) + print_err("Results to be freed without accompanied traceeval instance!"); + return; + } + + data_release(teval->nr_val_types, &results, teval->val_types); +} From patchwork Wed Aug 9 17:53:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stevie Alvarez X-Patchwork-Id: 13348280 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 86EF6C04FDF for ; Wed, 9 Aug 2023 17:53:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229978AbjHIRxy (ORCPT ); Wed, 9 Aug 2023 13:53:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229913AbjHIRxy (ORCPT ); Wed, 9 Aug 2023 13:53:54 -0400 Received: from mail-yw1-x112f.google.com (mail-yw1-x112f.google.com [IPv6:2607:f8b0:4864:20::112f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3286510D2 for ; Wed, 9 Aug 2023 10:53:53 -0700 (PDT) Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-58439daf39fso1893407b3.1 for ; Wed, 09 Aug 2023 10:53:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691603632; x=1692208432; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=H/GVNR1+f6ibHYECjkTJhLM5qBcdko90xedLKCgtzDM=; b=QVHc80OLn9QbsKPVPTQNZMCUdSU7ViidHsRHuSNJnXpronG6y463ZphIJD9lrok3Ub zx5ExzsUrBqcqgXAHs62HhCkXumOJe1Kr3grZyqfOHEYTFM+NRleX7tJvgf+HG65YMOO QxcAC6ZicqLufFE5jLLvzvO1FmExWdgGWYPW8Nm6xTjE7rTv2iiRVf0xiMBxllEz3paR HGntegmAtzEK6SL6+oXi4VhCJMrmG0h5uAJolz1IVx0TtYLAKrfDyVL03xngDfjoQ0Tp u2go1GUkiFkFStmUHw0BSW2JMDkmj+eCo+OkVT7lgvJXlL6ZvZDwj4regKnWYz8rju/P Wq4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691603632; x=1692208432; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H/GVNR1+f6ibHYECjkTJhLM5qBcdko90xedLKCgtzDM=; b=dtCiwYQZTwo5RIuYPtyXzRTdDxaG9sAm1EEFbRb1e3LPJSoMGfiljXgggmcMSFUik0 uibQd+4U2xr+Vs9TZuou7bBSRRXMH7yvCv1MwrlCNlPwSuYr9MMcb6n47Qvz2EX5E+NL UgpvnYTdEmEOp/h4cMK8WXUVXUHPDyCHa1t+IH1kaZmEaVbTcSUZUTHle7edjJlcDCQF 2V1A/Nr0/CgoVlGCoaI2ZBPps/8Ydu/MEHPm4/QmD/R33p3zL9LijYRg8dlDIYTYzRyI F7gG/47x2iGgTGkN1abxKxT1e7owBlBWg9oiZ4mzmhq1a6wa7H/IeHrA/x2V41P2dKg4 eA3w== X-Gm-Message-State: AOJu0Yy/sxqs8WF1qo1Un2dRI1CoXihAfYF36Bn9CUdTiJ2ipkyf7PHI 2dBFTzy3Yv0ounN3BqeXNN1rAxSiZWo= X-Google-Smtp-Source: AGHT+IGPM1wmwNCP17/C6n27AFhli8woEieVQGra3AZ2A7p9Fo3lrwdA5hMT9pj/YX/ie0HLVFlaAQ== X-Received: by 2002:a81:914e:0:b0:576:777f:28bc with SMTP id i75-20020a81914e000000b00576777f28bcmr13341ywg.21.1691603631982; Wed, 09 Aug 2023 10:53:51 -0700 (PDT) Received: from 3xKetch.hsd1.ma.comcast.net ([2601:180:8300:500::2409]) by smtp.gmail.com with ESMTPSA id x4-20020a0c8e84000000b00623839cba8csm4633646qvb.44.2023.08.09.10.53.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 10:53:51 -0700 (PDT) From: Stevie Alvarez To: linux-trace-devel@vger.kernel.org Cc: Stevie Alvarez , Steven Rostedt , Ross Zwisler Subject: [PATCH v4 5/5] histograms: Add traceeval insert Date: Wed, 9 Aug 2023 13:53:38 -0400 Message-ID: <20230809175340.3066-6-stevie.6strings@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809175340.3066-1-stevie.6strings@gmail.com> References: <20230809175340.3066-1-stevie.6strings@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Stevie Alvarez (Google) traceeval_insert() updates the provided struct traceeval's histogram. If an entry that exists with a keys field that match the keys argument, the entries vals field are updated with a copy of the vals argument. If such an entry does not exist, a new entry is added to the histogram, with respect to the keys and vals arguments. Signed-off-by: Stevie Alvarez (Google) --- include/traceeval-hist.h | 4 ++ src/histograms.c | 114 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h index 82cfbb1..08e0696 100644 --- a/include/traceeval-hist.h +++ b/include/traceeval-hist.h @@ -134,6 +134,10 @@ struct traceeval *traceeval_init(const struct traceeval_type *keys, void traceeval_release(struct traceeval *teval); +int traceeval_insert(struct traceeval *teval, + const union traceeval_data *keys, + const union traceeval_data *vals); + int traceeval_query(struct traceeval *teval, const union traceeval_data *keys, union traceeval_data **results); diff --git a/src/histograms.c b/src/histograms.c index 1b6e3a0..a159892 100644 --- a/src/histograms.c +++ b/src/histograms.c @@ -688,3 +688,117 @@ void traceeval_results_release(struct traceeval *teval, data_release(teval->nr_val_types, &results, teval->val_types); } + +/* + * Create a new entry in @teval with respect to @keys and @vals. + * + * Returns 0 on success, -1 on error. + */ +static int create_entry(struct traceeval *teval, + const union traceeval_data *keys, + const union traceeval_data *vals) +{ + union traceeval_data *new_keys; + union traceeval_data *new_vals; + struct entry *tmp_map; + struct hist_table *hist = teval->hist; + + /* copy keys */ + if (copy_traceeval_data_set(teval->nr_key_types, teval->key_types, + keys, &new_keys) == -1) + return -1; + + /* copy vals */ + if (copy_traceeval_data_set(teval->nr_val_types, teval->val_types, + vals, &new_vals) == -1) + goto fail_vals; + + /* create new entry */ + tmp_map = realloc(hist->map, ++hist->nr_entries * sizeof(*tmp_map)); + if (!tmp_map) + goto fail; + tmp_map->keys = new_keys; + tmp_map->vals = new_vals; + hist->map = tmp_map; + + return 0; + +fail: + data_release(teval->nr_val_types, &new_vals, teval->val_types); + +fail_vals: + data_release(teval->nr_key_types, &new_keys, teval->key_types); + return -1; +} + +/* + * Update @entry's vals field with a copy of @vals, with respect to @teval. + * + * Frees the old vals field of @entry, unless an error occurs. + * + * Return 0 on success, -1 on error. + */ +static int update_entry(struct traceeval *teval, struct entry *entry, + const union traceeval_data *vals) +{ + union traceeval_data *new_vals; + + if (copy_traceeval_data_set(teval->nr_val_types, teval->val_types, + vals, &new_vals) == -1) + return -1; + + clean_data_set(entry->vals, teval->val_types, teval->nr_val_types); + entry->vals = new_vals; + return 0; +} + +/* + * traceeval_insert - insert an item into the traceeval descriptor + * @teval: The descriptor to insert into + * @keys: The list of keys that defines what is being inserted. + * @vals: The list of values that defines what is being inserted. + * + * The @keys is an array that holds the data in the order of the keys + * passed into traceeval_init(). That is, if traceeval_init() had + * keys = { { .type = TRACEEVAL_STRING }, { .type = TRACEEVAL_NUMBER_8 }, + * { .type = TRACEEVAL_NONE } }; then the @keys array passed in must + * be a string (char *) followed by a 8 byte number (char). + * + * The @keys and @vals are only examined to where it expects data. That is, + * if the traceeval_init() keys had 3 items where the first two was defining + * data, and the last one was the TRACEEVAL_TYPE_NONE, then the @keys + * here only needs to be an array of 2, inserting the two items defined + * by traceeval_init(). The same goes for @vals. + * + * If an entry with keys that match @keys exists, it's vals field is freed and + * set to a copy of @vals. This process calls dyn_release() on any data of + * type TRACEEVAL_TYPE_DYNAMIC. + * Otherwise, a new entry is created with copies of @keys and @vals. + * + * For all elements of @keys and @vals that correspond to a struct + * traceeval_type of type TRACEEVAL_TYPE_STRING, the string field must be set + * a valid pointer or NULL. + * + * On error, @teval is left unchanged. + * + * Returns 0 on success, and -1 on error. + */ +int traceeval_insert(struct traceeval *teval, + const union traceeval_data *keys, + const union traceeval_data *vals) +{ + struct entry *entry; + int check; + + entry = NULL; + check = get_entry(teval, keys, &entry); + + if (check == -1) + return check; + + /* insert key-value pair */ + if (check == 0) + return create_entry(teval, keys, vals); + else + return update_entry(teval, entry, vals); +}