]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/tests/functional/pyocf/types/shared.py
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / ocf / tests / functional / pyocf / types / shared.py
1 #
2 # Copyright(c) 2019 Intel Corporation
3 # SPDX-License-Identifier: BSD-3-Clause-Clear
4 #
5
6 import logging
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
10
11 from ..utils import Size as S
12
13
14 class OcfErrorCode(IntEnum):
15 OCF_ERR_INVAL = 1000000
16 OCF_ERR_INTR = auto()
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()
42
43
44 class OcfCompletion:
45 """
46 This class provides Completion mechanism for interacting with OCF async
47 management API.
48 """
49
50 def __init__(self, completion_args: list):
51 """
52 Provide ctypes arg list, and optionally index of status argument in
53 completion function which will be extracted (default - last argument).
54
55 :param completion_args: list of tuples (parameter name, parameter type)
56 for OCF completion function
57 """
58 self.e = Event()
59 self.completion_args = completion_args
60 self._as_parameter_ = self.callback
61 self.results = None
62
63 @property
64 def callback(self):
65 arg_types = list(list(zip(*self.completion_args))[1])
66
67 @CFUNCTYPE(c_void_p, *arg_types)
68 def complete(*args):
69 self.results = {}
70 for i, arg in enumerate(args):
71 self.results[self.completion_args[i][0]] = arg
72 self.e.set()
73
74 return complete
75
76 def wait(self):
77 self.e.wait()
78
79
80 class OcfError(BaseException):
81 def __init__(self, msg, error_code):
82 super().__init__(self, msg)
83 self.error_code = OcfErrorCode(abs(error_code))
84 self.msg = msg
85
86 def __str__(self):
87 return "{} ({})".format(self.msg, repr(self.error_code))
88
89
90 class SharedOcfObject(Structure):
91 _instances_ = {}
92
93 def __init__(self):
94 super().__init__()
95 type(self)._instances_[self._as_parameter_] = self
96
97 @classmethod
98 def get_instance(cls, ref: int):
99 try:
100 return cls._instances_[ref]
101 except:
102 logging.getLogger("pyocf").error(
103 "OcfSharedObject corruption. wanted: {} instances: {}".format(
104 ref, cls._instances_
105 )
106 )
107 return None
108
109 @classmethod
110 def del_object(cls, ref: int):
111 del cls._instances_[ref]
112
113
114 class Uuid(Structure):
115 _fields_ = [("_size", c_size_t), ("_data", c_char_p)]
116
117
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)
124 DEFAULT = LINE_4KiB
125
126
127 class SeqCutOffPolicy(IntEnum):
128 ALWAYS = 0
129 FULL = 1
130 NEVER = 2
131 DEFAULT = FULL
132
133
134 class CacheLines(S):
135 def __init__(self, count: int, line_size: CacheLineSize):
136 self.bytes = count * line_size
137 self.line_size = line_size
138
139 def __int__(self):
140 return int(self.bytes / self.line_size)