From f86f2f4e9a20d235b24ea86e4dddd0485165306f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 21 May 2017 13:11:42 -0500 Subject: Cleaup chrt --- lib/lib.c | 10 ++++++ lib/lib.h | 1 + lib/portability.h | 10 ++++-- toys/pending/chrt.c | 91 +++++++++++++++++++++++------------------------------ 4 files changed, 58 insertions(+), 54 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index ceced263..b6277f3b 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -512,6 +512,16 @@ void msleep(long miliseconds) nanosleep(&ts, &ts); } +// return 1<>= 1; + + return i-1; +} + // Inefficient, but deals with unaligned access int64_t peek_le(void *ptr, unsigned size) { diff --git a/lib/lib.h b/lib/lib.h index efdc1fa4..5d2bb4df 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -192,6 +192,7 @@ struct string_list **splitpath(char *path, struct string_list **list); char *readfileat(int dirfd, char *name, char *buf, off_t *len); char *readfile(char *name, char *buf, off_t len); void msleep(long miliseconds); +int highest_bit(unsigned long l); int64_t peek_le(void *ptr, unsigned size); int64_t peek_be(void *ptr, unsigned size); int64_t peek(void *ptr, unsigned size); diff --git a/lib/portability.h b/lib/portability.h index 01c77c67..e62de139 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -45,9 +45,13 @@ #define RLIMIT_RTTIME 15 #endif +// Introduced in Linux 3.1 #ifndef SEEK_DATA #define SEEK_DATA 3 #endif +#ifndef SEEK_HOLE +#define SEEK_HOLE 4 +#endif // We don't define GNU_dammit because we're not part of the gnu project, and // don't want to get any FSF on us. Unfortunately glibc (gnu libc) @@ -199,18 +203,18 @@ static inline void endutxent(void) {;} #ifndef O_NOFOLLOW #define O_NOFOLLOW 0 #endif - #ifndef O_NOATIME #define O_NOATIME 01000000 #endif - #ifndef O_CLOEXEC #define O_CLOEXEC 02000000 #endif - #ifndef O_PATH #define O_PATH 010000000 #endif +#ifndef SCHED_RESET_ON_FORK +#define SCHED_RESET_ON_FORK (1<<30) +#endif // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite" // even though linux has nothing to do with the FSF and never has. diff --git a/toys/pending/chrt.c b/toys/pending/chrt.c index d2b73a42..01076b93 100644 --- a/toys/pending/chrt.c +++ b/toys/pending/chrt.c @@ -1,92 +1,81 @@ /* chrt.c - Get/set real-time (scheduling) attributes * * Copyright 2016 The Android Open Source Project + * + * Note: -ibrfo flags sorted to match SCHED positions for highest_bit() -USE_CHRT(NEWTOY(chrt, "mp#bfiorR[!bfior]", TOYFLAG_USR|TOYFLAG_SBIN)) +USE_CHRT(NEWTOY(chrt, "^mp#<0iRbrfo[!ibrfo]", TOYFLAG_USR|TOYFLAG_SBIN)) config CHRT bool "chrt" default n help - usage: chrt [-m] [-p PID] [POLICY PRIO] [COMMAND [ARGS...]] + usage: chrt [-Rmofrbi] {-p PID [PRIORITY] | [PRIORITY COMMAND...]} - Get/set a process' real-time (scheduling) attributes. + Get/set a process' real-time scheduling policy and priority. - -p Apply to given pid + -p Set/query given pid (instead of running COMMAND) -R Set SCHED_RESET_ON_FORK -m Show min/max priorities available - Policies: - -b SCHED_BATCH -f SCHED_FIFO -i SCHED_IDLE - -o SCHED_OTHER -r SCHED_RR + Set policy (default -r): + + -o SCHED_OTHER -f SCHED_FIFO -r SCHED_RR + -b SCHED_BATCH -i SCHED_IDLE */ #define FOR_chrt #include "toys.h" -#include - GLOBALS( long pid; ) -static char *policy_name(int policy) { - char *policy_names[] = { "SCHED_OTHER", "SCHED_FIFO", "SCHED_RR", - "SCHED_BATCH", "4", "SCHED_IDLE", "SCHED_DEADLINE" }; - - return policy < ARRAY_LEN(policy_names) ? policy_names[policy] : "???"; -} +char *polnames[] = { + "SCHED_OTHER", "SCHED_FIFO", "SCHED_RR", "SCHED_BATCH", 0, "SCHED_IDLE", + "SCHED_DEADLINE" +}; void chrt_main(void) { - int policy = SCHED_RR; - struct sched_param p; + int pol, pri; // Show min/maxes? if (toys.optflags&FLAG_m) { - for (policy = SCHED_OTHER; policy <= SCHED_IDLE; ++policy) - if (policy != 4) // There's an unused hole in the priorities. - printf("%s min/max priority\t: %d/%d\n", policy_name(policy), - sched_get_priority_min(policy), sched_get_priority_max(policy)); + for (pol = 0; pol