Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 1 | /*! \file cpu_sched_vty.c |
| 2 | * Implementation to CPU / Threading / Scheduler properties from VTY configuration. |
| 3 | */ |
| 4 | /* (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * |
| 6 | * Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | * SPDX-License-Identifier: GPLv2+ |
| 24 | */ |
| 25 | |
| 26 | #define _GNU_SOURCE |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <errno.h> |
| 31 | #include <limits.h> |
| 32 | #include <unistd.h> |
| 33 | #include <sched.h> |
| 34 | #include <ctype.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <dirent.h> |
| 39 | #include <pthread.h> |
| 40 | #include <inttypes.h> |
| 41 | |
| 42 | #include <osmocom/vty/vty.h> |
| 43 | #include <osmocom/vty/command.h> |
| 44 | #include <osmocom/vty/tdef_vty.h> |
| 45 | #include <osmocom/core/tdef.h> |
| 46 | #include <osmocom/core/fsm.h> |
| 47 | #include <osmocom/core/linuxlist.h> |
| 48 | |
| 49 | /*! \addtogroup Tdef_VTY |
| 50 | * |
| 51 | * CPU Scheduling related VTY API. |
| 52 | * |
| 53 | * @{ |
| 54 | * \file cpu_sched_vty.c |
| 55 | */ |
| 56 | |
| 57 | enum sched_vty_thread_id { |
| 58 | SCHED_VTY_THREAD_SELF, |
| 59 | SCHED_VTY_THREAD_ALL, |
| 60 | SCHED_VTY_THREAD_ID, |
| 61 | SCHED_VTY_THREAD_NAME, |
| 62 | SCHED_VTY_THREAD_UNKNOWN, |
| 63 | }; |
| 64 | |
| 65 | struct cpu_affinity_it { |
| 66 | struct llist_head entry; |
| 67 | enum sched_vty_thread_id tid_type; |
| 68 | char bufname[64]; |
| 69 | cpu_set_t *cpuset; |
| 70 | size_t cpuset_size; |
| 71 | bool delay; |
| 72 | }; |
| 73 | |
| 74 | struct sched_vty_opts { |
| 75 | void *tall_ctx; |
| 76 | int sched_rr_prio; |
| 77 | struct llist_head cpu_affinity_li; |
| 78 | pthread_mutex_t cpu_affinity_li_mutex; |
| 79 | }; |
| 80 | |
| 81 | static struct sched_vty_opts *sched_vty_opts; |
| 82 | |
| 83 | static struct cmd_node sched_node = { |
| 84 | L_CPU_SCHED_NODE, |
Pau Espin Pedrol | 62be97e | 2020-08-18 12:29:57 +0200 | [diff] [blame] | 85 | "%s(config-cpu-sched)# ", |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 86 | 1, |
| 87 | }; |
| 88 | |
| 89 | /* returns number of configured CPUs in the system, or negative otherwise */ |
| 90 | static int get_num_cpus() { |
| 91 | static unsigned int num_cpus = 0; |
| 92 | long ln; |
| 93 | |
| 94 | if (num_cpus) |
| 95 | return num_cpus; |
| 96 | |
| 97 | /* This is expensive (goes across /sys, so let's do it only once. It is |
| 98 | * guaranteed it won't change during process span anyway). */ |
| 99 | ln = sysconf(_SC_NPROCESSORS_CONF); |
| 100 | if (ln < 0) { |
| 101 | LOGP(DLGLOBAL, LOGL_ERROR, "sysconf(_SC_NPROCESSORS_CONF) failed: %s\n", |
| 102 | strerror(errno)); |
| 103 | return -1; |
| 104 | } |
| 105 | num_cpus = (unsigned int) ln; |
| 106 | return num_cpus; |
| 107 | } |
| 108 | |
| 109 | /* Parses string with CPU hex Affinity Mask, with right-most bit being CPU0, and |
| 110 | * fills a cpuset of size cpuset_size. |
| 111 | */ |
| 112 | static int parse_cpu_hex_mask(const char *str, cpu_set_t *cpuset, size_t cpuset_size) |
| 113 | { |
| 114 | int len = strlen(str); |
| 115 | const char *ptr = str + len - 1; |
| 116 | int cpu = 0; |
| 117 | |
| 118 | /* skip optional '0x' prefix format */ |
| 119 | if (len >= 2 && str[0] == '0' && str[1] == 'x') |
| 120 | str += 2; |
| 121 | CPU_ZERO_S(cpuset_size, cpuset); |
| 122 | |
| 123 | while (ptr >= str) { |
| 124 | char c = *ptr; |
| 125 | uint8_t val; |
| 126 | |
| 127 | if (c >= '0' && c <= '9') { |
| 128 | val = c - '0'; |
| 129 | } else { |
| 130 | c = (char)tolower((int)c); |
| 131 | if (c >= 'a' && c <= 'f') |
| 132 | val = c + (10 - 'a'); |
| 133 | else |
| 134 | return -1; |
| 135 | } |
| 136 | if (val & 0x01) |
| 137 | CPU_SET_S(cpu, cpuset_size, cpuset); |
| 138 | if (val & 0x02) |
| 139 | CPU_SET_S(cpu + 1, cpuset_size, cpuset); |
| 140 | if (val & 0x04) |
| 141 | CPU_SET_S(cpu + 2, cpuset_size, cpuset); |
| 142 | if (val & 0x08) |
| 143 | CPU_SET_S(cpu + 3, cpuset_size, cpuset); |
| 144 | ptr--; |
| 145 | cpu += 4; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* Generates a hexstring in str from cpuset of size cpuset_size */ |
| 152 | static int generate_cpu_hex_mask(char *str, size_t str_buf_size, |
| 153 | cpu_set_t *cpuset, size_t cpuset_size) |
| 154 | { |
| 155 | char *ptr = str; |
| 156 | int cpu; |
| 157 | bool first_nonzero_found = false; |
| 158 | |
| 159 | /* 2 char per byte, + '0x' prefix + '\0' */ |
| 160 | if (cpuset_size * 2 + 2 + 1 > str_buf_size) |
| 161 | return -1; |
| 162 | |
| 163 | *ptr++ = '0'; |
| 164 | *ptr++ = 'x'; |
| 165 | |
| 166 | for (cpu = cpuset_size*8 - 4; cpu >= 0; cpu -= 4) { |
| 167 | uint8_t val = 0; |
| 168 | |
| 169 | if (CPU_ISSET_S(cpu, cpuset_size, cpuset)) |
| 170 | val |= 0x01; |
| 171 | if (CPU_ISSET_S(cpu + 1, cpuset_size, cpuset)) |
| 172 | val |= 0x02; |
| 173 | if (CPU_ISSET_S(cpu + 2, cpuset_size, cpuset)) |
| 174 | val |= 0x04; |
| 175 | if (CPU_ISSET_S(cpu + 3, cpuset_size, cpuset)) |
| 176 | val |= 0x08; |
| 177 | |
Vadim Yanitskiy | e0ed147 | 2020-09-11 20:13:19 +0700 | [diff] [blame] | 178 | if (val < 10) |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 179 | *ptr = '0' + val; |
| 180 | else |
| 181 | *ptr = ('a' - 10) + val; |
| 182 | if (val) |
| 183 | first_nonzero_found = true; |
| 184 | if (first_nonzero_found) |
| 185 | ptr++; |
| 186 | |
| 187 | } |
| 188 | if (!first_nonzero_found) |
| 189 | *ptr++ = '0'; |
| 190 | *ptr = '\0'; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* Checks whther a thread identified by tid exists and belongs to the running process */ |
| 195 | static bool proc_tid_exists(pid_t tid) |
| 196 | { |
| 197 | DIR *proc_dir; |
| 198 | struct dirent *entry; |
| 199 | char dirname[100]; |
| 200 | int tid_it; |
| 201 | bool found = false; |
| 202 | |
| 203 | snprintf(dirname, sizeof(dirname), "/proc/%ld/task", (long int)getpid()); |
| 204 | proc_dir = opendir(dirname); |
| 205 | if (!proc_dir) |
| 206 | return false; /*FIXME; print error */ |
| 207 | |
| 208 | while ((entry = readdir(proc_dir))) { |
| 209 | if (entry->d_name[0] == '.') |
| 210 | continue; |
| 211 | tid_it = atoi(entry->d_name); |
| 212 | if (tid_it == tid) { |
| 213 | found = true; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | closedir(proc_dir); |
| 219 | return found; |
| 220 | } |
| 221 | |
| 222 | /* Checks whther a thread identified by name exists and belongs to the running |
| 223 | * process, and returns its disocevered TID in res_pid. |
| 224 | */ |
| 225 | static bool proc_name_exists(const char *name, pid_t *res_pid) |
| 226 | { |
| 227 | DIR *proc_dir; |
| 228 | struct dirent *entry; |
| 229 | char path[100]; |
| 230 | char buf[17]; /* 15 + \n + \0 */ |
| 231 | int tid_it; |
| 232 | int fd; |
| 233 | pid_t mypid = getpid(); |
| 234 | bool found = false; |
| 235 | int rc; |
| 236 | |
| 237 | *res_pid = 0; |
| 238 | |
| 239 | snprintf(path, sizeof(path), "/proc/%ld/task", (long int)mypid); |
| 240 | proc_dir = opendir(path); |
| 241 | if (!proc_dir) |
| 242 | return false; |
| 243 | |
| 244 | while ((entry = readdir(proc_dir))) |
| 245 | { |
| 246 | if (entry->d_name[0] == '.') |
| 247 | continue; |
| 248 | |
| 249 | tid_it = atoi(entry->d_name); |
| 250 | snprintf(path, sizeof(path), "/proc/%ld/task/%ld/comm", (long int)mypid, (long int) tid_it); |
| 251 | if ((fd = open(path, O_RDONLY)) == -1) |
| 252 | continue; |
| 253 | rc = read(fd, buf, sizeof(buf) - 1); |
| 254 | if (rc >= 0) { |
| 255 | /* Last may char contain a '\n', get rid of it */ |
| 256 | if (rc > 0 && buf[rc - 1] == '\n') |
| 257 | buf[rc - 1] = '\0'; |
| 258 | else |
| 259 | buf[rc] = '\0'; |
| 260 | if (strcmp(name, buf) == 0) { |
| 261 | *res_pid = tid_it; |
| 262 | found = true; |
| 263 | } |
| 264 | } |
| 265 | close(fd); |
| 266 | |
| 267 | if (found) |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | closedir(proc_dir); |
| 272 | return found; |
| 273 | } |
| 274 | |
| 275 | /* Parse VTY THREADNAME variable, return its type and fill discovered res_pid if required */ |
| 276 | static enum sched_vty_thread_id procname2pid(pid_t *res_pid, const char *str, bool applynow) |
| 277 | { |
| 278 | size_t i, len; |
| 279 | char *end; |
| 280 | bool is_pid = true; |
| 281 | |
| 282 | if (strcmp(str, "all") == 0) { |
| 283 | *res_pid = 0; |
| 284 | return SCHED_VTY_THREAD_ALL; |
| 285 | } |
| 286 | |
| 287 | if (strcmp(str, "self") == 0) { |
| 288 | *res_pid = 0; |
| 289 | return SCHED_VTY_THREAD_SELF; |
| 290 | } |
| 291 | |
| 292 | len = strlen(str); |
| 293 | for (i = 0; i < len; i++) { |
| 294 | if (!isdigit(str[i])) { |
| 295 | is_pid = false; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | if (is_pid) { |
| 300 | errno = 0; |
| 301 | *res_pid = strtoul(str, &end, 0); |
| 302 | if ((errno == ERANGE && *res_pid == ULONG_MAX) || (errno && !*res_pid) || |
| 303 | str == end) { |
| 304 | return SCHED_VTY_THREAD_UNKNOWN; |
| 305 | } |
| 306 | if (!applynow || proc_tid_exists(*res_pid)) |
| 307 | return SCHED_VTY_THREAD_ID; |
| 308 | else |
| 309 | return SCHED_VTY_THREAD_UNKNOWN; |
| 310 | } |
| 311 | |
| 312 | if (len > 15) { |
| 313 | /* Thread names only allow up to 15+1 null chars, see man pthread_setname_np */ |
| 314 | return SCHED_VTY_THREAD_UNKNOWN; |
| 315 | } |
| 316 | |
| 317 | if (applynow) { |
| 318 | if (proc_name_exists(str, res_pid)) |
| 319 | return SCHED_VTY_THREAD_NAME; |
| 320 | else |
| 321 | return SCHED_VTY_THREAD_UNKNOWN; |
| 322 | } else { |
| 323 | /* assume a thread will be named after it */ |
| 324 | *res_pid = 0; |
| 325 | return SCHED_VTY_THREAD_NAME; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /* Wrapper for sched_setaffinity applying to single thread or all threads in process based on tid_type. */ |
| 330 | static int my_sched_setaffinity(enum sched_vty_thread_id tid_type, pid_t pid, |
| 331 | cpu_set_t *cpuset, size_t cpuset_size) |
| 332 | { |
| 333 | DIR *proc_dir; |
| 334 | struct dirent *entry; |
| 335 | char dirname[100]; |
| 336 | char str_mask[1024]; |
| 337 | int tid_it; |
| 338 | int rc = 0; |
| 339 | |
| 340 | if (generate_cpu_hex_mask(str_mask, sizeof(str_mask), cpuset, cpuset_size) < 0) |
| 341 | str_mask[0] = '\0'; |
| 342 | |
| 343 | if (tid_type != SCHED_VTY_THREAD_ALL) { |
| 344 | LOGP(DLGLOBAL, LOGL_NOTICE, "Setting CPU affinity mask for tid %lu to: %s\n", |
| 345 | (unsigned long) pid, str_mask); |
| 346 | |
| 347 | rc = sched_setaffinity(pid, sizeof(cpu_set_t), cpuset); |
| 348 | return rc; |
| 349 | } |
| 350 | |
| 351 | snprintf(dirname, sizeof(dirname), "/proc/%ld/task", (long int)getpid()); |
| 352 | proc_dir = opendir(dirname); |
| 353 | if (!proc_dir) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | while ((entry = readdir(proc_dir))) |
| 357 | { |
| 358 | if (entry->d_name[0] == '.') |
| 359 | continue; |
| 360 | tid_it = atoi(entry->d_name); |
| 361 | LOGP(DLGLOBAL, LOGL_NOTICE, "Setting CPU affinity mask for tid %lu to: %s\n", |
| 362 | (unsigned long) tid_it, str_mask); |
| 363 | |
| 364 | rc = sched_setaffinity(tid_it, sizeof(cpu_set_t), cpuset); |
| 365 | if (rc == -1) |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | closedir(proc_dir); |
| 370 | return rc; |
| 371 | |
| 372 | } |
| 373 | |
Pau Espin Pedrol | 64c67bb | 2020-11-09 11:24:21 +0100 | [diff] [blame] | 374 | DEFUN_ATTR(cfg_sched_cpu_affinity, cfg_sched_cpu_affinity_cmd, |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 375 | "cpu-affinity (self|all|<0-4294967295>|THREADNAME) CPUHEXMASK [delay]", |
| 376 | "Set CPU affinity mask on a (group of) thread(s)\n" |
| 377 | "Set CPU affinity mask on thread running the VTY\n" |
| 378 | "Set CPU affinity mask on all process' threads\n" |
| 379 | "Set CPU affinity mask on a thread with specified PID\n" |
| 380 | "Set CPU affinity mask on a thread with specified thread name\n" |
| 381 | "CPU affinity mask\n" |
Pau Espin Pedrol | 64c67bb | 2020-11-09 11:24:21 +0100 | [diff] [blame] | 382 | "If set, delay applying the affinity mask now and let the app handle it at a later point\n", |
| 383 | CMD_ATTR_IMMEDIATE) |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 384 | { |
| 385 | const char* str_who = argv[0]; |
| 386 | const char *str_mask = argv[1]; |
| 387 | bool applynow = (argc != 3); |
| 388 | int rc; |
| 389 | pid_t pid; |
| 390 | enum sched_vty_thread_id tid_type; |
| 391 | struct cpu_affinity_it *it, *it_next; |
| 392 | cpu_set_t *cpuset; |
| 393 | size_t cpuset_size; |
| 394 | |
| 395 | tid_type = procname2pid(&pid, str_who, applynow); |
| 396 | if (tid_type == SCHED_VTY_THREAD_UNKNOWN) { |
| 397 | vty_out(vty, "%% Failed parsing target thread %s%s", |
| 398 | str_who, VTY_NEWLINE); |
| 399 | return CMD_WARNING; |
| 400 | } |
| 401 | |
| 402 | if (tid_type == SCHED_VTY_THREAD_ID && !applynow) { |
| 403 | vty_out(vty, "%% It makes no sense to delay applying cpu-affinity on tid %lu%s", |
| 404 | (unsigned long)pid, VTY_NEWLINE); |
| 405 | return CMD_WARNING; |
| 406 | } |
| 407 | if (tid_type == SCHED_VTY_THREAD_ALL && !applynow) { |
| 408 | vty_out(vty, "%% It makes no sense to delay applying cpu-affinity on all threads%s", |
| 409 | VTY_NEWLINE); |
| 410 | return CMD_WARNING; |
| 411 | } |
| 412 | |
| 413 | cpuset = CPU_ALLOC(get_num_cpus()); |
| 414 | cpuset_size = CPU_ALLOC_SIZE(get_num_cpus()); |
| 415 | if (parse_cpu_hex_mask(str_mask, cpuset, cpuset_size) < 0) { |
| 416 | vty_out(vty, "%% Failed parsing CPU Affinity Mask %s%s", |
| 417 | str_mask, VTY_NEWLINE); |
| 418 | CPU_FREE(cpuset); |
| 419 | return CMD_WARNING; |
| 420 | } |
| 421 | |
| 422 | if (applynow) { |
| 423 | rc = my_sched_setaffinity(tid_type, pid, cpuset, cpuset_size); |
| 424 | if (rc == -1) { |
| 425 | vty_out(vty, "%% Failed setting sched CPU Affinity Mask %s: %s%s", |
| 426 | str_mask, strerror(errno), VTY_NEWLINE); |
| 427 | CPU_FREE(cpuset); |
| 428 | return CMD_WARNING; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* Keep history of cmds applied to be able to rewrite config. If PID was passed |
| 433 | directly it makes no sense to store it since PIDs are temporary */ |
| 434 | if (tid_type == SCHED_VTY_THREAD_SELF || |
| 435 | tid_type == SCHED_VTY_THREAD_ALL || |
| 436 | tid_type == SCHED_VTY_THREAD_NAME) { |
| 437 | pthread_mutex_lock(&sched_vty_opts->cpu_affinity_li_mutex); |
| 438 | |
| 439 | /* Drop previous entries matching, since they will be overwritten */ |
| 440 | llist_for_each_entry_safe(it, it_next, &sched_vty_opts->cpu_affinity_li, entry) { |
| 441 | if (strcmp(it->bufname, str_who) == 0) { |
| 442 | llist_del(&it->entry); |
| 443 | CPU_FREE(it->cpuset); |
| 444 | talloc_free(it); |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | it = talloc_zero(sched_vty_opts->tall_ctx, struct cpu_affinity_it); |
| 449 | OSMO_STRLCPY_ARRAY(it->bufname, str_who); |
| 450 | it->tid_type = tid_type; |
| 451 | it->cpuset = cpuset; |
| 452 | it->cpuset_size = cpuset_size; |
| 453 | it->delay = !applynow; |
| 454 | llist_add_tail(&it->entry, &sched_vty_opts->cpu_affinity_li); |
| 455 | |
| 456 | pthread_mutex_unlock(&sched_vty_opts->cpu_affinity_li_mutex); |
| 457 | } else { |
| 458 | /* We don't need cpuset for later, free it: */ |
| 459 | CPU_FREE(cpuset); |
| 460 | } |
| 461 | return CMD_SUCCESS; |
| 462 | } |
| 463 | |
| 464 | static int set_sched_rr(unsigned int prio) |
| 465 | { |
| 466 | struct sched_param param; |
| 467 | int rc; |
| 468 | memset(¶m, 0, sizeof(param)); |
| 469 | param.sched_priority = prio; |
| 470 | LOGP(DLGLOBAL, LOGL_NOTICE, "Setting SCHED_RR priority %d\n", param.sched_priority); |
| 471 | rc = sched_setscheduler(getpid(), SCHED_RR, ¶m); |
| 472 | if (rc == -1) { |
Eric | dde3272 | 2020-08-14 03:15:10 +0200 | [diff] [blame] | 473 | LOGP(DLGLOBAL, LOGL_ERROR, "Setting SCHED_RR priority %d failed: %s\n", |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 474 | param.sched_priority, strerror(errno)); |
| 475 | return -1; |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
Pau Espin Pedrol | 64c67bb | 2020-11-09 11:24:21 +0100 | [diff] [blame] | 480 | DEFUN_ATTR(cfg_sched_policy, cfg_sched_policy_cmd, |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 481 | "policy rr <1-32>", |
| 482 | "Set the scheduling policy to use for the process\n" |
| 483 | "Use the SCHED_RR real-time scheduling algorithm\n" |
Pau Espin Pedrol | 64c67bb | 2020-11-09 11:24:21 +0100 | [diff] [blame] | 484 | "Set the SCHED_RR real-time priority\n", |
| 485 | CMD_ATTR_IMMEDIATE) |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 486 | { |
| 487 | sched_vty_opts->sched_rr_prio = atoi(argv[0]); |
| 488 | |
| 489 | if (set_sched_rr(sched_vty_opts->sched_rr_prio) < 0) { |
| 490 | vty_out(vty, "%% Failed setting SCHED_RR priority %d%s", |
| 491 | sched_vty_opts->sched_rr_prio, VTY_NEWLINE); |
| 492 | return CMD_WARNING; |
| 493 | } |
| 494 | |
| 495 | return CMD_SUCCESS; |
| 496 | } |
| 497 | |
| 498 | DEFUN(cfg_sched, |
| 499 | cfg_sched_cmd, |
| 500 | "cpu-sched", "Configure CPU Scheduler related settings") |
| 501 | { |
| 502 | vty->index = NULL; |
| 503 | vty->node = L_CPU_SCHED_NODE; |
| 504 | |
| 505 | return CMD_SUCCESS; |
| 506 | } |
| 507 | |
| 508 | DEFUN(show_sched_threads, show_sched_threads_cmd, |
| 509 | "show cpu-sched threads", |
| 510 | SHOW_STR |
| 511 | "Show Sched section information\n" |
| 512 | "Show information about running threads)\n") |
| 513 | { |
| 514 | DIR *proc_dir; |
| 515 | struct dirent *entry; |
| 516 | char path[100]; |
| 517 | char name[17]; |
| 518 | char str_mask[1024]; |
| 519 | int tid_it; |
| 520 | int fd; |
| 521 | pid_t mypid = getpid(); |
| 522 | int rc; |
| 523 | cpu_set_t *cpuset; |
| 524 | size_t cpuset_size; |
| 525 | |
| 526 | vty_out(vty, "Thread list for PID %lu:%s", (unsigned long) mypid, VTY_NEWLINE); |
| 527 | |
| 528 | snprintf(path, sizeof(path), "/proc/%ld/task", (long int)mypid); |
| 529 | proc_dir = opendir(path); |
| 530 | if (!proc_dir) { |
| 531 | vty_out(vty, "%% Failed opening dir%s%s", path, VTY_NEWLINE); |
| 532 | return CMD_WARNING; |
| 533 | } |
| 534 | |
| 535 | while ((entry = readdir(proc_dir))) |
| 536 | { |
| 537 | if (entry->d_name[0] == '.') |
| 538 | continue; |
| 539 | |
| 540 | tid_it = atoi(entry->d_name); |
| 541 | snprintf(path, sizeof(path), "/proc/%ld/task/%ld/comm", (long int)mypid, (long int)tid_it); |
| 542 | if ((fd = open(path, O_RDONLY)) != -1) { |
| 543 | rc = read(fd, name, sizeof(name) - 1); |
| 544 | if (rc >= 0) { |
| 545 | /* Last may char contain a '\n', get rid of it */ |
| 546 | if (rc > 0 && name[rc - 1] == '\n') |
| 547 | name[rc - 1] = '\0'; |
| 548 | else |
| 549 | name[rc] = '\0'; |
| 550 | } |
| 551 | close(fd); |
| 552 | } else { |
| 553 | name[0] = '\0'; |
| 554 | } |
| 555 | |
| 556 | str_mask[0] = '\0'; |
| 557 | cpuset = CPU_ALLOC(get_num_cpus()); |
| 558 | cpuset_size = CPU_ALLOC_SIZE(get_num_cpus()); |
| 559 | CPU_ZERO_S(cpuset_size, cpuset); |
| 560 | if (sched_getaffinity(tid_it, cpuset_size, cpuset) == 0) { |
| 561 | if (generate_cpu_hex_mask(str_mask, sizeof(str_mask), cpuset, cpuset_size) < 0) |
| 562 | str_mask[0] = '\0'; |
| 563 | } |
| 564 | CPU_FREE(cpuset); |
| 565 | |
| 566 | vty_out(vty, " TID: %lu, NAME: '%s', cpu-affinity: %s%s", |
| 567 | (unsigned long) tid_it, name, str_mask, VTY_NEWLINE); |
| 568 | } |
| 569 | |
| 570 | closedir(proc_dir); |
| 571 | return CMD_SUCCESS; |
| 572 | } |
| 573 | |
| 574 | static int config_write_sched(struct vty *vty) |
| 575 | { |
| 576 | struct cpu_affinity_it *it; |
| 577 | char str_mask[1024]; |
| 578 | |
| 579 | /* Only add the node if there's something to write under it */ |
| 580 | if (sched_vty_opts->sched_rr_prio || !llist_empty(&sched_vty_opts->cpu_affinity_li)) |
| 581 | vty_out(vty, "cpu-sched%s", VTY_NEWLINE); |
| 582 | |
| 583 | if (sched_vty_opts->sched_rr_prio) |
| 584 | vty_out(vty, " policy rr %d%s", sched_vty_opts->sched_rr_prio, VTY_NEWLINE); |
| 585 | |
| 586 | llist_for_each_entry(it, &sched_vty_opts->cpu_affinity_li, entry) { |
| 587 | if (generate_cpu_hex_mask(str_mask, sizeof(str_mask), it->cpuset, it->cpuset_size) < 0) |
| 588 | OSMO_STRLCPY_ARRAY(str_mask, "ERROR"); |
| 589 | vty_out(vty, " cpu-affinity %s %s%s%s", it->bufname, str_mask, |
| 590 | it->delay ? " delay" : "", VTY_NEWLINE); |
| 591 | } |
| 592 | |
| 593 | return CMD_SUCCESS; |
| 594 | } |
| 595 | |
| 596 | /*! Initialize sched VTY nodes |
| 597 | * \param[in] tall_ctx Talloc context to use internally by vty_sched subsystem. |
| 598 | * \return 0 on success, non-zero on error. |
| 599 | */ |
| 600 | int osmo_cpu_sched_vty_init(void *tall_ctx) |
| 601 | { |
| 602 | OSMO_ASSERT(!sched_vty_opts); /* assert only called once */ |
| 603 | |
| 604 | sched_vty_opts = talloc_zero(tall_ctx, struct sched_vty_opts); |
| 605 | sched_vty_opts->tall_ctx = tall_ctx; |
| 606 | INIT_LLIST_HEAD(&sched_vty_opts->cpu_affinity_li); |
| 607 | pthread_mutex_init(&sched_vty_opts->cpu_affinity_li_mutex, NULL); |
| 608 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 609 | install_lib_element(CONFIG_NODE, &cfg_sched_cmd); |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 610 | install_node(&sched_node, config_write_sched); |
| 611 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 612 | install_lib_element(L_CPU_SCHED_NODE, &cfg_sched_policy_cmd); |
| 613 | install_lib_element(L_CPU_SCHED_NODE, &cfg_sched_cpu_affinity_cmd); |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 614 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 615 | install_lib_element_ve(&show_sched_threads_cmd); |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 616 | |
| 617 | /* Initialize amount of cpus now */ |
| 618 | if (get_num_cpus() < 0) |
| 619 | return -1; |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /*! Apply cpu-affinity on calling thread based on VTY configuration |
| 625 | * \return 0 on success, non-zero on error. |
| 626 | */ |
| 627 | int osmo_cpu_sched_vty_apply_localthread(void) |
| 628 | { |
| 629 | struct cpu_affinity_it *it, *it_match = NULL; |
| 630 | char name[16]; /* 15 + \0 */ |
| 631 | char str_mask[1024]; |
| 632 | bool has_name = false; |
| 633 | int rc = 0; |
| 634 | |
| 635 | /* Assert subsystem was inited and structs are preset */ |
Eric | dde3272 | 2020-08-14 03:15:10 +0200 | [diff] [blame] | 636 | if (!sched_vty_opts) { |
| 637 | LOGP(DLGLOBAL, LOGL_FATAL, "Setting cpu-affinity mask impossible: no opts!\n"); |
| 638 | return 0; |
| 639 | } |
Pau Espin Pedrol | eb6882f | 2020-07-28 11:57:51 +0200 | [diff] [blame] | 640 | |
| 641 | if (pthread_getname_np(pthread_self(), name, sizeof(name)) == 0) |
| 642 | has_name = true; |
| 643 | |
| 644 | /* Get latest matching mask for the thread */ |
| 645 | pthread_mutex_lock(&sched_vty_opts->cpu_affinity_li_mutex); |
| 646 | llist_for_each_entry(it, &sched_vty_opts->cpu_affinity_li, entry) { |
| 647 | switch (it->tid_type) { |
| 648 | case SCHED_VTY_THREAD_SELF: |
| 649 | continue; /* self to the VTY thread, not us */ |
| 650 | case SCHED_VTY_THREAD_ALL: |
| 651 | it_match = it; |
| 652 | break; |
| 653 | case SCHED_VTY_THREAD_NAME: |
| 654 | if (!has_name) |
| 655 | continue; |
| 656 | if (strcmp(name, it->bufname) != 0) |
| 657 | continue; |
| 658 | it_match = it; |
| 659 | break; |
| 660 | default: |
| 661 | OSMO_ASSERT(0); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if (it_match) { |
| 666 | rc = my_sched_setaffinity(SCHED_VTY_THREAD_SELF, 0, it_match->cpuset, it_match->cpuset_size); |
| 667 | if (rc == -1) { |
| 668 | if (generate_cpu_hex_mask(str_mask, sizeof(str_mask), |
| 669 | it_match->cpuset, it_match->cpuset_size) < 0) |
| 670 | str_mask[0] = '\0'; |
| 671 | LOGP(DLGLOBAL, LOGL_FATAL, "Setting cpu-affinity mask %s failed: %s\n", |
| 672 | str_mask, strerror(errno)); |
| 673 | } |
| 674 | } |
| 675 | pthread_mutex_unlock(&sched_vty_opts->cpu_affinity_li_mutex); |
| 676 | return rc; |
| 677 | } |
| 678 | |
| 679 | /*! @} */ |