module Language.Atom.Example.Probes where
import Data.Word
import Language.Atom
main :: IO ()
main = do
let atomCfg = defaults { cCode = prePostCode , cRuleCoverage = False }
(sched, _, _, _, _) <- compile "probe_example" atomCfg example
putStrLn $ reportSchedule sched
probeStr :: (Name, Type) -> String
probeStr (n, t) = "// Probe: " ++ n ++ ", type: " ++ show t
logProbe :: (String, UE) -> Atom ()
logProbe (str, ue_) = action probeFn [ue_]
where probeFn v = "PROBE_PRINTF(\"%u, " ++ str ++
": %i\\n\", __global_clock, " ++ head v ++ ")"
example :: Atom ()
example = do
sec <- tickSecond
probe "Minutes" $ (value sec) `div_` 60
probe "Hours" $ (value sec) `div_` 3600
period 200 $ atom "monitor" $ do
mapM_ logProbe =<< probes
prePostCode :: [Name] -> [Name] -> [(Name, Type)] -> (String, String)
prePostCode _ _ probeList =
( unlines $ [ "// ---- This source is automatically generated by Atom ----"
, "#define PROBE_PRINTF printf"
, "#include <stdio.h>"
, "#include <stdlib.h>"
, "#include <unistd.h>"
] ++ map probeStr probeList
, unlines [ "int main(void) {"
, " while (true) {"
, " probe_example();"
, " usleep(1000);"
, " }"
, " return 0;"
, "}"
, "// ---- End automatically-generated source ----"
])
tickSecond :: Atom (V Word64)
tickSecond = do
sec <- word64 "seconds" 0
probe "Seconds" $ value sec
period 1000 $ exactPhase 0 $ atom "second" $ incr sec
return sec