mirror of
https://github.com/systemd/systemd
synced 2024-07-21 10:17:21 +00:00
basic: delete loadavg.h copy
loadavg.h is an internal header of the Linux source repository, and as such it is licensed as GPLv2-only, without syscall exception. We use it only for 4 macros, which are simply doing some math calculations that cannot thus be subject to copyright. Reimplement the same calculations in another internal header and delete loadavg.h from our tree.
This commit is contained in:
parent
3f67dccca0
commit
f0e2cfc6dc
|
@ -1,48 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef _LINUX_SCHED_LOADAVG_H
|
||||
#define _LINUX_SCHED_LOADAVG_H
|
||||
|
||||
/*
|
||||
* These are the constant used to fake the fixed-point load-average
|
||||
* counting. Some notes:
|
||||
* - 11 bit fractions expand to 22 bits by the multiplies: this gives
|
||||
* a load-average precision of 10 bits integer + 11 bits fractional
|
||||
* - if you want to count load-averages more often, you need more
|
||||
* precision, or rounding will get you. With 2-second counting freq,
|
||||
* the EXP_n values would be 1981, 2034 and 2043 if still using only
|
||||
* 11 bit fractions.
|
||||
*/
|
||||
extern unsigned long avenrun[]; /* Load averages */
|
||||
extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
|
||||
|
||||
#define FSHIFT 11 /* nr of bits of precision */
|
||||
#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
|
||||
#define LOAD_FREQ (5*HZ+1) /* 5 sec intervals */
|
||||
#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
|
||||
#define EXP_5 2014 /* 1/exp(5sec/5min) */
|
||||
#define EXP_15 2037 /* 1/exp(5sec/15min) */
|
||||
|
||||
/*
|
||||
* a1 = a0 * e + a * (1 - e)
|
||||
*/
|
||||
static inline unsigned long
|
||||
calc_load(unsigned long load, unsigned long exp, unsigned long active)
|
||||
{
|
||||
unsigned long newload;
|
||||
|
||||
newload = load * exp + active * (FIXED_1 - exp);
|
||||
if (active >= load)
|
||||
newload += FIXED_1-1;
|
||||
|
||||
return newload / FIXED_1;
|
||||
}
|
||||
|
||||
extern unsigned long calc_load_n(unsigned long load, unsigned long exp,
|
||||
unsigned long active, unsigned int n);
|
||||
|
||||
#define LOAD_INT(x) ((x) >> FSHIFT)
|
||||
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
|
||||
|
||||
extern void calc_global_load(void);
|
||||
|
||||
#endif /* _LINUX_SCHED_LOADAVG_H */
|
|
@ -4,11 +4,7 @@ set -eu
|
|||
set -o pipefail
|
||||
|
||||
for i in *.h */*.h; do
|
||||
if [[ "$i" == "loadavg.h" ]]; then
|
||||
curl --fail "https://raw.githubusercontent.com/torvalds/linux/master/include/linux/sched/$i" -o "$i"
|
||||
else
|
||||
curl --fail "https://raw.githubusercontent.com/torvalds/linux/master/include/uapi/linux/$i" -o "$i"
|
||||
fi
|
||||
curl --fail "https://raw.githubusercontent.com/torvalds/linux/master/include/uapi/linux/$i" -o "$i"
|
||||
|
||||
sed -i -e 's/__user //g' -e '/^#include <linux\/compiler.h>/ d' "$i"
|
||||
done
|
||||
|
|
|
@ -99,7 +99,6 @@ basic_sources = files('''
|
|||
linux/ipv6_route.h
|
||||
linux/l2tp.h
|
||||
linux/libc-compat.h
|
||||
linux/loadavg.h
|
||||
linux/mrp_bridge.h
|
||||
linux/netdevice.h
|
||||
linux/netfilter/nf_tables.h
|
||||
|
|
|
@ -740,13 +740,13 @@ int parse_oom_score_adjust(const char *s, int *ret) {
|
|||
int store_loadavg_fixed_point(unsigned long i, unsigned long f, loadavg_t *ret) {
|
||||
assert(ret);
|
||||
|
||||
if (i >= (~0UL << FSHIFT))
|
||||
if (i >= (~0UL << PRECISION_BITS))
|
||||
return -ERANGE;
|
||||
|
||||
i = i << FSHIFT;
|
||||
f = DIV_ROUND_UP((f << FSHIFT), 100);
|
||||
i = i << PRECISION_BITS;
|
||||
f = DIV_ROUND_UP((f << PRECISION_BITS), 100);
|
||||
|
||||
if (f >= FIXED_1)
|
||||
if (f >= FIXED_POINT_1_0)
|
||||
return -ERANGE;
|
||||
|
||||
*ret = i | f;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <linux/loadavg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
@ -136,6 +135,14 @@ int parse_ip_prefix_length(const char *s, int *ret);
|
|||
|
||||
int parse_oom_score_adjust(const char *s, int *ret);
|
||||
|
||||
/* Implement floating point using fixed integers, to improve performance when
|
||||
* calculating load averages. These macros can be used to extract the integer
|
||||
* and decimal parts of a value. */
|
||||
#define PRECISION_BITS 11
|
||||
#define FIXED_POINT_1_0 (1 << PRECISION_BITS)
|
||||
#define INT_SIDE(x) ((x) >> PRECISION_BITS)
|
||||
#define DECIMAL_SIDE(x) INT_SIDE(((x) & (FIXED_POINT_1_0 - 1)) * 100)
|
||||
|
||||
/* Given a Linux load average (e.g. decimal number 34.89 where 34 is passed as i and 89 is passed as f), convert it
|
||||
* to a loadavg_t. */
|
||||
int store_loadavg_fixed_point(unsigned long i, unsigned long f, loadavg_t *ret);
|
||||
|
|
|
@ -502,8 +502,8 @@ static int monitor_memory_pressure_contexts_handler(sd_event_source *s, uint64_t
|
|||
|
||||
log_debug("Memory pressure for %s is %lu.%02lu%% > %lu.%02lu%% for > %s with reclaim activity",
|
||||
t->path,
|
||||
LOAD_INT(t->memory_pressure.avg10), LOAD_FRAC(t->memory_pressure.avg10),
|
||||
LOAD_INT(t->mem_pressure_limit), LOAD_FRAC(t->mem_pressure_limit),
|
||||
INT_SIDE(t->memory_pressure.avg10), DECIMAL_SIDE(t->memory_pressure.avg10),
|
||||
INT_SIDE(t->mem_pressure_limit), DECIMAL_SIDE(t->mem_pressure_limit),
|
||||
FORMAT_TIMESPAN(m->default_mem_pressure_duration_usec, USEC_PER_SEC));
|
||||
|
||||
r = update_monitored_cgroup_contexts_candidates(
|
||||
|
@ -527,8 +527,8 @@ static int monitor_memory_pressure_contexts_handler(sd_event_source *s, uint64_t
|
|||
log_notice("Killed %s due to memory pressure for %s being %lu.%02lu%% > %lu.%02lu%%"
|
||||
" for > %s with reclaim activity",
|
||||
selected, t->path,
|
||||
LOAD_INT(t->memory_pressure.avg10), LOAD_FRAC(t->memory_pressure.avg10),
|
||||
LOAD_INT(t->mem_pressure_limit), LOAD_FRAC(t->mem_pressure_limit),
|
||||
INT_SIDE(t->memory_pressure.avg10), DECIMAL_SIDE(t->memory_pressure.avg10),
|
||||
INT_SIDE(t->mem_pressure_limit), DECIMAL_SIDE(t->mem_pressure_limit),
|
||||
FORMAT_TIMESPAN(m->default_mem_pressure_duration_usec, USEC_PER_SEC));
|
||||
return 0;
|
||||
}
|
||||
|
@ -811,7 +811,7 @@ int manager_get_dump_string(Manager *m, char **ret) {
|
|||
"System Context:\n",
|
||||
yes_no(m->dry_run),
|
||||
PERMYRIAD_AS_PERCENT_FORMAT_VAL(m->swap_used_limit_permyriad),
|
||||
LOAD_INT(m->default_mem_pressure_limit), LOAD_FRAC(m->default_mem_pressure_limit),
|
||||
INT_SIDE(m->default_mem_pressure_limit), DECIMAL_SIDE(m->default_mem_pressure_limit),
|
||||
FORMAT_TIMESPAN(m->default_mem_pressure_duration_usec, USEC_PER_SEC));
|
||||
oomd_dump_system_context(&m->system_context, f, "\t");
|
||||
|
||||
|
|
|
@ -530,11 +530,11 @@ void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE
|
|||
"%s\tPressure: Avg10: %lu.%02lu Avg60: %lu.%02lu Avg300: %lu.%02lu Total: %s\n"
|
||||
"%s\tCurrent Memory Usage: %s\n",
|
||||
strempty(prefix), ctx->path,
|
||||
strempty(prefix), LOAD_INT(ctx->mem_pressure_limit), LOAD_FRAC(ctx->mem_pressure_limit),
|
||||
strempty(prefix), INT_SIDE(ctx->mem_pressure_limit), DECIMAL_SIDE(ctx->mem_pressure_limit),
|
||||
strempty(prefix),
|
||||
LOAD_INT(ctx->memory_pressure.avg10), LOAD_FRAC(ctx->memory_pressure.avg10),
|
||||
LOAD_INT(ctx->memory_pressure.avg60), LOAD_FRAC(ctx->memory_pressure.avg60),
|
||||
LOAD_INT(ctx->memory_pressure.avg300), LOAD_FRAC(ctx->memory_pressure.avg300),
|
||||
INT_SIDE(ctx->memory_pressure.avg10), DECIMAL_SIDE(ctx->memory_pressure.avg10),
|
||||
INT_SIDE(ctx->memory_pressure.avg60), DECIMAL_SIDE(ctx->memory_pressure.avg60),
|
||||
INT_SIDE(ctx->memory_pressure.avg300), DECIMAL_SIDE(ctx->memory_pressure.avg300),
|
||||
FORMAT_TIMESPAN(ctx->memory_pressure.total, USEC_PER_SEC),
|
||||
strempty(prefix), FORMAT_BYTES(ctx->current_memory_usage));
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/loadavg.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
#include <sys/socket.h>
|
||||
|
@ -805,24 +804,24 @@ static void test_parse_loadavg_fixed_point(void) {
|
|||
loadavg_t fp;
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("1.23", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 1);
|
||||
assert_se(LOAD_FRAC(fp) == 23);
|
||||
assert_se(INT_SIDE(fp) == 1);
|
||||
assert_se(DECIMAL_SIDE(fp) == 23);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("1.80", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 1);
|
||||
assert_se(LOAD_FRAC(fp) == 80);
|
||||
assert_se(INT_SIDE(fp) == 1);
|
||||
assert_se(DECIMAL_SIDE(fp) == 80);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("0.07", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 0);
|
||||
assert_se(LOAD_FRAC(fp) == 7);
|
||||
assert_se(INT_SIDE(fp) == 0);
|
||||
assert_se(DECIMAL_SIDE(fp) == 7);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("0.00", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 0);
|
||||
assert_se(LOAD_FRAC(fp) == 0);
|
||||
assert_se(INT_SIDE(fp) == 0);
|
||||
assert_se(DECIMAL_SIDE(fp) == 0);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("4096.57", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 4096);
|
||||
assert_se(LOAD_FRAC(fp) == 57);
|
||||
assert_se(INT_SIDE(fp) == 4096);
|
||||
assert_se(DECIMAL_SIDE(fp) == 57);
|
||||
|
||||
/* Caps out at 2 digit fracs */
|
||||
assert_se(parse_loadavg_fixed_point("1.100", &fp) == -ERANGE);
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <linux/loadavg.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "psi-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
|
@ -36,40 +35,40 @@ static void test_read_mem_pressure(void) {
|
|||
assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
|
||||
"full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
|
||||
assert_se(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 22);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 17);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 11);
|
||||
assert_se(INT_SIDE(rp.avg10) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg10) == 22);
|
||||
assert_se(INT_SIDE(rp.avg60) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg60) == 17);
|
||||
assert_se(INT_SIDE(rp.avg300) == 1);
|
||||
assert_se(DECIMAL_SIDE(rp.avg300) == 11);
|
||||
assert_se(rp.total == 58761459);
|
||||
assert(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
|
||||
assert_se(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 23);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 16);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 8);
|
||||
assert_se(INT_SIDE(rp.avg10) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg10) == 23);
|
||||
assert_se(INT_SIDE(rp.avg60) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg60) == 16);
|
||||
assert_se(INT_SIDE(rp.avg300) == 1);
|
||||
assert_se(DECIMAL_SIDE(rp.avg300) == 8);
|
||||
assert_se(rp.total == 58464525);
|
||||
|
||||
/* Pressure file with extra unsupported fields */
|
||||
assert_se(write_string_file(path, "some avg5=0.55 avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
|
||||
"full avg10=0.23 avg60=0.16 avg300=1.08 avg600=2.00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
|
||||
assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
|
||||
assert_se(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 22);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 17);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 11);
|
||||
assert_se(INT_SIDE(rp.avg10) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg10) == 22);
|
||||
assert_se(INT_SIDE(rp.avg60) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg60) == 17);
|
||||
assert_se(INT_SIDE(rp.avg300) == 1);
|
||||
assert_se(DECIMAL_SIDE(rp.avg300) == 11);
|
||||
assert_se(rp.total == 58761459);
|
||||
assert(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
|
||||
assert_se(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 23);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 16);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 8);
|
||||
assert_se(INT_SIDE(rp.avg10) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg10) == 23);
|
||||
assert_se(INT_SIDE(rp.avg60) == 0);
|
||||
assert_se(DECIMAL_SIDE(rp.avg60) == 16);
|
||||
assert_se(INT_SIDE(rp.avg300) == 1);
|
||||
assert_se(DECIMAL_SIDE(rp.avg300) == 8);
|
||||
assert_se(rp.total == 58464525);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue