From: Ross Zwisler <zwisler@google.com>
To: Stevie Alvarez <stevie.6strings@gmail.com>
Cc: linux-trace-devel@vger.kernel.org, Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [PATCH 4/5] histograms: traceeval compare
Date: Wed, 2 Aug 2023 16:51:36 -0600 [thread overview]
Message-ID: <20230802225136.GD2416079@google.com> (raw)
In-Reply-To: <20230728190515.23088-4-stevie.6strings@gmail.com>
On Fri, Jul 28, 2023 at 03:04:39PM -0400, Stevie Alvarez wrote:
> From: "Stevie Alvarez (Google)" <stevie.6strings@gmail.com>
>
> traceeval_compare() compares two struct traceeval instances for
> equality. This suite of comparitors was made for testing purposes.
>
> Signed-off-by: Stevie Alvarez (Google) <stevie.6strings@gmail.com>
> ---
> src/histograms.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 176 insertions(+), 2 deletions(-)
>
> diff --git a/src/histograms.c b/src/histograms.c
> index f46a0e0..5f1c7ef 100644
> --- a/src/histograms.c
> +++ b/src/histograms.c
> @@ -20,6 +20,19 @@
> #define for_each_key(i, keys) \
> for (i = 0; (keys)[(i)].type != TRACEEVAL_TYPE_NONE; (i)++)
>
> +/**
> + * 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;
> @@ -56,10 +69,171 @@ static void print_err(const char *fmt, ...)
> fprintf(stderr, "\n");
> }
>
> -// TODO
> +/**
> + * Return 0 if @orig and @copy are the same, 1 otherwise.
> + */
> +static int compare_traceeval_type(struct traceeval_type *orig,
> + struct traceeval_type *copy)
> +{
> + int o_name_null;
> + int c_name_null;
> +
> + // same memory/null
> + if (orig == copy)
> + return 0;
> +
> + size_t i = 0;
Best practice is to put all the variable definitions at the top of the
function.
> + do {
> + if (orig[i].type != copy[i].type)
> + return 1;
> + if (orig[i].type == TRACEEVAL_TYPE_NONE)
> + return 0;
> + if (orig[i].flags != copy[i].flags)
> + return 1;
> + if (orig[i].id != copy[i].id)
> + return 1;
> + if (orig[i].dyn_release != copy[i].dyn_release)
> + return 1;
> + if (orig[i].dyn_cmp != copy[i].dyn_cmp)
> + return 1;
> +
> + // make sure both names are same type
> + o_name_null = !orig[i].name;
> + c_name_null = !copy[i].name;
> + if (o_name_null != c_name_null)
> + return 1;
> + if (o_name_null)
> + continue;
> + if (strcmp(orig[i].name, copy[i].name) != 0)
> + return 1;
> + } while (orig[i++].type != TRACEEVAL_TYPE_NONE);
> +
> + return 0;
> +}
<snip>
> +/**
> + * Return 0 if struct hist_table of @orig and @copy are the same, 1 if not,
> + * and -1 on error.
> + */
> +static int compare_hist(struct traceeval *orig, struct traceeval *copy)
> +{
> + struct hist_table *o_hist = orig->hist;
> + struct hist_table *c_hist = copy->hist;
> + int cnt = !(o_hist->nr_entries == c_hist->nr_entries);
> + if (cnt)
> + return 1;
> +
> + for (size_t i = 0; i < o_hist->nr_entries; i++) {
> + // cmp each entry
> + compare_entries(&o_hist->map[i], &c_hist->map[i], orig);
Need to check the return value of 'compare_entries()' and return it if it's
nonzero.
> + }
> + return 0;
> +}
> +
> +/**
> + * Return 0 if @orig and @copy are the same, 1 if not, -1 if error.
> + */
> int traceeval_compare(struct traceeval *orig, struct traceeval *copy)
> {
> - return -1;
> + if ((!orig) || (!copy))
No need for parens, this can just be:
if (!orig || !copy)
return -1;
> + return -1;
> +
> + int keys = compare_traceeval_type(orig->def_keys, copy->def_keys);
> + int vals = compare_traceeval_type(orig->def_vals, copy->def_vals);
> + int hists = compare_hist(orig, copy);
> +
> + return (keys || vals || hists);
> }
>
> /**
> --
> 2.41.0
>
next prev parent reply other threads:[~2023-08-02 22:51 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-28 19:04 [PATCH 1/5] histograms: initial histograms interface Stevie Alvarez
2023-07-28 19:04 ` [PATCH 2/5] histograms: traceeval initialize Stevie Alvarez
2023-07-28 22:03 ` Steven Rostedt
2023-08-02 21:07 ` Ross Zwisler
2023-08-02 21:39 ` Steven Rostedt
2023-07-28 19:04 ` [PATCH 3/5] histograms: traceeval release Stevie Alvarez
2023-07-28 22:07 ` Steven Rostedt
2023-08-02 21:54 ` Ross Zwisler
2023-08-02 22:20 ` Steven Rostedt
2023-08-02 22:21 ` Steven Rostedt
2023-07-28 19:04 ` [PATCH 4/5] histograms: traceeval compare Stevie Alvarez
2023-07-28 22:25 ` Steven Rostedt
2023-08-02 22:51 ` Ross Zwisler [this message]
2023-07-28 19:04 ` [PATCH 5/5] histograms: Add struct traceeval unit tests Stevie Alvarez
2023-07-28 22:30 ` Steven Rostedt
2023-08-03 16:27 ` Ross Zwisler
2023-07-28 20:25 ` [PATCH 1/5] histograms: initial histograms interface Steven Rostedt
2023-07-31 20:53 ` Stevie Alvarez
2023-07-31 21:04 ` Steven Rostedt
2023-08-01 19:08 ` Stevie Alvarez
2023-08-01 19:29 ` Steven Rostedt
2023-08-01 0:36 ` Ross Zwisler
2023-08-01 1:25 ` 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=20230802225136.GD2416079@google.com \
--to=zwisler@google.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=stevie.6strings@gmail.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).