xref: /PHP-7.4/ext/opcache/shared_alloc_mmap.c (revision da919a8b)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "zend_shared_alloc.h"
23 
24 #ifdef USE_MMAP
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/mman.h>
31 
32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
33 # define MAP_ANONYMOUS MAP_ANON
34 #endif
35 #if defined(MAP_ALIGNED_SUPER)
36 # define MAP_HUGETLB MAP_ALIGNED_SUPER
37 #endif
38 
create_segments(size_t requested_size,zend_shared_segment *** shared_segments_p,int * shared_segments_count,char ** error_in)39 static int create_segments(size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
40 {
41 	zend_shared_segment *shared_segment;
42 
43 	*shared_segments_count = 1;
44 	*shared_segments_p = (zend_shared_segment **) calloc(1, sizeof(zend_shared_segment) + sizeof(void *));
45 	if (!*shared_segments_p) {
46 		*error_in = "calloc";
47 		return ALLOC_FAILURE;
48 	}
49 	shared_segment = (zend_shared_segment *)((char *)(*shared_segments_p) + sizeof(void *));
50 	(*shared_segments_p)[0] = shared_segment;
51 
52 #ifdef MAP_HUGETLB
53 	/* Try to allocate huge pages first to reduce dTLB misses.
54 	 * OSes has to be configured properly
55 	 * on Linux
56 	 * (e.g. https://wiki.debian.org/Hugepages#Enabling_HugeTlbPage)
57 	 * You may verify huge page usage with the following command:
58 	 * `grep "Huge" /proc/meminfo`
59 	 * on FreeBSD
60 	 * sysctl vm.pmap.pg_ps_enabled entry
61 	 * (boot time config only, but enabled by default on most arches).
62 	 */
63 	shared_segment->p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0);
64 	if (shared_segment->p != MAP_FAILED) {
65 		shared_segment->pos = 0;
66 		shared_segment->size = requested_size;
67 
68 		return ALLOC_SUCCESS;
69 	}
70 #endif
71 
72 	shared_segment->p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
73 	if (shared_segment->p == MAP_FAILED) {
74 		*error_in = "mmap";
75 		return ALLOC_FAILURE;
76 	}
77 
78 	shared_segment->pos = 0;
79 	shared_segment->size = requested_size;
80 
81 	return ALLOC_SUCCESS;
82 }
83 
detach_segment(zend_shared_segment * shared_segment)84 static int detach_segment(zend_shared_segment *shared_segment)
85 {
86 	munmap(shared_segment->p, shared_segment->size);
87 	return 0;
88 }
89 
segment_type_size(void)90 static size_t segment_type_size(void)
91 {
92 	return sizeof(zend_shared_segment);
93 }
94 
95 zend_shared_memory_handlers zend_alloc_mmap_handlers = {
96 	create_segments,
97 	detach_segment,
98 	segment_type_size
99 };
100 
101 #endif /* USE_MMAP */
102