[DTrace-devel] [PATCH 4/5] Add -xcpu support to cpc provider

eugene.loh at oracle.com eugene.loh at oracle.com
Tue Sep 5 04:11:41 UTC 2023


From: Eugene Loh <eugene.loh at oracle.com>

Signed-off-by: Eugene Loh <eugene.loh at oracle.com>
---
 libdtrace/dt_prov_cpc.c              | 17 +++++++++++------
 test/unittest/options/tst.cpu-cpc.sh | 20 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 6 deletions(-)
 create mode 100755 test/unittest/options/tst.cpu-cpc.sh

diff --git a/libdtrace/dt_prov_cpc.c b/libdtrace/dt_prov_cpc.c
index fc4bb43b..9e0f5542 100644
--- a/libdtrace/dt_prov_cpc.c
+++ b/libdtrace/dt_prov_cpc.c
@@ -32,6 +32,9 @@ static const dtrace_pattr_t	pattr = {
 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
 };
 
+
+#define FDS_CNT	(dtp->dt_options[DTRACEOPT_CPU] != DTRACEOPT_UNSET ? 1 : dtp->dt_conf.num_online_cpus)
+
 typedef struct cpc_probe {
 	char	*name;
 	int	*fds;
@@ -54,7 +57,7 @@ static dt_probe_t *cpc_probe_insert(dtrace_hdl_t *dtp, dt_provider_t *prv,
 				    const char *prb)
 {
 	cpc_probe_t	*datap;
-	int		i, cnt = dtp->dt_conf.num_online_cpus;
+	int		i, cnt = FDS_CNT;
 
 	datap = dt_zalloc(dtp, sizeof(cpc_probe_t));
 	if (datap == NULL)
@@ -418,8 +421,7 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
 {
 	cpc_probe_t		*datap = prp->prv_data;
 	struct perf_event_attr	attr;
-	int			i, nattach = 0;;
-	int			cnt = dtp->dt_conf.num_online_cpus;
+	int			i, nattach = 0, cnt = FDS_CNT;
 	char			*name = datap->name;  /* same as prp->desc->prb */
 
 	memset(&attr, 0, sizeof(attr));
@@ -429,9 +431,12 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
 
 	for (i = 0; i < cnt; i++) {
 		int fd;
+		long long cpuid = dtp->dt_options[DTRACEOPT_CPU];
+
+		if (cpuid == DTRACEOPT_UNSET)
+			cpuid = dtp->dt_conf.cpus[i].cpu_id;
 
-		fd = perf_event_open(&attr, -1, dtp->dt_conf.cpus[i].cpu_id,
-				     -1, 0);
+		fd = perf_event_open(&attr, -1, cpuid, -1, 0);
 		if (fd < 0)
 			continue;
 		if (ioctl(fd, PERF_EVENT_IOC_SET_BPF, bpf_fd) < 0) {
@@ -458,7 +463,7 @@ static int probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
 static void detach(dtrace_hdl_t *dtp, const dt_probe_t *prp)
 {
 	cpc_probe_t	*datap = prp->prv_data;
-	int		i, cnt = dtp->dt_conf.num_online_cpus;
+	int		i, cnt = FDS_CNT;
 
 	for (i = 0; i < cnt; i++) {
 		if (datap->fds[i] != -1)
diff --git a/test/unittest/options/tst.cpu-cpc.sh b/test/unittest/options/tst.cpu-cpc.sh
new file mode 100755
index 00000000..e1acaf99
--- /dev/null
+++ b/test/unittest/options/tst.cpu-cpc.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2023, 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.
+#
+
+dtrace=$1
+
+nerr=0
+for cpu0 in `awk '/^processor[ 	]*: [0-9]*$/ {print $3}' /proc/cpuinfo`; do
+	cpu=`$dtrace $dt_flags -xcpu=$cpu0 -qn 'cpc:::cpu_clock-all-100000000 { trace(cpu); exit(0); }'`
+	echo expected cpu $cpu0 got cpu $cpu
+	if [ $cpu != $cpu0 ]; then
+		nerr=$(($nerr + 1))
+	fi
+done
+
+exit $nerr
-- 
2.18.4




More information about the DTrace-devel mailing list