Timur Davydov has uploaded this change for review.

View Change

vty: make CPU scheduling optional based on platform support

Add configure checks for sched_* APIs and guard their usage
in cpu_sched_vty with HAVE_SCHED_* macros.

If scheduler functions are not available, return -ENOTSUP
instead of failing at build time.

Also change osmo_cpu_sched_vty_init() to return int.

This improves portability on platforms lacking full sched
support (e.g. non-Linux or restricted environments).

Change-Id: Ic5b7e39fac16531d370cb81f769ba87fef18cb81
---
M configure.ac
M include/osmocom/vty/cpu_sched_vty.h
M src/vty/cpu_sched_vty.c
3 files changed, 15 insertions(+), 2 deletions(-)

git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/53/42653/1
diff --git a/configure.ac b/configure.ac
index b5f858f..cde4446 100644
--- a/configure.ac
+++ b/configure.ac
@@ -162,6 +162,10 @@
AC_CHECK_HEADERS([sched.h sys/mount.h sys/param.h])
AC_CHECK_FUNCS([setns unshare mount])

+dnl Check the scheduller functions
+AC_CHECK_HEADERS([sched.h])
+AC_CHECK_FUNCS([sched_setaffinity sched_setscheduler sched_getaffinity])
+
dnl Check whether CLONE_NEWNET is available
AC_CHECK_DECL([CLONE_NEWNET],
[AC_DEFINE([HAVE_CLONE_NEWNET], [1],
diff --git a/include/osmocom/vty/cpu_sched_vty.h b/include/osmocom/vty/cpu_sched_vty.h
index 171f168..b04ee34 100644
--- a/include/osmocom/vty/cpu_sched_vty.h
+++ b/include/osmocom/vty/cpu_sched_vty.h
@@ -31,7 +31,7 @@
* \file cpu_sched_vty.h
*/

-void osmo_cpu_sched_vty_init(void *tall_ctx);
+int osmo_cpu_sched_vty_init(void *tall_ctx);
int osmo_cpu_sched_vty_apply_localthread(void);

/*! @} */
diff --git a/src/vty/cpu_sched_vty.c b/src/vty/cpu_sched_vty.c
index 4f3eeb1..a712860 100644
--- a/src/vty/cpu_sched_vty.c
+++ b/src/vty/cpu_sched_vty.c
@@ -331,6 +331,7 @@
static int my_sched_setaffinity(enum sched_vty_thread_id tid_type, pid_t pid,
cpu_set_t *cpuset, size_t cpuset_size)
{
+#ifdef HAVE_SCHED_SETAFFINITY
DIR *proc_dir;
struct dirent *entry;
char dirname[100];
@@ -369,7 +370,9 @@

closedir(proc_dir);
return rc;
-
+#else /* HAVE_SCHED_SETAFFINITY */
+ return -ENOTSUP;
+#endif /* HAVE_SCHED_SETAFFINITY */
}

DEFUN_ATTR(cfg_sched_cpu_affinity, cfg_sched_cpu_affinity_cmd,
@@ -464,6 +467,7 @@

static int set_sched_rr(unsigned int prio)
{
+#ifdef HAVE_SCHED_SETSCHEDULER
struct sched_param param;
int rc;
memset(&param, 0, sizeof(param));
@@ -476,6 +480,9 @@
return -1;
}
return 0;
+#else /* HAVE_SCHED_SETSCHEDULER */
+ return -ENOTSUP;
+#endif /* HAVE_SCHED_SETSCHEDULER */
}

DEFUN_ATTR(cfg_sched_policy, cfg_sched_policy_cmd,
@@ -558,10 +565,12 @@
cpuset = CPU_ALLOC(get_num_cpus());
cpuset_size = CPU_ALLOC_SIZE(get_num_cpus());
CPU_ZERO_S(cpuset_size, cpuset);
+#ifdef HAVE_SCHED_GETAFFINITY
if (sched_getaffinity(tid_it, cpuset_size, cpuset) == 0) {
if (generate_cpu_hex_mask(str_mask, sizeof(str_mask), cpuset, cpuset_size) < 0)
str_mask[0] = '\0';
}
+#endif /* HAVE_SCHED_GETAFFINITY */
CPU_FREE(cpuset);

vty_out(vty, " TID: %lu, NAME: '%s', cpu-affinity: %s%s",

To view, visit change 42653. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ic5b7e39fac16531d370cb81f769ba87fef18cb81
Gerrit-Change-Number: 42653
Gerrit-PatchSet: 1
Gerrit-Owner: Timur Davydov <dtv.comp@gmail.com>