1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
|
/*
schedtool
Copyright (C) 2002-2006 by Freek
Please contact me via freshmeat.net
Release under GPL, version 2
Use at your own risk.
Inspired by setbatch (C) 2002 Ingo Molnar
01/2006:
included support for SCHED_IDLEPRIO for ck kernels
01/2004:
included SCHED_ISO patch by Con Kolivas
11/2004:
add probing for some features (priority)
09/2008:
change affinity calls to new cpu_set_t API
Born in the need of querying and setting SCHED_* policies.
All output, even errors, go to STDOUT to ease piping.
Content:
main code
cmd-line parsing
the engine
set_/print_process
usage
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sched.h>
#include <unistd.h>
#include <stdint.h>
#include "error.h"
#include "util.h"
/* various operation modes: print/set/affinity/fork */
#define MODE_NOTHING 0x0
#define MODE_PRINT 0x1
#define MODE_SETPOLICY 0x2
#define MODE_AFFINITY 0x4
#define MODE_EXEC 0x8
#define MODE_NICE 0x10
#define VERSION "1.3.0"
/*
constants are from the O(1)-sched kernel's include/sched.h
I don't want to include kernel-headers.
Included those defines for improved readability.
*/
#undef SCHED_NORMAL
#undef SCHED_FIFO
#undef SCHED_RR
#undef SCHED_BATCH
#define SCHED_NORMAL 0
#define SCHED_FIFO 1
#define SCHED_RR 2
#define SCHED_BATCH 3
#define SCHED_ISO 4
#define SCHED_IDLEPRIO 5
/* for loops */
#define SCHED_MIN SCHED_NORMAL
#define SCHED_MAX SCHED_IDLEPRIO
#define CHECK_RANGE_POLICY(p) (p <= SCHED_MAX && p >= SCHED_MIN)
#define CHECK_RANGE_NICE(n) (n <= 20 && n >= -20)
/*
4 bits of CPU_SETSIZE will naturally reduce to 1 char, so
we'd only need [CPU_SETSIZE / 4 + 1]
to be sure leave a bit of room and only do CPU_SETSIZE / 2
*/
#define CPUSET_HEXSTRING(name) char name[CPU_SETSIZE / 2]
char *TAB[] = {
"N: SCHED_NORMAL",
"F: SCHED_FIFO",
"R: SCHED_RR",
"B: SCHED_BATCH",
"I: SCHED_ISO",
"D: SCHED_IDLEPRIO",
0
};
/* call it engine_s in lack of a better name */
struct engine_s {
int mode;
int policy;
int prio;
int nice;
cpu_set_t aff_mask;
/* # of args when going in PID-mode */
int n;
char **args;
};
int engine(struct engine_s *e);
int set_process(pid_t pid, int policy, int prio);
static inline int val_to_char(int v);
static char * cpuset_to_str(cpu_set_t *mask, char *str);
static inline int char_to_val(int c);
static int str_to_cpuset(cpu_set_t *mask, const char* str);
int parse_affinity(cpu_set_t *, char *arg);
int set_affinity(pid_t pid, cpu_set_t *mask);
int set_niceness(pid_t pid, int nice);
void probe_sched_features();
void get_prio_min_max(int policy, int *min, int *max);
void print_prio_min_max(int policy);
void print_process(pid_t pid);
void usage(void);
extern char *optarg;
extern int optind, opterr, optopt;
extern int errno;
int main(int ac, char **dc)
{
/*
policy: -1, to indicate it was not set;
nice: 10, as nice/renice have as default;
prio: 0 per default
mode: MODE_NOTHING, no options set
*/
int policy=-1, nice=10, prio=0, mode=MODE_NOTHING;
/*
aff_mask: zero it out
*/
cpu_set_t aff_mask;
CPU_ZERO(&aff_mask);
/* for getopt() */
int c;
if (ac < 2) {
usage();
return(0);
}
while((c=getopt(ac, dc, "+NFRBID012345M:a:p:n:ervh")) != -1) {
switch(c) {
case '0':
case 'N':
policy=SCHED_NORMAL;
mode |= MODE_SETPOLICY;
break;
case '1':
case 'F':
policy=SCHED_FIFO;
mode |= MODE_SETPOLICY;
break;
case '2':
case 'R':
policy=SCHED_RR;
mode |= MODE_SETPOLICY;
break;
case '3':
case 'B':
policy=SCHED_BATCH;
mode |= MODE_SETPOLICY;
break;
case '4':
case 'I':
policy=SCHED_ISO;
mode |= MODE_SETPOLICY;
break;
case '5':
case 'D':
policy=SCHED_IDLEPRIO;
mode |= MODE_SETPOLICY;
break;
case 'M':
/* manual setting */
policy=atoi(optarg);
mode |= MODE_SETPOLICY;
break;
case 'a':
mode |= MODE_AFFINITY;
parse_affinity(&aff_mask, optarg);
break;
case 'n':
mode |= MODE_NICE;
nice=atoi(optarg);
break;
case 'e':
mode |= MODE_EXEC;
break;
case 'p':
prio=atoi(optarg);
break;
case 'r':
probe_sched_features();
break;
case 'v':
/* the user wants feedback for each process */
mode |= MODE_PRINT;
break;
case 'V':
case 'h':
usage();
return(0);
default:
//printf("ERROR: unknown switch\n");
usage();
return(1);
}
}
/*
DAMN FUCKING
parameter checking
ARGH!
*/
/* for _BATCH and _NORMAL, prio is ignored and must be 0*/
if((policy==SCHED_NORMAL || policy==SCHED_BATCH) && prio) {
decode_error("%s call may fail as static PRIO must be 0 or omitted",
TAB[policy]
);
/* _FIFO and _RR MUST have prio set */
} else if((policy==SCHED_FIFO || policy==SCHED_RR || policy==SCHED_ISO)) {
#define CHECK_RANGE_PRIO(p, p_low, p_high) (p <= (p_high) && p >= (p_low))
/* FIFO and RR - check min/max priority */
int prio_min, prio_max;
get_prio_min_max(policy, &prio_min, &prio_max);
//int prio_max=sched_get_priority_max(policy);
//int prio_min=sched_get_priority_min(policy);
if(! CHECK_RANGE_PRIO(prio, prio_min, prio_max)) {
// this could be problematic on very special custom kernels
if(prio == 0) {
decode_error("missing priority; specify static priority via -p");
} else {
decode_error("PRIO %d is out of range %d-%d for %s",
prio,
prio_min,
prio_max,
TAB[policy]
);
}
/* treat as all calls have failed */
return(ac-optind);
}
#undef CHECK_RANGE_PRIO
}
/* no mode -> do querying */
if(! mode) {
mode |= MODE_PRINT;
}
if( mode_set(mode, MODE_EXEC) && ! ( mode_set(mode, MODE_SETPOLICY)
|| mode_set(mode, MODE_AFFINITY)
|| mode_set(mode, MODE_NICE) )
) {
/* we have nothing to do */
decode_error("Option -e needs scheduling-parameters, not given - exiting");
return(-1);
}
if(! CHECK_RANGE_NICE(nice)) {
decode_error("NICE %d is out of range -20 to 20", nice);
return(-1);
}
#undef CHECK_RANGE_NICE
/* and: ignition */
{
struct engine_s stuff;
/* now fill struct */
stuff.mode=mode;
stuff.policy=policy;
stuff.prio=prio;
stuff.aff_mask=aff_mask;
stuff.nice=nice;
/* we have this much real args/PIDs to process */
stuff.n=ac-optind;
/* this is the first real arg */
stuff.args=dc+optind;
/* now go on and do what we were told */
return(engine(&stuff));
}
}
int engine(struct engine_s *e)
{
int ret=0;
int i;
#ifdef DEBUG
do {
CPUSET_HEXSTRING(tmpaff);
printf("Dumping mode: 0x%x\n", e->mode);
printf("Dumping affinity: 0x%s\n", cpuset_to_str(&(e->aff_mask), tmpaff));
printf("We have %d args to do\n", e->n);
for(i=0;i < e->n; i++) {
printf("Dump arg %d: %s\n", i, e->args[i]);
}
} while(0);
#endif
/*
handle normal query/set operation:
set/query all given PIDs
*/
for(i=0; i < e->n; i++) {
int pid, tmpret=0;
cpu_set_t affi;
CPU_ZERO(&affi);
CPU_SET(0, &affi);
/* if in MODE_EXEC skip check for PIDs */
if(mode_set(e->mode, MODE_EXEC)) {
pid=getpid();
goto exec_mode_special;
}
if(! (isdigit( *(e->args[i])) ) ) {
decode_error("Ignoring arg %s: is not a PID", e->args[i]);
continue;
}
pid=atoi(e->args[i]);
exec_mode_special:
if(mode_set(e->mode, MODE_SETPOLICY)) {
/*
accumulate possible errors
the return value of main will indicate
how much set-calls went wrong
set_process returns -1 upon failure
*/
tmpret=set_process(pid,e->policy,e->prio);
ret += tmpret;
/* don't proceed as something went wrong already */
if(tmpret) {
continue;
}
}
if(mode_set(e->mode, MODE_NICE)) {
tmpret=set_niceness(pid, e->nice);
ret += tmpret;
if(tmpret) {
continue;
}
}
if(mode_set(e->mode, MODE_AFFINITY)) {
tmpret=set_affinity(pid, &(e->aff_mask));
ret += tmpret;
if(tmpret) {
continue;
}
}
/* and print process info when set, too */
if(mode_set(e->mode, MODE_PRINT)) {
print_process(pid);
}
/* EXECUTE: at the end */
if(mode_set(e->mode, MODE_EXEC)) {
char **new_argv=e->args;
ret=execvp(*new_argv, new_argv);
/* only reached on error */
decode_error("schedtool: Could not exec %s", *new_argv);
return(ret);
}
}
/*
indicate how many errors we got; as ret is accumulated negative,
convert to positive
*/
return(abs(ret));
}
int set_process(pid_t pid, int policy, int prio)
{
struct sched_param p;
int ret;
char *msg1="could not set PID %d to %s";
char *msg2="could not set PID %d to raw policy #%d";
p.sched_priority=prio;
/* anything other than 0 indicates error */
if((ret=sched_setscheduler(pid, policy, &p))) {
/* la la pointer mismatch .. lala */
decode_error((CHECK_RANGE_POLICY(policy) ? msg1 : msg2),
pid,
(CHECK_RANGE_POLICY(policy) ? TAB[policy] : policy)
);
return(ret);
}
return(0);
}
/*
the following functions have been taken from taskset of util-linux
(C) 2004 by Robert Love
*/
static inline int val_to_char(int v)
{
if (v >= 0 && v < 10)
return '0' + v;
else if (v >= 10 && v < 16)
return ('a' - 10) + v;
else
return -1;
}
/*
str seems to need to hold [CPU_SETSIZE * 7] chars, as in used in taskset
however, 4 bits of CPU_SETSIZE will naturally reduce to 1 char
(bits "1111" -> char "f") so CPU_SETSIZE / 4 + 1 should be sufficient
will pad with zeroes to the start, so print the string starting at the
returned char *
*/
static char * cpuset_to_str(cpu_set_t *mask, char *str)
{
int base;
char *ptr = str;
char *ret = 0;
for (base = CPU_SETSIZE - 4; base >= 0; base -= 4) {
char val = 0;
if (CPU_ISSET(base, mask))
val |= 1;
if (CPU_ISSET(base + 1, mask))
val |= 2;
if (CPU_ISSET(base + 2, mask))
val |= 4;
if (CPU_ISSET(base + 3, mask))
val |= 8;
if (!ret && val)
ret = ptr;
*ptr++ = val_to_char(val);
}
*ptr = 0;
return ret ? ret : ptr - 1;
}
static inline int char_to_val(int c)
{
int cl;
cl = tolower(c);
if (c >= '0' && c <= '9')
return c - '0';
else if (cl >= 'a' && cl <= 'f')
return cl + (10 - 'a');
else
return -1;
}
static int str_to_cpuset(cpu_set_t *mask, const char* str)
{
int len = strlen(str);
const char *ptr = str + len - 1;
int base = 0;
/* skip 0x, it's all hex anyway */
if(len > 1 && str[0] == '0' && str[1] == 'x') {
str += 2;
}
CPU_ZERO(mask);
while (ptr >= str) {
char val = char_to_val(*ptr);
if (val == (char) -1)
return -1;
if (val & 1)
CPU_SET(base, mask);
if (val & 2)
CPU_SET(base + 1, mask);
if (val & 4)
CPU_SET(base + 2, mask);
if (val & 8)
CPU_SET(base + 3, mask);
len--;
ptr--;
base += 4;
}
return 0;
}
/* end of functions taken */
/* mhm - we need something clever for all that CPU_SET() and CPU_ISSET() stuff */
int parse_affinity(cpu_set_t *mask, char *arg)
{
cpu_set_t tmp_aff;
char *tmp_arg;
size_t valid_len;
CPU_ZERO(&tmp_aff);
if(*arg == '0' && *(arg+1) == 'x') {
/* we're in standard hex mode */
str_to_cpuset(&tmp_aff, arg);
} else if( (valid_len=strspn(arg, "0123456789,.")) ) {
/* new list mode: schedtool -a 0,2 -> run on CPU0 and CPU2 */
/* split on ',' and '.', because '.' is near ',' :) */
while((tmp_arg=strsep(&arg, ",."))) {
int tmp_cpu;
if(isdigit((int)*tmp_arg)) {
tmp_cpu=atoi(tmp_arg);
CPU_SET(tmp_cpu, &tmp_aff);
#ifdef DEBUG
printf("tmp_arg: %s -> tmp_cpu: %d\n", tmp_arg, tmp_cpu);
#endif
}
}
} else {
decode_error("affinity %s is not parseable", arg);
exit(1);
}
*mask=tmp_aff;
return 0;
}
int set_affinity(pid_t pid, cpu_set_t *mask)
{
int ret;
CPUSET_HEXSTRING(aff_hex);
if((ret=sched_setaffinity(pid, sizeof(cpu_set_t), mask)) == -1) {
decode_error("could not set PID %d to affinity 0x%s",
pid,
cpuset_to_str(mask, aff_hex)
);
return(ret);
}
return(0);
}
int set_niceness(pid_t pid, int nice)
{
int ret;
if((ret=setpriority(PRIO_PROCESS, pid, nice))) {
decode_error("could not set PID %d to nice %d",
pid,
nice
);
return(ret);
}
return(0);
}
/*
probe some features; just basic right now
*/
void probe_sched_features()
{
int i;
for(i=SCHED_MIN; i <= SCHED_MAX; i++) {
print_prio_min_max(i);
}
}
/*
get the min/max static priorites of a given policy. Max only work
for SCHED_FIFO / SCHED_RR
*/
void get_prio_min_max(int policy, int *min, int *max)
{
*min=sched_get_priority_min(policy);
*max=sched_get_priority_max(policy);
}
/* print the min and max priority of a given policy, like chrt does */
void print_prio_min_max(int policy)
{
int min, max;
get_prio_min_max(policy, &min, &max);
switch(min|max) {
case -1:
printf("%-17s: policy not implemented\n", TAB[policy]);
break;
default:
printf("%-17s: prio_min %d, prio_max %d\n", TAB[policy], min, max);
break;
}
}
/*
Be more careful with at least the affinity call; someone may use an
affinity-compiled version on a non-affinity kernel.
This is getting more and more fu-gly.
*/
void print_process(pid_t pid)
{
int policy, nice;
struct sched_param p;
cpu_set_t aff_mask;
CPUSET_HEXSTRING(aff_mask_hex);
CPU_ZERO(&aff_mask);
/* strict error checking not needed - it works or not. */
errno=0;
if( ((policy=sched_getscheduler(pid)) < 0)
|| (sched_getparam(pid, &p) < 0)
/* getpriority may successfully return negative values, so errno needs to be checked */
|| ((nice=getpriority(PRIO_PROCESS, pid)) && errno)
) {
decode_error("could not get scheduling-information for PID %d", pid);
} else {
/* do custom output for unknown policy */
if(! CHECK_RANGE_POLICY(policy)) {
printf("PID %5d: PRIO %3d, POLICY %-5d <UNKNOWN>, NICE %3d",
pid,
p.sched_priority,
policy,
nice
);
} else {
printf("PID %5d: PRIO %3d, POLICY %-17s, NICE %3d",
pid,
p.sched_priority,
TAB[policy],
nice
);
}
/*
sched_getaffinity() seems to also return (int)4 on 2.6.8+ on x86 when successful.
this goes against the documentation
*/
if(sched_getaffinity(pid, sizeof(aff_mask), &aff_mask) == -1) {
/*
error or -ENOSYS
simply ignore and reset errno!
*/
errno=0;
} else {
printf(", AFFINITY 0x%s", cpuset_to_str(&aff_mask, aff_mask_hex));
}
printf("\n");
}
}
void usage(void)
{
printf(
"get/set scheduling policies - v" VERSION ", GPL'd, NO WARRANTY\n" \
"USAGE: schedtool PIDS - query PIDS\n" \
" schedtool [OPTIONS] PIDS - set PIDS\n" \
" schedtool [OPTIONS] -e COMMAND - exec COMMAND\n" \
"\n" \
"set scheduling policies:\n" \
" -N for SCHED_NORMAL\n" \
" -F -p PRIO for SCHED_FIFO only as root\n" \
" -R -p PRIO for SCHED_RR only as root\n" \
" -B for SCHED_BATCH\n" \
" -I -p PRIO for SCHED_ISO\n" \
" -D for SCHED_IDLEPRIO\n" \
"\n" \
" -M POLICY for manual mode; raw number for POLICY\n" \
" -p STATIC_PRIORITY usually 1-99; only for FIFO or RR\n" \
" higher numbers means higher priority\n" \
" -n NICE_LEVEL set niceness to NICE_LEVEL\n" \
" -a AFFINITY_MASK set CPU-affinity to bitmask or list\n\n" \
" -e COMMAND [ARGS] start COMMAND with specified policy/priority\n" \
" -r display priority min/max for each policy\n" \
" -v be verbose\n" \
"\n" \
);
/* printf("Parent ");
print_process(getppid()); */
}
|