Merge pull request #7915 from poettering/pids-max-tweak
This commit is contained in:
@@ -150,6 +150,8 @@ basic_sources = files('''
|
||||
proc-cmdline.h
|
||||
process-util.c
|
||||
process-util.h
|
||||
procfs-util.c
|
||||
procfs-util.h
|
||||
random-util.c
|
||||
random-util.h
|
||||
ratelimit.c
|
||||
|
||||
@@ -184,3 +184,22 @@ static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
|
||||
}
|
||||
|
||||
int fork_agent(const char *name, const int except[], unsigned n_except, pid_t *pid, const char *path, ...);
|
||||
|
||||
#if SIZEOF_PID_T == 4
|
||||
/* The highest possibly (theoretic) pid_t value on this architecture. */
|
||||
#define PID_T_MAX ((pid_t) INT32_MAX)
|
||||
/* The maximum number of concurrent processes Linux allows on this architecture, as well as the highest valid PID value
|
||||
* the kernel will potentially assign. This reflects a value compiled into the kernel (PID_MAX_LIMIT), and sets the
|
||||
* upper boundary on what may be written to the /proc/sys/kernel/pid_max sysctl (but do note that the sysctl is off by
|
||||
* 1, since PID 0 can never exist and there can hence only be one process less than the limit would suggest). Since
|
||||
* these values are documented in proc(5) we feel quite confident that they are stable enough for the near future at
|
||||
* least to define them here too. */
|
||||
#define TASKS_MAX 4194303U
|
||||
#elif SIZEOF_PID_T == 2
|
||||
#define PID_T_MAX ((pid_t) INT16_MAX)
|
||||
#define TASKS_MAX 32767U
|
||||
#else
|
||||
#error "Unknown pid_t size"
|
||||
#endif
|
||||
|
||||
assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX)
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "fileio.h"
|
||||
#include "parse-util.h"
|
||||
#include "process-util.h"
|
||||
#include "procfs-util.h"
|
||||
#include "stdio-util.h"
|
||||
#include "string-util.h"
|
||||
|
||||
int procfs_tasks_get_limit(uint64_t *ret) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
uint64_t pid_max, threads_max;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
|
||||
/* So there are two sysctl files that control the system limit of processes:
|
||||
*
|
||||
* 1. kernel.threads-max: this is probably the sysctl that makes more sense, as it directly puts a limit on
|
||||
* concurrent tasks.
|
||||
*
|
||||
* 2. kernel.pid_max: this limits the numeric range PIDs can take, and thus indirectly also limits the number
|
||||
* of concurrent threads. AFAICS it's primarily a compatibility concept: some crappy old code used a signed
|
||||
* 16bit type for PIDs, hence the kernel provides a way to ensure the PIDs never go beyond INT16_MAX by
|
||||
* default.
|
||||
*
|
||||
* By default #2 is set to much lower values than #1, hence the limit people come into contact with first, as
|
||||
* it's the lowest boundary they need to bump when they want higher number of processes.
|
||||
*
|
||||
* Also note the weird definition of #2: PIDs assigned will be kept below this value, which means the number of
|
||||
* tasks that can be created is one lower, as PID 0 is not a valid process ID. */
|
||||
|
||||
r = read_one_line_file("/proc/sys/kernel/pid_max", &value);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = safe_atou64(value, &pid_max);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
value = mfree(value);
|
||||
r = read_one_line_file("/proc/sys/kernel/threads-max", &value);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = safe_atou64(value, &threads_max);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* Subtract one from pid_max, since PID 0 is not a valid PID */
|
||||
*ret = MIN(pid_max-1, threads_max);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int procfs_tasks_set_limit(uint64_t limit) {
|
||||
char buffer[DECIMAL_STR_MAX(uint64_t)+1];
|
||||
_cleanup_free_ char *value = NULL;
|
||||
uint64_t pid_max;
|
||||
int r;
|
||||
|
||||
if (limit == 0) /* This makes no sense, we are userspace and hence count as tasks too, and we want to live,
|
||||
* hence the limit conceptually has to be above 0. Also, most likely if anyone asks for a zero
|
||||
* limit he/she probably means "no limit", hence let's better refuse this to avoid
|
||||
* confusion. */
|
||||
return -EINVAL;
|
||||
|
||||
/* The Linux kernel doesn't allow this value to go below 20, hence don't allow this either, higher values than
|
||||
* TASKS_MAX are not accepted by the pid_max sysctl. We'll treat anything this high as "unbounded" and hence
|
||||
* set it to the maximum. */
|
||||
limit = CLAMP(limit, 20U, TASKS_MAX);
|
||||
|
||||
r = read_one_line_file("/proc/sys/kernel/pid_max", &value);
|
||||
if (r < 0)
|
||||
return r;
|
||||
r = safe_atou64(value, &pid_max);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* As pid_max is about the numeric pid_t range we'll bump it if necessary, but only ever increase it, never
|
||||
* decrease it, as threads-max is the much more relevant sysctl. */
|
||||
if (limit > pid_max-1) {
|
||||
sprintf(buffer, "%" PRIu64, limit+1); /* Add one, since PID 0 is not a valid PID */
|
||||
r = write_string_file("/proc/sys/kernel/pid_max", buffer, WRITE_STRING_FILE_DISABLE_BUFFER);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
sprintf(buffer, "%" PRIu64, limit);
|
||||
r = write_string_file("/proc/sys/kernel/threads-max", buffer, WRITE_STRING_FILE_DISABLE_BUFFER);
|
||||
if (r < 0) {
|
||||
uint64_t threads_max;
|
||||
|
||||
/* Hmm, we couldn't write this? If so, maybe it was already set properly? In that case let's not
|
||||
* generate an error */
|
||||
|
||||
value = mfree(value);
|
||||
if (read_one_line_file("/proc/sys/kernel/threads-max", &value) < 0)
|
||||
return r; /* return original error */
|
||||
|
||||
if (safe_atou64(value, &threads_max) < 0)
|
||||
return r; /* return original error */
|
||||
|
||||
if (MIN(pid_max-1, threads_max) != limit)
|
||||
return r; /* return original error */
|
||||
|
||||
/* Yay! Value set already matches what we were trying to set, hence consider this a success. */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int procfs_tasks_get_current(uint64_t *ret) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
const char *p, *nr;
|
||||
size_t n;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
|
||||
r = read_one_line_file("/proc/loadavg", &value);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* Look for the second part of the fourth field, which is separated by a slash from the first part. None of the
|
||||
* earlier fields use a slash, hence let's use this to find the right spot. */
|
||||
p = strchr(value, '/');
|
||||
if (!p)
|
||||
return -EINVAL;
|
||||
|
||||
p++;
|
||||
n = strspn(p, DIGITS);
|
||||
nr = strndupa(p, n);
|
||||
|
||||
return safe_atou64(nr, ret);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
int procfs_tasks_get_limit(uint64_t *ret);
|
||||
int procfs_tasks_set_limit(uint64_t limit);
|
||||
int procfs_tasks_get_current(uint64_t *ret);
|
||||
+6
-14
@@ -52,6 +52,7 @@
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
#include "procfs-util.h"
|
||||
#include "set.h"
|
||||
#include "signal-util.h"
|
||||
#include "stat-util.h"
|
||||
@@ -473,31 +474,22 @@ uint64_t physical_memory_scale(uint64_t v, uint64_t max) {
|
||||
|
||||
uint64_t system_tasks_max(void) {
|
||||
|
||||
#if SIZEOF_PID_T == 4
|
||||
#define TASKS_MAX ((uint64_t) (INT32_MAX-1))
|
||||
#elif SIZEOF_PID_T == 2
|
||||
#define TASKS_MAX ((uint64_t) (INT16_MAX-1))
|
||||
#else
|
||||
#error "Unknown pid_t size"
|
||||
#endif
|
||||
|
||||
_cleanup_free_ char *value = NULL, *root = NULL;
|
||||
uint64_t a = TASKS_MAX, b = TASKS_MAX;
|
||||
_cleanup_free_ char *root = NULL;
|
||||
|
||||
/* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
|
||||
* limit:
|
||||
*
|
||||
* a) the maximum value for the pid_t type
|
||||
* a) the maximum tasks value the kernel allows on this architecture
|
||||
* b) the cgroups pids_max attribute for the system
|
||||
* c) the kernel's configure maximum PID value
|
||||
* c) the kernel's configured maximum PID value
|
||||
*
|
||||
* And then pick the smallest of the three */
|
||||
|
||||
if (read_one_line_file("/proc/sys/kernel/pid_max", &value) >= 0)
|
||||
(void) safe_atou64(value, &a);
|
||||
(void) procfs_tasks_get_limit(&a);
|
||||
|
||||
if (cg_get_root_path(&root) >= 0) {
|
||||
value = mfree(value);
|
||||
_cleanup_free_ char *value = NULL;
|
||||
|
||||
if (cg_get_attribute("pids", root, "pids.max", &value) >= 0)
|
||||
(void) safe_atou64(value, &b);
|
||||
|
||||
Reference in New Issue
Block a user