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
|
################################################################################
# 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.
################################################################################
"""
Common module which defines functions used in other modules.
"""
from __future__ import absolute_import, division, print_function
import ctypes
import ctypes.util
from pqos.error import ERRORS, PqosError
def pqos_handle_error(func_name, retval, expected=0):
"""
Handles errors from PQoS library, raises a relevant exception
if the returned error code is different than the expected one.
"""
if retval == expected:
return
pqos_error_cls = ERRORS.get(retval, PqosError)
err = pqos_error_cls(f'{func_name} returned {retval}', retval)
raise err
def get_mask_int(mask):
"Returns a bitmask as an integer."
if isinstance(mask, type('')):
if mask.lower().startswith('0x'):
return int(mask.lower(), 16)
return int(mask)
if isinstance(mask, int):
return mask
if mask is None:
return 0
raise ValueError('Please specify mask as either a string, an integer or None')
class COSBase(object):
"Cache allocation class of service configuration"
def __init__(self, class_id, mask=None, code_mask=None, data_mask=None):
"""
Initializes cache allocation COS configuration object.
Parameters:
class_id: a class of service
mask: a bitmask (an integer or a string starting from '0x')
representing used cache ways or None, if None is given,
then code_mask and data_mask must be set (default None)
code_mask: a bitmask (an integer or a string starting from '0x')
representing used cache ways for code or None,
if None is given, then mask must be set
(default None)
data_mask: a bitmask (an integer or a string starting from '0x')
representing used cache ways for data or None,
if None is given, then mask must be set
(default None)
"""
if not mask and (not code_mask or not data_mask):
raise ValueError('Please specify mask or code mask and data mask')
self.class_id = class_id
self.mask = get_mask_int(mask)
self.code_mask = get_mask_int(code_mask)
self.data_mask = get_mask_int(data_mask)
self.cdp = bool(code_mask is not None or \
data_mask is not None)
def __repr__(self):
return f'COS(class_id={self.class_id}, mask={repr(self.mask)}, ' \
f'code_mask={repr(self.code_mask)}, data_mask={repr(self.data_mask)})'
def convert_from_cos(cos, cls):
"Creates ctypes COS object from COS object."
ctypes_cos = cls(class_id=cos.class_id, cdp=int(cos.cdp))
if cos.cdp:
ctypes_cos.u.s.data_mask = cos.data_mask
ctypes_cos.u.s.code_mask = cos.code_mask
else:
ctypes_cos.u.ways_mask = cos.mask
return ctypes_cos
def convert_to_cos(ctypes_cos, cls):
"Creates COS object from ctypes COS object."
mask = None
code_mask = None
data_mask = None
if ctypes_cos.cdp:
code_mask = ctypes_cos.u.s.code_mask
data_mask = ctypes_cos.u.s.data_mask
else:
mask = ctypes_cos.u.ways_mask
class_id = ctypes_cos.class_id
return cls(class_id, mask, code_mask, data_mask)
def free_memory(ptr):
"Releases memory allocated by the library."
libc_path = ctypes.util.find_library('c')
if not libc_path:
raise OSError('Cannot find libc')
libc = ctypes.cdll.LoadLibrary(libc_path)
libc.free(ptr)
|