<div dir="auto">Pure speculation: are you paying for a lot of conversions between FilePath (string) and C strings?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 9, 2019, 10:02 PM Magicloud Magiclouds <<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
I have asked this in Stackoverflow without getting an answer.<br>
Wondering if people here could have some thoughts.<br>
<br>
I have a function reading the content of /proc every second.<br>
Surprisingly, its CPU usage in top is around 5%, peak at 8%. But same<br>
logic in C or Rust just takes like 1% or 2%. Wondering if this can be<br>
improved. /proc is virtual filesystem, so this is not related to HDD<br>
performance. And I noticed this difference because my CPU is too old<br>
(Core Gen2). On modern CPU, as tested by others, the difference is<br>
barely noticeable.<br>
<br>
import Control.Exception<br>
import Control.Concurrent<br>
import Control.Monad<br>
import Data.Char<br>
import Data.Maybe<br>
import System.Directory<br>
import System.FilePath<br>
import System.Posix.Files<br>
import System.Posix.Signals<br>
import System.Posix.Types<br>
import System.Posix.User<br>
import System.IO.Strict as Strict<br>
<br>
watch u limit0s limit0h = do<br>
listDirectory "/proc/" >>= mapM_ (\fp -> do<br>
isMyPid' <- maybe False id <$> wrap2Maybe (isMyPid fp u)<br>
wrap2Maybe (Strict.readFile ("/proc/" </> fp </> "stat")))<br>
threadDelay 1000000<br>
watch u limit0s limit0h<br>
where<br>
wrap2Maybe :: IO a -> IO (Maybe a)<br>
wrap2Maybe f = catch ((<$>) Just $! f) (\(_ :: IOException) -><br>
return Nothing)<br>
isMyPid :: FilePath -> UserID -> IO Bool<br>
isMyPid fp me = do<br>
let areDigit = fp >= "0" && fp <= "9"<br>
isDir <- doesDirectoryExist $ "/proc/" </> fp<br>
owner <- fileOwner <$> getFileStatus ("/proc" </> fp)<br>
return $ areDigit && isDir && (owner == me)<br>
<br>
<br>
-- <br>
竹密岂妨流水过<br>
山高哪阻野云飞<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>