Skip to content

Commit 4ace9a7

Browse files
robbycochranJoukoVirtanenerthalion
committed
ROX-25477 Adjust ringbuffer to always be power of 2 (#1762)
Adjust the bpf ring buffer sizing to meet falco requirements --------- Co-authored-by: JoukoVirtanen <jouko@stackrox.com> Co-authored-by: Dmitrii Dolgov <9erthalion6@gmail.com>
1 parent f4bbb34 commit 4ace9a7

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

collector/lib/CollectorConfig.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,21 @@ unsigned int CollectorConfig::GetSinspBufferSize() const {
440440

441441
max_buffer_size = std::ceil((float)sinsp_total_buffer_size_ / (float)n_buffers);
442442

443-
// It would be awkward to have a buffer size of e.g. 3.14159..., align by the
444-
// highest level, e.g. MiB, KiB.
445-
int magnitude = 0;
446-
while (max_buffer_size >> 10) {
447-
max_buffer_size = max_buffer_size >> 10;
448-
magnitude++;
449-
}
450-
451-
effective_buffer_size = std::min(sinsp_buffer_size_,
452-
(max_buffer_size << magnitude * 10));
443+
// Determine largest power of two that is not greater than max_buffer_size,
444+
// is a multiple of the page size (4096), and greater than two pages to meet
445+
// the requirements for ringbuffer dimensions in
446+
// falcosecurity-libs/driver/ppm_ringbuffer.h
447+
unsigned int maximum_power_of_two_exponent = static_cast<unsigned int>(std::log2(max_buffer_size));
448+
449+
// The max_buffer_size could be arbitrary small here, so verify that the
450+
// resulting exponent value is large enough.
451+
//
452+
// A power of two is always a multiple of one page size if the exponent is
453+
// larger than 12 (2^12 = 4096). Falco also requires this value to be greater
454+
// than two pages, meaning that the exponent has to be at least 14.
455+
maximum_power_of_two_exponent = std::max((unsigned int)14, maximum_power_of_two_exponent);
456+
457+
effective_buffer_size = std::min(sinsp_buffer_size_, (unsigned int)(1 << maximum_power_of_two_exponent));
453458

454459
if (effective_buffer_size != sinsp_buffer_size_) {
455460
CLOG(INFO) << "Use modified ringbuf size of "

collector/test/CollectorConfigTest.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,22 @@ TEST(CollectorConfigTest, TestSinspCpuPerBufferAdjusted) {
6969
config.MockSetHostConfig(&hconfig);
7070
EXPECT_EQ(8 * 1024 * 1024, config.GetSinspBufferSize());
7171

72-
// High number of CPUs, adjusted value, aligned to MiB
72+
// High number of CPUs, adjusted value to power of 2
7373
hconfig.SetNumPossibleCPUs(150);
7474
config.MockSetHostConfig(&hconfig);
75-
EXPECT_EQ(3 * 1024 * 1024, config.GetSinspBufferSize());
75+
EXPECT_EQ(2 * 1024 * 1024, config.GetSinspBufferSize());
7676

77-
// Extreme number of CPUs, adjusted value, aligned to KiB
77+
// Extreme number of CPUs, adjusted value to power of 2
7878
hconfig.SetNumPossibleCPUs(1024);
7979
config.MockSetHostConfig(&hconfig);
8080
EXPECT_EQ(512 * 1024, config.GetSinspBufferSize());
81+
82+
// Extreme number of CPUs and low total buffer size, adjusted value is not
83+
// less than one page
84+
config.MockSetSinspTotalBufferSize(512 * 1024);
85+
hconfig.SetNumPossibleCPUs(1024);
86+
config.MockSetHostConfig(&hconfig);
87+
EXPECT_EQ(16384, config.GetSinspBufferSize());
8188
}
8289

8390
} // namespace collector

docs/references.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ special, it instructs sinsp to allocate only one buffer no matter how many CPUs
6767
are there. This parameter affects CO-RE BPF only.
6868

6969
* `ROX_COLLECTOR_SINSP_BUFFER_SIZE`: Specifies the size of a sinsp buffer in
70-
bytes. The default value is 16 MB.
70+
bytes. The default value is 8MB. This value must be a power of 2, a multiple
71+
of the system page size and greater than `2 * page_size`.
7172

7273
* `ROX_COLLECTOR_SINSP_TOTAL_BUFFER_SIZE`: Specifies the allowed total size of
7374
all sinsp buffer in bytes. If the actual value will be larger than that due to
@@ -87,7 +88,7 @@ is quite verbose. The default is true.
8788

8889
* `ROX_COLLECTOR_INTROSPECTION_ENABLE`: Enable the introspection API and publish the
8990
corresponding endpoints. With this API, it is possible to dump some of the
90-
internal state of Collector. Refer to the
91+
internal state of Collector. Refer to the
9192
[troubleshooting](troubleshooting.md#introspection-endpoints) section for more details.
9293
The default is false.
9394

0 commit comments

Comments
 (0)