The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 <sys/time.h> |
| 36 | #include <sys/resource.h> |
| 37 | #include <sched.h> |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 38 | #include <getopt.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | |
| 40 | static void |
| 41 | usage(const char *s) |
| 42 | { |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 43 | fprintf(stderr, "USAGE: %s [[-r] [-t TYPE] priority pids ...] [-g pid]\n", s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | exit(EXIT_FAILURE); |
| 45 | } |
| 46 | |
| 47 | void print_prio(pid_t pid) |
| 48 | { |
| 49 | int sched; |
| 50 | struct sched_param sp; |
| 51 | |
| 52 | printf("pid %d's priority: %d\n", pid, getpriority(PRIO_PROCESS, pid)); |
| 53 | |
| 54 | printf("scheduling class: "); |
| 55 | sched = sched_getscheduler(pid); |
| 56 | switch (sched) { |
| 57 | case SCHED_FIFO: |
| 58 | printf("FIFO\n"); |
| 59 | break; |
| 60 | case SCHED_RR: |
| 61 | printf("RR\n"); |
| 62 | break; |
| 63 | case SCHED_OTHER: |
| 64 | printf("Normal\n"); |
| 65 | break; |
| 66 | case -1: |
| 67 | perror("sched_getscheduler"); |
| 68 | break; |
| 69 | default: |
| 70 | printf("Unknown\n"); |
| 71 | } |
| 72 | |
| 73 | sched_getparam(pid, &sp); |
| 74 | printf("RT prio: %d (of %d to %d)\n", sp.sched_priority, |
| 75 | sched_get_priority_min(sched), sched_get_priority_max(sched)); |
| 76 | } |
| 77 | |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 78 | int get_sched(char *str) |
| 79 | { |
| 80 | if (strcasecmp(str, "RR") == 0) |
| 81 | return SCHED_RR; |
| 82 | else if (strcasecmp(str, "FIFO") == 0) |
| 83 | return SCHED_FIFO; |
| 84 | else if (strcasecmp(str, "NORMAL") == 0) |
| 85 | return SCHED_OTHER; |
| 86 | else if (strcasecmp(str, "OTHER") == 0) |
| 87 | return SCHED_OTHER; |
| 88 | return SCHED_RR; |
| 89 | } |
| 90 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | int renice_main(int argc, char *argv[]) |
| 92 | { |
| 93 | int prio; |
| 94 | int realtime = 0; |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 95 | int opt; |
| 96 | int sched = SCHED_RR; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | char *cmd = argv[0]; |
| 98 | |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 99 | do { |
| 100 | opt = getopt(argc, argv, "rt:g:"); |
| 101 | if (opt == -1) |
| 102 | break; |
| 103 | switch (opt) { |
| 104 | case 'r': |
| 105 | // do realtime priority adjustment |
| 106 | realtime = 1; |
| 107 | break; |
| 108 | case 't': |
| 109 | sched = get_sched(optarg); |
| 110 | break; |
| 111 | case 'g': |
| 112 | print_prio(atoi(optarg)); |
| 113 | return 0; |
| 114 | default: |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 115 | usage(cmd); |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 116 | } |
| 117 | } while (1); |
| 118 | |
| 119 | argc -= optind; |
| 120 | argv += optind; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | |
| 122 | if (argc < 1) |
| 123 | usage(cmd); |
| 124 | |
| 125 | prio = atoi(argv[0]); |
| 126 | argc--; |
| 127 | argv++; |
| 128 | |
| 129 | if (argc < 1) |
| 130 | usage(cmd); |
| 131 | |
| 132 | while(argc) { |
| 133 | pid_t pid; |
| 134 | |
| 135 | pid = atoi(argv[0]); |
| 136 | argc--; |
| 137 | argv++; |
| 138 | |
| 139 | if (realtime) { |
| 140 | struct sched_param sp = { .sched_priority = prio }; |
| 141 | int ret; |
| 142 | |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 143 | ret = sched_setscheduler(pid, sched, &sp); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | if (ret) { |
| 145 | perror("sched_set_scheduler"); |
| 146 | exit(EXIT_FAILURE); |
| 147 | } |
| 148 | } else { |
| 149 | int ret; |
| 150 | |
| 151 | ret = setpriority(PRIO_PROCESS, pid, prio); |
| 152 | if (ret) { |
| 153 | perror("setpriority"); |
| 154 | exit(EXIT_FAILURE); |
| 155 | } |
| 156 | } |
| 157 | } |
Dmitry Shmidt | 4db7df7 | 2013-01-22 15:41:58 -0800 | [diff] [blame^] | 158 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | return 0; |
| 160 | } |