Permalink
Browse files

Support for reading CPU stats on Linux

  • Loading branch information...
jystic committed Jun 12, 2011
0 parents commit fb646049dd1a414dbaf25385ab0e274cbabc9ee5
Showing with 138 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +30 −0 LICENSE
  3. +2 −0 Setup.hs
  4. +69 −0 src/System/Stats/CPU.hs
  5. +34 −0 system-stats.cabal
@@ -0,0 +1,3 @@
+*.swp
+*~
+/dist/
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c)2011, Jacob Stanley <jacob@stanley.io>
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Jacob Stanley <jacob@stanley.io> nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,69 @@
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+module System.Stats.CPU
+ ( CPUStats (..)
+ , getCPUStats
+ ) where
+
+import Control.Applicative ((<$>))
+import Control.Exception (bracket)
+import Data.Maybe (fromJust)
+import Data.List (find)
+import System.IO (openFile, hClose, IOMode(..))
+
+import Data.ByteString (ByteString)
+import qualified Data.ByteString.Char8 as B
+
+------------------------------------------------------------------------
+-- Types
+
+-- | Represents a percentage where 0% = 0.0 and 100% = 1.0
+type Percentage = Double
+
+-- | CPU usage statistics for a moment in time.
+data CPUStats = CPUStats
+ { cpuUser :: Percentage -- ^ Time spent executing processes in user mode
+ , cpuSystem :: Percentage -- ^ Time spent executing processes in system/kernel mode
+ , cpuIdle :: Percentage -- ^ Time spent doing nothing
+ } deriving (Show)
+
+
+------------------------------------------------------------------------
+-- Top level functions
+
+-- | Samples the CPU statistics for the current time.
+getCPUStats :: IO CPUStats
+#ifdef linux_HOST_OS
+getCPUStats = parseStat <$> readFile' "/proc/stat"
+#else
+getCPUStats = error "getCPUStats: not supported on this platform"
+#endif
+
+------------------------------------------------------------------------
+-- Linux
+
+parseStat :: ByteString -> CPUStats
+parseStat = parseStatCPU . fromJust . (find (B.isPrefixOf "cpu ")) . B.lines
+
+parseStatCPU :: ByteString -> CPUStats
+parseStatCPU s = CPUStats
+ { cpuUser = (user + nice) / total
+ , cpuSystem = sys / total
+ , cpuIdle = idle / total
+ }
+ where
+ total = user + nice + sys + idle + iowait + hardirq + softirq
+
+ (_:user:nice:sys:idle:iowait:hardirq:softirq:_) = map readNumber (B.words s)
+ readNumber = fromInteger . toInteger . fst . fromJust . B.readInt
+
+------------------------------------------------------------------------
+-- Utils
+
+-- | Reads an entire file strictly into a 'ByteString'. This differs
+-- from the 'B.readFile' shipped with the bytestring package in that it
+-- supports reading special \"zero sized\" files like the ones linux
+-- uses to report system information (eg. \/proc\/stat).
+readFile' :: FilePath -> IO ByteString
+readFile' f = bracket (openFile f ReadMode) hClose B.hGetContents
@@ -0,0 +1,34 @@
+name: system-stats
+version: 0.1
+synopsis: Cross platform access to stats like CPU, RAM & Disk usage
+--Description:
+homepage: http://github.com/jystic/system-stats
+license: BSD3
+license-file: LICENSE
+author: Jacob Stanley <jacob@stanley.io>
+maintainer: Jacob Stanley <jacob@stanley.io>
+copyright: Copyright (c) Jacob Stanley 2011
+category: System
+build-type: Simple
+cabal-version: >=1.2
+
+source-repository head
+ type: git
+ location: https://github.com/jystic/system-stats.git
+
+library
+ hs-source-dirs: src
+
+ exposed-modules:
+ System.Stats.CPU
+
+ build-depends:
+ base == 4.*,
+ bytestring == 0.9.*
+
+ ghc-options:
+ -Wall
+ -funbox-strict-fields
+ -fwarn-tabs
+ -fno-warn-orphans
+ -fno-warn-unused-do-bind

0 comments on commit fb64604

Please sign in to comment.