[gnome-online-miners] miner-main: Lower the CPU and disk I/O scheduling and priority



commit 25a094a4d8ebf4d413d99894930b9858240cd649
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Jan 16 10:09:58 2014 +0100

    miner-main: Lower the CPU and disk I/O scheduling and priority
    
    The following files were borrowed from tracker:
     - src/tracker-ioprio.c
     - src/tracker-ioprio.h
     - src/tracker-sched.c
     - src/tracker-sched.h
    
    https://bugzilla.gnome.org/show_bug.cgi?id=722278

 src/Makefile.am      |    4 +
 src/gom-miner-main.c |   17 ++++++
 src/tracker-ioprio.c |  150 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/tracker-ioprio.h |   30 ++++++++++
 src/tracker-sched.c  |   83 +++++++++++++++++++++++++++
 src/tracker-sched.h  |   29 ++++++++++
 6 files changed, 313 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 2741cef..56f5e8a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,6 +7,10 @@ libgom_1_0_la_SOURCES = \
     gom-tracker.h \
     gom-utils.c \
     gom-utils.h \
+    tracker-ioprio.c \
+    tracker-ioprio.h \
+    tracker-sched.c \
+    tracker-sched.h \
     $(NULL)
 
 libgom_1_0_la_CPPFLAGS = \
diff --git a/src/gom-miner-main.c b/src/gom-miner-main.c
index 2e0b0a0..d8e7447 100644
--- a/src/gom-miner-main.c
+++ b/src/gom-miner-main.c
@@ -25,9 +25,14 @@
 #error "gom-miner-main.c is meant to be included, not compiled standalone"
 #endif
 
+#include <unistd.h>
+
 #include <glib-unix.h>
 #include <glib.h>
 
+#include "tracker-ioprio.h"
+#include "tracker-sched.h"
+
 #define AUTOQUIT_TIMEOUT 5 /* seconds */
 
 static const gchar introspection_xml[] =
@@ -238,6 +243,18 @@ int
 main (int argc,
       char **argv)
 {
+  tracker_sched_idle ();
+  tracker_ioprio_init ();
+
+  errno = 0;
+  if (nice (19) == -1 && errno != 0)
+    {
+      const gchar *str;
+
+      str = g_strerror (errno);
+      g_warning ("Couldn't set nice value to 19, %s", (str != NULL) ? str : "no error given");
+    }
+
   ensure_autoquit_on ();
   loop = g_main_loop_new (NULL, FALSE);
 
diff --git a/src/tracker-ioprio.c b/src/tracker-ioprio.c
new file mode 100644
index 0000000..6554f48
--- /dev/null
+++ b/src/tracker-ioprio.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2005, Novell, Inc.
+ * Copyright (C) 2006, Jamie McCracken <jamiemcc gnome org>
+ * Copyright (C) 2006, Anders Aagaard
+ *
+ * Based mostly on code by Robert Love <rml novell com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#ifdef __linux__
+
+#include <stdio.h>
+#include <errno.h>
+
+#ifdef HAVE_LINUX_UNISTD_H
+#include <linux/unistd.h>
+#endif
+
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <glib/gstdio.h>
+
+#endif /* __linux__ */
+
+#include "tracker-ioprio.h"
+
+/* We assume ALL linux architectures have the syscalls defined here */
+#ifdef __linux__
+
+/* Make sure the system call is supported */
+#ifndef __NR_ioprio_set
+
+#if defined(__i386__)
+#define __NR_ioprio_set                 289
+#define __NR_ioprio_get                 290
+#elif defined(__powerpc__) || defined(__powerpc64__)
+#define __NR_ioprio_set                 273
+#define __NR_ioprio_get                 274
+#elif defined(__x86_64__)
+#define __NR_ioprio_set                 251
+#define __NR_ioprio_get                 252
+#elif defined(__ia64__)
+#define __NR_ioprio_set                 1274
+#define __NR_ioprio_get                 1275
+#elif defined(__alpha__)
+#define __NR_ioprio_set                 442
+#define __NR_ioprio_get                 443
+#elif defined(__s390x__) || defined(__s390__)
+#define __NR_ioprio_set                 282
+#define __NR_ioprio_get                 283
+#elif defined(__SH4__)
+#define __NR_ioprio_set                 288
+#define __NR_ioprio_get                 289
+#elif defined(__SH5__)
+#define __NR_ioprio_set                 316
+#define __NR_ioprio_get                 317
+#elif defined(__sparc__) || defined(__sparc64__)
+#define __NR_ioprio_set                 196
+#define __NR_ioprio_get                 218
+#elif defined(__arm__)
+#define __NR_ioprio_set                 314
+#define __NR_ioprio_get                 315
+#else
+#error "Unsupported architecture!"
+#endif
+
+#endif /* __NR_ioprio_set */
+
+enum {
+       IOPRIO_CLASS_NONE,
+       IOPRIO_CLASS_RT,
+       IOPRIO_CLASS_BE,
+       IOPRIO_CLASS_IDLE,
+};
+
+enum {
+       IOPRIO_WHO_PROCESS = 1,
+       IOPRIO_WHO_PGRP,
+       IOPRIO_WHO_USER,
+};
+
+#define IOPRIO_CLASS_SHIFT 13
+
+static inline int
+ioprio_set (int which, int who, int ioprio_val)
+{
+       return syscall (__NR_ioprio_set, which, who, ioprio_val);
+}
+
+static int
+set_io_priority_idle (void)
+{
+       int ioprio, ioclass;
+
+       ioprio = 7; /* priority is ignored with idle class */
+       ioclass = IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT;
+
+       return ioprio_set (IOPRIO_WHO_PROCESS, 0, ioprio | ioclass);
+}
+
+static int
+set_io_priority_best_effort (int ioprio_val)
+{
+       int ioclass;
+
+       ioclass = IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT;
+
+       return ioprio_set (IOPRIO_WHO_PROCESS, 0, ioprio_val | ioclass);
+}
+
+void
+tracker_ioprio_init (void)
+{
+       if (set_io_priority_idle () == -1) {
+               g_message ("Could not set idle IO priority, attempting best effort of 7");
+
+               if (set_io_priority_best_effort (7) == -1) {
+                       g_message ("Could not set best effort IO priority either, giving up");
+               }
+       }
+}
+
+#else  /* __linux__ */
+
+void
+tracker_ioprio_init (void)
+{
+}
+
+#endif /* __linux__ */
diff --git a/src/tracker-ioprio.h b/src/tracker-ioprio.h
new file mode 100644
index 0000000..69c1846
--- /dev/null
+++ b/src/tracker-ioprio.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2006, Anders Aagaard
+ * Copyright (C) 2008, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __LIBTRACKER_COMMON_IOPRIO_H__
+#define __LIBTRACKER_COMMON_IOPRIO_H__
+
+G_BEGIN_DECLS
+
+void tracker_ioprio_init (void);
+
+G_END_DECLS
+
+#endif /* __LIBTRACKER_COMMON_IOPRIO_H__ */
diff --git a/src/tracker-sched.c b/src/tracker-sched.c
new file mode 100644
index 0000000..71f6529
--- /dev/null
+++ b/src/tracker-sched.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2011, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+
+#ifdef __linux__
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <errno.h>
+#include <sched.h>
+
+#include <glib.h>
+
+#include "tracker-sched.h"
+
+gboolean
+tracker_sched_idle (void)
+{
+       struct sched_param sp;
+
+       /* Set process scheduling parameters:
+        * This is used so we don't steal scheduling priority from
+        * the most important applications - like the phone
+        * application which has a real time requirement here. This
+        * is detailed in Nokia bug #95573
+        */
+       g_message ("Setting scheduler policy to SCHED_IDLE");
+
+       if (sched_getparam (0, &sp) == 0) {
+               if (sched_setscheduler (0, SCHED_IDLE, &sp) != 0) {
+//LCOV_EXCL_START
+                       const gchar *str = g_strerror (errno);
+
+                       g_warning ("Could not set scheduler policy, %s",
+                                  str ? str : "no error given");
+
+                       return FALSE;
+               }
+       } else {
+               const gchar *str = g_strerror (errno);
+
+               g_warning ("Could not get scheduler policy, %s",
+                          str ? str : "no error given");
+
+               return FALSE;
+       }
+//LCOV_EXCL_END
+
+       return TRUE;
+}
+
+#else /* __linux__ */
+
+#include <glib.h>
+
+#include "tracker-sched.h"
+
+gboolean
+tracker_sched_idle (void)
+{
+       return TRUE;
+}
+
+#endif /* __linux__ */
diff --git a/src/tracker-sched.h b/src/tracker-sched.h
new file mode 100644
index 0000000..37a548e
--- /dev/null
+++ b/src/tracker-sched.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2011, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __LIBTRACKER_COMMON_SCHED_H__
+#define __LIBTRACKER_COMMON_SCHED_H__
+
+G_BEGIN_DECLS
+
+gboolean tracker_sched_idle (void);
+
+G_END_DECLS
+
+#endif /* __LIBTRACKER_COMMON_SCHED_H__ */


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