-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Implement perf stat --control for cgi #22537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iluuu1994
wants to merge
4
commits into
php:master
Choose a base branch
from
iluuu1994:perf-stat-control
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| +----------------------------------------------------------------------+ | ||
| | Zend Engine | | ||
| +----------------------------------------------------------------------+ | ||
| | Copyright © Zend Technologies Ltd., a subsidiary company of | | ||
| | Perforce Software, Inc., and Contributors. | | ||
| +----------------------------------------------------------------------+ | ||
| | This source file is subject to the Modified BSD License that is | | ||
| | bundled with this package in the file LICENSE, and is available | | ||
| | through the World Wide Web at <https://www.php.net/license/>. | | ||
| | | | ||
| | SPDX-License-Identifier: BSD-3-Clause | | ||
| +----------------------------------------------------------------------+ | ||
| | Authors: Ilija Tovilo <ilutov@php.net> | | ||
| +----------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| #ifndef ZEND_PERF_STAT_H | ||
| #define ZEND_PERF_STAT_H | ||
|
|
||
| #include "zend_portability.h" | ||
|
|
||
| # if !(HAVE_FCNTL_H && HAVE_SYS_SELECT_H && HAVE_SYS_STAT_H && HAVE_SYS_TIME_H && HAVE_UNISTD_H) | ||
| static void zend_perf_stat_enable(void) {} | ||
| static void zend_perf_stat_disable(void) {} | ||
| # else | ||
|
|
||
| # include <fcntl.h> | ||
| # include <sys/select.h> | ||
| # include <sys/stat.h> | ||
| # include <unistd.h> | ||
|
|
||
| # include "Zend/zend.h" | ||
|
|
||
| # define ZPS_CTL_FIFO_ENV "PERF_STAT_CTL_FIFO" | ||
| # define ZPS_ACK_FIFO_ENV "PERF_STAT_ACK_FIFO" | ||
|
|
||
| static int ctl_fd = -2; | ||
| static int ack_fd = -2; | ||
|
|
||
| static int zps_open_fifo(const char *env_name, int flags) | ||
| { | ||
| const char *path = getenv(env_name); | ||
|
|
||
| if (path == NULL || path[0] == '\0') { | ||
| return -1; | ||
| } | ||
|
|
||
| # ifdef O_CLOEXEC | ||
| flags |= O_CLOEXEC; | ||
| # endif | ||
| int fd = open(path, flags | O_NONBLOCK); | ||
| if (fd < 0) { | ||
| fprintf(stderr, "Failed to open fifo %s\n", path); | ||
| fflush(stderr); | ||
| zend_bailout(); | ||
| } | ||
|
|
||
| struct stat st; | ||
| if (fstat(fd, &st) != 0 || !S_ISFIFO(st.st_mode)) { | ||
| close(fd); | ||
| fprintf(stderr, "File %s is not a fifo\n", path); | ||
| fflush(stderr); | ||
| zend_bailout(); | ||
| } | ||
| return fd; | ||
| } | ||
|
|
||
| static void zps_init(void) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| static void zps_wait_ack(void) | ||
| { | ||
| if (ack_fd < 0) { | ||
| return; | ||
| } | ||
|
|
||
| struct timeval timeout; | ||
| timeout.tv_sec = 1; | ||
| timeout.tv_usec = 0; | ||
|
|
||
| fd_set readfds; | ||
| FD_ZERO(&readfds); | ||
| FD_SET(ack_fd, &readfds); | ||
| if (select(ack_fd + 1, &readfds, NULL, NULL, &timeout) <= 0) { | ||
| return; | ||
| } | ||
|
|
||
| char ack[sizeof("ack\n") - 1]; | ||
| ssize_t bytes_read; | ||
| do { | ||
| bytes_read = read(ack_fd, ack, sizeof(ack)); | ||
| } while (bytes_read > 0); | ||
| } | ||
|
|
||
| static void zps_control(const char *command, size_t command_len) | ||
| { | ||
| zps_init(); | ||
|
|
||
| if (ctl_fd < 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (write(ctl_fd, command, command_len) != (ssize_t) command_len) { | ||
| close(ctl_fd); | ||
| ctl_fd = -1; | ||
| return; | ||
| } | ||
|
|
||
| zps_wait_ack(); | ||
| } | ||
|
|
||
| static void zend_perf_stat_enable(void) | ||
| { | ||
| static const char command[] = "enable\n"; | ||
|
|
||
| zps_control(command, sizeof(command) - 1); | ||
| } | ||
|
|
||
| static void zend_perf_stat_disable(void) | ||
| { | ||
| static const char command[] = "disable\n"; | ||
|
|
||
| zps_control(command, sizeof(command) - 1); | ||
| } | ||
|
|
||
| # endif | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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