blob: 758eaa1a86c5638041fd57e697ded0f119365a7f [file] [log] [blame]
Damjan Marion3b64d632017-09-08 12:26:12 +02001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Damjan Marion01914ce2017-09-14 19:04:50 +020016#include <vppinfra/clib.h>
17#include <vppinfra/clib_error.h>
18#include <vppinfra/format.h>
Damjan Marion6db633b2021-04-19 21:44:21 +020019#include <vppinfra/bitmap.h>
Damjan Marion3b64d632017-09-08 12:26:12 +020020
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <dirent.h>
25
Damjan Mariondae1c7e2020-10-17 13:32:25 +020026__clib_export clib_error_t *
Damjan Marion01914ce2017-09-14 19:04:50 +020027clib_sysfs_write (char *file_name, char *fmt, ...)
Damjan Marion3b64d632017-09-08 12:26:12 +020028{
29 u8 *s;
30 int fd;
31 clib_error_t *error = 0;
32
33 fd = open (file_name, O_WRONLY);
34 if (fd < 0)
35 return clib_error_return_unix (0, "open `%s'", file_name);
36
37 va_list va;
38 va_start (va, fmt);
39 s = va_format (0, fmt, &va);
40 va_end (va);
41
42 if (write (fd, s, vec_len (s)) < 0)
43 error = clib_error_return_unix (0, "write `%s'", file_name);
44
45 vec_free (s);
46 close (fd);
47 return error;
48}
49
Damjan Mariondae1c7e2020-10-17 13:32:25 +020050__clib_export clib_error_t *
Damjan Marion01914ce2017-09-14 19:04:50 +020051clib_sysfs_read (char *file_name, char *fmt, ...)
Damjan Marion3b64d632017-09-08 12:26:12 +020052{
53 unformat_input_t input;
54 u8 *s = 0;
55 int fd;
56 ssize_t sz;
57 uword result;
58
59 fd = open (file_name, O_RDONLY);
60 if (fd < 0)
61 return clib_error_return_unix (0, "open `%s'", file_name);
62
63 vec_validate (s, 4095);
64
65 sz = read (fd, s, vec_len (s));
66 if (sz < 0)
67 {
68 close (fd);
69 vec_free (s);
70 return clib_error_return_unix (0, "read `%s'", file_name);
71 }
72
73 _vec_len (s) = sz;
74 unformat_init_vector (&input, s);
75
76 va_list va;
77 va_start (va, fmt);
78 result = va_unformat (&input, fmt, &va);
79 va_end (va);
80
81 vec_free (s);
82 close (fd);
83
84 if (result == 0)
85 return clib_error_return (0, "unformat error");
86
87 return 0;
88}
89
Damjan Mariondae1c7e2020-10-17 13:32:25 +020090__clib_export u8 *
Damjan Marion01914ce2017-09-14 19:04:50 +020091clib_sysfs_link_to_name (char *link)
Damjan Marion3b64d632017-09-08 12:26:12 +020092{
93 char *p, buffer[64];
94 unformat_input_t in;
95 u8 *s = 0;
96 int r;
97
98 r = readlink (link, buffer, sizeof (buffer) - 1);
99
100 if (r < 0)
101 return 0;
102
103 buffer[r] = 0;
104 p = strrchr (buffer, '/');
105
106 if (!p)
107 return 0;
108
109 unformat_init_string (&in, p + 1, strlen (p + 1));
110 if (unformat (&in, "%s", &s) != 1)
111 clib_unix_warning ("no string?");
112 unformat_free (&in);
113
114 return s;
115}
116
117clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200118clib_sysfs_set_nr_hugepages (int numa_node, int log2_page_size, int nr)
Damjan Marion3b64d632017-09-08 12:26:12 +0200119{
120 clib_error_t *error = 0;
121 struct stat sb;
122 u8 *p = 0;
Damjan Marion9787f5f2018-10-24 12:56:32 +0200123 uword page_size;
124
125 if (log2_page_size == 0)
126 log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
127
128 page_size = 1ULL << (log2_page_size - 10);
Damjan Marion3b64d632017-09-08 12:26:12 +0200129
130 p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
131
132 if (stat ((char *) p, &sb) == 0)
133 {
134 if (S_ISDIR (sb.st_mode) == 0)
135 {
136 error = clib_error_return (0, "'%s' is not directory", p);
137 goto done;
138 }
139 }
140 else if (numa_node == 0)
141 {
142 vec_reset_length (p);
143 p = format (p, "/sys/kernel/mm%c", 0);
144 if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
145 {
146 error = clib_error_return (0, "'%s' does not exist or it is not "
147 "directory", p);
148 goto done;
149 }
150 }
151 else
152 {
153 error = clib_error_return (0, "'%s' does not exist", p);
154 goto done;
155 }
156
157 _vec_len (p) -= 1;
158 p = format (p, "/hugepages/hugepages-%ukB/nr_hugepages%c", page_size, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200159 clib_sysfs_write ((char *) p, "%d", nr);
Damjan Marion3b64d632017-09-08 12:26:12 +0200160
161done:
162 vec_free (p);
163 return error;
164}
165
166
167static clib_error_t *
Damjan Marion01914ce2017-09-14 19:04:50 +0200168clib_sysfs_get_xxx_hugepages (char *type, int numa_node,
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200169 int log2_page_size, int *val)
Damjan Marion3b64d632017-09-08 12:26:12 +0200170{
171 clib_error_t *error = 0;
172 struct stat sb;
173 u8 *p = 0;
Damjan Marion9787f5f2018-10-24 12:56:32 +0200174
175 uword page_size;
176
177 if (log2_page_size == 0)
178 log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
179
180 page_size = 1ULL << (log2_page_size - 10);
181
Damjan Marion3b64d632017-09-08 12:26:12 +0200182
183 p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
184
185 if (stat ((char *) p, &sb) == 0)
186 {
187 if (S_ISDIR (sb.st_mode) == 0)
188 {
189 error = clib_error_return (0, "'%s' is not directory", p);
190 goto done;
191 }
192 }
193 else if (numa_node == 0)
194 {
195 vec_reset_length (p);
196 p = format (p, "/sys/kernel/mm%c", 0);
197 if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
198 {
199 error = clib_error_return (0, "'%s' does not exist or it is not "
200 "directory", p);
201 goto done;
202 }
203 }
204 else
205 {
206 error = clib_error_return (0, "'%s' does not exist", p);
207 goto done;
208 }
209
210 _vec_len (p) -= 1;
211 p = format (p, "/hugepages/hugepages-%ukB/%s_hugepages%c", page_size,
212 type, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200213 error = clib_sysfs_read ((char *) p, "%d", val);
Damjan Marion3b64d632017-09-08 12:26:12 +0200214
215done:
216 vec_free (p);
217 return error;
218}
219
220clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200221clib_sysfs_get_free_hugepages (int numa_node, int log2_page_size, int *v)
Damjan Marion3b64d632017-09-08 12:26:12 +0200222{
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200223 return clib_sysfs_get_xxx_hugepages ("free", numa_node, log2_page_size, v);
Damjan Marion3b64d632017-09-08 12:26:12 +0200224}
225
226clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200227clib_sysfs_get_nr_hugepages (int numa_node, int log2_page_size, int *v)
Damjan Marion3b64d632017-09-08 12:26:12 +0200228{
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200229 return clib_sysfs_get_xxx_hugepages ("nr", numa_node, log2_page_size, v);
Damjan Marion3b64d632017-09-08 12:26:12 +0200230}
231
232clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200233clib_sysfs_get_surplus_hugepages (int numa_node, int log2_page_size, int *v)
Damjan Marion3b64d632017-09-08 12:26:12 +0200234{
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200235 return clib_sysfs_get_xxx_hugepages ("surplus", numa_node, log2_page_size,
236 v);
Damjan Marion3b64d632017-09-08 12:26:12 +0200237}
238
239clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200240clib_sysfs_prealloc_hugepages (int numa_node, int log2_page_size, int nr)
Damjan Marion3b64d632017-09-08 12:26:12 +0200241{
242 clib_error_t *error = 0;
243 int n, needed;
Damjan Marion9787f5f2018-10-24 12:56:32 +0200244 uword page_size;
245
246 if (log2_page_size == 0)
247 log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
248
249 page_size = 1ULL << (log2_page_size - 10);
250
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200251 error = clib_sysfs_get_free_hugepages (numa_node, log2_page_size, &n);
Damjan Marion3b64d632017-09-08 12:26:12 +0200252 if (error)
253 return error;
254 needed = nr - n;
255 if (needed <= 0)
256 return 0;
257
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200258 error = clib_sysfs_get_nr_hugepages (numa_node, log2_page_size, &n);
Damjan Marion3b64d632017-09-08 12:26:12 +0200259 if (error)
260 return error;
261 clib_warning ("pre-allocating %u additional %uK hugepages on numa node %u",
262 needed, page_size, numa_node);
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200263 return clib_sysfs_set_nr_hugepages (numa_node, log2_page_size, n + needed);
Damjan Marion3b64d632017-09-08 12:26:12 +0200264}
265
Damjan Marion6db633b2021-04-19 21:44:21 +0200266__clib_export uword *
267clib_sysfs_list_to_bitmap (char *filename)
268{
269 FILE *fp;
270 uword *r = 0;
271
272 fp = fopen (filename, "r");
273
274 if (fp != NULL)
275 {
276 u8 *buffer = 0;
277 vec_validate (buffer, 256 - 1);
278 if (fgets ((char *) buffer, 256, fp))
279 {
280 unformat_input_t in;
281 unformat_init_string (&in, (char *) buffer,
282 strlen ((char *) buffer));
283 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
284 clib_warning ("unformat_bitmap_list failed");
285 unformat_free (&in);
286 }
287 vec_free (buffer);
288 fclose (fp);
289 }
290 return r;
291}
Damjan Marion3b64d632017-09-08 12:26:12 +0200292
293/*
294 * fd.io coding-style-patch-verification: ON
295 *
296 * Local Variables:
297 * eval: (c-set-style "gnu")
298 * End:
299 */