Skip to contentSkip to conversation

Action clear() should clear only one aggregation

PATCH Superseded [email protected] 2 participants 7 messages Aug 14, 2024 – Aug 15, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Action clear() should clear only one aggregation

From: Eugene Loh <[email protected]>

FIXME: Is libdtrace/libdtrace.ver correctly updated?

In dt_clear(), we:

*) extract an aggregation ID (dtrace_aggid_t)

*) increment the gen counter for this aggregation ID

*) clear the consumer copy of the aggregation, using:

      /* Also clear our own copy of the data, in case it gets printed. */
      dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, dtp);

However, this clears all the aggregations. While the next snapshot of the aggregations will refresh our copies of the aggregations -- after all, dtrace_aggregate_snap() itself starts with a call to dtrace_aggregate_clear() -- if we print aggregations before the next snapshot, we would display an erroneously cleared aggregation.

Therefore, change dt_clear() to clear only those aggregations that correspond to the specified aggregation ID.

Note that while we have "aggregation-walk" functions like

dtrace_aggregate_walk() dtrace_aggregate_walk_joined() dtrace_aggregate_walk_*sorted() (many of these)

the first one (dtrace_aggregate_walk) is used only twice:

- in dtrace_aggregate_clear() to clear all aggregations

- in dt_clear(), where we should clear only a specific aggregation ID

Therefore, rename dtrace_aggregate_walk() to be dtrace_aggregate_walk_oneid() that takes an aggid argument. If aggid==DTRACE_AGGVARIDNONE, walk all aggregations. Otherwise, walk only those aggregations that match the specified id.

Though trunc() appears not to be similarly afflicted, add a test for it too.

Orabug: 36900180

---
 libdtrace/dt_aggregate.c           |  8 ++++++--
 libdtrace/dt_consume.c             |  2 +-
 libdtrace/dtrace.h                 |  4 ++--
 libdtrace/libdtrace.ver            |  4 ++--
 test/unittest/aggs/tst.clear-one.d | 33 ++++++++++++++++++++++++++++++
 test/unittest/aggs/tst.clear-one.r | 13 ++++++++++++
 test/unittest/aggs/tst.trunc-one.d | 33 ++++++++++++++++++++++++++++++
 test/unittest/aggs/tst.trunc-one.r | 10 +++++++++
 8 files changed, 100 insertions(+), 7 deletions(-)
 create mode 100644 test/unittest/aggs/tst.clear-one.d
 create mode 100644 test/unittest/aggs/tst.clear-one.r
 create mode 100644 test/unittest/aggs/tst.trunc-one.d
 create mode 100644 test/unittest/aggs/tst.trunc-one.r
libdtrace/dt_aggregate.c +6 -2
index 51f0d622..e06025de 100644
@@ -1278,7 +1278,8 @@ dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
 }
 
 int
-dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
+dtrace_aggregate_walk_oneid(dtrace_hdl_t *dtp, dtrace_aggregate_f *func,
+			    void *arg, dtrace_aggid_t aid)
 {
 	dt_ahashent_t *h, *next;
 	dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
@@ -1291,6 +1292,8 @@ dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
 		 */
 		next = h->dtahe_nextall;
 
+		if (aid != DTRACE_AGGVARIDNONE && aid != h->dtahe_data.dtada_desc->dtagd_varid)
+			continue;
 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
 			return -1;
 	}
@@ -1820,10 +1823,11 @@ dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
 	return 0;
 }
 
+/* FIXME: dtrace_aggregate_clear() is called from only one site.  Should we inline it? */
 void
 dtrace_aggregate_clear(dtrace_hdl_t *dtp)
 {
-	dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, dtp);
+	dtrace_aggregate_walk_oneid(dtp, dt_aggregate_clear_one, dtp, DTRACE_AGGVARIDNONE);
 }
 
 void
libdtrace/dt_consume.c +1 -1
index 8d8c0f54..238b93ca 100644
@@ -1551,7 +1551,7 @@ dt_clear(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
 		return -1;
 
 	/* Also clear our own copy of the data, in case it gets printed. */
-	dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, dtp);
+	dtrace_aggregate_walk_oneid(dtp, dt_aggregate_clear_one, dtp, aid);
 
 	return 0;
 }
libdtrace/dtrace.h +2 -2
index 1f54c10c..4df3ebf4 100644
@@ -396,8 +396,8 @@ extern int dtrace_aggregate_snap(dtrace_hdl_t *dtp);
 extern int dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
     dtrace_aggregate_walk_f *func); /* func must take dtrace_print_aggdata_t */
 
-extern int dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func,
-    void *arg);
+extern int dtrace_aggregate_walk_oneid(dtrace_hdl_t *dtp, dtrace_aggregate_f *func,
+    void *arg, dtrace_aggid_t);
 
 extern int dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp,
     dtrace_aggid_t *aggvars, int naggvars,
libdtrace/libdtrace.ver +2 -2
index edbd5a1d..1510ad95 100644
@@ -1,6 +1,6 @@
 #
 # Oracle Linux DTrace.
-# Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
 # Licensed under the Universal Permissive License v 1.0 as shown at
 # http://oss.oracle.com/licenses/upl.
 
@@ -10,7 +10,7 @@ LIBDTRACE_1.0 {
 	dtrace_aggregate_clear;
 	dtrace_aggregate_print;
 	dtrace_aggregate_snap;
-	dtrace_aggregate_walk;
+	dtrace_aggregate_walk_oneid;
 	dtrace_aggregate_walk_joined;
 	dtrace_aggregate_walk_keyrevsorted;
 	dtrace_aggregate_walk_keysorted;
test/unittest/aggs/tst.clear-one.d new file +33 -0
new file mode 100644
index 00000000..f96c2db0
@@ -0,0 +1,33 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+/*
+ * ASSERTION: Clearing one aggregation clears only that one.
+ *
+ * SECTION: Aggregations/Clearing aggregations
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+BEGIN
+{
+	@a[1] = sum(100);
+	@a[2] = sum( 20);
+	@a[3] = sum(  3);
+	@b[4] = sum(400);
+	@b[5] = sum( 50);
+	@b[6] = sum(  6);
+	@c[7] = sum(700);
+	@c[8] = sum( 80);
+	@c[9] = sum(  9);
+	clear(@b);
+	printa(@a);
+	printa(@b);
+	printa(@c);
+	exit(0);
+}
test/unittest/aggs/tst.clear-one.r new file +13 -0
new file mode 100644
index 00000000..101d173f
@@ -0,0 +1,13 @@
+
+        3                3
+        2               20
+        1              100
+
+        4                0
+        5                0
+        6                0
+
+        9                9
+        8               80
+        7              700
+
test/unittest/aggs/tst.trunc-one.d new file +33 -0
new file mode 100644
index 00000000..0556bb34
@@ -0,0 +1,33 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+/*
+ * ASSERTION: Truncating one aggregation truncates only that one.
+ *
+ * SECTION: Aggregations/Clearing aggregations
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+BEGIN
+{
+	@a[1] = sum(100);
+	@a[2] = sum( 20);
+	@a[3] = sum(  3);
+	@b[4] = sum(400);
+	@b[5] = sum( 50);
+	@b[6] = sum(  6);
+	@c[7] = sum(700);
+	@c[8] = sum( 80);
+	@c[9] = sum(  9);
+	trunc(@b);
+	printa(@a);
+	printa(@b);
+	printa(@c);
+	exit(0);
+}
test/unittest/aggs/tst.trunc-one.r new file +10 -0
new file mode 100644
index 00000000..a611026d
@@ -0,0 +1,10 @@
+
+        3                3
+        2               20
+        1              100
+
+
+        9                9
+        8               80
+        7              700
+
Signature · 1 line
2.43.5
#1 lore raw
Quoted 32 lines · On Tue, Aug 13, 2024 at 10:59:25PM -0400, [email protected] wrote:

From: Eugene Loh <[email protected]>

FIXME: Is libdtrace/libdtrace.ver correctly updated?

In dt_clear(), we:

*) extract an aggregation ID (dtrace_aggid_t)

*) increment the gen counter for this aggregation ID

*) clear the consumer copy of the aggregation, using:

      /* Also clear our own copy of the data, in case it gets printed. */
      dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, dtp);

However, this clears all the aggregations. While the next snapshot of the aggregations will refresh our copies of the aggregations -- after all, dtrace_aggregate_snap() itself starts with a call to dtrace_aggregate_clear() -- if we print aggregations before the next snapshot, we would display an erroneously cleared aggregation.

Therefore, change dt_clear() to clear only those aggregations that correspond to the specified aggregation ID.

Note that while we have "aggregation-walk" functions like

dtrace_aggregate_walk() dtrace_aggregate_walk_joined() dtrace_aggregate_walk_*sorted() (many of these)

the first one (dtrace_aggregate_walk) is used only twice:

Quoted 8 lines

- in dtrace_aggregate_clear() to clear all aggregations

- in dt_clear(), where we should clear only a specific aggregation ID

Therefore, rename dtrace_aggregate_walk() to be dtrace_aggregate_walk_oneid() that takes an aggid argument. If aggid==DTRACE_AGGVARIDNONE, walk all aggregations. Otherwise, walk only those aggregations that match the specified id.

I think that keeping dtrace_aggregate_walk() as-is would be better, along with adding a function dtrace_aggregate_clear_one(dtp, aid) that performs the lookup from aid to dtrae_aggdata_t *agd, and then calls dt__aggregate_clear_one(agd, dtp) to do the actual clearing. There is no need to go through a walker function when we already know we need to operate on just one aggregation.

Quoted 221 lines

Though trunc() appears not to be similarly afflicted, add a test for it too.

Orabug: 36900180

---
 libdtrace/dt_aggregate.c           |  8 ++++++--
 libdtrace/dt_consume.c             |  2 +-
 libdtrace/dtrace.h                 |  4 ++--
 libdtrace/libdtrace.ver            |  4 ++--
 test/unittest/aggs/tst.clear-one.d | 33 ++++++++++++++++++++++++++++++
 test/unittest/aggs/tst.clear-one.r | 13 ++++++++++++
 test/unittest/aggs/tst.trunc-one.d | 33 ++++++++++++++++++++++++++++++
 test/unittest/aggs/tst.trunc-one.r | 10 +++++++++
 8 files changed, 100 insertions(+), 7 deletions(-)
 create mode 100644 test/unittest/aggs/tst.clear-one.d
 create mode 100644 test/unittest/aggs/tst.clear-one.r
 create mode 100644 test/unittest/aggs/tst.trunc-one.d
 create mode 100644 test/unittest/aggs/tst.trunc-one.r
libdtrace/dt_aggregate.c +6 -2
index 51f0d622..e06025de 100644
@@ -1278,7 +1278,8 @@ dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
 }
 
 int
-dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
+dtrace_aggregate_walk_oneid(dtrace_hdl_t *dtp, dtrace_aggregate_f *func,
+			    void *arg, dtrace_aggid_t aid)
 {
 	dt_ahashent_t *h, *next;
 	dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
@@ -1291,6 +1292,8 @@ dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
 		 */
 		next = h->dtahe_nextall;
 
+		if (aid != DTRACE_AGGVARIDNONE && aid != h->dtahe_data.dtada_desc->dtagd_varid)
+			continue;
 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
 			return -1;
 	}
@@ -1820,10 +1823,11 @@ dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
 	return 0;
 }
 
+/* FIXME: dtrace_aggregate_clear() is called from only one site.  Should we inline it? */
 void
 dtrace_aggregate_clear(dtrace_hdl_t *dtp)
 {
-	dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, dtp);
+	dtrace_aggregate_walk_oneid(dtp, dt_aggregate_clear_one, dtp, DTRACE_AGGVARIDNONE);
 }
 
 void
libdtrace/dt_consume.c +1 -1
index 8d8c0f54..238b93ca 100644
@@ -1551,7 +1551,7 @@ dt_clear(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
 		return -1;
 
 	/* Also clear our own copy of the data, in case it gets printed. */
-	dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, dtp);
+	dtrace_aggregate_walk_oneid(dtp, dt_aggregate_clear_one, dtp, aid);
 
 	return 0;
 }
libdtrace/dtrace.h +2 -2
index 1f54c10c..4df3ebf4 100644
@@ -396,8 +396,8 @@ extern int dtrace_aggregate_snap(dtrace_hdl_t *dtp);
 extern int dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
     dtrace_aggregate_walk_f *func); /* func must take dtrace_print_aggdata_t */
 
-extern int dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func,
-    void *arg);
+extern int dtrace_aggregate_walk_oneid(dtrace_hdl_t *dtp, dtrace_aggregate_f *func,
+    void *arg, dtrace_aggid_t);
 
 extern int dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp,
     dtrace_aggid_t *aggvars, int naggvars,
libdtrace/libdtrace.ver +2 -2
index edbd5a1d..1510ad95 100644
@@ -1,6 +1,6 @@
 #
 # Oracle Linux DTrace.
-# Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
 # Licensed under the Universal Permissive License v 1.0 as shown at
 # http://oss.oracle.com/licenses/upl.
 
@@ -10,7 +10,7 @@ LIBDTRACE_1.0 {
 	dtrace_aggregate_clear;
 	dtrace_aggregate_print;
 	dtrace_aggregate_snap;
-	dtrace_aggregate_walk;
+	dtrace_aggregate_walk_oneid;
 	dtrace_aggregate_walk_joined;
 	dtrace_aggregate_walk_keyrevsorted;
 	dtrace_aggregate_walk_keysorted;
test/unittest/aggs/tst.clear-one.d new file +33 -0
new file mode 100644
index 00000000..f96c2db0
@@ -0,0 +1,33 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+/*
+ * ASSERTION: Clearing one aggregation clears only that one.
+ *
+ * SECTION: Aggregations/Clearing aggregations
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+BEGIN
+{
+	@a[1] = sum(100);
+	@a[2] = sum( 20);
+	@a[3] = sum(  3);
+	@b[4] = sum(400);
+	@b[5] = sum( 50);
+	@b[6] = sum(  6);
+	@c[7] = sum(700);
+	@c[8] = sum( 80);
+	@c[9] = sum(  9);
+	clear(@b);
+	printa(@a);
+	printa(@b);
+	printa(@c);
+	exit(0);
+}
test/unittest/aggs/tst.clear-one.r new file +13 -0
new file mode 100644
index 00000000..101d173f
@@ -0,0 +1,13 @@
+
+        3                3
+        2               20
+        1              100
+
+        4                0
+        5                0
+        6                0
+
+        9                9
+        8               80
+        7              700
+
test/unittest/aggs/tst.trunc-one.d new file +33 -0
new file mode 100644
index 00000000..0556bb34
@@ -0,0 +1,33 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+/*
+ * ASSERTION: Truncating one aggregation truncates only that one.
+ *
+ * SECTION: Aggregations/Clearing aggregations
+ */
+/* @@nosort */
+
+#pragma D option quiet
+
+BEGIN
+{
+	@a[1] = sum(100);
+	@a[2] = sum( 20);
+	@a[3] = sum(  3);
+	@b[4] = sum(400);
+	@b[5] = sum( 50);
+	@b[6] = sum(  6);
+	@c[7] = sum(700);
+	@c[8] = sum( 80);
+	@c[9] = sum(  9);
+	trunc(@b);
+	printa(@a);
+	printa(@b);
+	printa(@c);
+	exit(0);
+}
test/unittest/aggs/tst.trunc-one.r new file +10 -0
new file mode 100644
index 00000000..a611026d
@@ -0,0 +1,10 @@
+
+        3                3
+        2               20
+        1              100
+
+
+        9                9
+        8               80
+        7              700
+
Signature · 1 line
2.43.5
author
#2 lore raw
Quoted 4 lines · Kris Van Hees
Quoted 3 lines · On Tue, Aug 13, 2024 at 10:59:25PM -0400, [email protected] wrote:

From: Eugene Loh <[email protected]>

FIXME: Is libdtrace/libdtrace.ver correctly updated?

Incidentally, what has to be done in this file?

Quoted 6 lines

I think that keeping dtrace_aggregate_walk() as-is would be better, along with adding a function dtrace_aggregate_clear_one(dtp, aid) that performs the lookup from aid to dtrae_aggdata_t *agd, and then calls dt__aggregate_clear_one(agd, dtp) to do the actual clearing. There is no need to go through a walker function when we already know we need to operate on just one aggregation.

I'm not convinced.

"Just one aggregation"?  Yes, just one aggid, but possibly many keys.  To narrow down to which agd we want, we need to know the key -- and clear() doesn't give us the key, plus there will generally be more than one key anyhow.

Now, one can argue that in the current implementation and its current hash function, the aggid tells us at least which hash bucket to search.  So we could write a special, reduced loop.  Maybe so, but that would be wed to this particular choice of hash function, and I'd argue the current choice is a bad one.  E.g., consider:

        tick-10 { @pc[uregs[R_PC]] = count() } syscall::write:entry { @writeentry = count() }

Lots of agd's will be sharing one bucket and another agd is sitting all by itself.  We ought to change the hash function, and then we'd have to undo the "search just one bucket" optimization.

PS  Here is an oddity:

    # /usr/sbin/dtrace -n 'BEGIN { @a[1] = count(); @a[2] = count(); clear(@a[1]); exit(0); }'
    dtrace: description 'BEGIN ' matched 1 probe
     CPU     ID                    FUNCTION:NAME
      34      1                           :BEGIN

            1                0
            2                0

You *can* specify the key in the clear()!  But dtrace ignores the key.  Silently.  Same behavior in Solaris.

#3 lore raw
Quoted 16 lines · Eugene Loh
Quoted 4 lines · Kris Van Hees
Quoted 3 lines · On Tue, Aug 13, 2024 at 10:59:25PM -0400, [email protected] wrote:

From: Eugene Loh <[email protected]>

FIXME: Is libdtrace/libdtrace.ver correctly updated?

Incidentally, what has to be done in this file?

Quoted 6 lines

I think that keeping dtrace_aggregate_walk() as-is would be better, along with adding a function dtrace_aggregate_clear_one(dtp, aid) that performs the lookup from aid to dtrae_aggdata_t *agd, and then calls dt__aggregate_clear_one(agd, dtp) to do the actual clearing. There is no need to go through a walker function when we already know we need to operate on just one aggregation.

Quoted 6 lines

I'm not convinced.

"Just one aggregation"?  Yes, just one aggid, but possibly many keys.  To narrow down to which agd we want, we need to know the key -- and clear() doesn't give us the key, plus there will generally be more than one key anyhow.

Ah true. I blanked on the fact that clear(@a) clears that aggregation, and any indexed aggregations with the same name (and thus same aid).

Anyway, given that dtrace_aggregate_walk() is an API function that is exported by libdtrace, we should not change it.

So, I'd propose that instead of introducing a new function, we can make use of the existing dtrace_aggregate_walk() by passing both dtp and the aid as an arg struct. E.g.

typedef struct dt_clear_arg {
	dtrace_hdl_t	*dtp;
	dtrace_aggid_t	aid;
} dt_clear_arg_t;

void
dtrace_aggregate_clear(dtrace_hdl_t *dtp)

{
	dt_clear_arg_t	arg = { dtp, aid };

        dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, &arg);
}

nd then dt_aggregate_clear_one() can check the aid and return if the aid is not IDNOE and does not match the one for the aggregation it is called for. That means you also need to change dt_aggregate_snap_one() but there you can also define that arg and set aid as IDNONE in that because we know that if we call dt_aggregate_clear_one() from here weknow it is for an aggregation that needs to be cleared.

Quoted 26 lines

Now, one can argue that in the current implementation and its current hash function, the aggid tells us at least which hash bucket to search.  So we could write a special, reduced loop.  Maybe so, but that would be wed to this particular choice of hash function, and I'd argue the current choice is a bad one.  E.g., consider:

        tick-10 { @pc[uregs[R_PC]] = count() } syscall::write:entry { @writeentry = count() }

Lots of agd's will be sharing one bucket and another agd is sitting all by itself.  We ought to change the hash function, and then we'd have to undo the "search just one bucket" optimization.

PS  Here is an oddity:

    # /usr/sbin/dtrace -n 'BEGIN { @a[1] = count(); @a[2] = count();
clear(@a[1]); exit(0); }'
    dtrace: description 'BEGIN ' matched 1 probe
     CPU     ID                    FUNCTION:NAME
      34      1                           :BEGIN

            1                0
            2                0

You *can* specify the key in the clear()!  But dtrace ignores the key. Silently.  Same behavior in Solaris.

author
#4 lore raw
Quoted 27 lines · Kris Van Hees
Quoted 14 lines · Eugene Loh
Quoted 4 lines · Kris Van Hees
Quoted 3 lines · On Tue, Aug 13, 2024 at 10:59:25PM -0400, [email protected] wrote:

From: Eugene Loh <[email protected]>

FIXME: Is libdtrace/libdtrace.ver correctly updated?

Incidentally, what has to be done in this file?

Quoted 6 lines

I think that keeping dtrace_aggregate_walk() as-is would be better, along with adding a function dtrace_aggregate_clear_one(dtp, aid) that performs the lookup from aid to dtrae_aggdata_t *agd, and then calls dt__aggregate_clear_one(agd, dtp) to do the actual clearing. There is no need to go through a walker function when we already know we need to operate on just one aggregation.

Quoted 6 lines

I'm not convinced.

"Just one aggregation"?  Yes, just one aggid, but possibly many keys.  To narrow down to which agd we want, we need to know the key -- and clear() doesn't give us the key, plus there will generally be more than one key anyhow.

Ah true. I blanked on the fact that clear(@a) clears that aggregation, and any indexed aggregations with the same name (and thus same aid).

Anyway, given that dtrace_aggregate_walk() is an API function that is exported by libdtrace, we should not change it.

Quoted 8 lines

So, I'd propose that instead of introducing a new function, we can make use of the existing dtrace_aggregate_walk() by passing both dtp and the aid as an arg struct. E.g.

typedef struct dt_clear_arg {
	dtrace_hdl_t	*dtp;
	dtrace_aggid_t	aid;
} dt_clear_arg_t;

Hmm.  Then both dt_consume.c and dt_aggregate.c need to know about this new struct.  So, copy it to both files?  Introduce a new dt_aggregate.h?  Ick.

Maybe the thing to do is use dtrace_aggregate_walk() with a callback in dt_consume.c.  The callback can filter on the agg id and call dt_aggregate_clear_one() accordingly... I *think* that works.  Let me know if you're on board with this approach.

Quoted 41 lines

void
dtrace_aggregate_clear(dtrace_hdl_t *dtp)

{
	dt_clear_arg_t	arg = { dtp, aid };

         dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, &arg);
}

nd then dt_aggregate_clear_one() can check the aid and return if the aid is not IDNOE and does not match the one for the aggregation it is called for. That means you also need to change dt_aggregate_snap_one() but there you can also define that arg and set aid as IDNONE in that because we know that if we call dt_aggregate_clear_one() from here weknow it is for an aggregation that needs to be cleared.

Quoted 26 lines

Now, one can argue that in the current implementation and its current hash function, the aggid tells us at least which hash bucket to search.  So we could write a special, reduced loop.  Maybe so, but that would be wed to this particular choice of hash function, and I'd argue the current choice is a bad one.  E.g., consider:

        tick-10 { @pc[uregs[R_PC]] = count() } syscall::write:entry { @writeentry = count() }

Lots of agd's will be sharing one bucket and another agd is sitting all by itself.  We ought to change the hash function, and then we'd have to undo the "search just one bucket" optimization.

PS  Here is an oddity:

     # /usr/sbin/dtrace -n 'BEGIN { @a[1] = count(); @a[2] = count();
clear(@a[1]); exit(0); }'
     dtrace: description 'BEGIN ' matched 1 probe
      CPU     ID                    FUNCTION:NAME
       34      1                           :BEGIN

            1                0
            2                0

You *can* specify the key in the clear()!  But dtrace ignores the key. Silently.  Same behavior in Solaris.

#5 lore raw
Quoted 33 lines · Eugene Loh
Quoted 31 lines · Kris Van Hees
Quoted 14 lines · Eugene Loh
Quoted 4 lines · Kris Van Hees
Quoted 3 lines · On Tue, Aug 13, 2024 at 10:59:25PM -0400, [email protected] wrote:

From: Eugene Loh <[email protected]>

FIXME: Is libdtrace/libdtrace.ver correctly updated?

Incidentally, what has to be done in this file?

Quoted 6 lines

I think that keeping dtrace_aggregate_walk() as-is would be better, along with adding a function dtrace_aggregate_clear_one(dtp, aid) that performs the lookup from aid to dtrae_aggdata_t *agd, and then calls dt__aggregate_clear_one(agd, dtp) to do the actual clearing. There is no need to go through a walker function when we already know we need to operate on just one aggregation.

Quoted 6 lines

I'm not convinced.

"Just one aggregation"?  Yes, just one aggid, but possibly many keys.  To narrow down to which agd we want, we need to know the key -- and clear() doesn't give us the key, plus there will generally be more than one key anyhow.

Ah true. I blanked on the fact that clear(@a) clears that aggregation, and any indexed aggregations with the same name (and thus same aid).

Anyway, given that dtrace_aggregate_walk() is an API function that is exported by libdtrace, we should not change it.

So, I'd propose that instead of introducing a new function, we can make use of the existing dtrace_aggregate_walk() by passing both dtp and the aid as an arg struct. E.g.

Quoted 7 lines
Quoted 4 lines
typedef struct dt_clear_arg {
	dtrace_hdl_t	*dtp;
	dtrace_aggid_t	aid;
} dt_clear_arg_t;

Hmm.  Then both dt_consume.c and dt_aggregate.c need to know about this new struct.  So, copy it to both files?  Introduce a new dt_aggregate.h?  Ick.

It would be appropriate to add it to dt_aggregate.h because it is a struct that defines the callback argument. There is enough precedent for that - in fact, it is the way this is typically done.

Quoted 4 lines

Maybe the thing to do is use dtrace_aggregate_walk() with a callback in dt_consume.c.  The callback can filter on the agg id and call dt_aggregate_clear_one() accordingly... I *think* that works.  Let me know if you're on board with this approach.

I think I'd rather see the dt_clear_arg_t thing. I don't think a callback in dt_consume.c would work without this struct argument because you still would not be able to get both dtp and aid passed to that callback.

Quoted 41 lines
Quoted 41 lines

void
dtrace_aggregate_clear(dtrace_hdl_t *dtp)

{
	dt_clear_arg_t	arg = { dtp, aid };

         dtrace_aggregate_walk(dtp, dt_aggregate_clear_one, &arg);
}

nd then dt_aggregate_clear_one() can check the aid and return if the aid is not IDNOE and does not match the one for the aggregation it is called for. That means you also need to change dt_aggregate_snap_one() but there you can also define that arg and set aid as IDNONE in that because we know that if we call dt_aggregate_clear_one() from here weknow it is for an aggregation that needs to be cleared.

Quoted 26 lines

Now, one can argue that in the current implementation and its current hash function, the aggid tells us at least which hash bucket to search.  So we could write a special, reduced loop.  Maybe so, but that would be wed to this particular choice of hash function, and I'd argue the current choice is a bad one.  E.g., consider:

        tick-10 { @pc[uregs[R_PC]] = count() } syscall::write:entry { @writeentry = count() }

Lots of agd's will be sharing one bucket and another agd is sitting all by itself.  We ought to change the hash function, and then we'd have to undo the "search just one bucket" optimization.

PS  Here is an oddity:

     # /usr/sbin/dtrace -n 'BEGIN { @a[1] = count(); @a[2] = count();
clear(@a[1]); exit(0); }'
     dtrace: description 'BEGIN ' matched 1 probe
      CPU     ID                    FUNCTION:NAME
       34      1                           :BEGIN

            1                0
            2                0

You *can* specify the key in the clear()!  But dtrace ignores the key. Silently.  Same behavior in Solaris.

author
#6 lore raw
Quoted 19 lines · Kris Van Hees
Quoted 8 lines · Eugene Loh
Quoted 7 lines · Kris Van Hees

given that dtrace_aggregate_walk() is an API function that is exported by libdtrace, we should not change it.

So, I'd propose that instead of introducing a new function, we can make use of the existing dtrace_aggregate_walk() by passing both dtp and the aid as an arg struct. E.g.

Quoted 6 lines
Quoted 4 lines
typedef struct dt_clear_arg {
	dtrace_hdl_t	*dtp;
	dtrace_aggid_t	aid;
} dt_clear_arg_t;

Hmm.  Then both dt_consume.c and dt_aggregate.c need to know about this new struct.  So, copy it to both files?  Introduce a new dt_aggregate.h?  Ick.

It would be appropriate to add it to dt_aggregate.h because it is a struct that defines the callback argument. There is enough precedent for that - in fact, it is the way this is typically done.

Quoted 7 lines
Quoted 4 lines

Maybe the thing to do is use dtrace_aggregate_walk() with a callback in dt_consume.c.  The callback can filter on the agg id and call dt_aggregate_clear_one() accordingly... I *think* that works.  Let me know if you're on board with this approach.

I think I'd rather see the dt_clear_arg_t thing. I don't think a callback in dt_consume.c would work without this struct argument because you still would not be able to get both dtp and aid passed to that callback.

My point was not to avoid the struct arg, but only that by putting the callback in dt_consume.c the struct declaration could be in dt_consume.c instead of introducing a new file dt_aggregate.h. Anyhow, v2 of the patch coming up with a new dt_aggregate.h file.