Omit an aggregation record if [u][sym|mod] translation fails
PATCH 1/2 Omit an aggregation record if [u][sym|mod] translation fails
From: Eugene Loh <[email protected]>
An aggregation key can be a sym(), mod(), usym(), or umod() translation of an address. It is passed from producer to user space as an address, and the consumer must translate the address. It is possible for the translation to fail.
Omit a record if the translation fails. This addresses failures seen intest/unittest/profile-n/tst.ufunc.sh test/unittest/profile-n/tst.usym.sh
The problem was that the kernel's aggregation buffers are snapshot multiple times. If a translation ever fails, the raw address is used instead. Later on, when the aggregation is printed, if the translation is successful, the raw key will report a count of 0.
--- libdtrace/dt_aggregate.c | 51 ++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 15 deletions(-)
libdtrace/dt_aggregate.c +36 -15
@@ -317,61 +317,76 @@ dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)return 0 ; } -static void +static int dt_aggregate_usym (dtrace_hdl_t *dtp,uint64_t *data) {uint64_t tgid = data[0 ];uint64_t *pc = &data[1 ];pid_t pid;GElf_Sym sym; +int rc =0 ;if (dtp->dt_vector !=NULL ) -return ; +return -1 ; pid =dt_proc_grab_lock (dtp, tgid,DTRACE_PROC_WAITING |DTRACE_PROC_SHORTLIVED );if (pid <0 ) -return ; +return -1 ;if (dt_Plookup_by_addr (dtp, pid, *pc,NULL , &sym) ==0 ) *pc = sym.st_value; +else + rc =-1 ;dt_proc_release_unlock (dtp, pid); + +return rc; } -static void +static int dt_aggregate_umod (dtrace_hdl_t *dtp,uint64_t *data) {uint64_t tgid = data[0 ];uint64_t *pc = &data[1 ];pid_t pid;const prmap_t *map; +int rc =0 ;if (dtp->dt_vector !=NULL ) -return ; +return -1 ; pid =dt_proc_grab_lock (dtp, tgid,DTRACE_PROC_WAITING |DTRACE_PROC_SHORTLIVED );if (pid <0 ) -return ; +return -1 ;if ((map =dt_Paddr_to_map (dtp, pid, *pc)) !=NULL ) *pc = map->pr_vaddr; +else + rc =-1 ;dt_proc_release_unlock (dtp, pid); + +return rc; } -static void +static int dt_aggregate_sym (dtrace_hdl_t *dtp,uint64_t *data) {GElf_Sym sym;uint64_t *pc = data; +int rc =0 ;if (dtrace_lookup_by_addr (dtp, *pc, &sym,NULL ) ==0 ) *pc = sym.st_value; +else + rc =-1 ; + +return rc; } -static void +static int dt_aggregate_mod (dtrace_hdl_t *dtp,uint64_t *addr) {dt_module_t *dmp;
@@ -385,7 +400,7 @@ dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *addr)* appear more than once in aggregation output). It seems * unlikely that anyone will ever notice or care... */ -return ; +return -1 ; } for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
@@ -400,7 +415,7 @@ dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *addr) dtrace_addr_range_cmp) != NULL) { *addr = dmp->dm_text_addrs[0 ].dar_va; -return ; +return 0 ; } if (dmp->dm_data_addrs != NULL &&
@@ -413,9 +428,11 @@ dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *addr)else *addr = dmp->dm_data_addrs[0 ].dar_va; -return ; +return 0 ; } } + +return -1 ; } static dtrace_aggid_t
@@ -606,16 +623,20 @@ dt_aggregate_snap_one(dtrace_hdl_t *dtp, int aggid, int cpu, const char *key,switch (agg->dtagd_krecs[i].dtrd_action) {case DTRACEACT_USYM : -dt_aggregate_usym (dtp, p); +if (dt_aggregate_usym (dtp, p) ==-1 ) +return 0 ;break ;case DTRACEACT_UMOD : -dt_aggregate_umod (dtp, p); +if (dt_aggregate_umod (dtp, p) ==-1 ) +return 0 ;break ;case DTRACEACT_SYM : -dt_aggregate_sym (dtp, p); +if (dt_aggregate_sym (dtp, p) ==-1 ) +return 0 ;break ;case DTRACEACT_MOD : -dt_aggregate_mod (dtp, p); +if (dt_aggregate_mod (dtp, p) ==-1 ) +return 0 ;break ;default :break ;
Signature · 1 line
2.43.5
PATCH 2/2 Snapshot aggregations just in time
From: Eugene Loh <[email protected]>
Currently, dtrace periodically calls dtrace_work(), which in turn calls dtrace_consume(), which among other things calls dtrace_aggregate_snap(). But aggregations are kept in entirety in the kernel's BPF maps. There is no need to snapshot the aggregations into user space unless we're actually going to do something with aggregations.
Snapshot aggregations just in time -- that is, if there is a clear(), trunc(), or printa() or if aggregations are to be printed at the end of a dtrace session.
Skip the aggrate-slow test. Just-in-time snapshots mean the semantics of aggrate have changed. A fast aggrate means nothing. A slow aggrate means we are supposed to use stale aggregation data, which would be baffling. A future patch is advised to deprecate aggrate entirely.
--- libdtrace/dt_aggregate.c | 7 +++++++ libdtrace/dt_consume.c | 9 +++++++-- libdtrace/dt_impl.h | 1 + test/unittest/options/tst.aggrate-slow.d | 1 + 4 files changed, 16 insertions(+), 2 deletions(-)
libdtrace/dt_aggregate.c +7 -0
@@ -817,6 +817,10 @@ dtrace_aggregate_snap(dtrace_hdl_t *dtp) dtp->dt_lastagg = now; } +if (dtp->dt_haveagg) +return DTRACE_WORKSTATUS_OKAY ; + dtp->dt_haveagg =1 ; +dtrace_aggregate_clear (dtp);for (i =0 ; i < dtp->dt_conf.num_online_cpus; i++) {
@@ -1901,6 +1905,9 @@ dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp, {dtrace_print_aggdata_t pd; + dtp->dt_haveagg =0 ; +dtrace_aggregate_snap (dtp); +if (dtp->dt_maxaggdsize ==0 )return 0 ;
libdtrace/dt_consume.c +7 -2
@@ -2353,11 +2353,15 @@ dt_consume_one_probe(dtrace_hdl_t *dtp, FILE *fp, char *data, uint32_t size, i++;continue ;case DT_ACT_CLEAR : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (dt_clear (dtp, data, rec) !=0 )return DTRACE_WORKSTATUS_ERROR ;continue ;case DT_ACT_TRUNC : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (i == epd->dtdd_nrecs -1 )return dt_set_errno (dtp,EDT_BADTRUNC );
@@ -2519,6 +2523,8 @@ dt_consume_one_probe(dtrace_hdl_t *dtp, FILE *fp, char *data, uint32_t size, func = dtrace_fprintf;break ;case DTRACEACT_PRINTA : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (rec->dtrd_format !=NULL ) func = dtrace_fprinta;else
@@ -3113,8 +3119,7 @@ dtrace_consume(dtrace_hdl_t *dtp, FILE *fp, dtrace_consume_probe_f *pf, } } -if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) -return DTRACE_WORKSTATUS_ERROR ; + dtp->dt_haveagg =0 ;/* * If dtp->dt_beganon is not -1, we did not process the BEGIN probe
libdtrace/dt_impl.h +1 -0
@@ -405,6 +405,7 @@ struct dtrace_hdl {dt_percpu_drops_t *dt_drops;/* per-CPU drop counters cache */ uint64_t dt_specdrops;/* consumer-side spec drops counter */ int dt_statusgen;/* current status generation */ +int dt_haveagg;/* FIXME: figure out a good name for this */ hrtime_t dt_laststatus;/* last status */ hrtime_t dt_lastswitch;/* last switch of buffer data */ hrtime_t dt_lastagg;/* last snapshot of aggregation data */
test/unittest/options/tst.aggrate-slow.d +1 -0
@@ -9,6 +9,7 @@ * When the aggrate is slower than the switchrate and the pace of printa() * actions, multiple printa() should all reflect the same stale count. */ +/* @@skip: aggrate makes no sense */ /* @@trigger: periodic_output */ /* @@nosort */
Signature · 1 line
2.43.5
Re: [PATCH 2/2] Snapshot aggregations just in time
I am testing this a bit and seeing if we can centralize the call to dtrace_aggregate_snap() a bit more (unlikely), but I would be inclined to suggest to keep the aggrate code and only do the on-demand call to dtrace_aggregate_snap() portion of this patch. I could see some use for using aggrate to rate-limit retrieving aggregate snapshots in cases where the probe firing frequency is sufficiently high to make aggregate retrieval a noticable cost, and where therefore limiting it can be useful. Of course, that would generally mean that the script is poorly written and should be fixed to get the desired results.
But it has the added benefit of not introducing a new global variable whole not getting rid of (a now obsolete) one. Since aggrate is by default 0, keeping that code (for now) is safe and will still result in the default behaviour being 100% on-demand.
Quoted 116 lines · On Tue, May 27, 2025 at 01:43:13AM -0400, [email protected] wrote:
From: Eugene Loh <[email protected]>
Currently, dtrace periodically calls
dtrace_work(), which in turn callsdtrace_consume(), which among other things callsdtrace_aggregate_snap(). But aggregations are kept in entirety in the kernel's BPF maps. There is no need to snapshot the aggregations into user space unless we're actually going to do something with aggregations.Snapshot aggregations just in time -- that is, if there is a
clear(),trunc(), orprinta()or if aggregations are to be printed at the end of a dtrace session.Skip the aggrate-slow test. Just-in-time snapshots mean the semantics of aggrate have changed. A fast aggrate means nothing. A slow aggrate means we are supposed to use stale aggregation data, which would be baffling. A future patch is advised to deprecate aggrate entirely.
Signed-off-by Eugene Loh <[email protected]>--- libdtrace/dt_aggregate.c | 7 +++++++ libdtrace/dt_consume.c | 9 +++++++-- libdtrace/dt_impl.h | 1 + test/unittest/options/tst.aggrate-slow.d | 1 + 4 files changed, 16 insertions(+), 2 deletions(-)libdtrace/dt_aggregate.c +7
-0@@ -817,6 +817,10 @@ dtrace_aggregate_snap(dtrace_hdl_t *dtp) dtp->dt_lastagg = now; } +if (dtp->dt_haveagg) +return DTRACE_WORKSTATUS_OKAY ; + dtp->dt_haveagg =1 ; +dtrace_aggregate_clear (dtp);for (i =0 ; i < dtp->dt_conf.num_online_cpus; i++) {@@ -1901,6 +1905,9 @@ dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp, {dtrace_print_aggdata_t pd; + dtp->dt_haveagg =0 ; +dtrace_aggregate_snap (dtp); +if (dtp->dt_maxaggdsize ==0 )return 0 ;libdtrace/dt_consume.c +7
-2@@ -2353,11 +2353,15 @@ dt_consume_one_probe(dtrace_hdl_t *dtp, FILE *fp, char *data, uint32_t size, i++;continue ;case DT_ACT_CLEAR : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (dt_clear (dtp, data, rec) !=0 )return DTRACE_WORKSTATUS_ERROR ;continue ;case DT_ACT_TRUNC : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (i == epd->dtdd_nrecs -1 )return dt_set_errno (dtp,EDT_BADTRUNC );@@ -2519,6 +2523,8 @@ dt_consume_one_probe(dtrace_hdl_t *dtp, FILE *fp, char *data, uint32_t size, func = dtrace_fprintf;break ;case DTRACEACT_PRINTA : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (rec->dtrd_format !=NULL ) func = dtrace_fprinta;else @@ -3113,8 +3119,7 @@ dtrace_consume(dtrace_hdl_t *dtp, FILE *fp, dtrace_consume_probe_f *pf, } } -if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) -return DTRACE_WORKSTATUS_ERROR ; + dtp->dt_haveagg =0 ;/* * If dtp->dt_beganon is not -1, we did not process the BEGIN probe libdtrace/dt_impl.h +1
-0@@ -405,6 +405,7 @@ struct dtrace_hdl {dt_percpu_drops_t *dt_drops;/* per-CPU drop counters cache */ uint64_t dt_specdrops;/* consumer-side spec drops counter */ int dt_statusgen;/* current status generation */ +int dt_haveagg;/* FIXME: figure out a good name for this */ hrtime_t dt_laststatus;/* last status */ hrtime_t dt_lastswitch;/* last switch of buffer data */ hrtime_t dt_lastagg;/* last snapshot of aggregation data */ test/unittest/options/tst.aggrate-slow.d +1
-0@@ -9,6 +9,7 @@ * When the aggrate is slower than the switchrate and the pace of printa() * actions, multiple printa() should all reflect the same stale count. */ +/* @@skip: aggrate makes no sense */ /* @@trigger: periodic_output */ /* @@nosort */Signature · 1 line
2.43.5
Re: [PATCH 2/2] Snapshot aggregations just in time
Actually, I think it might be better to add a DTRACE_A_VALID flag, add a dt_aggregate_clear_flag() function, and instead of using haveagg, set that flag wherever you current set haveagg to 1, and clear the flag whenever you set haveagg to 0. (The dt_aggregate_clear_flag() is only really needed in dt_consume.c because you cannot access dtp->dt_aggregate->dtat_flags from there.
That avoids haveagg, and also stores the validity of the agg data in the dtp->dt_aggregate member, where it belongs.
Quoted 132 lines · Kris Van Hees
I am testing this a bit and seeing if we can centralize the call to
dtrace_aggregate_snap()a bit more (unlikely), but I would be inclined to suggest to keep the aggrate code and only do the on-demand call todtrace_aggregate_snap()portion of this patch. I could see some use for using aggrate to rate-limit retrieving aggregate snapshots in cases where the probe firing frequency is sufficiently high to make aggregate retrieval a noticable cost, and where therefore limiting it can be useful. Of course, that would generally mean that the script is poorly written and should be fixed to get the desired results.But it has the added benefit of not introducing a new global variable whole not getting rid of (a now obsolete) one. Since aggrate is by default 0, keeping that code (for now) is safe and will still result in the default behaviour being 100% on-demand.
Quoted 116 lines · On Tue, May 27, 2025 at 01:43:13AM -0400, [email protected] wrote:
From: Eugene Loh <[email protected]>
Currently, dtrace periodically calls
dtrace_work(), which in turn callsdtrace_consume(), which among other things callsdtrace_aggregate_snap(). But aggregations are kept in entirety in the kernel's BPF maps. There is no need to snapshot the aggregations into user space unless we're actually going to do something with aggregations.Snapshot aggregations just in time -- that is, if there is a
clear(),trunc(), orprinta()or if aggregations are to be printed at the end of a dtrace session.Skip the aggrate-slow test. Just-in-time snapshots mean the semantics of aggrate have changed. A fast aggrate means nothing. A slow aggrate means we are supposed to use stale aggregation data, which would be baffling. A future patch is advised to deprecate aggrate entirely.
Signed-off-by Eugene Loh <[email protected]>--- libdtrace/dt_aggregate.c | 7 +++++++ libdtrace/dt_consume.c | 9 +++++++-- libdtrace/dt_impl.h | 1 + test/unittest/options/tst.aggrate-slow.d | 1 + 4 files changed, 16 insertions(+), 2 deletions(-)libdtrace/dt_aggregate.c +7
-0@@ -817,6 +817,10 @@ dtrace_aggregate_snap(dtrace_hdl_t *dtp) dtp->dt_lastagg = now; } +if (dtp->dt_haveagg) +return DTRACE_WORKSTATUS_OKAY ; + dtp->dt_haveagg =1 ; +dtrace_aggregate_clear (dtp);for (i =0 ; i < dtp->dt_conf.num_online_cpus; i++) {@@ -1901,6 +1905,9 @@ dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp, {dtrace_print_aggdata_t pd; + dtp->dt_haveagg =0 ; +dtrace_aggregate_snap (dtp); +if (dtp->dt_maxaggdsize ==0 )return 0 ;libdtrace/dt_consume.c +7
-2@@ -2353,11 +2353,15 @@ dt_consume_one_probe(dtrace_hdl_t *dtp, FILE *fp, char *data, uint32_t size, i++;continue ;case DT_ACT_CLEAR : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (dt_clear (dtp, data, rec) !=0 )return DTRACE_WORKSTATUS_ERROR ;continue ;case DT_ACT_TRUNC : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (i == epd->dtdd_nrecs -1 )return dt_set_errno (dtp,EDT_BADTRUNC );@@ -2519,6 +2523,8 @@ dt_consume_one_probe(dtrace_hdl_t *dtp, FILE *fp, char *data, uint32_t size, func = dtrace_fprintf;break ;case DTRACEACT_PRINTA : +if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) +return DTRACE_WORKSTATUS_ERROR ;if (rec->dtrd_format !=NULL ) func = dtrace_fprinta;else @@ -3113,8 +3119,7 @@ dtrace_consume(dtrace_hdl_t *dtp, FILE *fp, dtrace_consume_probe_f *pf, } } -if (dtrace_aggregate_snap (dtp) ==DTRACE_WORKSTATUS_ERROR ) -return DTRACE_WORKSTATUS_ERROR ; + dtp->dt_haveagg =0 ;/* * If dtp->dt_beganon is not -1, we did not process the BEGIN probe libdtrace/dt_impl.h +1
-0@@ -405,6 +405,7 @@ struct dtrace_hdl {dt_percpu_drops_t *dt_drops;/* per-CPU drop counters cache */ uint64_t dt_specdrops;/* consumer-side spec drops counter */ int dt_statusgen;/* current status generation */ +int dt_haveagg;/* FIXME: figure out a good name for this */ hrtime_t dt_laststatus;/* last status */ hrtime_t dt_lastswitch;/* last switch of buffer data */ hrtime_t dt_lastagg;/* last snapshot of aggregation data */ test/unittest/options/tst.aggrate-slow.d +1
-0@@ -9,6 +9,7 @@ * When the aggrate is slower than the switchrate and the pace of printa() * actions, multiple printa() should all reflect the same stale count. */ +/* @@skip: aggrate makes no sense */ /* @@trigger: periodic_output */ /* @@nosort */Signature · 1 line
2.43.5