Skip to content

Commit e75a019

Browse files
committed
MINOR: startup: tune.renice.{startup,runtime} allow to change priorities
This commit introduces the tune.renice.startup and tune.renice.runtime global keywords that allows to change the priority with setpriority(). tune.renice.startup is parsed and applied in the worker or the standalone process for configuration parsing. If this keyword is used alone, the nice value is changed to the previous one after configuration parsing. tune.renice.runtime is applied after configuration parsing, so in the worker or a standalone process. Combined with tune.renice.startup it allows to have a different nice value during configuration parsing and during runtime. The feature was discussed in github issue #1919. Example: global tune.renice.startup 15 tune.renice.runtime 0
1 parent 2092199 commit e75a019

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

doc/configuration.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,8 @@ The following keywords are supported in the "global" section :
15241524
- tune.quic.retry-threshold
15251525
- tune.quic.socket-owner
15261526
- tune.quic.zero-copy-fwd-send
1527+
- tune.renice.runtime
1528+
- tune.renice.startup
15271529
- tune.rcvbuf.backend
15281530
- tune.rcvbuf.client
15291531
- tune.rcvbuf.frontend
@@ -4060,6 +4062,26 @@ tune.quic.zero-copy-fwd-send { on | off }
40604062

40614063
See also: tune.disable-zero-copy-forwarding
40624064

4065+
tune.renice.runtime <number>
4066+
This configuration option takes a value between -20 and 19. It applies a
4067+
scheduling priority as documented in man 2 setpriority. This priority is
4068+
applied after the configuration parsing, which means only the worker or the
4069+
standalone process will apply it. It is usually configured to set a higher
4070+
priority than a process doing configuration parsing (tune.renice.startup).
4071+
4072+
See also: tune.renice.startup
4073+
4074+
tune.renice.startup <number>
4075+
This configuration option takes a value between -20 and 19. It applies a
4076+
scheduling priority as documented in man 2 setpriority. This priority is
4077+
applied before applying the rest of the configuration which can be useful if
4078+
you want to lower the priority for configuration parsing. This is applied on
4079+
the standalone process or the worker before configuration parsing. Once the
4080+
configuration is parsed, the previous priority is restored unless
4081+
tune.renice.runtime is used.
4082+
4083+
See also: tune.renice.runtime
4084+
40634085
tune.rcvbuf.backend <number>
40644086
tune.rcvbuf.frontend <number>
40654087
For the kernel socket receive buffer size on non-connected sockets to this

include/haproxy/global-t.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ struct global {
190190
int pool_low_count; /* max number of opened fd before we stop using new idle connections */
191191
int pool_high_count; /* max number of opened fd before we start killing idle connections when creating new connections */
192192
size_t pool_cache_size; /* per-thread cache size per pool (defaults to CONFIG_HAP_POOL_CACHE_SIZE) */
193+
int renice_startup; /* startup nice()+100 value during startup; 0 = unset */
194+
int renice_runtime; /* startup nice()+100 value during runtime; 0 = unset */
193195
unsigned short idle_timer; /* how long before an empty buffer is considered idle (ms) */
194196
unsigned short no_zero_copy_fwd; /* Flags to disable zero-copy fast-forwarding (global & per-protocols) */
195197
int nb_stk_ctr; /* number of stick counters, defaults to MAX_SESS_STKCTR */

src/cfgparse-global.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <grp.h>
99
#include <errno.h>
1010
#include <sys/types.h>
11+
#include <sys/resource.h>
1112
#include <sys/stat.h>
1213
#include <unistd.h>
1314

@@ -1540,6 +1541,54 @@ static int cfg_parse_global_parser_pause(char **args, int section_type,
15401541
return 0;
15411542
}
15421543

1544+
/* config parser for global "tune.renice.startup" and "tune.renice.runtime",
1545+
* accepts -20 to +19 inclusive, stored as 80..119.
1546+
*/
1547+
static int cfg_parse_tune_renice(char **args, int section_type, struct proxy *curpx,
1548+
const struct proxy *defpx, const char *file, int line,
1549+
char **err)
1550+
{
1551+
int prio;
1552+
char *stop;
1553+
1554+
if (too_many_args(1, args, err, NULL))
1555+
return -1;
1556+
1557+
prio = strtol(args[1], &stop, 10);
1558+
if ((*stop != '\0') || (prio < -20 || prio > 19)) {
1559+
memprintf(err, "'%s' only supports values between -20 and 19 inclusive (was given %s)", args[0], args[1]);
1560+
return -1;
1561+
}
1562+
1563+
/* 'runtime' vs 'startup' */
1564+
if (args[0][12] == 'r') {
1565+
/* runtime is executed once parsing is done */
1566+
1567+
global.tune.renice_runtime = prio + 100;
1568+
} else if (args[0][12] == 's') {
1569+
/* startup is executed during cfg parsing */
1570+
1571+
global.tune.renice_startup = prio + 100;
1572+
if (setpriority(PRIO_PROCESS, 0, prio) == -1)
1573+
ha_warning("couldn't set the startup nice value to %d: %s\n", prio, strerror(errno));
1574+
1575+
/* try to store the previous priority in the runtime priority */
1576+
prio = getpriority(PRIO_PROCESS, 0);
1577+
if (prio == -1) {
1578+
ha_warning("couldn't get the runtime nice value: %s\n", strerror(errno));
1579+
} else {
1580+
/* if there wasn't a renice runtime option set */
1581+
if (global.tune.renice_runtime == 0)
1582+
global.tune.renice_runtime = prio + 100;
1583+
}
1584+
1585+
} else {
1586+
BUG_ON(1, "Triggered in cfg_parse_tune_renice() by unsupported keyword.\n");
1587+
}
1588+
1589+
return 0;
1590+
}
1591+
15431592
static struct cfg_kw_list cfg_kws = {ILH, {
15441593
{ CFG_GLOBAL, "prealloc-fd", cfg_parse_prealloc_fd },
15451594
{ CFG_GLOBAL, "force-cfg-parser-pause", cfg_parse_global_parser_pause, KWF_EXPERIMENTAL },
@@ -1563,6 +1612,8 @@ static struct cfg_kw_list cfg_kws = {ILH, {
15631612
{ CFG_GLOBAL, "tune.bufsize", cfg_parse_global_tune_opts },
15641613
{ CFG_GLOBAL, "tune.maxrewrite", cfg_parse_global_tune_opts },
15651614
{ CFG_GLOBAL, "tune.idletimer", cfg_parse_global_tune_opts },
1615+
{ CFG_GLOBAL, "tune.renice.startup", cfg_parse_tune_renice },
1616+
{ CFG_GLOBAL, "tune.renice.runtime", cfg_parse_tune_renice },
15661617
{ CFG_GLOBAL, "tune.rcvbuf.client", cfg_parse_global_tune_opts },
15671618
{ CFG_GLOBAL, "tune.rcvbuf.server", cfg_parse_global_tune_opts },
15681619
{ CFG_GLOBAL, "tune.sndbuf.client", cfg_parse_global_tune_opts },

src/haproxy.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,6 +3942,15 @@ int main(int argc, char **argv)
39423942
}
39433943
}
39443944

3945+
/* applies the renice value in the worker or standalone after configuration parsing
3946+
* but before chaning identity */
3947+
if (!master && global.tune.renice_runtime) {
3948+
if (setpriority(PRIO_PROCESS, 0, global.tune.renice_runtime - 100) == -1) {
3949+
ha_warning("[%s.main()] couldn't set the runtime nice value to %d: %s\n",
3950+
argv[0], global.tune.renice_runtime - 100, strerror(errno));
3951+
}
3952+
}
3953+
39453954
/* Must chroot and setgid/setuid in the children */
39463955
/* chroot if needed */
39473956
if (global.chroot != NULL) {

0 commit comments

Comments
 (0)