Spaces:
Runtime error
Runtime error
| """ | |
| Virtual Driver Module - Translates write commands into block operations | |
| """ | |
| import threading | |
| from typing import Optional | |
| class VirtualDriver: | |
| """ | |
| Simulates a device driver that translates high-level write commands | |
| into block-level operations for the SSD controller. | |
| """ | |
| def __init__(self, vvd, block_size: int = 4096): | |
| self.vvd = vvd | |
| self.block_size = block_size | |
| self.lock = threading.RLock() | |
| print(f"VirtualDriver initialized with block size {block_size} bytes") | |
| def send_write(self, logical_block: int, data: bytes) -> bool: | |
| """ | |
| Send a write command to the SSD controller. | |
| Ensures data is properly sized for the block. | |
| """ | |
| with self.lock: | |
| if len(data) > self.block_size: | |
| print(f"Warning: Data size {len(data)} exceeds block size {self.block_size}. Truncating.") | |
| data = data[:self.block_size] | |
| elif len(data) < self.block_size: | |
| # Pad with zeros to fill the block | |
| data = data + b'\x00' * (self.block_size - len(data)) | |
| return self.vvd.write_page(logical_block, data) | |
| def send_read(self, logical_block: int) -> Optional[bytes]: | |
| """ | |
| Send a read command to the SSD controller. | |
| """ | |
| with self.lock: | |
| return self.vvd.read_page(logical_block) | |
| def send_trim(self, logical_block: int): | |
| """ | |
| Send a TRIM command to the SSD controller. | |
| """ | |
| with self.lock: | |
| self.vvd.erase_block(logical_block) | |
| def get_driver_stats(self) -> dict: | |
| """ | |
| Get driver statistics (mostly pass-through to SSD controller). | |
| """ | |
| return { | |
| "flash_stats": self.vvd.pvd_flash.get_flash_stats(), | |
| "ftl_stats": self.vvd.pvd_controller.get_ftl_stats(), | |
| "file_system_stats": self.vvd.pvd_file_system.get_usage_stats() | |
| } | |
| def shutdown(self): | |
| """ | |
| Shutdown the driver. | |
| """ | |
| print("VirtualDriver shutdown complete") | |
| def __del__(self): | |
| try: | |
| self.shutdown() | |
| except: | |
| pass | |