2 # Copyright(c) 2019 Intel Corporation
3 # SPDX-License-Identifier: BSD-3-Clause-Clear
7 from ctypes
import CFUNCTYPE
, c_size_t
, c_char_p
, Structure
, c_void_p
8 from enum
import IntEnum
, auto
9 from threading
import Event
11 from ..utils
import Size
as S
14 class OcfErrorCode(IntEnum
):
15 OCF_ERR_INVAL
= 1000000
17 OCF_ERR_NO_MEM
= auto()
18 OCF_ERR_NO_LOCK
= auto()
19 OCF_ERR_INVAL_VOLUME_TYPE
= auto()
20 OCF_ERR_UNKNOWN
= auto()
21 OCF_ERR_TOO_MANY_CACHES
= auto()
22 OCF_ERR_NO_FREE_RAM
= auto()
23 OCF_ERR_START_CACHE_FAIL
= auto()
24 OCF_ERR_CACHE_IN_USE
= auto()
25 OCF_ERR_CACHE_NOT_EXIST
= auto()
26 OCF_ERR_CACHE_EXIST
= auto()
27 OCF_ERR_TOO_MANY_CORES
= auto()
28 OCF_ERR_CORE_NOT_AVAIL
= auto()
29 OCF_ERR_NOT_OPEN_EXC
= auto()
30 OCF_ERR_CACHE_NOT_AVAIL
= auto()
31 OCF_ERR_IO_CLASS_NOT_EXIST
= auto()
32 OCF_ERR_WRITE_CACHE
= auto()
33 OCF_ERR_WRITE_CORE
= auto()
34 OCF_ERR_DIRTY_SHUTDOWN
= auto()
35 OCF_ERR_DIRTY_EXISTS
= auto()
36 OCF_ERR_FLUSHING_INTERRUPTED
= auto()
37 OCF_ERR_CANNOT_ADD_CORE_TO_POOL
= auto()
38 OCF_ERR_CACHE_IN_INCOMPLETE_STATE
= auto()
39 OCF_ERR_CORE_IN_INACTIVE_STATE
= auto()
40 OCF_ERR_INVALID_CACHE_MODE
= auto()
41 OCF_ERR_INVALID_CACHE_LINE_SIZE
= auto()
46 This class provides Completion mechanism for interacting with OCF async
50 def __init__(self
, completion_args
: list):
52 Provide ctypes arg list, and optionally index of status argument in
53 completion function which will be extracted (default - last argument).
55 :param completion_args: list of tuples (parameter name, parameter type)
56 for OCF completion function
59 self
.completion_args
= completion_args
60 self
._as
_parameter
_ = self
.callback
65 arg_types
= list(list(zip(*self
.completion_args
))[1])
67 @CFUNCTYPE(c_void_p
, *arg_types
)
70 for i
, arg
in enumerate(args
):
71 self
.results
[self
.completion_args
[i
][0]] = arg
80 class OcfError(BaseException
):
81 def __init__(self
, msg
, error_code
):
82 super().__init
__(self
, msg
)
83 self
.error_code
= OcfErrorCode(abs(error_code
))
87 return "{} ({})".format(self
.msg
, repr(self
.error_code
))
90 class SharedOcfObject(Structure
):
95 type(self
)._instances
_[self
._as
_parameter
_] = self
98 def get_instance(cls
, ref
: int):
100 return cls
._instances
_[ref
]
102 logging
.getLogger("pyocf").error(
103 "OcfSharedObject corruption. wanted: {} instances: {}".format(
110 def del_object(cls
, ref
: int):
111 del cls
._instances
_[ref
]
114 class Uuid(Structure
):
115 _fields_
= [("_size", c_size_t
), ("_data", c_char_p
)]
118 class CacheLineSize(IntEnum
):
119 LINE_4KiB
= S
.from_KiB(4)
120 LINE_8KiB
= S
.from_KiB(8)
121 LINE_16KiB
= S
.from_KiB(16)
122 LINE_32KiB
= S
.from_KiB(32)
123 LINE_64KiB
= S
.from_KiB(64)
127 class SeqCutOffPolicy(IntEnum
):
135 def __init__(self
, count
: int, line_size
: CacheLineSize
):
136 self
.bytes
= count
* line_size
137 self
.line_size
= line_size
140 return int(self
.bytes
/ self
.line_size
)