[libvirt] [PATCH 1/3] util: Add virProcessSetScheduler() function for scheduler settings

Martin Kletzander mkletzan at redhat.com
Tue Jan 13 07:57:02 UTC 2015


This function uses sched_setscheduler() function so it works with
processes and threads as well (even threads not created by us, which is
what we'll need in the future).

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virprocess.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 src/util/virprocess.h    |  6 +++++-
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index fb5d003..de3476e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1882,6 +1882,7 @@ virProcessSetMaxFiles;
 virProcessSetMaxMemLock;
 virProcessSetMaxProcesses;
 virProcessSetNamespaces;
+virProcessSetScheduler;
 virProcessTranslateStatus;
 virProcessWait;

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index d0a1500..85da5e8 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -1,7 +1,7 @@
 /*
  * virprocess.c: interaction with processes
  *
- * Copyright (C) 2010-2014 Red Hat, Inc.
+ * Copyright (C) 2010-2015 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -32,7 +32,6 @@
 # include <sys/time.h>
 # include <sys/resource.h>
 #endif
-#include <sched.h>

 #if defined(__FreeBSD__) || HAVE_BSD_CPU_AFFINITY
 # include <sys/param.h>
@@ -1052,3 +1051,46 @@ virProcessExitWithStatus(int status)
     }
     exit(value);
 }
+
+int
+virProcessSetScheduler(pid_t pid, int policy, int priority)
+{
+    struct sched_param param = {0};
+
+    VIR_DEBUG("pid=%d, policy=%d, priority=%u", pid, policy, priority);
+
+    if (policy == SCHED_FIFO || policy == SCHED_RR) {
+        int min = 0;
+        int max = 0;
+
+        if ((min = sched_get_priority_min(policy)) < 0) {
+            virReportSystemError(errno, "%s",
+                                 _("Cannot get minimum scheduler priority value"));
+            return -1;
+        }
+
+        if ((max = sched_get_priority_max(policy)) < 0) {
+            virReportSystemError(errno, "%s",
+                                 _("Cannot get maximum scheduler priority value"));
+            return -1;
+        }
+
+        if (priority < min || priority > max) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("Scheduler priority %d out of range [%d, %d]"),
+                           priority, min, max);
+            return -1;
+        }
+
+        param.sched_priority = priority;
+    }
+
+    if (sched_setscheduler(pid, policy, &param) < 0) {
+        virReportSystemError(errno,
+                             _("Cannot set scheduler parameters for pid %d"),
+                             pid);
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/src/util/virprocess.h b/src/util/virprocess.h
index bcaede5..34b15a4 100644
--- a/src/util/virprocess.h
+++ b/src/util/virprocess.h
@@ -1,7 +1,7 @@
 /*
  * virprocess.h: interaction with processes
  *
- * Copyright (C) 2010-2014 Red Hat, Inc.
+ * Copyright (C) 2010-2015 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +23,7 @@
 # define __VIR_PROCESS_H__

 # include <sys/types.h>
+# include <sched.h>

 # include "internal.h"
 # include "virbitmap.h"
@@ -73,4 +74,7 @@ typedef int (*virProcessNamespaceCallback)(pid_t pid, void *opaque);
 int virProcessRunInMountNamespace(pid_t pid,
                                   virProcessNamespaceCallback cb,
                                   void *opaque);
+
+int virProcessSetScheduler(pid_t pid, int policy, int priority);
+
 #endif /* __VIR_PROCESS_H__ */
-- 
2.2.1




More information about the libvir-list mailing list