1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
3 | #ident "$Id$" |
4 | /*====== |
5 | This file is part of PerconaFT. |
6 | |
7 | |
8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
9 | |
10 | PerconaFT is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License, version 2, |
12 | as published by the Free Software Foundation. |
13 | |
14 | PerconaFT is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
21 | |
22 | ---------------------------------------- |
23 | |
24 | PerconaFT is free software: you can redistribute it and/or modify |
25 | it under the terms of the GNU Affero General Public License, version 3, |
26 | as published by the Free Software Foundation. |
27 | |
28 | PerconaFT is distributed in the hope that it will be useful, |
29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31 | GNU Affero General Public License for more details. |
32 | |
33 | You should have received a copy of the GNU Affero General Public License |
34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
35 | ======= */ |
36 | |
37 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
38 | |
39 | #include <sys/mman.h> |
40 | #include <stdlib.h> |
41 | #include <stdio.h> |
42 | #include <string.h> |
43 | |
44 | #include <portability/toku_assert.h> |
45 | #include <portability/toku_os.h> |
46 | |
47 | static bool check_huge_pages_config_file(const char *fname) |
48 | // Effect: Return true if huge pages are there. If so, print diagnostics. |
49 | { |
50 | bool huge_pages_enabled = false; |
51 | FILE *f = fopen(fname, "r" ); |
52 | if (f) { |
53 | // It's redhat and the feature appears to be there. Is it enabled? |
54 | char buf[1000]; |
55 | char *r = fgets(buf, sizeof(buf), f); |
56 | assert(r != NULL); |
57 | if (strstr(buf, "[always]" )) { |
58 | fprintf(stderr, "Transparent huge pages are enabled, according to %s\n" , fname); |
59 | huge_pages_enabled = true; |
60 | } else { |
61 | huge_pages_enabled =false; |
62 | } |
63 | fclose(f); |
64 | } |
65 | return huge_pages_enabled; |
66 | } |
67 | |
68 | static bool check_huge_pages_in_practice(void) |
69 | // Effect: Return true if huge pages appear to be defined in practice. |
70 | { |
71 | #ifdef HAVE_MINCORE |
72 | #ifdef HAVE_MAP_ANONYMOUS |
73 | const int map_anonymous = MAP_ANONYMOUS; |
74 | #else |
75 | const int map_anonymous = MAP_ANON; |
76 | #endif |
77 | const size_t TWO_MB = 2UL*1024UL*1024UL; |
78 | |
79 | void *first = mmap(NULL, 2*TWO_MB, PROT_READ|PROT_WRITE, MAP_PRIVATE|map_anonymous, -1, 0); |
80 | if ((long)first==-1) perror("mmap failed" ); |
81 | { |
82 | int r = munmap(first, 2*TWO_MB); |
83 | assert(r==0); |
84 | } |
85 | |
86 | void *second_addr = (void*)(((unsigned long)first + TWO_MB) & ~(TWO_MB -1)); |
87 | void *second = mmap(second_addr, TWO_MB, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE|map_anonymous, -1, 0); |
88 | if ((long)second==-1) perror("mmap failed" ); |
89 | assert((long)second%TWO_MB == 0); |
90 | |
91 | const long pagesize = 4096; |
92 | const long n_pages = TWO_MB/pagesize; |
93 | #ifdef __linux__ |
94 | // On linux mincore is defined as mincore(void *, size_t, unsigned char *) |
95 | unsigned char vec[n_pages]; |
96 | #else |
97 | // On BSD (OS X included) it is defined as mincore(void *, size_t, char *) |
98 | char vec[n_pages]; |
99 | #endif |
100 | { |
101 | int r = mincore(second, TWO_MB, vec); |
102 | if (r!=0 && errno==ENOMEM) { |
103 | // On some kernels (e.g., Centos 5.8), mincore doesn't work. It seems unlikely that huge pages are here. |
104 | munmap(second, TWO_MB); |
105 | return false; |
106 | } |
107 | assert(r==0); |
108 | } |
109 | for (long i=0; i<n_pages; i++) { |
110 | assert(!vec[i]); |
111 | } |
112 | ((char*)second)[0] = 1; |
113 | { |
114 | int r = mincore(second, TWO_MB, vec); |
115 | // If the mincore worked the first time, it probably works here too.x |
116 | assert(r==0); |
117 | } |
118 | assert(vec[0]); |
119 | { |
120 | int r = munmap(second, TWO_MB); |
121 | assert(r==0); |
122 | } |
123 | if (vec[1]) { |
124 | fprintf(stderr, "Transparent huge pages appear to be enabled according to mincore()\n" ); |
125 | return true; |
126 | } else { |
127 | return false; |
128 | } |
129 | #else |
130 | // No mincore, so no way to check this in practice |
131 | return false; |
132 | #endif |
133 | } |
134 | |
135 | bool toku_os_huge_pages_enabled(void) |
136 | // Effect: Return true if huge pages appear to be enabled. If so, print some diagnostics to stderr. |
137 | // If environment variable TOKU_HUGE_PAGES_OK is set, then don't complain. |
138 | { |
139 | char *toku_huge_pages_ok = getenv("TOKU_HUGE_PAGES_OK" ); |
140 | if (toku_huge_pages_ok) { |
141 | return false; |
142 | } else { |
143 | bool conf1 = check_huge_pages_config_file("/sys/kernel/mm/redhat_transparent_hugepage/enabled" ); |
144 | bool conf2 = check_huge_pages_config_file("/sys/kernel/mm/transparent_hugepage/enabled" ); |
145 | bool prac = check_huge_pages_in_practice(); |
146 | return conf1|conf2|prac; |
147 | } |
148 | } |
149 | |