blob: 931310db53ef36564b1a8bb24443c75770a53b22 [file] [log] [blame]
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001Upstream-Status: Backport
2Signed-off-by: Ross Burton <ross.burton@arm.com>
3
4PR28804: tune default stap -s ## buffer size on small RAM machines
5
6Insert a forgotten division by num_online_cpu() to adjust downward the
7calculated bufsize. Tweak normal defaults back to 128 * 2 * 64K
8(16MB) per CPU, as the stap man page indicates. This may need further
9tweaking when balancing against staprun consumption performance, but
10at least we have the docs lined up with the code at the moment.
11
12PR28804: tune default stap -s ## buffer size on small RAM machines
13
14Use si_meminfo to limit default buffer size. Note in the man page
15that the "-s ##" parameter is per-CPU.
16
17diff --git a/man/stap.1.in b/man/stap.1.in
18index 55dbc2c93..285a27b34 100644
19--- a/man/stap.1.in
20+++ b/man/stap.1.in
21@@ -239,8 +239,8 @@ and average amount of time spent in each probe-point. Also shows
22 the derivation for each probe-point.
23 .TP
24 .BI \-s " NUM"
25-Use NUM megabyte buffers for kernel-to-user data transfer. On a
26-multiprocessor in bulk mode, this is a per-processor amount.
27+Use NUM megabyte buffers for kernel-to-user data transfer per processor.
28+The default is 16MB, or less on smaller memory machines.
29 .TP
30 .BI \-I " DIR"
31 Add the given directory to the tapset search directory. See the
32diff --git a/runtime/transport/transport.c b/runtime/transport/transport.c
33index 18ecccea2..44afff814 100644
34--- a/runtime/transport/transport.c
35+++ b/runtime/transport/transport.c
36@@ -72,8 +72,11 @@ static inline void _stp_unlock_inode(struct inode *inode);
37 #include "procfs.c"
38 #include "control.c"
39
40-static unsigned _stp_nsubbufs = 256;
41-static unsigned _stp_subbuf_size = 8 * STP_BUFFER_SIZE; /* 64K */
42+/* set default buffer parameters. User may override these via stap -s #, and
43+ the runtime may auto-shrink it on low memory machines too. */
44+/* NB: Note default in man/stap.1.in */
45+static unsigned _stp_nsubbufs = 128;
46+static unsigned _stp_subbuf_size = 2 * STP_BUFFER_SIZE; /* 2 * 64K */
47
48 /* module parameters */
49 static int _stp_bufsize;
50@@ -602,17 +605,30 @@ static int _stp_transport_init(void)
51 _stp_need_kallsyms_stext = 0;
52 #endif
53
54- if (_stp_bufsize) {
55- unsigned size = _stp_bufsize * 1024 * 1024;
56+ if (_stp_bufsize == 0) { // option not specified?
57+ struct sysinfo si;
58+ long _stp_bufsize_avail;
59+ si_meminfo(&si);
60+ _stp_bufsize_avail = (long)((si.freeram + si.bufferram) / 4 / num_online_cpus())
61+ << PAGE_SHIFT; // limit to quarter of free ram total
62+ if ((_stp_nsubbufs * _stp_subbuf_size * num_online_cpus()) > _stp_bufsize_avail) {
63+ _stp_bufsize = max_t (int, 1, _stp_bufsize_avail / 1024 / 1024);
64+ dbug_trans(1, "Shrinking default _stp_bufsize to %d MB/cpu due to low free memory\n", _stp_bufsize);
65+ }
66+ }
67+
68+ if (_stp_bufsize) { // overridden by user or by si_meminfo heuristic?
69+ long size = _stp_bufsize * 1024 * 1024;
70 _stp_subbuf_size = 65536;
71+ // bump up subbuf size from 64K to 1M to keep _stp_nsubbufs not too large
72 while (size / _stp_subbuf_size > 64 &&
73 _stp_subbuf_size < 1024 * 1024) {
74 _stp_subbuf_size <<= 1;
75 }
76 _stp_nsubbufs = size / _stp_subbuf_size;
77- dbug_trans(1, "Using %d subbufs of size %d\n", _stp_nsubbufs, _stp_subbuf_size);
78 }
79-
80+ dbug_trans(1, "Using %d subbufs of size %d\n", _stp_nsubbufs, _stp_subbuf_size);
81+
82 ret = _stp_transport_fs_init(THIS_MODULE->name);
83 if (ret)
84 goto err0;