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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vlib/unix/unix.h>
19 #include <sys/types.h>
25 vlib_sysfs_write (char *file_name, char *fmt, ...)
29 clib_error_t *error = 0;
31 fd = open (file_name, O_WRONLY);
33 return clib_error_return_unix (0, "open `%s'", file_name);
37 s = va_format (0, fmt, &va);
40 if (write (fd, s, vec_len (s)) < 0)
41 error = clib_error_return_unix (0, "write `%s'", file_name);
49 vlib_sysfs_read (char *file_name, char *fmt, ...)
51 unformat_input_t input;
57 fd = open (file_name, O_RDONLY);
59 return clib_error_return_unix (0, "open `%s'", file_name);
61 vec_validate (s, 4095);
63 sz = read (fd, s, vec_len (s));
68 return clib_error_return_unix (0, "read `%s'", file_name);
72 unformat_init_vector (&input, s);
76 result = va_unformat (&input, fmt, &va);
83 return clib_error_return (0, "unformat error");
89 vlib_sysfs_link_to_name (char *link)
96 r = readlink (link, buffer, sizeof (buffer) - 1);
102 p = strrchr (buffer, '/');
107 unformat_init_string (&in, p + 1, strlen (p + 1));
108 if (unformat (&in, "%s", &s) != 1)
109 clib_unix_warning ("no string?");
116 vlib_sysfs_set_nr_hugepages (unsigned int numa_node, int page_size, int nr)
118 clib_error_t *error = 0;
122 p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
124 if (stat ((char *) p, &sb) == 0)
126 if (S_ISDIR (sb.st_mode) == 0)
128 error = clib_error_return (0, "'%s' is not directory", p);
132 else if (numa_node == 0)
134 vec_reset_length (p);
135 p = format (p, "/sys/kernel/mm%c", 0);
136 if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
138 error = clib_error_return (0, "'%s' does not exist or it is not "
145 error = clib_error_return (0, "'%s' does not exist", p);
150 p = format (p, "/hugepages/hugepages-%ukB/nr_hugepages%c", page_size, 0);
151 vlib_sysfs_write ((char *) p, "%d", nr);
159 static clib_error_t *
160 vlib_sysfs_get_xxx_hugepages (char *type, unsigned int numa_node,
161 int page_size, int *val)
163 clib_error_t *error = 0;
167 p = format (p, "/sys/devices/system/node/node%u%c", numa_node, 0);
169 if (stat ((char *) p, &sb) == 0)
171 if (S_ISDIR (sb.st_mode) == 0)
173 error = clib_error_return (0, "'%s' is not directory", p);
177 else if (numa_node == 0)
179 vec_reset_length (p);
180 p = format (p, "/sys/kernel/mm%c", 0);
181 if (stat ((char *) p, &sb) < 0 || S_ISDIR (sb.st_mode) == 0)
183 error = clib_error_return (0, "'%s' does not exist or it is not "
190 error = clib_error_return (0, "'%s' does not exist", p);
195 p = format (p, "/hugepages/hugepages-%ukB/%s_hugepages%c", page_size,
197 error = vlib_sysfs_read ((char *) p, "%d", val);
205 vlib_sysfs_get_free_hugepages (unsigned int numa_node, int page_size, int *v)
207 return vlib_sysfs_get_xxx_hugepages ("free", numa_node, page_size, v);
211 vlib_sysfs_get_nr_hugepages (unsigned int numa_node, int page_size, int *v)
213 return vlib_sysfs_get_xxx_hugepages ("nr", numa_node, page_size, v);
217 vlib_sysfs_get_surplus_hugepages (unsigned int numa_node, int page_size,
220 return vlib_sysfs_get_xxx_hugepages ("surplus", numa_node, page_size, v);
224 vlib_sysfs_prealloc_hugepages (unsigned int numa_node, int page_size, int nr)
226 clib_error_t *error = 0;
228 error = vlib_sysfs_get_free_hugepages (numa_node, page_size, &n);
235 error = vlib_sysfs_get_nr_hugepages (numa_node, page_size, &n);
238 clib_warning ("pre-allocating %u additional %uK hugepages on numa node %u",
239 needed, page_size, numa_node);
240 return vlib_sysfs_set_nr_hugepages (numa_node, page_size, n + needed);
245 * fd.io coding-style-patch-verification: ON
248 * eval: (c-set-style "gnu")