From 4280cd16f18903959f1b148e46342957e8ca7d08 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Tue, 21 Feb 2012 20:32:04 +0100 Subject: [PATCH] xsys: included psched routines into xsys --- src/ct_server.c | 1 - src/ifpps.c | 1 - src/netsniff-ng.c | 1 - src/netsniff-ng/CMakeLists.txt | 1 - src/psched.c | 155 ----------------------------------------- src/psched.h | 46 ------------ src/trafgen.c | 1 - src/trafgen/CMakeLists.txt | 1 - src/xsys.c | 117 +++++++++++++++++++++++++++++++ src/xsys.h | 31 +++++++++ 10 files changed, 148 insertions(+), 207 deletions(-) delete mode 100644 src/psched.c delete mode 100644 src/psched.h diff --git a/src/ct_server.c b/src/ct_server.c index 04e37d66..85b251e4 100644 --- a/src/ct_server.c +++ b/src/ct_server.c @@ -32,7 +32,6 @@ #include "die.h" #include "xsys.h" #include "xio.h" -#include "psched.h" #include "xmalloc.h" #include "curvetun.h" #include "curve.h" diff --git a/src/ifpps.c b/src/ifpps.c index 9cb95552..8e7432c1 100644 --- a/src/ifpps.c +++ b/src/ifpps.c @@ -122,7 +122,6 @@ Please report bugs to #include "die.h" #include "xmalloc.h" -#include "psched.h" #include "timespec.h" #include "xsys.h" #include "xio.h" diff --git a/src/netsniff-ng.c b/src/netsniff-ng.c index 19242a4b..35f8f051 100644 --- a/src/netsniff-ng.c +++ b/src/netsniff-ng.c @@ -49,7 +49,6 @@ #include "tprintf.h" #include "dissector.h" #include "xmalloc.h" -#include "psched.h" #include "mtrand.h" #define CPU_UNKNOWN -1 diff --git a/src/netsniff-ng/CMakeLists.txt b/src/netsniff-ng/CMakeLists.txt index f3738557..3eb60341 100644 --- a/src/netsniff-ng/CMakeLists.txt +++ b/src/netsniff-ng/CMakeLists.txt @@ -14,7 +14,6 @@ IF (CMAKE_HAVE_PTHREAD_CREATE) ../strlcpy.c ../bpf.c ../parser.c - ../psched.c ../pcap.c ../pcap_rw.c ../pcap_sg.c diff --git a/src/psched.c b/src/psched.c deleted file mode 100644 index 2807a34b..00000000 --- a/src/psched.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * netsniff-ng - the packet sniffing beast - * By Daniel Borkmann - * Copyright 2009-2011 Daniel Borkmann. - * Copyright 2010 Emmanuel Roullit. - * Subject to the GPL, version 2. - */ - -/* Process RT scheduling */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include - -#include "psched.h" -#include "die.h" - -static inline const char *next_token(const char *q, int sep) -{ - if (q) - q = strchr(q, sep); - if (q) - q++; - - return (q); -} - -int set_cpu_affinity(const char *str, int inverted) -{ - int ret, i, cpus; - const char *p, *q; - cpu_set_t cpu_bitmask; - - q = str; - cpus = get_number_cpus(); - CPU_ZERO(&cpu_bitmask); - - for (i = 0; inverted && i < cpus; ++i) - CPU_SET(i, &cpu_bitmask); - - while (p = q, q = next_token(q, ','), p) { - unsigned int a; /* Beginning of range */ - unsigned int b; /* End of range */ - unsigned int s; /* Stride */ - const char *c1, *c2; - - if (sscanf(p, "%u", &a) < 1) - return -EINVAL; - - b = a; - s = 1; - - c1 = next_token(p, '-'); - c2 = next_token(p, ','); - - if (c1 != NULL && (c2 == NULL || c1 < c2)) { - if (sscanf(c1, "%u", &b) < 1) - return -EINVAL; - c1 = next_token(c1, ':'); - if (c1 != NULL && (c2 == NULL || c1 < c2)) - if (sscanf(c1, "%u", &s) < 1) - return -EINVAL; - } - - if (!(a <= b)) - return -EINVAL; - - while (a <= b) { - if (inverted) - CPU_CLR(a, &cpu_bitmask); - else - CPU_SET(a, &cpu_bitmask); - a += s; - } - } - - ret = sched_setaffinity(getpid(), sizeof(cpu_bitmask), - &cpu_bitmask); - if (ret) - panic("Can't set this cpu affinity!\n"); - return 0; -} - -char *get_cpu_affinity(char *cpu_string, size_t len) -{ - int ret, i, cpu; - cpu_set_t cpu_bitmask; - - if (len != get_number_cpus() + 1) - return NULL; - CPU_ZERO(&cpu_bitmask); - - ret = sched_getaffinity(getpid(), sizeof(cpu_bitmask), - &cpu_bitmask); - if (ret) { - whine("Can't fetch cpu affinity!\n"); - return NULL; - } - - for (i = 0, cpu_string[len - 1] = 0; i < len - 1; ++i) { - cpu = CPU_ISSET(i, &cpu_bitmask); - cpu_string[i] = (cpu ? '1' : '0'); - } - - return cpu_string; -} - -int set_proc_prio(int priority) -{ - /* - * setpriority() is clever, even if you put a nice value which - * is out of range it corrects it to the closest valid nice value - */ - int ret = setpriority(PRIO_PROCESS, getpid(), priority); - if (ret) - panic("Can't set nice val to %i!\n", priority); - return 0; -} - -int set_sched_status(int policy, int priority) -{ - int ret, min_prio, max_prio; - struct sched_param sp; - - max_prio = sched_get_priority_max(policy); - min_prio = sched_get_priority_min(policy); - - if (max_prio == -1 || min_prio == -1) - whine("Cannot determine scheduler prio limits!\n"); - else if (priority < min_prio) - priority = min_prio; - else if (priority > max_prio) - priority = max_prio; - - memset(&sp, 0, sizeof(sp)); - sp.sched_priority = priority; - - ret = sched_setscheduler(getpid(), policy, &sp); - if (ret) { - whine("Cannot set scheduler policy!\n"); - return -EINVAL; - } - - ret = sched_setparam(getpid(), &sp); - if (ret) { - whine("Cannot set scheduler prio!\n"); - return -EINVAL; - } - - return 0; -} - diff --git a/src/psched.h b/src/psched.h deleted file mode 100644 index ffc53087..00000000 --- a/src/psched.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * netsniff-ng - the packet sniffing beast - * By Daniel Borkmann - * Copyright 2009, 2010 Daniel Borkmann. - * Copyright 2010 Emmanuel Roullit. - * Subject to the GPL, version 2. - */ - -#ifndef PSCHED_H -#define PSCHED_H - -#include -#include -#include - -extern int set_cpu_affinity(const char *str, int inverted); -extern char *get_cpu_affinity(char *cpu_string, size_t len); -extern int set_proc_prio(int prio); -extern int set_sched_status(int policy, int priority); - -static inline int get_default_sched_policy(void) -{ - return SCHED_FIFO; -} - -static inline int get_default_sched_prio(void) -{ - return sched_get_priority_max(get_default_sched_policy()); -} - -static inline int get_number_cpus(void) -{ - return sysconf(_SC_NPROCESSORS_CONF); -} - -static inline int get_number_cpus_online(void) -{ - return sysconf(_SC_NPROCESSORS_ONLN); -} - -static inline int get_default_proc_prio(void) -{ - return -20; -} - -#endif /* PSCHED_H */ diff --git a/src/trafgen.c b/src/trafgen.c index 9e70d4a1..427e85ed 100644 --- a/src/trafgen.c +++ b/src/trafgen.c @@ -172,7 +172,6 @@ Please report bugs to #include "parser.h" #include "die.h" #include "xsys.h" -#include "psched.h" #include "xio.h" #include "timespec.h" #include "mtrand.h" diff --git a/src/trafgen/CMakeLists.txt b/src/trafgen/CMakeLists.txt index fe0ec342..a2796f34 100644 --- a/src/trafgen/CMakeLists.txt +++ b/src/trafgen/CMakeLists.txt @@ -6,7 +6,6 @@ ADD_EXECUTABLE(${PROJECT_NAME} ../xmalloc.c ../xio.c ../xsys.c ../strlcpy.c - ../psched.c ../parser.c ../mtrand.c ../opt_memcpy.c diff --git a/src/xsys.c b/src/xsys.c index 760a721f..46a01e2b 100644 --- a/src/xsys.c +++ b/src/xsys.c @@ -6,6 +6,7 @@ * Subject to the GPL, version 2. */ +#define _GNU_SOURCE #include #include #include @@ -25,6 +26,8 @@ #include #include #include +#include +#include #include "die.h" #include "xsys.h" @@ -542,3 +545,117 @@ int poll_error_maybe_die(int sock, struct pollfd *pfd) } return POLL_NEXT_PKT; } + +static inline const char *next_token(const char *q, int sep) +{ + if (q) + q = strchr(q, sep); + if (q) + q++; + return (q); +} + +int set_cpu_affinity(const char *str, int inverted) +{ + int ret, i, cpus; + const char *p, *q; + cpu_set_t cpu_bitmask; + q = str; + cpus = get_number_cpus(); + CPU_ZERO(&cpu_bitmask); + for (i = 0; inverted && i < cpus; ++i) + CPU_SET(i, &cpu_bitmask); + while (p = q, q = next_token(q, ','), p) { + unsigned int a; /* Beginning of range */ + unsigned int b; /* End of range */ + unsigned int s; /* Stride */ + const char *c1, *c2; + if (sscanf(p, "%u", &a) < 1) + return -EINVAL; + b = a; + s = 1; + c1 = next_token(p, '-'); + c2 = next_token(p, ','); + if (c1 != NULL && (c2 == NULL || c1 < c2)) { + if (sscanf(c1, "%u", &b) < 1) + return -EINVAL; + c1 = next_token(c1, ':'); + if (c1 != NULL && (c2 == NULL || c1 < c2)) + if (sscanf(c1, "%u", &s) < 1) + return -EINVAL; + } + if (!(a <= b)) + return -EINVAL; + while (a <= b) { + if (inverted) + CPU_CLR(a, &cpu_bitmask); + else + CPU_SET(a, &cpu_bitmask); + a += s; + } + } + ret = sched_setaffinity(getpid(), sizeof(cpu_bitmask), + &cpu_bitmask); + if (ret) + panic("Can't set this cpu affinity!\n"); + return 0; +} + +char *get_cpu_affinity(char *cpu_string, size_t len) +{ + int ret, i, cpu; + cpu_set_t cpu_bitmask; + if (len != get_number_cpus() + 1) + return NULL; + CPU_ZERO(&cpu_bitmask); + ret = sched_getaffinity(getpid(), sizeof(cpu_bitmask), + &cpu_bitmask); + if (ret) { + whine("Can't fetch cpu affinity!\n"); + return NULL; + } + for (i = 0, cpu_string[len - 1] = 0; i < len - 1; ++i) { + cpu = CPU_ISSET(i, &cpu_bitmask); + cpu_string[i] = (cpu ? '1' : '0'); + } + return cpu_string; +} + +int set_proc_prio(int priority) +{ + /* + * setpriority() is clever, even if you put a nice value which + * is out of range it corrects it to the closest valid nice value + */ + int ret = setpriority(PRIO_PROCESS, getpid(), priority); + if (ret) + panic("Can't set nice val to %i!\n", priority); + return 0; +} + +int set_sched_status(int policy, int priority) +{ + int ret, min_prio, max_prio; + struct sched_param sp; + max_prio = sched_get_priority_max(policy); + min_prio = sched_get_priority_min(policy); + if (max_prio == -1 || min_prio == -1) + whine("Cannot determine scheduler prio limits!\n"); + else if (priority < min_prio) + priority = min_prio; + else if (priority > max_prio) + priority = max_prio; + memset(&sp, 0, sizeof(sp)); + sp.sched_priority = priority; + ret = sched_setscheduler(getpid(), policy, &sp); + if (ret) { + whine("Cannot set scheduler policy!\n"); + return -EINVAL; + } + ret = sched_setparam(getpid(), &sp); + if (ret) { + whine("Cannot set scheduler prio!\n"); + return -EINVAL; + } + return 0; +} diff --git a/src/xsys.h b/src/xsys.h index 871f6d90..36465484 100644 --- a/src/xsys.h +++ b/src/xsys.h @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "die.h" @@ -71,6 +73,31 @@ static inline void prepare_polling(int sock, struct pollfd *pfd) pfd->events = POLLIN | POLLRDNORM | POLLERR; } +static inline int get_default_sched_policy(void) +{ + return SCHED_FIFO; +} + +static inline int get_default_sched_prio(void) +{ + return sched_get_priority_max(get_default_sched_policy()); +} + +static inline int get_number_cpus(void) +{ + return sysconf(_SC_NPROCESSORS_CONF); +} + +static inline int get_number_cpus_online(void) +{ + return sysconf(_SC_NPROCESSORS_ONLN); +} + +static inline int get_default_proc_prio(void) +{ + return -20; +} + extern int af_socket(int af); extern int af_raw_socket(int af, int proto); extern int pf_socket(void); @@ -106,5 +133,9 @@ extern int device_up(char *ifname); extern int device_running(char *ifname); extern int device_up_and_running(char *ifname); extern int poll_error_maybe_die(int sock, struct pollfd *pfd); +extern int set_cpu_affinity(const char *str, int inverted); +extern char *get_cpu_affinity(char *cpu_string, size_t len); +extern int set_proc_prio(int prio); +extern int set_sched_status(int policy, int priority); #endif /* XSYS_H */ -- 2.11.4.GIT