git: c1b40aa4b6b3 - main - pmc: Exit with EX_OK when the pmc record time limit ends the capture

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 13:49:14 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=c1b40aa4b6b31aa3c09eeb863cf1f1151cd95b0b

commit c1b40aa4b6b31aa3c09eeb863cf1f1151cd95b0b
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-17 13:43:40 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-17 13:49:08 +0000

    pmc: Exit with EX_OK when the pmc record time limit ends the capture
    
    cmd_pmc_record() stores the return value of kevent() in status, which
    is also its exit status.  When the -t time limit elapses, the
    EVFILT_TIMER branch jumps to done with status still holding kevent()'s
    result, 1, so a timed recording exits with status 1, while one
    interrupted with SIGINT exits with EX_OK.
    
    Keep kevent()'s return value in its own variable and start status at
    EX_OK.  The kevent() failure and unknown signal cases called err() and
    exited without stopping and releasing the PMCs; set status to EX_OSERR
    and go through done instead, as the SIGIO case does.
    
    pmc_cli_test and amd/pmc_frontend_test accepted status 1 to work around
    this; make them require 0.
    
    Reported by:    llm-postcr
    Reviewed by:    mhorne
    Fixes:          a79a051e7d16 ("pmc: pmc record command")
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D61457
---
 tests/sys/pmc/amd/pmc_frontend_test.sh |  4 +---
 tests/sys/pmc/pmc_cli_test.sh          |  8 +-------
 usr.sbin/pmc/cmd_pmc_record.cc         | 33 ++++++++++++++++++++++-----------
 3 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/tests/sys/pmc/amd/pmc_frontend_test.sh b/tests/sys/pmc/amd/pmc_frontend_test.sh
index c3fe8215ec21..97964bebead0 100644
--- a/tests/sys/pmc/amd/pmc_frontend_test.sh
+++ b/tests/sys/pmc/amd/pmc_frontend_test.sh
@@ -32,13 +32,11 @@ pmc_frontend_ibs_body()
 	# Keep a CPU busy so that samples are taken while recording.
 	sh -c 'i=0; while [ $i -lt 3000000 ]; do i=$((i + 1)); done' &
 	load=$!
-	# pmc record returns kevent()'s result (1) when the -t timer ends
-	# the capture and 0 when it is interrupted.
 	pmc record -s frontend -t 3 frontend.pmc > record.out 2>&1
 	rc=$?
 	kill "$load" 2>/dev/null
 	wait
-	[ "$rc" -le 1 ] || \
+	[ "$rc" -eq 0 ] || \
 	    atf_fail "pmc record -s frontend failed: $(cat record.out)"
 
 	# Each decoded sample row of the analysis carries miss percentages.
diff --git a/tests/sys/pmc/pmc_cli_test.sh b/tests/sys/pmc/pmc_cli_test.sh
index eb824afd920d..e59e83370709 100644
--- a/tests/sys/pmc/pmc_cli_test.sh
+++ b/tests/sys/pmc/pmc_cli_test.sh
@@ -23,16 +23,10 @@ pmc_cli_setup()
 	    atf_skip "libpmc has no pmu-events table for this CPU"
 }
 
-# Run pmc record, saving its output in record.out.  pmc record returns
-# kevent()'s result (1) when the -t timer ends the capture and 0 when it is
-# interrupted; any other status means the capture failed.
+# Run pmc record, saving its output in record.out.
 pmc_record()
 {
 	pmc record "$@" > record.out 2>&1
-	case $? in
-	0|1) return 0 ;;
-	*) return 1 ;;
-	esac
 }
 
 atf_test_case pmc_info_missing_file
diff --git a/usr.sbin/pmc/cmd_pmc_record.cc b/usr.sbin/pmc/cmd_pmc_record.cc
index cff0db7fc9c7..fc1f099c5179 100644
--- a/usr.sbin/pmc/cmd_pmc_record.cc
+++ b/usr.sbin/pmc/cmd_pmc_record.cc
@@ -604,7 +604,7 @@ cmd_pmc_record(int argc, char **argv)
 	uint64_t rate = DEFAULT_RATE;
 	const char *logfile = "default.log";
 	int status;
-	int option, logfd, kq;
+	int n, option, logfd, kq;
 
 	CPU_ZERO(&mask);
 	if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
@@ -693,8 +693,11 @@ cmd_pmc_record(int argc, char **argv)
 		p.start();
 	}
 
-	if (clock_gettime(CLOCK_MONOTONIC, &start) < 0)
-		err(EX_OSERR, "ERROR: Could not get the current time");
+	if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
+		warn("ERROR: Could not get the current time");
+		status = EX_OSERR;
+		goto done;
+	}
 
 	printf("Recording performance trace press Ctrl-C to stop\n");
 
@@ -702,17 +705,22 @@ cmd_pmc_record(int argc, char **argv)
 	 * loop till either the target process (if any) exits, or we
 	 * are killed by a SIGINT or we reached the time duration.
 	 */
+	status = EX_OK;
 	while (1) {
-		status = kevent(kq, NULL, 0, &kev, 1, NULL);
-		if (status <= 0) {
-			if (errno != EINTR)
-				err(EX_OSERR, "ERROR: kevent failed");
-			else
+		n = kevent(kq, NULL, 0, &kev, 1, NULL);
+		if (n <= 0) {
+			if (errno == EINTR)
 				continue;
+			warn("ERROR: kevent failed");
+			status = EX_OSERR;
+			goto done;
 		}
 
-		if (kev.flags & EV_ERROR)
-			errc(EX_OSERR, kev.data, "ERROR: kevent failed");
+		if (kev.flags & EV_ERROR) {
+			warnc(kev.data, "ERROR: kevent failed");
+			status = EX_OSERR;
+			goto done;
+		}
 
 		switch (kev.filter) {
 		case EVFILT_READ:  /* log file data is present */
@@ -729,7 +737,10 @@ cmd_pmc_record(int argc, char **argv)
 				status = EX_OK;
 				goto done;
 			} else {
-				err(EX_OSERR, "Unknown signal recieved");
+				warnx("ERROR: Unknown signal %d received",
+				    (int)kev.ident);
+				status = EX_OSERR;
+				goto done;
 			}
 			break;
 		case EVFILT_TIMER: