1/*
2 * Copyright (c) 2008, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *  * Neither the name of Google, Inc. nor the names of its contributors
15 *    may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <unistd.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/time.h>
37#include <sys/resource.h>
38#include <sched.h>
39#include <getopt.h>
40
41static void
42usage(const char *s)
43{
44    fprintf(stderr, "USAGE: %s [[-r] [-t TYPE] priority pids ...] [-g pid]\n", s);
45    exit(EXIT_FAILURE);
46}
47
48void print_prio(pid_t pid)
49{
50    int sched;
51    struct sched_param sp;
52
53    printf("pid %d's priority: %d\n", pid, getpriority(PRIO_PROCESS, pid));
54
55    printf("scheduling class: ");
56    sched = sched_getscheduler(pid);
57    switch (sched) {
58    case SCHED_FIFO:
59        printf("FIFO\n");
60        break;
61    case SCHED_RR:
62        printf("RR\n");
63        break;
64    case SCHED_OTHER:
65        printf("Normal\n");
66        break;
67    case -1:
68        perror("sched_getscheduler");
69        break;
70    default:
71        printf("Unknown\n");
72    }
73
74    sched_getparam(pid, &sp);
75    printf("RT prio: %d (of %d to %d)\n", sp.sched_priority,
76           sched_get_priority_min(sched), sched_get_priority_max(sched));
77}
78
79int get_sched(char *str)
80{
81    if (strcasecmp(str, "RR") == 0)
82        return SCHED_RR;
83    else if (strcasecmp(str, "FIFO") == 0)
84        return SCHED_FIFO;
85    else if (strcasecmp(str, "NORMAL") == 0)
86        return SCHED_OTHER;
87    else if (strcasecmp(str, "OTHER") == 0)
88        return SCHED_OTHER;
89    return SCHED_RR;
90}
91
92int renice_main(int argc, char *argv[])
93{
94    int prio;
95    int realtime = 0;
96    int opt;
97    int sched = SCHED_RR;
98    char *cmd = argv[0];
99
100    do {
101        opt = getopt(argc, argv, "rt:g:");
102        if (opt == -1)
103            break;
104        switch (opt) {
105        case 'r':
106            // do realtime priority adjustment
107            realtime = 1;
108            break;
109        case 't':
110            sched = get_sched(optarg);
111            break;
112        case 'g':
113            print_prio(atoi(optarg));
114            return 0;
115        default:
116            usage(cmd);
117        }
118    } while (1);
119
120    argc -= optind;
121    argv += optind;
122
123    if (argc < 1)
124        usage(cmd);
125
126    prio = atoi(argv[0]);
127    argc--;
128    argv++;
129
130    if (argc < 1)
131        usage(cmd);
132
133    while(argc) {
134        pid_t pid;
135
136        pid = atoi(argv[0]);
137        argc--;
138        argv++;
139
140        if (realtime) {
141            struct sched_param sp = { .sched_priority = prio };
142            int ret;
143
144            ret = sched_setscheduler(pid, sched, &sp);
145            if (ret) {
146                perror("sched_set_scheduler");
147                exit(EXIT_FAILURE);
148            }
149        } else {
150            int ret;
151
152            ret = setpriority(PRIO_PROCESS, pid, prio);
153            if (ret) {
154                perror("setpriority");
155                exit(EXIT_FAILURE);
156            }
157        }
158    }
159
160    return 0;
161}
162