diff options
Diffstat (limited to 'src/vppinfra/unix-misc.c')
-rw-r--r-- | src/vppinfra/unix-misc.c | 157 |
1 files changed, 156 insertions, 1 deletions
diff --git a/src/vppinfra/unix-misc.c b/src/vppinfra/unix-misc.c index 5559a2392fe..29cbe0a557d 100644 --- a/src/vppinfra/unix-misc.c +++ b/src/vppinfra/unix-misc.c @@ -37,13 +37,22 @@ #include <vppinfra/error.h> #include <vppinfra/os.h> +#include <vppinfra/bitmap.h> #include <vppinfra/unix.h> +#include <vppinfra/format.h> +#ifdef __linux__ +#include <vppinfra/linux/sysfs.h> +#else +#include <sys/sysctl.h> +#endif #include <sys/stat.h> #include <sys/types.h> +#include <sys/syscall.h> #include <sys/uio.h> /* writev */ #include <fcntl.h> #include <stdio.h> /* for sprintf */ +#include <limits.h> __clib_export __thread uword __os_thread_index = 0; __clib_export __thread uword __os_numa_index = 0; @@ -131,6 +140,37 @@ clib_file_contents (char *file, u8 ** result) return error; } +__clib_export u8 * +clib_file_get_resolved_basename (char *fmt, ...) +{ + va_list va; + char *p, buffer[PATH_MAX]; + u8 *link, *s = 0; + int r; + + va_start (va, fmt); + link = va_format (0, fmt, &va); + va_end (va); + vec_add1 (link, 0); + + r = readlink ((char *) link, buffer, sizeof (buffer) - 1); + vec_free (link); + + if (r < 1) + return 0; + + buffer[r] = 0; + p = buffer + r - 1; + while (p > buffer && p[-1] != '/') + p--; + + while (p[0]) + vec_add1 (s, p++[0]); + + vec_add1 (s, 0); + return s; +} + clib_error_t * unix_proc_file_contents (char *file, u8 ** result) { @@ -158,7 +198,7 @@ unix_proc_file_contents (char *file, u8 ** result) if (bytes == 0) { - _vec_len (rv) = pos; + vec_set_len (rv, pos); break; } pos += bytes; @@ -227,6 +267,121 @@ os_get_nthreads (void) return 1; } +__clib_export clib_bitmap_t * +os_get_online_cpu_core_bitmap () +{ +#if __linux__ + return clib_sysfs_read_bitmap ("/sys/devices/system/cpu/online"); +#else + return 0; +#endif +} + +__clib_export clib_bitmap_t * +os_get_cpu_affinity_bitmap (int pid) +{ +#if __linux + int index, ret; + cpu_set_t cpuset; + uword *affinity_cpus; + + clib_bitmap_alloc (affinity_cpus, sizeof (cpu_set_t)); + clib_bitmap_zero (affinity_cpus); + + __CPU_ZERO_S (sizeof (cpu_set_t), &cpuset); + + ret = syscall (SYS_sched_getaffinity, 0, sizeof (cpu_set_t), &cpuset); + + if (ret < 0) + { + clib_bitmap_free (affinity_cpus); + return 0; + } + + for (index = 0; index < sizeof (cpu_set_t); index++) + if (__CPU_ISSET_S (index, sizeof (cpu_set_t), &cpuset)) + clib_bitmap_set (affinity_cpus, index, 1); + return affinity_cpus; +#else + return 0; +#endif +} + +__clib_export clib_bitmap_t * +os_get_online_cpu_node_bitmap () +{ +#if __linux__ + return clib_sysfs_read_bitmap ("/sys/devices/system/node/online"); +#else + return 0; +#endif +} +__clib_export clib_bitmap_t * +os_get_cpu_on_node_bitmap (int node) +{ +#if __linux__ + return clib_sysfs_read_bitmap ("/sys/devices/system/node/node%u/cpulist", + node); +#else + return 0; +#endif +} + +__clib_export clib_bitmap_t * +os_get_cpu_with_memory_bitmap () +{ +#if __linux__ + return clib_sysfs_read_bitmap ("/sys/devices/system/node/has_memory"); +#else + return 0; +#endif +} + +__clib_export int +os_get_cpu_phys_core_id (int cpu_id) +{ +#if __linux + int core_id = -1; + clib_error_t *err; + u8 *p; + + p = + format (0, "/sys/devices/system/cpu/cpu%u/topology/core_id%c", cpu_id, 0); + err = clib_sysfs_read ((char *) p, "%d", &core_id); + vec_free (p); + if (err) + { + clib_error_free (err); + return -1; + } + return core_id; +#else + return -1; +#endif +} + +__clib_export u8 * +os_get_exec_path () +{ + u8 *rv = 0; +#ifdef __linux__ + char tmp[PATH_MAX]; + ssize_t sz = readlink ("/proc/self/exe", tmp, sizeof (tmp)); + + if (sz <= 0) + return 0; +#else + char tmp[MAXPATHLEN]; + int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + size_t sz = MAXPATHLEN; + + if (sysctl (mib, 4, tmp, &sz, NULL, 0) == -1) + return 0; +#endif + vec_add (rv, tmp, sz); + return rv; +} + /* * fd.io coding-style-patch-verification: ON * |