Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Aug 28, 2024
1 parent 3926f4e commit a403586
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 172 deletions.
69 changes: 1 addition & 68 deletions src/cpulimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 1 addition & 47 deletions src/process_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 0 additions & 30 deletions src/process_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,6 @@
#include <kvm.h>
#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
{
Expand Down
22 changes: 22 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "util.h"
#include <string.h>

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;
}
113 changes: 113 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#ifndef __UTIL_H
#define __UTIL_H

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <time.h>
#include <sys/time.h>
#include <string.h>

/* 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
28 changes: 1 addition & 27 deletions tests/process_iterator_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "../src/process_iterator.h"
#include "../src/process_group.h"
#include "../src/util.h"

#ifndef __GNUC__
#define __attribute__(attr)
Expand All @@ -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)))
{
}
Expand Down

0 comments on commit a403586

Please sign in to comment.