module Monky.Disk
( DiskHandle
, getDiskReadWrite
, getDiskFree
, getDiskHandle
)
where
import Monky.Utility
import Data.Time.Clock.POSIX
import Data.IORef
import Monky.Disk.Common
import Monky.Disk.Btrfs
import Monky.Disk.Device
data DiskHandle = DiskH FSI [File] [IORef Int] [IORef Int] (IORef POSIXTime)
sectorSize :: Int
sectorSize = 512
getDiskReadWrite :: DiskHandle -> IO (Int, Int)
getDiskReadWrite (DiskH _ fs readrefs writerefs timeref) = do
contents <- mapM readValues fs
time <- getPOSIXTime
let nreads = map (\c -> (c !! 2) * sectorSize) contents
let writes = map (\c -> (c !! 6) * sectorSize) contents
oreads <- mapM readIORef readrefs
owrites <- mapM readIORef writerefs
otime <- readIORef timeref
let creads = zipWith () nreads oreads
let cwrites = zipWith () writes owrites
let ctime = time otime
_ <- sequence $zipWith writeIORef readrefs nreads
_ <- sequence $zipWith writeIORef writerefs writes
writeIORef timeref time
return (sum $map (`sdivBound` round ctime) creads, sum $map (`sdivBound` round ctime) cwrites)
getDiskFree :: DiskHandle -> IO Int
getDiskFree (DiskH (FSI h) _ _ _ _) = getFsFree h
getBtrfsDH :: (BtrfsHandle, [String]) -> IO DiskHandle
getBtrfsDH (h, devs) = do
fs <- mapM (\dev -> fopen (blBasePath ++ dev ++ "/stat")) devs
wfs <- mapM (\_ -> newIORef 0) devs
rfs <- mapM (\_ -> newIORef 0) devs
t <- newIORef 0
return (DiskH (FSI h) fs wfs rfs t)
getBlockDH :: (BlockHandle, String) -> IO DiskHandle
getBlockDH (h, dev) = do
f <- fopen (blBasePath ++ dev ++ "/stat")
wf <- newIORef 0
rf <- newIORef 0
t <- newIORef 0
return (DiskH (FSI h) [f] [wf] [rf] t)
getDiskHandle :: String -> IO DiskHandle
getDiskHandle uuid = do
btrfs <- getBtrfsHandle uuid
case btrfs of
(Just x) -> getBtrfsDH x
Nothing -> do
block <- getBlockHandle uuid
case block of
Just x -> getBlockDH x
Nothing -> error "Disk currently does not support your setup"