2 * Copyright (c) 2015 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 Copyright (c) 2005 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include <vppinfra/error.h>
39 #include <vppinfra/os.h>
40 #include <vppinfra/bitmap.h>
41 #include <vppinfra/unix.h>
42 #include <vppinfra/format.h>
44 #include <vppinfra/linux/sysfs.h>
46 #include <sys/sysctl.h>
50 #include <sys/types.h>
51 #include <sys/syscall.h>
52 #include <sys/uio.h> /* writev */
54 #include <stdio.h> /* for sprintf */
57 __clib_export __thread uword __os_thread_index = 0;
58 __clib_export __thread uword __os_numa_index = 0;
61 clib_file_n_bytes (char *file, uword * result)
65 if (stat (file, &s) < 0)
66 return clib_error_return_unix (0, "stat `%s'", file);
68 if (S_ISREG (s.st_mode))
73 return /* no error */ 0;
77 clib_file_read_contents (char *file, u8 * result, uword n_bytes)
81 clib_error_t *error = 0;
84 if ((fd = open (file, 0)) < 0)
85 return clib_error_return_unix (0, "open `%s'", file);
92 if ((n_read = read (fd, v + n_done, n_left)) < 0)
94 error = clib_error_return_unix (0, "open `%s'", file);
109 clib_error_return (0,
110 " `%s' expected to read %wd bytes; read only %wd",
111 file, n_bytes, n_bytes - n_left);
120 __clib_export clib_error_t *
121 clib_file_contents (char *file, u8 ** result)
124 clib_error_t *error = 0;
127 if ((error = clib_file_n_bytes (file, &n_bytes)))
131 vec_resize (v, n_bytes);
133 error = clib_file_read_contents (file, v, n_bytes);
144 clib_file_get_resolved_basename (char *fmt, ...)
147 char *p, buffer[PATH_MAX];
152 link = va_format (0, fmt, &va);
156 r = readlink ((char *) link, buffer, sizeof (buffer) - 1);
164 while (p > buffer && p[-1] != '/')
168 vec_add1 (s, p++[0]);
175 unix_proc_file_contents (char *file, u8 ** result)
181 /* Unfortunately, stat(/proc/XXX) returns zero... */
182 fd = open (file, O_RDONLY);
185 return clib_error_return_unix (0, "open `%s'", file);
187 vec_validate (rv, 4095);
191 bytes = read (fd, rv + pos, 4096);
196 return clib_error_return_unix (0, "read '%s'", file);
201 vec_set_len (rv, pos);
205 vec_validate (rv, pos + 4095);
212 void os_panic (void) __attribute__ ((weak));
220 void os_exit (int) __attribute__ ((weak));
228 void os_puts (u8 * string, uword string_length, uword is_error)
229 __attribute__ ((weak));
232 os_puts (u8 * string, uword string_length, uword is_error)
234 int cpu = os_get_thread_index ();
235 int nthreads = os_get_nthreads ();
237 int fd = is_error ? 2 : 1;
238 struct iovec iovs[2];
243 snprintf (buf, sizeof (buf), "%d: ", cpu);
245 iovs[n_iovs].iov_base = buf;
246 iovs[n_iovs].iov_len = strlen (buf);
250 iovs[n_iovs].iov_base = string;
251 iovs[n_iovs].iov_len = string_length;
254 if (writev (fd, iovs, n_iovs) < 0)
258 __clib_export __clib_weak void
259 os_out_of_memory (void)
264 __clib_export __clib_weak uword
265 os_get_nthreads (void)
270 __clib_export clib_bitmap_t *
271 os_get_online_cpu_core_bitmap ()
274 return clib_sysfs_read_bitmap ("/sys/devices/system/cpu/online");
280 __clib_export clib_bitmap_t *
281 os_get_cpu_affinity_bitmap (int pid)
286 uword *affinity_cpus;
288 clib_bitmap_alloc (affinity_cpus, sizeof (cpu_set_t));
289 clib_bitmap_zero (affinity_cpus);
291 __CPU_ZERO_S (sizeof (cpu_set_t), &cpuset);
293 ret = syscall (SYS_sched_getaffinity, 0, sizeof (cpu_set_t), &cpuset);
297 clib_bitmap_free (affinity_cpus);
301 for (index = 0; index < sizeof (cpu_set_t); index++)
302 if (__CPU_ISSET_S (index, sizeof (cpu_set_t), &cpuset))
303 clib_bitmap_set (affinity_cpus, index, 1);
304 return affinity_cpus;
310 __clib_export clib_bitmap_t *
311 os_get_online_cpu_node_bitmap ()
314 return clib_sysfs_read_bitmap ("/sys/devices/system/node/online");
319 __clib_export clib_bitmap_t *
320 os_get_cpu_on_node_bitmap (int node)
323 return clib_sysfs_read_bitmap ("/sys/devices/system/node/node%u/cpulist",
330 __clib_export clib_bitmap_t *
331 os_get_cpu_with_memory_bitmap ()
334 return clib_sysfs_read_bitmap ("/sys/devices/system/node/has_memory");
341 os_get_cpu_phys_core_id (int cpu_id)
349 format (0, "/sys/devices/system/cpu/cpu%u/topology/core_id%c", cpu_id, 0);
350 err = clib_sysfs_read ((char *) p, "%d", &core_id);
354 clib_error_free (err);
369 ssize_t sz = readlink ("/proc/self/exe", tmp, sizeof (tmp));
374 char tmp[MAXPATHLEN];
375 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
376 size_t sz = MAXPATHLEN;
378 if (sysctl (mib, 4, tmp, &sz, NULL, 0) == -1)
381 vec_add (rv, tmp, sz);
386 * fd.io coding-style-patch-verification: ON
389 * eval: (c-set-style "gnu")