From a4035867b89e81c53f145155f4011f80d4f9925a Mon Sep 17 00:00:00 2001 From: HiGarfield Date: Thu, 29 Aug 2024 04:21:05 +0800 Subject: [PATCH] refactor --- src/cpulimit.c | 69 +-------------------- src/process_group.c | 48 +-------------- src/process_iterator.h | 30 --------- src/util.c | 22 +++++++ src/util.h | 113 ++++++++++++++++++++++++++++++++++ tests/process_iterator_test.c | 28 +-------- 6 files changed, 138 insertions(+), 172 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/src/cpulimit.c b/src/cpulimit.c index 8d9b0778..395580f5 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -43,74 +43,7 @@ #include "process_group.h" #include "list.h" - -/* some useful macro */ -#ifndef MIN -#ifdef __GNUC__ -#define MIN(a, b) \ - (__extension__({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - (void)(&_a == &_b); \ - _a < _b ? _a : _b; \ - })) -#else -#define MIN(a, b) \ - (((a) < (b)) ? (a) : (b)) -#endif -#endif - -#ifndef MAX -#ifdef __GNUC__ -#define MAX(a, b) \ - (__extension__({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - (void)(&_a == &_b); \ - _a > _b ? _a : _b; \ - })) -#else -#define MAX(a, b) \ - (((a) > (b)) ? (a) : (b)) -#endif -#endif - -#ifndef basename -#ifdef __GNUC__ -#define basename(path) \ - (__extension__({ \ - const char *_path = (path); \ - const char *_p = strrchr(_path, '/'); \ - _p != NULL ? _p + 1 : _path; \ - })) -#else -static const char *__basename(const char *path) -{ - const char *p = strrchr(path, '/'); - return p != NULL ? p + 1 : path; -} -#define basename(path) __basename(path) -#endif -#endif - -/* inline void nsec2timespec(double nsec, struct timespec *t); */ -#ifndef nsec2timespec -#define nsec2timespec(nsec, t) \ - do \ - { \ - (t)->tv_sec = (time_t)((nsec) / 1e9); \ - (t)->tv_nsec = (long)((nsec) - (t)->tv_sec * 1e9); \ - } while (0) -#endif - -/* inline int sleep_timespec(struct timespec *t); */ -#ifndef sleep_timespec -#if defined(__linux__) && _POSIX_C_SOURCE >= 200112L && defined(CLOCK_TAI) -#define sleep_timespec(t) clock_nanosleep(CLOCK_TAI, 0, (t), NULL) -#else -#define sleep_timespec(t) nanosleep((t), NULL) -#endif -#endif +#include "util.h" #ifndef EPSILON #define EPSILON 1e-12 diff --git a/src/process_group.c b/src/process_group.c index 174bcdc5..1e2e18a4 100644 --- a/src/process_group.c +++ b/src/process_group.c @@ -34,48 +34,7 @@ #include "process_iterator.h" #include "process_group.h" #include "list.h" - -#ifndef get_time -#if _POSIX_TIMERS > 0 -#if defined(CLOCK_TAI) -#define get_time(ts) clock_gettime(CLOCK_TAI, (ts)) -#elif defined(CLOCK_MONOTONIC) -#define get_time(ts) clock_gettime(CLOCK_MONOTONIC, (ts)) -#endif -#endif -#endif -#ifndef get_time -static int __get_time(struct timespec *ts) -{ - struct timeval tv; - if (gettimeofday(&tv, NULL)) - { - return -1; - } - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000L; - return 0; -} -#define get_time(ts) __get_time(ts) -#endif - -#ifndef basename -#ifdef __GNUC__ -#define basename(path) \ - (__extension__({ \ - const char *_path = (path); \ - const char *_p = strrchr(_path, '/'); \ - _p != NULL ? _p + 1 : _path; \ - })) -#else -static const char *__basename(const char *path) -{ - const char *p = strrchr(path, '/'); - return p != NULL ? p + 1 : path; -} -#define basename(path) __basename(path) -#endif -#endif +#include "util.h" /* look for a process by pid search_pid : pid of the wanted process @@ -192,11 +151,6 @@ int close_process_group(struct process_group *pgroup) return 0; } -/* returns t1-t2 in millisecond */ -/* static inline double timediff_in_ms(const struct timespec *t1, const struct timespec *t2) */ -#define timediff_in_ms(t1, t2) \ - (((t1)->tv_sec - (t2)->tv_sec) * 1e3 + ((t1)->tv_nsec - (t2)->tv_nsec) / 1e6) - /* parameter in range 0-1 */ #define ALPHA 0.08 #define MIN_DT 20 diff --git a/src/process_iterator.h b/src/process_iterator.h index 7837ee4c..572bcace 100644 --- a/src/process_iterator.h +++ b/src/process_iterator.h @@ -38,36 +38,6 @@ #include #endif -#ifndef MIN -#ifdef __GNUC__ -#define MIN(a, b) \ - (__extension__({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - (void)(&_a == &_b); \ - _a < _b ? _a : _b; \ - })) -#else -#define MIN(a, b) \ - (((a) < (b)) ? (a) : (b)) -#endif -#endif - -#ifndef MAX -#ifdef __GNUC__ -#define MAX(a, b) \ - (__extension__({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - (void)(&_a == &_b); \ - _a > _b ? _a : _b; \ - })) -#else -#define MAX(a, b) \ - (((a) > (b)) ? (a) : (b)) -#endif -#endif - /* process descriptor */ struct process { diff --git a/src/util.c b/src/util.c new file mode 100644 index 00000000..7aad1dbf --- /dev/null +++ b/src/util.c @@ -0,0 +1,22 @@ +#include "util.h" +#include + +int __get_time(struct timespec *ts); +int __get_time(struct timespec *ts) +{ + struct timeval tv; + if (gettimeofday(&tv, NULL)) + { + return -1; + } + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000L; + return 0; +} + +const char *___basename(const char *path); +const char *___basename(const char *path) +{ + const char *p = strrchr(path, '/'); + return p != NULL ? p + 1 : path; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 00000000..240fd803 --- /dev/null +++ b/src/util.h @@ -0,0 +1,113 @@ +#ifndef __UTIL_H +#define __UTIL_H + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include +#include +#include + +/* some useful macro */ + +#if defined(__GNUC__) && !defined(__UNIQUE_ID) +#define ___PASTE(a, b) a##b +#define __PASTE(a, b) ___PASTE(a, b) +#define __UNIQUE_ID(prefix) \ + __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) +#endif + +#ifndef MIN +#ifdef __GNUC__ +#define __min(t1, t2, min1, min2, x, y) \ + (__extension__({ \ + t1 min1 = (x); \ + t2 min2 = (y); \ + (void) (&min1 == &min2); \ + min1 < min2 ? min1 : min2; })) +#define MIN(x, y) \ + __min(__typeof__(x), __typeof__(y), \ + __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ + x, y) +#else +#define MIN(a, b) \ + (((a) < (b)) ? (a) : (b)) +#endif +#endif + +#ifndef MAX +#ifdef __GNUC__ +#define __max(t1, t2, max1, max2, x, y) \ + (__extension__({ \ + t1 max1 = (x); \ + t2 max2 = (y); \ + (void) (&max1 == &max2); \ + max1 > max2 ? max1 : max2; })) +#define MAX(x, y) \ + __max(__typeof__(x), __typeof__(y), \ + __UNIQUE_ID(max1_), __UNIQUE_ID(max2_), \ + x, y) +#else +#define MAX(a, b) \ + (((a) > (b)) ? (a) : (b)) +#endif +#endif + +#ifndef basename +#ifdef __GNUC__ +#define __basename(path, path_full, p_pos) \ + (__extension__({ \ + const char *path_full = (path); \ + const char *p_pos = strrchr(path_full, '/'); \ + p_pos != NULL ? p_pos + 1 : path_full; \ + })) +#define basename(path) \ + __basename(path, __UNIQUE_ID(path_full_), __UNIQUE_ID(p_pos_)) +#else +const char *___basename(const char *path); +#define basename(path) ___basename(path) +#endif +#endif + +/* inline void nsec2timespec(double nsec, struct timespec *t); */ +#ifndef nsec2timespec +#define nsec2timespec(nsec, t) \ + do \ + { \ + (t)->tv_sec = (time_t)((nsec) / 1e9); \ + (t)->tv_nsec = (long)((nsec) - (t)->tv_sec * 1e9); \ + } while (0) +#endif + +/* inline int sleep_timespec(struct timespec *t); */ +#ifndef sleep_timespec +#if defined(__linux__) && _POSIX_C_SOURCE >= 200112L && defined(CLOCK_TAI) +#define sleep_timespec(t) clock_nanosleep(CLOCK_TAI, 0, (t), NULL) +#else +#define sleep_timespec(t) nanosleep((t), NULL) +#endif +#endif + +#ifndef get_time +#if _POSIX_TIMERS > 0 +#if defined(CLOCK_TAI) +#define get_time(ts) clock_gettime(CLOCK_TAI, (ts)) +#elif defined(CLOCK_MONOTONIC) +#define get_time(ts) clock_gettime(CLOCK_MONOTONIC, (ts)) +#endif +#endif +#endif +#ifndef get_time +int __get_time(struct timespec *ts); +#define get_time(ts) __get_time(ts) +#endif + +/* returns t1-t2 in millisecond */ +/* static inline double timediff_in_ms(const struct timespec *t1, const struct timespec *t2) */ +#ifndef timediff_in_ms +#define timediff_in_ms(t1, t2) \ + (((t1)->tv_sec - (t2)->tv_sec) * 1e3 + ((t1)->tv_nsec - (t2)->tv_nsec) / 1e6) +#endif + +#endif diff --git a/tests/process_iterator_test.c b/tests/process_iterator_test.c index a5b78e84..d724d2ca 100644 --- a/tests/process_iterator_test.c +++ b/tests/process_iterator_test.c @@ -36,6 +36,7 @@ #include "../src/process_iterator.h" #include "../src/process_group.h" +#include "../src/util.h" #ifndef __GNUC__ #define __attribute__(attr) @@ -56,33 +57,6 @@ static void increase_priority(void) } } -/* inline int sleep_timespec(struct timespec *t); */ -#ifndef sleep_timespec -#if defined(__linux__) && _POSIX_C_SOURCE >= 200112L && defined(CLOCK_TAI) -#define sleep_timespec(t) clock_nanosleep(CLOCK_TAI, 0, (t), NULL) -#else -#define sleep_timespec(t) nanosleep((t), NULL) -#endif -#endif - -#ifndef basename -#ifdef __GNUC__ -#define basename(path) \ - (__extension__({ \ - const char *_path = (path); \ - const char *_p = strrchr(_path, '/'); \ - _p != NULL ? _p + 1 : _path; \ - })) -#else -static const char *__basename(const char *path) -{ - const char *p = strrchr(path, '/'); - return p != NULL ? p + 1 : path; -} -#define basename(path) __basename(path) -#endif -#endif - static void ignore_signal(int sig __attribute__((unused))) { }