| 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> | 
|  | 38 |  | 
|  | 39 | static void | 
|  | 40 | usage(const char *s) | 
|  | 41 | { | 
|  | 42 | fprintf(stderr, "USAGE: %s [[-r] priority pids ...] [-g pid]\n", s); | 
|  | 43 | exit(EXIT_FAILURE); | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | void print_prio(pid_t pid) | 
|  | 47 | { | 
|  | 48 | int sched; | 
|  | 49 | struct sched_param sp; | 
|  | 50 |  | 
|  | 51 | printf("pid %d's priority: %d\n", pid, getpriority(PRIO_PROCESS, pid)); | 
|  | 52 |  | 
|  | 53 | printf("scheduling class: "); | 
|  | 54 | sched = sched_getscheduler(pid); | 
|  | 55 | switch (sched) { | 
|  | 56 | case SCHED_FIFO: | 
|  | 57 | printf("FIFO\n"); | 
|  | 58 | break; | 
|  | 59 | case SCHED_RR: | 
|  | 60 | printf("RR\n"); | 
|  | 61 | break; | 
|  | 62 | case SCHED_OTHER: | 
|  | 63 | printf("Normal\n"); | 
|  | 64 | break; | 
|  | 65 | case -1: | 
|  | 66 | perror("sched_getscheduler"); | 
|  | 67 | break; | 
|  | 68 | default: | 
|  | 69 | printf("Unknown\n"); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | sched_getparam(pid, &sp); | 
|  | 73 | printf("RT prio: %d (of %d to %d)\n", sp.sched_priority, | 
|  | 74 | sched_get_priority_min(sched), sched_get_priority_max(sched)); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | int renice_main(int argc, char *argv[]) | 
|  | 78 | { | 
|  | 79 | int prio; | 
|  | 80 | int realtime = 0; | 
|  | 81 | char *cmd = argv[0]; | 
|  | 82 |  | 
|  | 83 | // consume command name | 
|  | 84 | argc--; | 
|  | 85 | argv++; | 
|  | 86 |  | 
|  | 87 | if (argc < 1) | 
|  | 88 | usage(cmd); | 
|  | 89 |  | 
|  | 90 | if(strcmp("-r", argv[0]) == 0) { | 
|  | 91 | // do realtime priority adjustment | 
|  | 92 | realtime = 1; | 
|  | 93 | argc--; | 
|  | 94 | argv++; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | if(strcmp("-g", argv[0]) == 0) { | 
|  | 98 | if (argc < 2) | 
|  | 99 | usage(cmd); | 
|  | 100 | print_prio(atoi(argv[1])); | 
|  | 101 | return 0; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | if (argc < 1) | 
|  | 105 | usage(cmd); | 
|  | 106 |  | 
|  | 107 | prio = atoi(argv[0]); | 
|  | 108 | argc--; | 
|  | 109 | argv++; | 
|  | 110 |  | 
|  | 111 | if (argc < 1) | 
|  | 112 | usage(cmd); | 
|  | 113 |  | 
|  | 114 | while(argc) { | 
|  | 115 | pid_t pid; | 
|  | 116 |  | 
|  | 117 | pid = atoi(argv[0]); | 
|  | 118 | argc--; | 
|  | 119 | argv++; | 
|  | 120 |  | 
|  | 121 | if (realtime) { | 
|  | 122 | struct sched_param sp = { .sched_priority = prio }; | 
|  | 123 | int ret; | 
|  | 124 |  | 
|  | 125 | ret = sched_setscheduler(pid, SCHED_RR, &sp); | 
|  | 126 | if (ret) { | 
|  | 127 | perror("sched_set_scheduler"); | 
|  | 128 | exit(EXIT_FAILURE); | 
|  | 129 | } | 
|  | 130 | } else { | 
|  | 131 | int ret; | 
|  | 132 |  | 
|  | 133 | ret = setpriority(PRIO_PROCESS, pid, prio); | 
|  | 134 | if (ret) { | 
|  | 135 | perror("setpriority"); | 
|  | 136 | exit(EXIT_FAILURE); | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | return 0; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 |  |