summaryrefslogtreecommitdiff
path: root/src/trackerd/tracker-ioprio.c
blob: 066a003dda1016039e3a02f96ad3443b34781361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Tracker - indexer and metadata database engine
 * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc@gnome.org)
 *
 * Based mostly on code by 
 * Robert Love	<rml@novell.com>
 *
 * Copyright (C) 2005 Novell, Inc.
 *
 * 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 IOPRIO_SUPPORT

#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>

#include <libtracker-common/tracker-log.h>

#include <tracker-utils.h>

#include "tracker-ioprio.h"

#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

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);
}


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);
}

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
ioprio (void)
{
	

	tracker_log ("Setting ioprio...");

	if (set_io_priority_idle () == -1) {
		g_print ("Could not set idle IO priority...attempting best effort 7 priority\n");
		if (set_io_priority_best_effort (7) == -1) {
			tracker_error ("ERROR: ioprio_set failed");
		}
	}
	
}

#endif