Upload Tizen:Base source
[framework/base/util-linux-ng.git] / schedutils / chrt.c
1 /*
2  * chrt.c - chrt
3  * Command-line utility for manipulating a task's real-time attributes 
4  *
5  * Robert Love <rml@tech9.net>
6  * 27-Apr-2002: initial version
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License, v2, as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Copyright (C) 2004 Robert Love
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sched.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <errno.h>
30 #include <err.h>
31
32 #include "nls.h"
33
34 /* the SCHED_BATCH is supported since Linux 2.6.16
35  *  -- temporary workaround for people with old glibc headers
36  */
37 #if defined (__linux__) && !defined(SCHED_BATCH)
38 # define SCHED_BATCH 3
39 #endif
40
41 /* the SCHED_IDLE is supported since Linux 2.6.23
42  * commit id 0e6aca43e08a62a48d6770e9a159dbec167bf4c6
43  * -- temporary workaround for people with old glibc headers
44  */
45 #if defined (__linux__) && !defined(SCHED_IDLE)
46 # define SCHED_IDLE 5
47 #endif
48
49 #ifndef ARRAY_SIZE
50 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
51 #endif
52
53 static void show_usage(int rc)
54 {
55         fprintf(stdout, _(
56         "\nchrt - manipulate real-time attributes of a process.\n"
57         "\nSet policy:\n"
58         "  chrt [options] <policy> <priority> {<pid> | <command> [<arg> ...]}\n"
59         "\nGet policy:\n"
60         "  chrt [options] {<pid> | <command> [<arg> ...]}\n\n"
61         "\nScheduling policies:\n"
62         "  -b | --batch         set policy to SCHED_BATCH\n"
63         "  -f | --fifo          set policy to SCHED_FIFO\n"
64         "  -i | --idle          set policy to SCHED_IDLE\n"
65         "  -o | --other         set policy to SCHED_OTHER\n"
66         "  -r | --rr            set policy to SCHED_RR (default)\n"
67         "\nOptions:\n"
68         "  -h | --help          display this help\n"
69         "  -p | --pid           operate on existing given pid\n"
70         "  -m | --max           show min and max valid priorities\n"
71         "  -v | --verbose       display status information\n"
72         "  -V | --version       output version information\n\n"));
73
74         exit(rc);
75 }
76
77 static void show_rt_info(const char *what, pid_t pid)
78 {
79         struct sched_param sp;
80         int policy;
81
82         /* don't display "pid 0" as that is confusing */
83         if (!pid)
84                 pid = getpid();
85
86         policy = sched_getscheduler(pid);
87         if (policy == -1)
88                 err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
89
90         printf(_("pid %d's %s scheduling policy: "), pid, what);
91         switch (policy) {
92         case SCHED_OTHER:
93                 printf("SCHED_OTHER\n");
94                 break;
95         case SCHED_FIFO:
96                 printf("SCHED_FIFO\n");
97                 break;
98 #ifdef SCHED_IDLE
99         case SCHED_IDLE:
100                 printf("SCHED_IDLE\n");
101                 break;
102 #endif
103         case SCHED_RR:
104                 printf("SCHED_RR\n");
105                 break;
106 #ifdef SCHED_BATCH
107         case SCHED_BATCH:
108                 printf("SCHED_BATCH\n");
109                 break;
110 #endif
111         default:
112                 printf(_("unknown\n"));
113         }
114
115         if (sched_getparam(pid, &sp))
116                 err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
117
118         printf(_("pid %d's %s scheduling priority: %d\n"),
119                 pid, what, sp.sched_priority);
120 }
121
122 static void show_min_max(void)
123 {
124         int i;
125         int policies[] = { SCHED_OTHER, SCHED_FIFO, SCHED_RR,
126 #ifdef SCHED_BATCH
127                            SCHED_BATCH,
128 #endif
129 #ifdef SCHED_IDLE
130                            SCHED_IDLE,
131 #endif
132                          };
133         const char *names[] = { "OTHER", "FIFO", "RR",
134 #ifdef SCHED_BATCH
135                                 "BATCH",
136 #endif
137 #ifdef SCHED_IDLE
138                                 "IDLE",
139 #endif
140                               };
141
142         for (i = 0; i < ARRAY_SIZE(policies); i++) {
143                 int max = sched_get_priority_max(policies[i]);
144                 int min = sched_get_priority_min(policies[i]);
145
146                 if (max >= 0 && min >= 0)
147                         printf(_("SCHED_%s min/max priority\t: %d/%d\n"),
148                                         names[i], min, max);
149                 else
150                         printf(_("SCHED_%s not supported?\n"), names[i]);
151         }
152 }
153
154 int main(int argc, char *argv[])
155 {
156         int i, policy = SCHED_RR, priority = 0, verbose = 0;
157         struct sched_param sp;
158         pid_t pid = -1;
159
160         struct option longopts[] = {
161                 { "batch",      0, NULL, 'b' },
162                 { "fifo",       0, NULL, 'f' },
163                 { "idle",       0, NULL, 'i' },
164                 { "pid",        0, NULL, 'p' },
165                 { "help",       0, NULL, 'h' },
166                 { "max",        0, NULL, 'm' },
167                 { "other",      0, NULL, 'o' },
168                 { "rr",         0, NULL, 'r' },
169                 { "verbose",    0, NULL, 'v' },
170                 { "version",    0, NULL, 'V' },
171                 { NULL,         0, NULL, 0 }
172         };
173
174         setlocale(LC_ALL, "");
175         bindtextdomain(PACKAGE, LOCALEDIR);
176         textdomain(PACKAGE);
177
178         while((i = getopt_long(argc, argv, "+bfiphmorvV", longopts, NULL)) != -1)
179         {
180                 int ret = EXIT_FAILURE;
181
182                 switch (i) {
183                 case 'b':
184 #ifdef SCHED_BATCH
185                         policy = SCHED_BATCH;
186 #endif
187                         break;
188                 case 'f':
189                         policy = SCHED_FIFO;
190                         break;
191                 case 'i':
192 #ifdef SCHED_IDLE
193                         policy = SCHED_IDLE;
194 #endif
195                         break;
196                 case 'm':
197                         show_min_max();
198                         return 0;
199                 case 'o':
200                         policy = SCHED_OTHER;
201                         break;
202                 case 'p':
203                         errno = 0;
204                         pid = strtol(argv[argc - 1], NULL, 10);
205                         if (errno)
206                                 err(EXIT_FAILURE, _("failed to parse pid"));
207                         break;
208                 case 'r':
209                         policy = SCHED_RR;
210                         break;
211                 case 'v':
212                         verbose = 1;
213                         break;
214                 case 'V':
215                         printf("chrt (%s)\n", PACKAGE_STRING);
216                         return 0;
217                 case 'h':
218                         ret = EXIT_SUCCESS;
219                 default:
220                         show_usage(ret);
221                 }
222         }
223
224         if (((pid > -1) && argc - optind < 1) || ((pid == -1) && argc - optind < 2))
225                 show_usage(EXIT_FAILURE);
226
227         if ((pid > -1) && (verbose || argc - optind == 1)) {
228                 show_rt_info(_("current"), pid);
229                 if (argc - optind == 1)
230                         return EXIT_SUCCESS;
231         }
232
233         errno = 0;
234         priority = strtol(argv[optind], NULL, 10);
235         if (errno)
236                 err(EXIT_FAILURE, _("failed to parse priority"));
237
238         if (pid == -1)
239                 pid = 0;
240         sp.sched_priority = priority;
241         if (sched_setscheduler(pid, policy, &sp) == -1)
242                 err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid);
243
244         if (verbose)
245                 show_rt_info("new", pid);
246
247         if (!pid) {
248                 argv += optind + 1;
249                 execvp(argv[0], argv);
250                 perror("execvp");
251                 err(EXIT_FAILURE, _("failed to execute %s"), argv[0]);
252         }
253
254         return EXIT_SUCCESS;
255 }