Skip to content

Commit

Permalink
Add -xcpu support to profile provider
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Loh <eugene.loh@oracle.com>
Reviewed-by: Kris Van Hees <kris.van.hees@oracle.com>
  • Loading branch information
euloh authored and kvanhees committed Jan 23, 2024
1 parent b6b0228 commit 7905d23
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
13 changes: 10 additions & 3 deletions libdtrace/dt_prov_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ static const dtrace_pattr_t pattr = {
{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
};

#define FDS_CNT(kind) ((kind) == KIND_TICK ? 1 : dtp->dt_conf.num_online_cpus)
#define FDS_CNT(kind) ((kind) == KIND_TICK || \
dtp->dt_options[DTRACEOPT_CPU] != DTRACEOPT_UNSET ? \
1 : dtp->dt_conf.num_online_cpus)
typedef struct profile_probe {
int kind;
uint64_t period;
Expand Down Expand Up @@ -274,8 +276,13 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
for (i = 0; i < cnt; i++) {
int j = i, fd;

/* if there is only one fd, place it at random */
if (cnt == 1)
/*
* If -xcpu is set, use that CPU.
* If there is only one fd, place it at random.
*/
if (dtp->dt_options[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
j = dtp->dt_options[DTRACEOPT_CPU];
else if (cnt == 1)
j = rand() % dtp->dt_conf.num_online_cpus;

fd = dt_perf_event_open(&attr, -1, dtp->dt_conf.cpus[j].cpu_id,
Expand Down
27 changes: 27 additions & 0 deletions test/stress/options/tst.cpu-profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# 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.
#

dtrace=$1

nerr=0

# Loop over CPUs.
for cpu0 in `awk '/^processor[ ]*: [0-9]*$/ {print $3}' /proc/cpuinfo`; do
# Observe where DTrace runs.
cpu=`$dtrace $dt_flags -xcpu=$cpu0 -qn 'profile-100ms { trace(cpu); exit(0); }'`

# Check result.
echo expected cpu $cpu0 got cpu $cpu
if [ `echo $cpu | wc -w` -ne 1 ]; then
nerr=$(($nerr + 1))
elif [ $(($cpu + 0)) != $cpu0 ]; then
nerr=$(($nerr + 1))
fi
done

exit $nerr
27 changes: 27 additions & 0 deletions test/stress/options/tst.cpu-tick.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# 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.
#

dtrace=$1

nerr=0

# Loop over CPUs.
for cpu0 in `awk '/^processor[ ]*: [0-9]*$/ {print $3}' /proc/cpuinfo`; do
# Observe where DTrace runs.
cpu=`$dtrace $dt_flags -xcpu=$cpu0 -qn 'tick-100ms { trace(cpu); exit(0); }'`

# Check result.
echo expected cpu $cpu0 got cpu $cpu
if [ `echo $cpu | wc -w` -ne 1 ]; then
nerr=$(($nerr + 1))
elif [ $(($cpu + 0)) != $cpu0 ]; then
nerr=$(($nerr + 1))
fi
done

exit $nerr
27 changes: 27 additions & 0 deletions test/unittest/options/tst.cpu-profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# 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.
#

dtrace=$1

# Pick a CPU at random.
cpulist=( `awk '/^processor[ ]*: [0-9]*$/ {print $3}' /proc/cpuinfo` )
ncpus=${#cpulist[@]}
cpu0=${cpulist[$((RANDOM % $ncpus))]}

# Observe where DTrace runs.
cpu=`$dtrace $dt_flags -xcpu=$cpu0 -qn 'profile-100ms { trace(cpu); exit(0); }'`

# Check result.
echo expected cpu $cpu0 got cpu $cpu
if [ `echo $cpu | wc -w` -ne 1 ]; then
exit 1
elif [ $(($cpu + 0)) != $cpu0 ]; then
exit 1
fi

exit 0
27 changes: 27 additions & 0 deletions test/unittest/options/tst.cpu-tick.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# 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.
#

dtrace=$1

# Pick a CPU at random.
cpulist=( `awk '/^processor[ ]*: [0-9]*$/ {print $3}' /proc/cpuinfo` )
ncpus=${#cpulist[@]}
cpu0=${cpulist[$((RANDOM % $ncpus))]}

# Observe where DTrace runs.
cpu=`$dtrace $dt_flags -xcpu=$cpu0 -qn 'tick-100ms { trace(cpu); exit(0); }'`

# Check result.
echo expected cpu $cpu0 got cpu $cpu
if [ `echo $cpu | wc -w` -ne 1 ]; then
exit 1
elif [ $(($cpu + 0)) != $cpu0 ]; then
exit 1
fi

exit 0

0 comments on commit 7905d23

Please sign in to comment.