tracker-0.16.2/src/libtracker-common/tracker-ioprio.c

No issues found

  1 /*
  2  * Copyright (C) 2005, Novell, Inc.
  3  * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
  4  * Copyright (C) 2006, Anders Aagaard
  5  *
  6  * Based mostly on code by Robert Love <rml@novell.com>
  7  *
  8  * Permission is hereby granted, free of charge, to any person obtaining a
  9  * copy of this software and associated documentation files (the "Software"),
 10  * to deal in the Software without restriction, including without limitation
 11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 12  * and/or sell copies of the Software, and to permit persons to whom the
 13  * Software is furnished to do so, subject to the following conditions:
 14  *
 15  * The above copyright notice and this permission notice shall be included in
 16  * all copies or substantial portions of the Software.
 17  *
 18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 24  * DEALINGS IN THE SOFTWARE.
 25  */
 26 
 27 #include "config.h"
 28 
 29 #ifdef __linux__
 30 
 31 #include <stdio.h>
 32 #include <errno.h>
 33 
 34 #ifdef HAVE_LINUX_UNISTD_H
 35 #include <linux/unistd.h>
 36 #endif
 37 
 38 #include <sys/syscall.h>
 39 #include <unistd.h>
 40 
 41 #include <glib/gstdio.h>
 42 
 43 #endif /* __linux__ */
 44 
 45 #include <libtracker-common/tracker-log.h>
 46 
 47 #include "tracker-ioprio.h"
 48 
 49 /* We assume ALL linux architectures have the syscalls defined here */
 50 #ifdef __linux__
 51 
 52 /* Make sure the system call is supported */
 53 #ifndef __NR_ioprio_set
 54 
 55 #if defined(__i386__)
 56 #define __NR_ioprio_set                 289
 57 #define __NR_ioprio_get                 290
 58 #elif defined(__powerpc__) || defined(__powerpc64__)
 59 #define __NR_ioprio_set                 273
 60 #define __NR_ioprio_get                 274
 61 #elif defined(__x86_64__)
 62 #define __NR_ioprio_set                 251
 63 #define __NR_ioprio_get                 252
 64 #elif defined(__ia64__)
 65 #define __NR_ioprio_set                 1274
 66 #define __NR_ioprio_get                 1275
 67 #elif defined(__alpha__)
 68 #define __NR_ioprio_set                 442
 69 #define __NR_ioprio_get                 443
 70 #elif defined(__s390x__) || defined(__s390__)
 71 #define __NR_ioprio_set                 282
 72 #define __NR_ioprio_get                 283
 73 #elif defined(__SH4__)
 74 #define __NR_ioprio_set                 288
 75 #define __NR_ioprio_get                 289
 76 #elif defined(__SH5__)
 77 #define __NR_ioprio_set                 316
 78 #define __NR_ioprio_get                 317
 79 #elif defined(__sparc__) || defined(__sparc64__)
 80 #define __NR_ioprio_set                 196
 81 #define __NR_ioprio_get                 218
 82 #elif defined(__arm__)
 83 #define __NR_ioprio_set                 314
 84 #define __NR_ioprio_get                 315
 85 #else
 86 #error "Unsupported architecture!"
 87 #endif
 88 
 89 #endif /* __NR_ioprio_set */
 90 
 91 enum {
 92 	IOPRIO_CLASS_NONE,
 93 	IOPRIO_CLASS_RT,
 94 	IOPRIO_CLASS_BE,
 95 	IOPRIO_CLASS_IDLE,
 96 };
 97 
 98 enum {
 99 	IOPRIO_WHO_PROCESS = 1,
100 	IOPRIO_WHO_PGRP,
101 	IOPRIO_WHO_USER,
102 };
103 
104 #define IOPRIO_CLASS_SHIFT 13
105 
106 static inline int
107 ioprio_set (int which, int who, int ioprio_val)
108 {
109 	return syscall (__NR_ioprio_set, which, who, ioprio_val);
110 }
111 
112 static int
113 set_io_priority_idle (void)
114 {
115 	int ioprio, ioclass;
116 
117 	ioprio = 7; /* priority is ignored with idle class */
118 	ioclass = IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT;
119 
120 	return ioprio_set (IOPRIO_WHO_PROCESS, 0, ioprio | ioclass);
121 }
122 
123 static int
124 set_io_priority_best_effort (int ioprio_val)
125 {
126 	int ioclass;
127 
128 	ioclass = IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT;
129 
130 	return ioprio_set (IOPRIO_WHO_PROCESS, 0, ioprio_val | ioclass);
131 }
132 
133 void
134 tracker_ioprio_init (void)
135 {
136 	if (set_io_priority_idle () == -1) {
137 		g_message ("Could not set idle IO priority, attempting best effort of 7");
138 
139 		if (set_io_priority_best_effort (7) == -1) {
140 			g_message ("Could not set best effort IO priority either, giving up");
141 		}
142 	}
143 }
144 
145 #else  /* __linux__ */
146 
147 void
148 tracker_ioprio_init (void)
149 {
150 }
151 
152 #endif /* __linux__ */