Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8aa9e04

Browse files
committedJan 22, 2025·
new(test/libscap): add hotplug tests to libscap_test suite.
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
1 parent d5454c0 commit 8aa9e04

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed
 

‎test/libscap/helpers/engines.cpp‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <gtest/gtest.h>
22
#include <syscall.h>
3-
#include <libscap/scap.h>
43
#include <errno.h>
54
#include <fcntl.h>
5+
#include "engines.h"
66

77
/* We are supposing that if we overcome this threshold, all buffers are full.
88
* Probably this threshold is too low, but it depends on the machine's workload.
@@ -205,6 +205,53 @@ void check_event_order(scap_t *h) {
205205
}
206206
#endif
207207

208+
void check_hotplug_event(scap_t *h, std::ofstream &cpu_file) {
209+
// Start capture
210+
ASSERT_EQ(scap_start_capture(h), SCAP_SUCCESS);
211+
212+
// Enable back cpu 1
213+
cpu_file.seekp(0, std::ios::beg);
214+
cpu_file << "1";
215+
cpu_file.flush();
216+
217+
// Set the affinity on first CPU
218+
cpu_set_t set, starting_set;
219+
sched_getaffinity(0, sizeof(cpu_set_t), &starting_set);
220+
221+
CPU_ZERO(&set); // clear cpu mask
222+
CPU_SET(1, &set); // set cpu 1
223+
sched_setaffinity(0, sizeof(cpu_set_t), &set);
224+
225+
// Generate some syscalls on CPU 1 to make sure we generate an event
226+
cpu_file.close();
227+
228+
// Reset affinity
229+
sched_setaffinity(0, sizeof(cpu_set_t), &starting_set);
230+
231+
ASSERT_EQ(scap_stop_capture(h), SCAP_SUCCESS);
232+
233+
scap_evt *evt = NULL;
234+
uint16_t buffer_id;
235+
uint32_t flags;
236+
bool found_hotplug = false;
237+
238+
int num_consecutive_timeouts = 0;
239+
while(num_consecutive_timeouts < 50 && !found_hotplug) {
240+
if(scap_next(h, &evt, &buffer_id, &flags) == SCAP_SUCCESS) {
241+
if(evt->type == PPME_CPU_HOTPLUG_E) {
242+
found_hotplug = true;
243+
break;
244+
}
245+
} else {
246+
num_consecutive_timeouts++;
247+
}
248+
}
249+
250+
scap_close(h);
251+
252+
ASSERT_TRUE(found_hotplug);
253+
}
254+
208255
/* Right now this is used only by the modern bpf
209256
* This is extracted from `libbpf_num_possible_cpus()`.
210257
* We avoid to include libbpf just for this helper.

‎test/libscap/helpers/engines.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include <libscap/scap.h>
2+
#include <iostream>
3+
#include <fstream>
24

35
void check_event_is_not_overwritten(scap_t* h);
46

57
void check_event_order(scap_t* h);
68

9+
void check_hotplug_event(scap_t* h, std::ofstream& cpu_file);
10+
711
int num_possible_cpus(void);

‎test/libscap/test_suites/engines/bpf/bpf.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,21 @@ TEST(bpf, metrics_v2_check_empty) {
279279
ASSERT_EQ(rc, SCAP_SUCCESS);
280280
scap_close(h);
281281
}
282+
283+
TEST(bpf, hotplug) {
284+
char error_buffer[FILENAME_MAX] = {0};
285+
int ret = 0;
286+
287+
// Disable cpu 1
288+
std::ofstream cpu_file("/sys/devices/system/cpu/cpu1/online");
289+
ASSERT_TRUE(cpu_file.is_open());
290+
cpu_file << "0";
291+
cpu_file.flush();
292+
293+
// open scap
294+
scap_t* h = open_bpf_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_BPF_PROBE_PATH);
295+
ASSERT_FALSE(!h || ret != SCAP_SUCCESS)
296+
<< "unable to open bpf engine: " << error_buffer << std::endl;
297+
298+
check_hotplug_event(h, cpu_file);
299+
}

‎test/libscap/test_suites/engines/kmod/kmod.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,21 @@ TEST(kmod, metrics_v2_check_empty) {
333333
ASSERT_EQ(rc, SCAP_SUCCESS);
334334
scap_close(h);
335335
}
336+
337+
TEST(kmod, hotplug) {
338+
char error_buffer[FILENAME_MAX] = {0};
339+
int ret = 0;
340+
341+
// Disable cpu 1
342+
std::ofstream cpu_file("/sys/devices/system/cpu/cpu1/online");
343+
ASSERT_TRUE(cpu_file.is_open());
344+
cpu_file << "0";
345+
cpu_file.flush();
346+
347+
// open scap
348+
scap_t* h = open_kmod_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_KERNEL_MODULE_PATH);
349+
ASSERT_FALSE(!h || ret != SCAP_SUCCESS)
350+
<< "unable to open kmod engine: " << error_buffer << std::endl;
351+
352+
check_hotplug_event(h, cpu_file);
353+
}

‎test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,21 @@ TEST(modern_bpf, double_metrics_v2_call) {
417417

418418
scap_close(h);
419419
}
420+
421+
TEST(modern_bpf, hotplug) {
422+
char error_buffer[FILENAME_MAX] = {0};
423+
int ret = 0;
424+
425+
// Disable cpu 1
426+
std::ofstream cpu_file("/sys/devices/system/cpu/cpu1/online");
427+
ASSERT_TRUE(cpu_file.is_open());
428+
cpu_file << "0";
429+
cpu_file.flush();
430+
431+
// open scap
432+
scap_t* h = open_modern_bpf_engine(error_buffer, &ret, 1 * 1024 * 1024, 0, true);
433+
ASSERT_FALSE(!h || ret != SCAP_SUCCESS)
434+
<< "unable to open modern bpf engine: " << error_buffer << std::endl;
435+
436+
check_hotplug_event(h, cpu_file);
437+
}

0 commit comments

Comments
 (0)
Please sign in to comment.