Skip to content

Commit c51e45f

Browse files
Nathan Bakerpoiana
authored andcommitted
Added conditional compilation to avoid building Linux code on non-Linux systems
Signed-off-by: Nathan Baker <[email protected]>
1 parent db969a4 commit c51e45f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

userspace/libsinsp/examples/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515

1616
include(jsoncpp)
1717

18-
add_executable(sinsp-example util.cpp test.cpp cpu_usage.cpp)
18+
set(sources util.cpp test.cpp)
19+
20+
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
21+
set(sources ${sources} cpu_usage.cpp)
22+
endif()
23+
24+
add_executable(sinsp-example ${sources})
1925

2026
target_link_libraries(sinsp-example sinsp)
2127

userspace/libsinsp/examples/test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ extern "C" {
4343

4444
using namespace std;
4545

46+
#if __linux__
4647
// Utility function to calculate CPU usage
4748
int get_cpu_usage_percent();
49+
#endif // __linux__
4850

4951
// Functions used for dumping to stdout
5052
void raw_dump(sinsp&, sinsp_evt* ev);
@@ -547,6 +549,7 @@ int main(int argc, char** argv) {
547549
++num_events;
548550
uint64_t evt_diff = num_events - last_events;
549551
if(perftest) {
552+
#if __linux__
550553
// Perftest mode does not print individual events but instead prints a running throughput every second
551554
if(ts_ns - last_ts_ns > 1'000'000'000) {
552555
int cpu_usage = get_cpu_usage_percent();
@@ -560,6 +563,18 @@ int main(int argc, char** argv) {
560563
}
561564
last_ts_ns = ts_ns;
562565
last_events = num_events;
566+
#else // __linux__
567+
if(ts_ns - last_ts_ns > 1'000'000'000) {
568+
++num_samples;
569+
long double curr_throughput = evt_diff / (long double)1000;
570+
std::cout << "Events: " << (num_events - last_events) << " Events/ms: " << curr_throughput
571+
<< " \r" << std::flush;
572+
if(curr_throughput > max_throughput) {
573+
max_throughput = curr_throughput;
574+
}
575+
last_ts_ns = ts_ns;
576+
last_events = num_events;
577+
#endif // __linux__
563578
}
564579
} else if(!thread || g_all_threads || thread->is_main_thread()) {
565580
dump(inspector, ev);

0 commit comments

Comments
 (0)