Skip to content

Commit 0b1b3f8

Browse files
committed
Split main CMake into multiple smaller ones and add CMake options to disable checking for specific sampling backends.
1 parent f8117ba commit 0b1b3f8

6 files changed

Lines changed: 242 additions & 149 deletions

File tree

CMakeLists.txt

Lines changed: 18 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_SAMPLING_INTERVAL=${HW
6262

6363
# install fmt as dependency
6464
include(FetchContent)
65-
set(HWS_fmt_VERSION 11.0.2)
66-
find_package(fmt 11.0.2 QUIET)
65+
set(HWS_fmt_VERSION 11.1.4)
66+
find_package(fmt 11.1.4 QUIET)
6767
if (fmt_FOUND)
6868
message(STATUS "Found package fmt.")
6969
else ()
@@ -102,97 +102,10 @@ configure_file(
102102
####################################################################################################################
103103
## CPU measurements ##
104104
####################################################################################################################
105-
## check whether lscpu could be found -> used for the CPU targets as well as for ALL host measurements
106-
## -> checked even if no CPU targets where provided
107-
## LINUX only
108-
find_program(HWS_LSCPU_FOUND lscpu)
109-
if (HWS_LSCPU_FOUND)
110-
message(STATUS "Enable sampling of CPU information using lscpu.")
111-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_LSCPU_ENABLED)
112-
endif ()
113-
114-
## check whether free could be found -> used for the CPU targets as well as for ALL host measurements
115-
## -> checked even if no CPU targets where provided
116-
## LINUX only
117-
find_program(HWS_FREE_FOUND free)
118-
if (HWS_FREE_FOUND)
119-
message(STATUS "Enable sampling of CPU information using free.")
120-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_FREE_ENABLED)
121-
endif ()
122-
123-
## check whether turbostat could be found -> used for the CPU targets as well as for ALL host measurements
124-
## -> checked even if no CPU targets where provided
125-
find_program(HWS_TURBOSTAT_FOUND turbostat)
126-
if (HWS_TURBOSTAT_FOUND)
127-
## check if the turbostat command works as intended
128-
execute_process(COMMAND turbostat -n 1 -S -q
129-
RESULT_VARIABLE HWS_TURBOSTAT_WITHOUT_ROOT
130-
OUTPUT_QUIET
131-
ERROR_QUIET)
132-
if (NOT HWS_TURBOSTAT_WITHOUT_ROOT EQUAL 0)
133-
message(STATUS "Can't use turbostat without root permissions! Try using with sudo...")
134-
execute_process(COMMAND sudo -n turbostat -n 1 -i 0.001 -S -q
135-
RESULT_VARIABLE HWS_TURBOSTAT_ROOT_PASSWORD_REQUIRED
136-
OUTPUT_QUIET
137-
ERROR_QUIET)
138-
if (NOT HWS_TURBOSTAT_ROOT_PASSWORD_REQUIRED EQUAL 0)
139-
message(STATUS "Can't use turbostat with root if a sudo password is required! Please add turbostat to the sudoer group.")
140-
message(STATUS "Disabling turbostat support!")
141-
else ()
142-
execute_process(COMMAND sudo turbostat -n 1 -i 0.001 -S -q
143-
RESULT_VARIABLE HWS_TURBOSTAT_WITH_ROOT
144-
OUTPUT_QUIET
145-
ERROR_QUIET)
146-
if (NOT HWS_TURBOSTAT_WITH_ROOT EQUAL 0)
147-
message(STATUS "Can't use turbostat with root even if no sudo password is required!")
148-
message(STATUS "Disabling turbostat support!")
149-
else ()
150-
message(STATUS "Enable sampling of CPU information using turbostat.")
151-
152-
set(HWS_TURBOSTAT_EXECUTION_TYPE "root")
153-
# add compile definitions
154-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_TURBOSTAT_ENABLED)
155-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_TURBOSTAT_ROOT)
156-
endif ()
157-
endif ()
158-
else ()
159-
set(HWS_TURBOSTAT_EXECUTION_TYPE "without_root")
160-
# add compile definitions
161-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_TURBOSTAT_ENABLED)
162-
endif ()
163-
endif ()
164-
165-
## check if the CPU hardware tracker can be used
166-
if (HWS_LSCPU_FOUND OR HWS_FREE_FOUND OR HWS_TURBOSTAT_EXECUTION_TYPE)
167-
## try finding subprocess.h
168-
set(HWS_subprocess_VERSION b6e1611d430e3019c423d2af26bb162e7ed5c3ae)
169-
find_package(subprocess QUIET)
170-
if (subprocess_FOUND)
171-
message(STATUS "Found package subprocess.h.")
172-
target_include_directories(${HWS_LIBRARY_NAME} PRIVATE ${subprocess_INCLUDE_DIR})
173-
else ()
174-
include(FetchContent)
175-
message(STATUS "Couldn't find package subprocess.h. Building version ${HWS_subprocess_VERSION} from source.")
176-
# fetch subprocess library subprocess.h
177-
FetchContent_Declare(subprocess
178-
GIT_REPOSITORY https://github.com/sheredom/subprocess.h.git
179-
GIT_TAG ${HWS_subprocess_VERSION}
180-
QUIET
181-
)
182-
FetchContent_MakeAvailable(subprocess)
183-
target_include_directories(${HWS_LIBRARY_NAME} PRIVATE $<BUILD_INTERFACE:${subprocess_SOURCE_DIR}>)
184-
endif ()
185-
186-
# add source file to source file list
187-
target_sources(${HWS_LIBRARY_NAME} PRIVATE
188-
$<BUILD_INTERFACE:
189-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/cpu/hardware_sampler.cpp;
190-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/cpu/cpu_samples.cpp;
191-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/cpu/utility.cpp;
192-
>)
193-
194-
# add compile definitions
195-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_FOR_CPUS_ENABLED)
105+
set(HWS_ENABLE_CPU_SAMPLING AUTO CACHE STRING "Enable sampling of CPUs.")
106+
set_property(CACHE HWS_ENABLE_CPU_SAMPLING PROPERTY STRINGS AUTO ON OFF)
107+
if (HWS_ENABLE_CPU_SAMPLING MATCHES "AUTO" OR HWS_ENABLE_CPU_SAMPLING)
108+
add_subdirectory(src/hws/cpu)
196109
else ()
197110
message(STATUS "Hardware sampling for CPUs disabled!")
198111
endif ()
@@ -201,23 +114,10 @@ endif ()
201114
####################################################################################################################
202115
## NVIDIA GPU sampling via NVML ##
203116
####################################################################################################################
204-
# find libraries necessary for NVML and link against them
205-
find_package(CUDAToolkit QUIET)
206-
if (CUDAToolkit_FOUND)
207-
target_link_libraries(${HWS_LIBRARY_NAME} PRIVATE CUDA::nvml CUDA::cudart)
208-
209-
message(STATUS "Enable sampling of NVIDIA GPU information using NVML.")
210-
211-
# add source file to source file list
212-
target_sources(${HWS_LIBRARY_NAME} PRIVATE
213-
$<BUILD_INTERFACE:
214-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_nvidia/hardware_sampler.cpp;
215-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_nvidia/nvml_samples.cpp;
216-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_nvidia/utility.cpp
217-
>)
218-
219-
# add compile definition
220-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_FOR_NVIDIA_GPUS_ENABLED)
117+
set(HWS_ENABLE_GPU_NVIDIA_SAMPLING AUTO CACHE STRING "Enable sampling of NVIDIA GPUs.")
118+
set_property(CACHE HWS_ENABLE_GPU_NVIDIA_SAMPLING PROPERTY STRINGS AUTO ON OFF)
119+
if (HWS_ENABLE_GPU_NVIDIA_SAMPLING MATCHES "AUTO" OR HWS_ENABLE_GPU_NVIDIA_SAMPLING)
120+
add_subdirectory(src/hws/gpu_nvidia)
221121
else ()
222122
message(STATUS "Hardware sampling for NVIDIA GPUs disabled!")
223123
endif ()
@@ -226,25 +126,10 @@ endif ()
226126
####################################################################################################################
227127
## AMD GPU sampling via ROCm SMI lib ##
228128
####################################################################################################################
229-
## try finding ROCm SMI
230-
find_package(rocm_smi QUIET)
231-
if (rocm_smi_FOUND)
232-
find_package(HIP REQUIRED)
233-
target_link_libraries(${HWS_LIBRARY_NAME} PRIVATE -lrocm_smi64 hip::host)
234-
target_include_directories(${HWS_LIBRARY_NAME} PRIVATE ${ROCM_SMI_INCLUDE_DIR})
235-
236-
message(STATUS "Enable sampling of AMD GPU information using ROCm SMI.")
237-
238-
# add source file to source file list
239-
target_sources(${HWS_LIBRARY_NAME} PRIVATE
240-
$<BUILD_INTERFACE:
241-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_amd/hardware_sampler.cpp;
242-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_amd/rocm_smi_samples.cpp;
243-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_amd/utility.cpp
244-
>)
245-
246-
# add compile definition
247-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_FOR_AMD_GPUS_ENABLED)
129+
set(HWS_ENABLE_GPU_AMD_SAMPLING AUTO CACHE STRING "Enable sampling of AMD GPUs.")
130+
set_property(CACHE HWS_ENABLE_GPU_AMD_SAMPLING PROPERTY STRINGS AUTO ON OFF)
131+
if (HWS_ENABLE_GPU_AMD_SAMPLING MATCHES "AUTO" OR HWS_ENABLE_GPU_AMD_SAMPLING)
132+
add_subdirectory(src/hws/gpu_amd)
248133
else ()
249134
message(STATUS "Hardware sampling for AMD GPUs disabled!")
250135
endif ()
@@ -253,26 +138,10 @@ endif ()
253138
####################################################################################################################
254139
## Intel GPU sampling via Level Zero ##
255140
####################################################################################################################
256-
# set the CMAKE_MODULE_PATH to the cmake directory
257-
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
258-
259-
# try finding Level Zero
260-
find_package(level_zero QUIET)
261-
if (level_zero_FOUND)
262-
target_link_libraries(${HWS_LIBRARY_NAME} PRIVATE level_zero)
263-
264-
message(STATUS "Enable sampling of Intel GPU information using Level Zero.")
265-
266-
# add source file to source file list
267-
target_sources(${HWS_LIBRARY_NAME} PRIVATE
268-
$<BUILD_INTERFACE:
269-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_intel/hardware_sampler.cpp;
270-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_intel/level_zero_samples.cpp;
271-
${CMAKE_CURRENT_SOURCE_DIR}/src/hws/gpu_intel/utility.cpp
272-
>)
273-
274-
# add compile definition
275-
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_FOR_INTEL_GPUS_ENABLED)
141+
set(HWS_ENABLE_GPU_INTEL_SAMPLING AUTO CACHE STRING "Enable sampling of Intel GPUs.")
142+
set_property(CACHE HWS_ENABLE_GPU_INTEL_SAMPLING PROPERTY STRINGS AUTO ON OFF)
143+
if (HWS_ENABLE_GPU_INTEL_SAMPLING MATCHES "AUTO" OR HWS_ENABLE_GPU_INTEL_SAMPLING)
144+
add_subdirectory(src/hws/gpu_intel)
276145
else ()
277146
message(STATUS "Hardware sampling for Intel GPUs disabled!")
278147
endif ()

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ cmake --build . -j
5050

5151
The `[optional_options]` can be one or multiple of:
5252

53+
- `HWS_ENABLE_CPU_SAMPLING=ON|OFF|AUTO` (default: `AUTO`):
54+
- `ON`: check whether CPU information can be sampled and fail if this is not the case
55+
- `AUTO`: check whether CPU information can be sampled but **do not** fail if this is not the case
56+
- `OFF`: do not check whether CPU information can be sampled
57+
58+
- `HWS_ENABLE_GPU_NVIDIA_SAMPLING=ON|OFF|AUTO` (default: `AUTO`):
59+
- `ON`: check whether NVIDIA GPU information can be sampled and fail if this is not the case
60+
- `AUTO`: check whether NVIDIA GPU information can be sampled but **do not** fail if this is not the case
61+
- `OFF`: do not check whether NVIDIA GPU information can be sampled
62+
63+
- `HWS_ENABLE_GPU_AMD_SAMPLING=ON|OFF|AUTO` (default: `AUTO`):
64+
- `ON`: check whether AMD GPU information can be sampled and fail if this is not the case
65+
- `AUTO`: check whether AMD GPU information can be sampled but **do not** fail if this is not the case
66+
- `OFF`: do not check whether AMD GPU information can be sampled
67+
68+
- `HWS_ENABLE_GPU_INTEL_SAMPLING=ON|OFF|AUTO` (default: `AUTO`):
69+
- `ON`: check whether Intel GPU information can be sampled and fail if this is not the case
70+
- `AUTO`: check whether Intel GPU information can be sampled but **do not** fail if this is not the case
71+
- `OFF`: do not check whether Intel GPU information can be sampled
72+
5373
- `HWS_ENABLE_ERROR_CHECKS=ON|OFF` (default: `OFF`): enable sanity checks during hardware sampling, may be problematic
5474
with smaller sample intervals
5575
- `HWS_SAMPLING_INTERVAL=100ms` (default: `100ms`): set the sampling interval in milliseconds

src/hws/cpu/CMakeLists.txt

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
## Authors: Marcel Breyer
2+
## Copyright (C): 2024-today All Rights Reserved
3+
## License: This file is released under the MIT license.
4+
## See the LICENSE.md file in the project root for full license information.
5+
########################################################################################################################
6+
7+
## check whether lscpu could be found -> used for the CPU targets as well as for ALL host measurements
8+
## -> checked even if no CPU targets where provided
9+
## LINUX only
10+
find_program(HWS_LSCPU_FOUND lscpu)
11+
if (HWS_LSCPU_FOUND)
12+
message(STATUS "Enable sampling of CPU information using lscpu.")
13+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_LSCPU_ENABLED)
14+
endif ()
15+
16+
## check whether free could be found -> used for the CPU targets as well as for ALL host measurements
17+
## -> checked even if no CPU targets where provided
18+
## LINUX only
19+
find_program(HWS_FREE_FOUND free)
20+
if (HWS_FREE_FOUND)
21+
message(STATUS "Enable sampling of CPU information using free.")
22+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_FREE_ENABLED)
23+
endif ()
24+
25+
## check whether turbostat could be found -> used for the CPU targets as well as for ALL host measurements
26+
## -> checked even if no CPU targets where provided
27+
find_program(HWS_TURBOSTAT_FOUND turbostat)
28+
if (HWS_TURBOSTAT_FOUND)
29+
## check if the turbostat command works as intended
30+
execute_process(COMMAND turbostat -n 1 -S -q
31+
RESULT_VARIABLE HWS_TURBOSTAT_WITHOUT_ROOT
32+
OUTPUT_QUIET
33+
ERROR_QUIET)
34+
if (NOT HWS_TURBOSTAT_WITHOUT_ROOT EQUAL 0)
35+
message(STATUS "Can't use turbostat without root permissions! Try using with sudo...")
36+
execute_process(COMMAND sudo -n turbostat -n 1 -i 0.001 -S -q
37+
RESULT_VARIABLE HWS_TURBOSTAT_ROOT_PASSWORD_REQUIRED
38+
OUTPUT_QUIET
39+
ERROR_QUIET)
40+
if (NOT HWS_TURBOSTAT_ROOT_PASSWORD_REQUIRED EQUAL 0)
41+
message(STATUS "Can't use turbostat with root if a sudo password is required! Please add turbostat to the sudoer group.")
42+
message(STATUS "Disabling turbostat support!")
43+
else ()
44+
execute_process(COMMAND sudo turbostat -n 1 -i 0.001 -S -q
45+
RESULT_VARIABLE HWS_TURBOSTAT_WITH_ROOT
46+
OUTPUT_QUIET
47+
ERROR_QUIET)
48+
if (NOT HWS_TURBOSTAT_WITH_ROOT EQUAL 0)
49+
message(STATUS "Can't use turbostat with root even if no sudo password is required!")
50+
message(STATUS "Disabling turbostat support!")
51+
else ()
52+
message(STATUS "Enable sampling of CPU information using turbostat.")
53+
54+
set(HWS_TURBOSTAT_EXECUTION_TYPE "root")
55+
# add compile definitions
56+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_TURBOSTAT_ENABLED)
57+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_TURBOSTAT_ROOT)
58+
endif ()
59+
endif ()
60+
else ()
61+
set(HWS_TURBOSTAT_EXECUTION_TYPE "without_root")
62+
# add compile definitions
63+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_VIA_TURBOSTAT_ENABLED)
64+
endif ()
65+
endif ()
66+
67+
# check of any CPU related utility could be found
68+
if (NOT (HWS_LSCPU_FOUND OR HWS_FREE_FOUND OR HWS_TURBOSTAT_EXECUTION_TYPE))
69+
if (HWS_ENABLE_CPU_SAMPLING MATCHES "ON")
70+
message(SEND_ERROR "Cannot find any CPU utility program but CPU sampling was explicitly requested!")
71+
endif ()
72+
return()
73+
endif ()
74+
message(STATUS "Enable sampling of CPU information.")
75+
76+
## try finding subprocess.h
77+
set(HWS_subprocess_VERSION b6e1611d430e3019c423d2af26bb162e7ed5c3ae)
78+
find_package(subprocess QUIET)
79+
if (subprocess_FOUND)
80+
message(STATUS "Found package subprocess.h.")
81+
target_include_directories(${HWS_LIBRARY_NAME} PRIVATE ${subprocess_INCLUDE_DIR})
82+
else ()
83+
include(FetchContent)
84+
message(STATUS "Couldn't find package subprocess.h. Building version ${HWS_subprocess_VERSION} from source.")
85+
# fetch subprocess library subprocess.h
86+
FetchContent_Declare(subprocess
87+
GIT_REPOSITORY https://github.com/sheredom/subprocess.h.git
88+
GIT_TAG ${HWS_subprocess_VERSION}
89+
QUIET
90+
)
91+
FetchContent_MakeAvailable(subprocess)
92+
target_include_directories(${HWS_LIBRARY_NAME} PRIVATE $<BUILD_INTERFACE:${subprocess_SOURCE_DIR}>)
93+
endif ()
94+
95+
# add source file to source file list
96+
target_sources(${HWS_LIBRARY_NAME} PRIVATE
97+
$<BUILD_INTERFACE:
98+
${CMAKE_CURRENT_SOURCE_DIR}/hardware_sampler.cpp;
99+
${CMAKE_CURRENT_SOURCE_DIR}/cpu_samples.cpp;
100+
${CMAKE_CURRENT_SOURCE_DIR}/utility.cpp;
101+
>)
102+
103+
# add compile definitions
104+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_FOR_CPUS_ENABLED)

src/hws/gpu_amd/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Authors: Marcel Breyer
2+
## Copyright (C): 2024-today All Rights Reserved
3+
## License: This file is released under the MIT license.
4+
## See the LICENSE.md file in the project root for full license information.
5+
########################################################################################################################
6+
7+
# try finding ROCm SMI
8+
find_package(rocm_smi QUIET)
9+
10+
# check if ROCm SMI could be found
11+
if (NOT rocm_smi_FOUND)
12+
if (HWS_ENABLE_GPU_AMD_SAMPLING MATCHES "ON")
13+
message(SEND_ERROR "Cannot find ROCm SMI but AMD GPU sampling was explicitly requested!")
14+
endif ()
15+
return()
16+
endif ()
17+
message(STATUS "Enable sampling of AMD GPU information using ROCm SMI.")
18+
19+
# must also find HIP
20+
find_package(HIP REQUIRED)
21+
22+
# link against necessary libraries
23+
target_link_libraries(${HWS_LIBRARY_NAME} PRIVATE -lrocm_smi64 hip::host)
24+
target_include_directories(${HWS_LIBRARY_NAME} PRIVATE ${ROCM_SMI_INCLUDE_DIR})
25+
26+
# add source file to source file list
27+
target_sources(${HWS_LIBRARY_NAME} PRIVATE
28+
$<BUILD_INTERFACE:
29+
${CMAKE_CURRENT_SOURCE_DIR}/hardware_sampler.cpp;
30+
${CMAKE_CURRENT_SOURCE_DIR}/rocm_smi_samples.cpp;
31+
${CMAKE_CURRENT_SOURCE_DIR}/utility.cpp
32+
>)
33+
34+
# add compile definition
35+
target_compile_definitions(${HWS_LIBRARY_NAME} PUBLIC HWS_FOR_AMD_GPUS_ENABLED)

0 commit comments

Comments
 (0)