Skip to content

Implement perf stat --control for cgi#22537

Open
iluuu1994 wants to merge 4 commits into
php:masterfrom
iluuu1994:perf-stat-control
Open

Implement perf stat --control for cgi#22537
iluuu1994 wants to merge 4 commits into
php:masterfrom
iluuu1994:perf-stat-control

Conversation

@iluuu1994

@iluuu1994 iluuu1994 commented Jul 1, 2026

Copy link
Copy Markdown
Member

perf stat --control allows the profiled process to enable and disable counters at runtime. This allows us to skip profiling startup and shutdown for more accurate results. The same already exists for valgrind.

$ perf stat -D -1 --control fifo:/tmp/perfctl,/tmp/perfack

-D -1 starts perf stat with counters disabled. --control makes perf stat connect to the /tmp/perfctl and /tmp/perfack fifo files. These need to exist when starting perf, so create them using mkfifo /tmp/perf{ctl,ack}. The ctl fifo is written to by cgi to enable/disable counters, whereas the ack fifo is written to by perf stat to acknolwedge counters have been installed.

Additionally, you'll need to set the set the PERF_STAT_CTL_FIFO and PERF_STAT_ACK_FIFO env variables for cgi to find the fifo files.

Split off from iluuu1994#218.

perf stat --control allows the profiled process to enable and disable counters
at runtime. This allows us to skip profiling startup and shutdown for more
accurate results. The same already exists for valgrind.

$ perf stat -D -1 --control fifo:/tmp/perfctl,/tmp/perfack

-D -1 starts perf stat with counters disabled. --control makes perf stat connect
to the /tmp/perfctl and /tmp/perfack fifo files. These need to exist when
starting perf, so create them using mkfifo /tmp/perf{ctl,ack}. The ctl fifo is
written to by cgi to enable/disable counters, whereas the ack fifo is written to
by perf stat to acknolwedge counters have been installed.

Additionally, you'll need to set the set the PERF_STAT_CTL_FIFO and
PERF_STAT_ACK_FIFO env variables for cgi to find the fifo files.

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me otherwise!

Comment thread Zend/zend_perf_stat.h
Comment on lines +65 to +70
if (ctl_fd == -2) {
ctl_fd = zps_open_fifo(ZPS_CTL_FIFO_ENV, O_WRONLY);
}
if (ack_fd == -2) {
ack_fd = zps_open_fifo(ZPS_ACK_FIFO_ENV, O_RDONLY);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ease of debugging we should print an error message and exit when these functions fail while the env vars are set

Comment thread Zend/zend_perf_stat.h Outdated
Comment on lines +93 to +97
for (ssize_t i = 0; i < bytes_read; i++) {
if (ack[i] == '\n') {
return;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can likely omit this loop and just check that we have read strlen("ack\n") in total

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched the loop to just drain the pipe, that avoids potential synchronization issues.

@iluuu1994 iluuu1994 requested a review from arnaud-lb July 1, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants