pyocf: add sector size logic to Size class

Signed-off-by: Adam Rutkowski <adam.j.rutkowski@intel.com>
This commit is contained in:
Adam Rutkowski 2019-06-06 16:02:29 -04:00
parent 641fba1708
commit 31ab2b3fe6
3 changed files with 26 additions and 14 deletions

View File

@ -21,7 +21,7 @@ from enum import IntEnum
from hashlib import md5
import weakref
from ..utils import print_buffer
from ..utils import print_buffer, Size as S
class DataSeek(IntEnum):

View File

@ -56,9 +56,13 @@ class Size:
_MiB = _KiB * 1024
_GiB = _MiB * 1024
_TiB = _GiB * 1024
_SECTOR_SIZE = 512
def __init__(self, b: int):
self.bytes = b
def __init__(self, b: int, sector_aligned: bool = False):
if sector_aligned:
self.bytes = ((b + self._SECTOR_SIZE - 1) // self._SECTOR_SIZE) * self._SECTOR_SIZE
else:
self.bytes = b
def __int__(self):
return self.bytes
@ -67,24 +71,28 @@ class Size:
return self.bytes
@classmethod
def from_B(cls, value):
return cls(value)
def from_B(cls, value, sector_aligned = False):
return cls(value, sector_aligned)
@classmethod
def from_KiB(cls, value):
return cls(value * cls._KiB)
def from_KiB(cls, value, sector_aligned = False):
return cls(value * cls._KiB, sector_aligned)
@classmethod
def from_MiB(cls, value):
return cls(value * cls._MiB)
def from_MiB(cls, value, sector_aligned = False):
return cls(value * cls._MiB, sector_aligned)
@classmethod
def from_GiB(cls, value):
return cls(value * cls._GiB)
def from_GiB(cls, value, sector_aligned = False):
return cls(value * cls._GiB, sector_aligned)
@classmethod
def from_TiB(cls, value):
return cls(value * cls._TiB)
def from_TiB(cls, value, sector_aligned = False):
return cls(value * cls._TiB, sector_aligned)
@classmethod
def from_sector(cls, value):
return cls(value * cls._SECTOR_SIZE)
@property
def B(self):
@ -106,6 +114,10 @@ class Size:
def TiB(self):
return self.bytes / self._TiB
@property
def sectors(self):
return self.bytes // _SECTOR_SIZE
def __str__(self):
if self.bytes < self._KiB:
return "{} B".format(self.B)

View File

@ -93,7 +93,7 @@ def test_wo_read_data_consistency(pyocf_ctx):
# possible end sectors for test iteration
end_sec = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 23]
SECTOR_SIZE = 512
SECTOR_SIZE = Size.from_sector(1).B
CACHELINE_SIZE = 4096
WORKSET_SIZE = 3 * CACHELINE_SIZE
WORKSET_OFFSET = 1024 * CACHELINE_SIZE