Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 1 | // This is a utility program that prints out realtime priorities for processes |
| 2 | // on a system. It is useful both because the standard tools don't format that |
| 3 | // information very well and the roboRIO's busybox ones don't seem to do it at |
| 4 | // all. |
| 5 | // |
| 6 | // The output format is the following comma-separated columns: |
| 7 | // exe,name,cpumask,policy,nice,priority,tid,pid,ppid,sid,cpu |
| 8 | |
| 9 | #include <sched.h> |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 10 | #include <stdint.h> |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 13 | #include <sys/resource.h> |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 14 | #include <sys/time.h> |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | |
| 17 | #include <string> |
| 18 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 19 | #include "aos/logging/implementations.h" |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 20 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 21 | #include "aos/time/time.h" |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const char *policy_string(uint32_t policy) { |
| 26 | switch (policy) { |
| 27 | case SCHED_OTHER: |
| 28 | return "OTHER"; |
| 29 | case SCHED_BATCH: |
| 30 | return "BATCH"; |
| 31 | case SCHED_IDLE: |
| 32 | return "IDLE"; |
| 33 | case SCHED_FIFO: |
| 34 | return "FIFO"; |
| 35 | case SCHED_RR: |
| 36 | return "RR"; |
| 37 | #ifdef SCHED_DEADLINE |
| 38 | case SCHED_DEADLINE: |
| 39 | return "DEADLINE"; |
| 40 | #endif |
| 41 | default: |
| 42 | return "???"; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | ::std::string strip_string_prefix(size_t length, ::std::string str) { |
| 47 | str = str.substr(length); |
| 48 | while (str[0] == ' ' || str[0] == '\t') { |
| 49 | str = str.substr(1); |
| 50 | } |
| 51 | return str.substr(0, str.size() - 1); |
| 52 | } |
| 53 | |
| 54 | int find_pid_max() { |
| 55 | int r; |
| 56 | FILE *pid_max_file = fopen("/proc/sys/kernel/pid_max", "r"); |
| 57 | if (pid_max_file == nullptr) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 58 | AOS_PLOG(FATAL, "fopen(\"/proc/sys/kernel/pid_max\")"); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 59 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 60 | AOS_CHECK_EQ(1, fscanf(pid_max_file, "%d", &r)); |
| 61 | AOS_PCHECK(fclose(pid_max_file)); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 62 | return r; |
| 63 | } |
| 64 | |
| 65 | cpu_set_t find_all_cpus() { |
| 66 | long nproc = sysconf(_SC_NPROCESSORS_CONF); |
| 67 | if (nproc == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 68 | AOS_PLOG(FATAL, "sysconf(_SC_NPROCESSORS_CONF)"); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 69 | } |
| 70 | cpu_set_t r; |
| 71 | CPU_ZERO(&r); |
| 72 | for (long i = 0; i < nproc; ++i) { |
| 73 | CPU_SET(i, &r); |
| 74 | } |
| 75 | return r; |
| 76 | } |
| 77 | |
| 78 | cpu_set_t find_cpu_mask(int process, bool *not_there) { |
| 79 | cpu_set_t r; |
| 80 | const int result = sched_getaffinity(process, sizeof(r), &r); |
| 81 | if (result == -1 && errno == ESRCH) { |
| 82 | *not_there = true; |
| 83 | return cpu_set_t(); |
| 84 | } |
| 85 | if (result != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 86 | AOS_PLOG(FATAL, "sched_getaffinity(%d, %zu, %p)", process, sizeof(r), &r); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 87 | } |
| 88 | return r; |
| 89 | } |
| 90 | |
| 91 | sched_param find_sched_param(int process, bool *not_there) { |
| 92 | sched_param r; |
| 93 | const int result = sched_getparam(process, &r); |
| 94 | if (result == -1 && errno == ESRCH) { |
| 95 | *not_there = true; |
| 96 | return sched_param(); |
| 97 | } |
| 98 | if (result != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 99 | AOS_PLOG(FATAL, "sched_getparam(%d)", process); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 100 | } |
| 101 | return r; |
| 102 | } |
| 103 | |
| 104 | int find_scheduler(int process, bool *not_there) { |
| 105 | int scheduler = sched_getscheduler(process); |
| 106 | if (scheduler == -1 && errno == ESRCH) { |
| 107 | *not_there = true; |
| 108 | return 0; |
| 109 | } |
| 110 | if (scheduler == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 111 | AOS_PLOG(FATAL, "sched_getscheduler(%d)", process); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 112 | } |
| 113 | return scheduler; |
| 114 | } |
| 115 | |
| 116 | ::std::string find_exe(int process, bool *not_there) { |
| 117 | ::std::string exe_filename = "/proc/" + ::std::to_string(process) + "/exe"; |
| 118 | char exe_buffer[1024]; |
| 119 | ssize_t exe_size = |
| 120 | readlink(exe_filename.c_str(), exe_buffer, sizeof(exe_buffer)); |
| 121 | if (exe_size == -1 && errno == ENOENT) { |
| 122 | return "ENOENT"; |
| 123 | } else { |
| 124 | if (exe_size == -1 && errno == ESRCH) { |
| 125 | *not_there = true; |
| 126 | return ""; |
| 127 | } |
| 128 | if (exe_size == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 129 | AOS_PLOG(FATAL, "readlink(%s, %p, %zu)", exe_filename.c_str(), exe_buffer, |
| 130 | sizeof(exe_buffer)); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 131 | } |
| 132 | return ::std::string(exe_buffer, exe_size); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | int find_nice_value(int process, bool *not_there) { |
| 137 | errno = 0; |
| 138 | int nice_value = getpriority(PRIO_PROCESS, process); |
| 139 | if (errno == ESRCH) { |
| 140 | *not_there = true; |
| 141 | return 0; |
| 142 | } |
| 143 | if (errno != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 144 | AOS_PLOG(FATAL, "getpriority(PRIO_PROCESS, %d)", process); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 145 | } |
| 146 | return nice_value; |
| 147 | } |
| 148 | |
| 149 | void read_stat(int process, int *ppid, int *sid, bool *not_there) { |
| 150 | ::std::string stat_filename = "/proc/" + ::std::to_string(process) + "/stat"; |
| 151 | FILE *stat = fopen(stat_filename.c_str(), "r"); |
| 152 | if (stat == nullptr && errno == ENOENT) { |
| 153 | *not_there = true; |
| 154 | return; |
| 155 | } |
| 156 | if (stat == nullptr) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 157 | AOS_PLOG(FATAL, "fopen(%s, \"r\")", stat_filename.c_str()); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | char buffer[2048]; |
| 161 | if (fgets(buffer, sizeof(buffer), stat) == nullptr) { |
| 162 | if (ferror(stat)) { |
| 163 | if (errno == ESRCH) { |
| 164 | *not_there = true; |
| 165 | return; |
| 166 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 167 | AOS_PLOG(FATAL, "fgets(%p, %zu, %p)", buffer, sizeof(buffer), stat); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | int pid = 0; |
| 172 | |
| 173 | int field = 0; |
| 174 | size_t field_start = 0; |
| 175 | int parens = 0; |
| 176 | for (size_t i = 0; i < sizeof(buffer); ++i) { |
| 177 | if (buffer[i] == '\0') break; |
| 178 | if (buffer[i] == '(') ++parens; |
| 179 | if (parens > 0) { |
| 180 | if (buffer[i] == ')') --parens; |
| 181 | } else if (buffer[i] == ' ') { |
| 182 | ::std::string field_string(buffer, field_start, i - field_start); |
| 183 | switch (field) { |
| 184 | case 0: |
| 185 | pid = ::std::stoi(field_string); |
| 186 | break; |
| 187 | case 3: |
| 188 | *ppid = ::std::stoi(field_string); |
| 189 | break; |
| 190 | case 4: |
| 191 | *sid = ::std::stoi(field_string); |
| 192 | break; |
| 193 | default: |
| 194 | break; |
| 195 | } |
| 196 | ++field; |
| 197 | field_start = i + 1; |
| 198 | } |
| 199 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 200 | AOS_PCHECK(fclose(stat)); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 201 | |
| 202 | if (field < 4) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 203 | AOS_LOG(FATAL, "couldn't get fields from /proc/%d/stat\n", process); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 204 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 205 | AOS_CHECK_EQ(pid, process); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void read_status(int process, int ppid, int *pgrp, ::std::string *name, |
| 209 | bool *not_there) { |
| 210 | ::std::string status_filename = |
| 211 | "/proc/" + ::std::to_string(process) + "/status"; |
| 212 | FILE *status = fopen(status_filename.c_str(), "r"); |
| 213 | if (status == nullptr && errno == ENOENT) { |
| 214 | *not_there = true; |
| 215 | return; |
| 216 | } |
| 217 | if (status == nullptr) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 218 | AOS_PLOG(FATAL, "fopen(%s, \"r\")", status_filename.c_str()); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | int pid = 0, status_ppid = 0; |
| 222 | while (true) { |
| 223 | char buffer[1024]; |
| 224 | if (fgets(buffer, sizeof(buffer), status) == nullptr) { |
| 225 | if (ferror(status)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 226 | AOS_PLOG(FATAL, "fgets(%p, %zu, %p)", buffer, sizeof(buffer), status); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 227 | } else { |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | ::std::string line(buffer); |
| 232 | if (line.substr(0, 5) == "Name:") { |
| 233 | *name = strip_string_prefix(5, line); |
| 234 | } else if (line.substr(0, 4) == "Pid:") { |
| 235 | pid = ::std::stoi(strip_string_prefix(4, line)); |
| 236 | } else if (line.substr(0, 5) == "PPid:") { |
| 237 | status_ppid = ::std::stoi(strip_string_prefix(5, line)); |
| 238 | } else if (line.substr(0, 5) == "Tgid:") { |
| 239 | *pgrp = ::std::stoi(strip_string_prefix(5, line)); |
| 240 | } |
| 241 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 242 | AOS_PCHECK(fclose(status)); |
| 243 | AOS_CHECK_EQ(pid, process); |
| 244 | AOS_CHECK_EQ(status_ppid, ppid); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | } // namespace |
| 248 | |
| 249 | int main() { |
| 250 | ::aos::logging::Init(); |
Brian Silverman | 70f8851 | 2014-12-27 19:27:35 -0800 | [diff] [blame] | 251 | |
| 252 | const int pid_max = find_pid_max(); |
| 253 | const cpu_set_t all_cpus = find_all_cpus(); |
| 254 | |
| 255 | for (int i = 0; i < pid_max; ++i) { |
| 256 | bool not_there = false; |
| 257 | |
| 258 | const cpu_set_t cpu_mask = find_cpu_mask(i, ¬_there); |
| 259 | const sched_param param = find_sched_param(i, ¬_there); |
| 260 | const int scheduler = find_scheduler(i, ¬_there); |
| 261 | const ::std::string exe = find_exe(i, ¬_there); |
| 262 | const int nice_value = find_nice_value(i, ¬_there); |
| 263 | |
| 264 | int ppid = 0, sid = 0; |
| 265 | read_stat(i, &ppid, &sid, ¬_there); |
| 266 | |
| 267 | int pgrp = 0; |
| 268 | ::std::string name; |
| 269 | read_status(i, ppid, &pgrp, &name, ¬_there); |
| 270 | |
| 271 | if (not_there) continue; |
| 272 | |
| 273 | const char *cpu_mask_string = |
| 274 | CPU_EQUAL(&cpu_mask, &all_cpus) ? "all" : "???"; |
| 275 | |
| 276 | printf("%s,%s,%s,%s,%d,%d,%d,%d,%d,%d\n", exe.c_str(), name.c_str(), |
| 277 | cpu_mask_string, policy_string(scheduler), nice_value, |
| 278 | param.sched_priority, i, pgrp, ppid, sid); |
| 279 | } |
| 280 | } |