]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/tests/functional/pyocf/utils.py
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / ocf / tests / functional / pyocf / utils.py
1 #
2 # Copyright(c) 2019 Intel Corporation
3 # SPDX-License-Identifier: BSD-3-Clause-Clear
4 #
5
6 from ctypes import string_at
7
8
9 def print_buffer(buf, length, offset=0, width=16, stop_after_zeros=0):
10 end = offset + length
11 zero_lines = 0
12 buf = string_at(buf, length)
13 whole_buffer_empty = True
14 stop_after_zeros = int(stop_after_zeros / width)
15
16 for addr in range(offset, end, width):
17 cur_line = buf[addr : min(end, addr + width)]
18 byteline = ""
19 asciiline = ""
20 if not any(cur_line):
21 if stop_after_zeros and zero_lines > stop_after_zeros:
22 print(
23 "<{} bytes of empty space encountered, stopping>".format(
24 stop_after_zeros * width
25 )
26 )
27 return
28 zero_lines += 1
29 continue
30
31 if zero_lines:
32 print("<{} zero bytes omitted>".format(zero_lines * width))
33 zero_lines = 0
34
35 for byte in cur_line:
36 byte = int(byte)
37 byteline += "{:02X} ".format(byte)
38 if 31 < byte < 126:
39 char = chr(byte)
40 else:
41 char = "."
42 asciiline += char
43
44 print("0x{:08X}\t{}\t{}".format(addr, byteline, asciiline))
45 whole_buffer_empty = False
46
47 if whole_buffer_empty:
48 print("<whole buffer empty>")
49 elif zero_lines:
50 print("<zero until end>")
51
52
53 class Size:
54 _KiB = 1024
55 _MiB = _KiB * 1024
56 _GiB = _MiB * 1024
57 _TiB = _GiB * 1024
58
59 def __init__(self, b: int):
60 self.bytes = b
61
62 def __int__(self):
63 return self.bytes
64
65 def __index__(self):
66 return self.bytes
67
68 @classmethod
69 def from_B(cls, value):
70 return cls(value)
71
72 @classmethod
73 def from_KiB(cls, value):
74 return cls(value * cls._KiB)
75
76 @classmethod
77 def from_MiB(cls, value):
78 return cls(value * cls._MiB)
79
80 @classmethod
81 def from_GiB(cls, value):
82 return cls(value * cls._GiB)
83
84 @classmethod
85 def from_TiB(cls, value):
86 return cls(value * cls._TiB)
87
88 @property
89 def B(self):
90 return self.bytes
91
92 @property
93 def KiB(self):
94 return self.bytes / self._KiB
95
96 @property
97 def MiB(self):
98 return self.bytes / self._MiB
99
100 @property
101 def GiB(self):
102 return self.bytes / self._GiB
103
104 @property
105 def TiB(self):
106 return self.bytes / self._TiB
107
108 def __str__(self):
109 if self.bytes < self._KiB:
110 return "{} B".format(self.B)
111 elif self.bytes < self._MiB:
112 return "{} KiB".format(self.KiB)
113 elif self.bytes < self._GiB:
114 return "{} MiB".format(self.MiB)
115 elif self.bytes < self._TiB:
116 return "{} GiB".format(self.GiB)
117 else:
118 return "{} TiB".format(self.TiB)
119
120
121 def print_structure(struct, indent=0):
122 print(struct)
123 for field, field_type in struct._fields_:
124 value = getattr(struct, field)
125 if hasattr(value, "_fields_"):
126 print("{}{: <20} :".format(" " * indent, field))
127 print_structure(value, indent=indent + 1)
128 continue
129
130 print("{}{: <20} : {}".format(" " * indent, field, value))
131
132
133 def struct_to_dict(struct):
134 d = {}
135 for field, field_type in struct._fields_:
136 value = getattr(struct, field)
137 if hasattr(value, "_fields_"):
138 d[field] = struct_to_dict(value)
139 continue
140 d[field] = value
141
142 return d