On Thu, Sep 5, 2013 at 1:24 PM, Spyros Trigazis <[email protected]> wrote:
> Modify CPUload data collector to get the number of clock ticks per > second from Unistd. > > Signed-off-by: Spyros Trigazis <[email protected]> > --- > src/Ganeti/DataCollectors/CPUload.hs | 16 +++++++++------- > 1 file changed, 9 insertions(+), 7 deletions(-) > > diff --git a/src/Ganeti/DataCollectors/CPUload.hs > b/src/Ganeti/DataCollectors/CPUload.hs > index 0c899a7..4b1d199 100644 > --- a/src/Ganeti/DataCollectors/CPUload.hs > +++ b/src/Ganeti/DataCollectors/CPUload.hs > @@ -38,6 +38,7 @@ import Data.Attoparsec.Text.Lazy as A > import Data.Text.Lazy (pack, unpack) > import qualified Text.JSON as J > import qualified Data.Sequence as Seq > +import System.Posix.Unistd (getSysVar, SysVar(ClockTick)) > > import qualified Ganeti.BasicTypes as BT > import qualified Ganeti.Constants as C > @@ -155,8 +156,9 @@ dcUpdate mcd = do > > -- | Computes the average load for every CPU and the overall from data > read > -- from the map. > -computeAverage :: Buffer -> Integer -> [Double] > -computeAverage s w = > +computeAverage :: Buffer -> Integer -> IO [Double] > +computeAverage s w = do > + ticks <- getSysVar ClockTick > In general, keeping as many computations as possible pure is a good idea. So, instead of having computeAvarage in the IO monad, what do you think of using the getSysVar function in the caller (that is, in buildJsonReport) and then just pass "ticks" as a parameter to computeAverage? > let window = Seq.takeWhileL ((> w) . fst) s > go Seq.EmptyL _ = [] > go _ Seq.EmptyR = [] > @@ -164,15 +166,15 @@ computeAverage s w = > let (timestampL, listL) = leftmost > (timestampR, listR) = rightmost > work = zipWith (-) listL listR > - overall = (timestampL - timestampR) * 100 > + overall = (timestampL - timestampR) * ticks > map (\x -> fromIntegral x / fromIntegral overall) work > - in go (Seq.viewl window) (Seq.viewr window) > + return $ go (Seq.viewl window) (Seq.viewr window) > > -- | This function computes the JSON representation of the CPU load. > buildJsonReport :: Buffer -> IO J.JSValue > -buildJsonReport v = > - let res = computeAverage v windowSize > - in return . J.showJSON $ formatData res > +buildJsonReport v = do > + res <- computeAverage v windowSize > + return . J.showJSON $ formatData res > > -- | This function computes the DCReport for the CPU load. > buildDCReport :: Buffer -> IO DCReport > -- > 1.7.10.4 > > Thanks, Michele -- Google Germany GmbH Dienerstr. 12 80331 München Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Geschäftsführer: Graham Law, Christine Elizabeth Flores
