blob: 6d9f859640c964486626335186dbe41d4cbc1bc5 [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
Damjan Marion8bea5892022-04-04 22:40:45 +020073 vec_set_len (s, sz);
Damjan Marion3b64d632017-09-08 12:26:12 +020074 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 Marion3b64d632017-09-08 12:26:12 +020090
91clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +020092clib_sysfs_set_nr_hugepages (int numa_node, int log2_page_size, int nr)
Damjan Marion3b64d632017-09-08 12:26:12 +020093{
94 clib_error_t *error = 0;
95 struct stat sb;
96 u8 *p = 0;
Damjan Marion9787f5f2018-10-24 12:56:32 +020097 uword page_size;
98
99 if (log2_page_size == 0)
100 log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
101
102 page_size = 1ULL << (log2_page_size - 10);
Damjan Marion3b64d632017-09-08 12:26:12 +0200103
104 p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
105
106 if (stat ((char *) p, &sb) == 0)
107 {
108 if (S_ISDIR (sb.st_mode) == 0)
109 {
110 error = clib_error_return (0, "'%s' is not directory", p);
111 goto done;
112 }
113 }
114 else if (numa_node == 0)
115 {
116 vec_reset_length (p);
117 p = format (p, "/sys/kernel/mm%c", 0);
118 if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
119 {
120 error = clib_error_return (0, "'%s' does not exist or it is not "
121 "directory", p);
122 goto done;
123 }
124 }
125 else
126 {
127 error = clib_error_return (0, "'%s' does not exist", p);
128 goto done;
129 }
130
Damjan Marion8bea5892022-04-04 22:40:45 +0200131 vec_dec_len (p, 1);
Damjan Marion3b64d632017-09-08 12:26:12 +0200132 p = format (p, "/hugepages/hugepages-%ukB/nr_hugepages%c", page_size, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200133 clib_sysfs_write ((char *) p, "%d", nr);
Damjan Marion3b64d632017-09-08 12:26:12 +0200134
135done:
136 vec_free (p);
137 return error;
138}
139
140
141static clib_error_t *
Damjan Marion01914ce2017-09-14 19:04:50 +0200142clib_sysfs_get_xxx_hugepages (char *type, int numa_node,
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200143 int log2_page_size, int *val)
Damjan Marion3b64d632017-09-08 12:26:12 +0200144{
145 clib_error_t *error = 0;
146 struct stat sb;
147 u8 *p = 0;
Damjan Marion9787f5f2018-10-24 12:56:32 +0200148
149 uword page_size;
150
151 if (log2_page_size == 0)
152 log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
153
154 page_size = 1ULL << (log2_page_size - 10);
155
Damjan Marion3b64d632017-09-08 12:26:12 +0200156
157 p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
158
159 if (stat ((char *) p, &sb) == 0)
160 {
161 if (S_ISDIR (sb.st_mode) == 0)
162 {
163 error = clib_error_return (0, "'%s' is not directory", p);
164 goto done;
165 }
166 }
167 else if (numa_node == 0)
168 {
169 vec_reset_length (p);
170 p = format (p, "/sys/kernel/mm%c", 0);
171 if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
172 {
173 error = clib_error_return (0, "'%s' does not exist or it is not "
174 "directory", p);
175 goto done;
176 }
177 }
178 else
179 {
180 error = clib_error_return (0, "'%s' does not exist", p);
181 goto done;
182 }
183
Damjan Marion8bea5892022-04-04 22:40:45 +0200184 vec_dec_len (p, 1);
Damjan Marion3b64d632017-09-08 12:26:12 +0200185 p = format (p, "/hugepages/hugepages-%ukB/%s_hugepages%c", page_size,
186 type, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200187 error = clib_sysfs_read ((char *) p, "%d", val);
Damjan Marion3b64d632017-09-08 12:26:12 +0200188
189done:
190 vec_free (p);
191 return error;
192}
193
194clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200195clib_sysfs_get_free_hugepages (int numa_node, int log2_page_size, int *v)
Damjan Marion3b64d632017-09-08 12:26:12 +0200196{
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200197 return clib_sysfs_get_xxx_hugepages ("free", numa_node, log2_page_size, v);
Damjan Marion3b64d632017-09-08 12:26:12 +0200198}
199
200clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200201clib_sysfs_get_nr_hugepages (int numa_node, int log2_page_size, int *v)
Damjan Marion3b64d632017-09-08 12:26:12 +0200202{
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200203 return clib_sysfs_get_xxx_hugepages ("nr", numa_node, log2_page_size, v);
Damjan Marion3b64d632017-09-08 12:26:12 +0200204}
205
206clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200207clib_sysfs_get_surplus_hugepages (int numa_node, int log2_page_size, int *v)
Damjan Marion3b64d632017-09-08 12:26:12 +0200208{
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200209 return clib_sysfs_get_xxx_hugepages ("surplus", numa_node, log2_page_size,
210 v);
Damjan Marion3b64d632017-09-08 12:26:12 +0200211}
212
213clib_error_t *
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200214clib_sysfs_prealloc_hugepages (int numa_node, int log2_page_size, int nr)
Damjan Marion3b64d632017-09-08 12:26:12 +0200215{
216 clib_error_t *error = 0;
217 int n, needed;
Damjan Marion9787f5f2018-10-24 12:56:32 +0200218 uword page_size;
219
220 if (log2_page_size == 0)
221 log2_page_size = min_log2 (clib_mem_get_default_hugepage_size ());
222
223 page_size = 1ULL << (log2_page_size - 10);
224
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200225 error = clib_sysfs_get_free_hugepages (numa_node, log2_page_size, &n);
Damjan Marion3b64d632017-09-08 12:26:12 +0200226 if (error)
227 return error;
228 needed = nr - n;
229 if (needed <= 0)
230 return 0;
231
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200232 error = clib_sysfs_get_nr_hugepages (numa_node, log2_page_size, &n);
Damjan Marion3b64d632017-09-08 12:26:12 +0200233 if (error)
234 return error;
235 clib_warning ("pre-allocating %u additional %uK hugepages on numa node %u",
236 needed, page_size, numa_node);
Damjan Marion6f3f1cb2018-10-22 13:01:46 +0200237 return clib_sysfs_set_nr_hugepages (numa_node, log2_page_size, n + needed);
Damjan Marion3b64d632017-09-08 12:26:12 +0200238}
239
Damjan Marion6db633b2021-04-19 21:44:21 +0200240__clib_export uword *
241clib_sysfs_list_to_bitmap (char *filename)
242{
243 FILE *fp;
244 uword *r = 0;
245
246 fp = fopen (filename, "r");
247
248 if (fp != NULL)
249 {
250 u8 *buffer = 0;
251 vec_validate (buffer, 256 - 1);
252 if (fgets ((char *) buffer, 256, fp))
253 {
254 unformat_input_t in;
255 unformat_init_string (&in, (char *) buffer,
256 strlen ((char *) buffer));
257 if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1)
258 clib_warning ("unformat_bitmap_list failed");
259 unformat_free (&in);
260 }
261 vec_free (buffer);
262 fclose (fp);
263 }
264 return r;
265}
Damjan Marion3b64d632017-09-08 12:26:12 +0200266
267/*
268 * fd.io coding-style-patch-verification: ON
269 *
270 * Local Variables:
271 * eval: (c-set-style "gnu")
272 * End:
273 */