1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
################################################################################
# BSD LICENSE
#
# Copyright(c) 2019-2023 Intel Corporation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Intel Corporation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
"""
The module defines PqosCpuInfo which can be used to read CPU information
like number of cores, L2/L3 cache ID etc.
"""
from __future__ import absolute_import, division, print_function
import ctypes
from pqos.common import pqos_handle_error, free_memory
from pqos.native_struct import CPqosCpuInfo, CPqosCoreInfo
from pqos.error import PqosError, PqosErrorParam, PqosErrorResource
from pqos.pqos import Pqos
class PqosCoreInfo(object):
"Core information"
# pylint: disable=too-few-public-methods, too-many-arguments
def __init__(self, core, socket, l3_id, l2_id, l3cat_id, mba_id):
self.core = core
self.socket = socket
self.l3_id = l3_id
self.l2_id = l2_id
self.l3cat_id = l3cat_id
self.mba_id = mba_id
class PqosCacheInfo(object):
"Cache information"
# pylint: disable=too-few-public-methods, too-many-arguments
def __init__(self, num_ways, num_sets, num_partitions, line_size, way_size, total_size):
self.num_ways = num_ways
self.num_sets = num_sets
self.num_partitions = num_partitions
self.line_size = line_size
self.total_size = total_size
self.way_size = way_size
def _get_array_items(count, p_items):
"""
Converts ctypes array (given a pointer to the first element
and number of elements) to a list.
Parameters:
count: number of elements
p_items: a pointer to the first element of the array
Returns:
a list of elements
"""
if not count:
return []
items = [p_items[i] for i in range(count)]
return items
class PqosCpuInfo(object):
"PQoS CPU information"
def __init__(self):
self.pqos = Pqos()
self.p_cpu = ctypes.POINTER(CPqosCpuInfo)()
ret = self.pqos.lib.pqos_cap_get(None, ctypes.byref(self.p_cpu))
pqos_handle_error('pqos_cap_get', ret)
def _call_func_array(self, func, arg=None, use_arg=False):
"""
Calls a function from PQoS library and returns the result as a list of
integers.
Parameters:
func: a function from PQoS library
arg: a function argument
use_arg: if True then func will be invoked with arg as an argument
Returns:
a list of integers
"""
count = ctypes.c_uint(0)
count_ref = ctypes.byref(count)
func.restype = ctypes.POINTER(ctypes.c_uint)
if use_arg:
p_items = func(self.p_cpu, arg, count_ref)
else:
p_items = func(self.p_cpu, count_ref)
if not p_items:
return []
items = [p_items[i] for i in range(count.value)] if count.value > 0 else []
free_memory(p_items)
return items
def _call_func_ref(self, func, arg):
"""
Calls a function from PQoS library, handles errors and returns
an integer set by the called function.
Parameters:
func: a function from PQoS library
arg: a function argument
Returns:
an integer set by the called function
"""
result = ctypes.c_uint(0)
result_ref = ctypes.byref(result)
ret = func(self.p_cpu, arg, result_ref)
pqos_handle_error(func.__name__, ret)
return result.value
def get_vendor(self):
"""
Retrieves CPU vendor information from CPU info structure
Returns:
CPU vendor
"""
func = self.pqos.lib.pqos_get_vendor
func.restype = ctypes.c_int
vendor = func(self.p_cpu)
if vendor == CPqosCpuInfo.PQOS_VENDOR_INTEL:
return "INTEL"
if vendor == CPqosCpuInfo.PQOS_VENDOR_AMD:
return "AMD"
return "UNKNOWN"
def get_sockets(self):
"""
Retrieves socket IDs from CPU info structure.
Returns:
a list of socket IDs
"""
return self._call_func_array(self.pqos.lib.pqos_cpu_get_sockets)
def get_l2ids(self):
"""
Retrieves L2 IDs from CPU info structure.
Returns:
a list of L2 IDs
"""
return self._call_func_array(self.pqos.lib.pqos_cpu_get_l2ids)
def get_cores_l3id(self, l3_id):
"""
Creates a list of cores belonging to a given L3 cluster.
Parameters:
l3_id: L3 cluster ID
Returns:
a list of cores
"""
return self._call_func_array(self.pqos.lib.pqos_cpu_get_cores_l3id,
l3_id, use_arg=True)
def get_cores(self, socket):
"""
Retrieves core IDs from CPU info structure for a socket.
Parameters:
socket: socket ID
Returns:
a list of cores
"""
return self._call_func_array(self.pqos.lib.pqos_cpu_get_cores,
socket, use_arg=True)
def get_core_info(self, core):
"""
Retrieves core information from CPU info structure for a core.
Parameters:
core: core ID
Returns:
core information
"""
restype = ctypes.POINTER(CPqosCoreInfo)
self.pqos.lib.pqos_cpu_get_core_info.restype = restype
p_coreinfo = self.pqos.lib.pqos_cpu_get_core_info(self.p_cpu, core)
if not p_coreinfo:
raise PqosError('Core information not found')
coreinfo_struct = p_coreinfo.contents
coreinfo = PqosCoreInfo(core=coreinfo_struct.lcore,
socket=coreinfo_struct.socket,
l3_id=coreinfo_struct.l3_id,
l2_id=coreinfo_struct.l2_id,
l3cat_id=coreinfo_struct.l3cat_id,
mba_id=coreinfo_struct.mba_id)
return coreinfo
def get_cache_info(self, level):
"""
Retrieves cache information from CPU info structure
Parameters:
level: cache level
Returns:
cache information
"""
info = None
if level == 2:
info = self.p_cpu.contents.l2
if not info.detected:
raise PqosErrorResource("L2 cache not available in the system")
elif level == 3:
info = self.p_cpu.contents.l3
if not info.detected:
raise PqosErrorResource("L3 cache not available in the system")
else:
raise PqosErrorParam("Invalid cache level")
return PqosCacheInfo(num_ways=info.num_ways,
num_sets=info.num_sets,
num_partitions=info.num_partitions,
line_size=info.line_size,
way_size=info.way_size,
total_size=info.total_size)
def get_one_core(self, socket):
"""
Retrieves one core ID from CPU info structure for a socket.
Parameters:
socket: socket ID
Returns:
core ID
"""
return self._call_func_ref(self.pqos.lib.pqos_cpu_get_one_core, socket)
def get_one_by_l2id(self, l2_id):
"""
Retrieves one core ID from CPU info structure for L2 ID.
Parameters:
l2_id: L2 ID
Returns:
core ID
"""
return self._call_func_ref(self.pqos.lib.pqos_cpu_get_one_by_l2id,
l2_id)
def check_core(self, core):
"""
Verifies if a specified core is a valid logical core ID.
Parameters:
core: core ID
Returns:
True/False a given core number is valid/invalid
"""
ret = self.pqos.lib.pqos_cpu_check_core(self.p_cpu, core)
return ret == 0
def get_socketid(self, core):
"""
Retrieves socket ID for given logical core ID.
Parameters:
core: core ID
Returns:
socket ID
"""
return self._call_func_ref(self.pqos.lib.pqos_cpu_get_socketid, core)
def get_clusterid(self, core):
"""
Retrieves monitoring cluster ID for given logical core ID.
Parameters:
core: core ID
Returns:
cluster ID
"""
return self._call_func_ref(self.pqos.lib.pqos_cpu_get_clusterid, core)
|