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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jan 19, 2015 at 10:12:34PM -0500, John Ferlan wrote:


On 01/13/2015 02:57 AM, Martin Kletzander wrote:
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@xxxxxxxxxx>
---
 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>

hmm.. moving to virprocess.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>

Does moving this into here work for other build platforms?  Is it
required - it doesn't seem so given all that changes doesn't include
anything sched.h specific.


I moved this so that whoever includes this file has SCHED_* macros
automatically available.  Unfortunately, this breaks the build on
mingw, you're right.  I'll try a different approach.  Or maybe just
guarding this include by ifndef WIN32 would work since we can't (and
won't) change the scheduler on windows.  Otherwise the translation
needs to be done in util/ and the enum has to be renamed and moved
here as well so that this not depend on conf/.

John

 # 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__ */

Attachment: pgpKqYKnJGMyW.pgp
Description: PGP signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]