func (ts *ticksource_cmd) Start(prod chan<- t.Ticks) { cmdout := make(chan string) go ts.cmd.Start(nil, cmdout) lastval := t.Ticks(0) if (ts.sourcetype&t.SRC_Z != 0) || (ts.sourcetype&t.SRC_O == 0) { // we produce zero as origin in this case. // If the command also produces an origin, just wait for it and then ignore. if ts.sourcetype&t.SRC_O != 0 { <-cmdout } // then emit the zero origin. prod <- t.Ticks(0) } for { tickstr := <-cmdout v, err := strconv.ParseFloat(tickstr, 64) Assert(err == nil, "parsing ticks", ":", err) val := t.Ticks(v) if ts.sourcetype&t.SRC_M != 0 { tmp := val - lastval lastval = val val = tmp } prod <- val } }
func parse(input <-chan string, ticksctl chan<- t.Ticks, supplycmd chan<- SupplyCmd, acmd chan<- bool, statusctl chan<- bool) { for cmd := range input { cmdargs := strings.Split(cmd, " ") switch cmdargs[0] { case ".": Assert(len(cmdargs) == 2, "invalid syntax for . on input: ", cmd) v, err := strconv.ParseFloat(cmdargs[1], 64) Assert(err == nil, "parsing . on input", ":", err) ticksctl <- t.Ticks(v) case "+": Assert(len(cmdargs) == 2, "invalid syntax for + on input: ", cmd) v, err := strconv.ParseFloat(cmdargs[1], 64) Assert(err == nil, "parsing + on input", ":", err) supplycmd <- SupplyCmd{t.StuffSteps(v)} case "aon": acmd <- true case "aoff": acmd <- false case "?": Assert(len(cmdargs) == 1, "invalid syntax for ? on input: ", cmd) statusctl <- true } } }
func (ts *ticksource_timer) Start(prod chan<- t.Ticks) { ticker := time.NewTicker(ts.period) src := (*ticker).C var div float64 if ts.divper { div = 1 / float64(ts.period) } else { div = 1 / 1e9 } var lasttime time.Time if ts.stype&t.SRC_Z != 0 { // origin forced to zero, so consume // first tick to define origin lasttime = <-src } for v := range src { d := v.Sub(lasttime) lasttime = v prod <- t.Ticks(float64(d) * div) } }
func throttle_ticks(minperiod t.Ticks, src <-chan t.Ticks, prod chan<- t.Ticks) { val := float64(0) mp := float64(minperiod) for v := range src { val += float64(v) if val >= mp { prod <- t.Ticks(val) val = 0 } } }
func mergeticks(src_ext <-chan t.Ticks, src_ctl <-chan t.Ticks, prod chan<- t.Ticks) { prod <- <-src_ext // forward init for { // forward deltas from either external tick source // or input stream v := t.Ticks(0) select { case v = <-src_ctl: case v = <-src_ext: } if v > 0 { prod <- v } } }
func (qs *qstate) make_status(supply t.StuffSteps) t.Status { qs.ticks += qs.dticks qs.steps += qs.dsteps st := t.Status{ Ticks: qs.ticks, TicksDelta: qs.dticks, Steps: qs.steps, StepsDelta: qs.dsteps, Supply: supply, Delta: supply - qs.psupply, } qs.psupply = supply qs.dticks = t.Ticks(0) qs.dsteps = t.Steps(0) return st }
func (ts *ticksource_dummy) Start(prod chan<- t.Ticks) { prod <- t.Ticks(ts.v) // init, // then nothing }
func main() { ifname := getopt.StringLong("input", 'i', "-", "Set input stream for supplies (default '-', stdin)", "FILE") ofname := getopt.StringLong("output", 'o', "-", "Set output stream for reports (default '-', stdout)", "FILE") tspec := getopt.StringLong("ticks", 't', "", "Set tick generator (produces tick events)", "SPEC") sspec := getopt.StringLong("steps", 's', "", "Set progress indicator (measures steps)", "SPEC") mspec := getopt.StringLong("monitor", 'm', "", "Set resource monitor (measures stuff)", "SPEC") aspec := getopt.StringLong("actuator", 'a', "", "Set actuator (triggered upon supply exhaustion)", "SPEC") gran := getopt.StringLong("granularity", 'g', "0", "Force tick granularity (default 0, disabled)", "N") thr := getopt.StringLong("periodic-output", 'p', "none", "Configure periodic output (default none)", "PER") help := getopt.BoolLong("help", 'h', "Print this help") ver := getopt.BoolLong("version", 0, "Report version number") getopt.SetParameters("") getopt.Parse() /*** -h / -v ***/ if *help { getopt.Usage() fmt.Println("\nReport bugs to http://github.com/knz/reg/issues") os.Exit(0) } if *ver { fmt.Println("reg (REG-ulator)", version) os.Exit(0) } /*** -i / -o ***/ var fin, fout *os.File var err error if *ifname == "-" { fin = os.Stdin } else { fin, err = os.Open(*ifname) Assert(err == nil, "-i :", err) } if *ofname == "-" { fout = os.Stdout } else { fout, err = os.OpenFile(*ofname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) Assert(err == nil, "-o :", err) } /*** -g ***/ g, err := strconv.ParseFloat(*gran, 64) Assert(err == nil, "-g :", err) Assert(g >= 0, "-g : granularity cannot be negative") /*** -p ***/ throttle_type := reg.OUTPUT_EXPLICIT_ONLY throttle_period := float64(0) spec_type, spec_flags, spec_arg := split(*thr) switch spec_type { case "none": Assert(spec_flags == "" && spec_arg == "", "-p none does not accept flags/argument") throttle_type = reg.OUTPUT_EXPLICIT_ONLY case "flood": Assert(spec_flags == "" && spec_arg == "", "-p flood does not accept flags/argument") throttle_type = reg.OUTPUT_FLOOD case "ticks": throttle_type = reg.OUTPUT_THROTTLE_TICKS if spec_arg != "" { throttle_period, err = strconv.ParseFloat(spec_arg, 64) Assert(err == nil, "-p ticks :", err) } Assert(spec_flags == "", "-p ticks does not accept flags") case "steps": throttle_type = reg.OUTPUT_THROTTLE_STEPS if spec_arg != "" { throttle_period, err = strconv.ParseFloat(spec_arg, 64) Assert(err == nil, "-p steps :", err) } Assert(spec_flags == "", "-p steps does not accept flags") default: Assert(false, "invalid syntax for -p") } Assert(throttle_period >= 0, "-p : period cannot be negative") /*** -a ***/ var a act.Actuator spec_type, spec_flags, spec_arg = split(*aspec) Assert(spec_flags == "", "-a does not accept flags") switch spec_type { case "discard": Assert(spec_arg == "", "-a discard does not accept argument") a = act.MakeDummyActuator() case "print": var af *os.File if spec_arg == "-" || spec_arg == "" { af = os.Stdout } else { af, err = os.OpenFile(spec_arg, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) Assert(err == nil, "-a :", err) } a = act.MakePrinterActuator(af) case "proc": a = act.MakeCommandActuator(cmd.MakeInteractiveCommand(spec_arg)) case "cmd": a = act.MakeCommandActuator(cmd.MakeOneShotCommand(spec_arg)) default: Assert(false, "invalid -a, or -a not specified") } /*** -t ***/ var ts ticks.Source spec_type, spec_flags, spec_arg = split(*tspec) switch spec_type { case "instant": Assert(spec_flags == "", "-t instant does not accept flags") v := float64(0) if spec_arg != "" { v, err = strconv.ParseFloat(spec_arg, 64) Assert(err == nil, "-t instant:", err) } ts = ticks.MakeDummySource(t.Ticks(v)) case "time", "ptime", "cmd", "proc": stype, ok := flagconv(spec_flags) Assert(ok, "invalid flags for -t") switch spec_type { case "time", "ptime": d, err := time.ParseDuration(spec_arg) Assert(err == nil, "-t :", err) ts = ticks.MakeTimerSource(d, stype, spec_type == "ptype") case "cmd": ts = ticks.MakeCommandSource(cmd.MakeOneShotCommand(spec_arg), stype) case "proc": ts = ticks.MakeCommandSource(cmd.MakeInteractiveCommand(spec_arg), stype) } default: Assert(false, "invalid -t, or -t not specified") } /*** -s ***/ var ss steps.Source spec_type, spec_flags, spec_arg = split(*sspec) switch spec_type { case "const": Assert(spec_flags == "", "-s dummy does not accept flags") v := float64(0) if spec_arg != "" { v, err = strconv.ParseFloat(spec_arg, 64) Assert(err == nil, "-s const:", err) Assert(v >= 0, "-s const: constant cannot be negative") } ss = steps.MakeDummySource(t.Steps(v)) case "cmd", "proc": stype, ok := flagconv(spec_flags) Assert(ok, "invalid flags for -s") switch spec_type { case "cmd": ss = steps.MakeCommandSource(cmd.MakeOneShotCommand(spec_arg), stype) case "proc": ss = steps.MakeCommandSource(cmd.MakeInteractiveCommand(spec_arg), stype) } default: Assert(false, "invalid -s, or -s not specified") } /*** -m ***/ var m sample.Sampler spec_type, spec_flags, spec_arg = split(*mspec) switch spec_type { case "const": Assert(spec_flags == "", "-m const does not accept flags") v := float64(0) if spec_arg != "" { v, err = strconv.ParseFloat(spec_arg, 64) Assert(err == nil, "-m const:", err) } m = sample.MakeDummySampler(t.Stuff(v)) case "cmd", "proc": Assert(spec_flags == "", "-m cmd/proc does not accept flags") switch spec_type { case "cmd": m = sample.MakeCommandSampler(cmd.MakeOneShotCommand(spec_arg)) case "proc": m = sample.MakeCommandSampler(cmd.MakeInteractiveCommand(spec_arg)) } default: Assert(false, "invalid -m, or -m not specified") } d := reg.MakeDomain(ts, ss, a, m) d.Start(fin, fout, throttle_type, throttle_period, t.Ticks(g)) d.Wait() }