Skip to content
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

ext/opcache: use C11 atomics for "restart_in" #10276

Merged
merged 1 commit into from
Jan 12, 2023

Conversation

MaxKellermann
Copy link
Contributor

Cheaper than fcntl(F_SETLK). The same is done already on Windows, so if that works, why not use it everywhere? (Of course, only if the compiler supports this C11 feature.)

As a bonus, the code in this commit also works on C++ via C++11 std::atomic, just in case somebody adds some C++ code to the opcache extension one day.

MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 10, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - to be removed after
php#10276 is merged.
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

LGTM but I have very limited experience with atomics.

ext/opcache/ZendAccelerator.h Outdated Show resolved Hide resolved
ext/opcache/ZendAccelerator.c Outdated Show resolved Hide resolved
Cheaper than fcntl(F_SETLK).  The same is done already on Windows, so
if that works, why not use it everywhere?  (Of course, only if the
compiler supports this C11 feature.)

As a bonus, the code in this commit also works on C++ via C++11
std::atomic, just in case somebody adds some C++ code to the opcache
extension one day.
@iluuu1994 iluuu1994 merged commit 061fcdb into php:master Jan 12, 2023
@iluuu1994
Copy link
Member

Thank you!

@MaxKellermann MaxKellermann deleted the opcache_c11_atomic branch January 13, 2023 09:30
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 13, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
@dstogov
Copy link
Member

dstogov commented Jan 16, 2023

This commit is wrong. It completely disables kill_all_lockers(). So the hanging workers are not going to be killed and restart could never happen. I think, this should be reverted.

@ iluuu1994 please be more careful reviewing not obvious changes and ask my review in case of doubt.

@iluuu1994
Copy link
Member

Hi @dstogov! Sorry. In the past I've felt like I was overly cautious with merging PRs but I probably overcorrected for that here. Would definitely not have hurt to wait a bit longer. I will revert this commit when I get the chance.

@MaxKellermann
Copy link
Contributor Author

It completely disables kill_all_lockers(). So the hanging workers are not going to be killed and restart could never happen

True. This now uses the same code paths as on Windows, wher kill_all_lockers() doesn't exist either. It looked obvious to me that those who wrote the existing code felt that kill_all_lockers() wasn't necessary, or else somebody would have implemented it on Windows.

@dstogov, can you please explain the difference, why it is necessary on most operating systems, but not on Windows?

There should be a code comment with this explanation.

@MaxKellermann
Copy link
Contributor Author

those who wrote the existing code felt that kill_all_lockers() wasn't necessary

@dstogov, turns it out was you who decided to omit kill_all_lockers() for the atomic code path (i.e. Windows) - see commit 528006a

Please explain.

@dstogov
Copy link
Member

dstogov commented Jan 16, 2023

This is not my decision. This code was originally developed as a propitiate software before me, and I worked on Open Sourcing it.

Anyway, this never worked on Windows, but this doesn't mean you should broke this for others without any comments.

@MaxKellermann
Copy link
Contributor Author

Anyway, this never worked on Windows, but this doesn't mean you should broke this for others without any comments.

Okay, this changes everything - the Windows implementation should be considered broken? There was no code comment indicating this. There should be one, to avoid confusing others like it did confuse me.

MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 16, 2023
This reverts commit 061fcdb.

While the commit does indeed improve performance, @dstogov complained
that it disables the code path calling kill_all_lockers(), and thus
hanging workers are never killed and restarted.
php#10276 (comment)

The fact that this feature was not implemented in the existing atomic
code path (i.e. Windows) did not mean that the feature was considered
not strictly necessary, but that the Windows implementation was
incomplete and broken.
php#10276 (comment)
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 16, 2023
kill_all_lockers() is not implemented on Windows, thus the code is
incomplete and broken.  See
php#10276 (comment) for
the explanation.
@MaxKellermann
Copy link
Contributor Author

@dstogov here's my revert PR which also adds a code comment: #10339

@cmb69
Copy link
Contributor

cmb69 commented Jan 16, 2023

From the docs:

This directive is not supported on Windows.

So yeah, kill_all_lockers() is not called on Windows. That is probably because there are no workers which are children of a "master" process. There is no PHP-FPM on Windows at all, because there is no fork(2). On Windows, the typical SAPIs are FCGI (where only the Webserver knows about the PHP processes), and Apache (which only has a single worker process with multiple threads).

And the fact that OPcache has "limitations" on Windows, is known. That cannot be fixed, though (unless for multithreaded SAPIs, which may not be the best solution wrt. stability).

MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 17, 2023
This reverts commit 061fcdb.

While the commit does indeed improve performance, @dstogov complained
that it disables the code path calling kill_all_lockers(), and thus
hanging workers are never killed and restarted.
php#10276 (comment)

The fact that this feature was not implemented in the existing atomic
code path (i.e. Windows) did not mean that the feature was considered
not strictly necessary, but that the Windows implementation just did
not need the feature because SAPIs that work on Windows do not manage
child processes
php#10276 (comment)
php#10276 (comment)
iluuu1994 pushed a commit that referenced this pull request Jan 19, 2023
* Revert "ext/opcache: use C11 atomics for "restart_in" (#10276)"

This reverts commit 061fcdb.

While the commit does indeed improve performance, @dstogov complained
that it disables the code path calling kill_all_lockers(), and thus
hanging workers are never killed and restarted.
#10276 (comment)

The fact that this feature was not implemented in the existing atomic
code path (i.e. Windows) did not mean that the feature was considered
not strictly necessary, but that the Windows implementation just did
not need the feature because SAPIs that work on Windows do not manage
child processes
#10276 (comment)
#10276 (comment)

* ext/opcache: document lack of kill_all_lockers() on Windows

kill_all_lockers() is not implemented on Windows, and does not need to
be.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 20, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 7, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 7, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 9, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 13, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 24, 2023
pthread mutexes are lighter than fcntl(F_SETLK) because non-contending
operations can avoid the system call.

Note that this commit preserves the "lock_file" variable because it is
used by ZendAccelerator.c - now that
php#10276 is merged, we may consider
removing it.
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.

None yet

4 participants