Skip to content

Commit bd3b4d7

Browse files
committed
opcache posix creating special shared segments for FreeBSD 13 and above.
From this release, it is permitted to create shared memory blocks tagged as large for faster accesses for a size compatible with otherwise we fallback to a classic creation.
1 parent 706398f commit bd3b4d7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

ext/opcache/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ if test "$PHP_OPCACHE" != "no"; then
105105

106106
fi
107107

108-
AC_CHECK_FUNCS([mprotect])
108+
AC_CHECK_FUNCS([mprotect shm_create_largepage])
109109

110110
AC_MSG_CHECKING(for sysvipc shared memory support)
111111
AC_RUN_IFELSE([AC_LANG_SOURCE([[

ext/opcache/shared_alloc_posix.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ static int create_segments(size_t requested_size, zend_shared_segment_posix ***s
4040
{
4141
zend_shared_segment_posix *shared_segment;
4242
char shared_segment_name[sizeof("/ZendAccelerator.") + 20];
43+
int shared_segment_flags = O_RDWR|O_CREAT|O_TRUNC;
44+
mode_t shared_segment_mode = 0600;
45+
46+
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
47+
/**
48+
* architectures have 3 entries max and we are interested
49+
* from the second offset minimum to be worthy creating
50+
* a special shared segment tagged as 'large'.
51+
* only then amd64/i386/arm64 and perharps risc64*
52+
* archs are on interest here.
53+
*/
54+
size_t i, shared_segment_lg_index = 0;
55+
static size_t pgindexes[3] = {0};
56+
static size_t pgentries = sizeof(pgindexes) / sizeof(pgindexes[0]);
57+
58+
if (pgindexes[0] == 0)
59+
getpagesizes(pgindexes, pgentries);
60+
61+
for (i = 1; i < pgentries; i ++) {
62+
if (!(requested_size % pgindexes[i])) {
63+
shared_segment_lg_index = i;
64+
break;
65+
}
66+
}
67+
#endif
4368

4469
*shared_segments_count = 1;
4570
*shared_segments_p = (zend_shared_segment_posix **) calloc(1, sizeof(zend_shared_segment_posix) + sizeof(void *));
@@ -51,7 +76,12 @@ static int create_segments(size_t requested_size, zend_shared_segment_posix ***s
5176
(*shared_segments_p)[0] = shared_segment;
5277

5378
sprintf(shared_segment_name, "/ZendAccelerator.%d", getpid());
54-
shared_segment->shm_fd = shm_open(shared_segment_name, O_RDWR|O_CREAT|O_TRUNC, 0600);
79+
shared_segment->shm_fd =
80+
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
81+
shared_segment_lg_index > 0 ?
82+
shm_create_largepage(shared_segment_name, shared_segment_flags, shared_segment_lg_index, SHM_LARGEPAGE_ALLOC_DEFAULT, shared_segment_mode) :
83+
#endif
84+
shm_open(shared_segment_name, shared_segment_flags, shared_segment_mode);
5585
if (shared_segment->shm_fd == -1) {
5686
*error_in = "shm_open";
5787
return ALLOC_FAILURE;

0 commit comments

Comments
 (0)