package main import ( "errors" "os" "os/signal" "runtime" "syscall" "time" "amuz.es/src/infra/cpu_ctrl/daemon" "amuz.es/src/infra/cpu_ctrl/logger" "amuz.es/src/infra/cpu_ctrl/processor" "amuz.es/src/infra/cpu_ctrl/util" ) var ( log = logger.NewLogger("cpu_ctrl") ) func init() { logger.InitLogger(true, "", &logger.Config{FileName: "Stderr"}) setMaxProcs() } func setMaxProcs() { // TODO(vmarmol): Consider limiting if we have a CPU mask in effect. // Allow as many threads as we have cores unless the user specified a value. var numProcs int // if *maxProcs < 1 { numProcs = runtime.NumCPU() // } else { // numProcs = *maxProcs // } runtime.GOMAXPROCS(numProcs) // Check if the setting was successful. actualNumProcs := runtime.GOMAXPROCS(0) if actualNumProcs != numProcs { log.Printf("Specified max procs of %v but using %v\n", numProcs, actualNumProcs) } } func broadcastTempeture(sender <-chan float64, handler util.Handler, receivers ...<-chan float64) { defer handler.DecreaseWait() defer func() { if err := recover(); err != nil { handler.NotifyError(err.(error)) } }() for { select { case tempeture := <-sender: for _, receiver := range receivers { select { case receiver <- tempeture: default: log.Warn("Some Tempeture consumer blocked!") } } case <-handler.Done(): return } } } func main() { var ( processorCount = processor.GetProcessorCount() processors []processor.Processor exitSignal = make(chan os.Signal, 1) handler = util.NewHandler() sampleDuration = time.Second ) log.Info("Cpu fan controller v0.2") if processorCount == 0 { handler.NotifyError(errors.New("cpu not found!")) } processors = make([]processor.Processor, 0, processorCount) for i := 0; i < processorCount; i++ { if info, err := processor.NewProcessorInfo(handler, i, sampleDuration, 1.5, 0.4, 2.0, 38.0, 0x64, 0x4, ); err != nil { handler.NotifyError(err) } else { processors = append(processors, info) go info.StartMonitoring() } } handler.IncreaseWait() go TempetureControl(processors, tempetureChange, errorChan, ctx, waiter) signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) daemon.NotifyDaemon(daemon.DaemonStarted) defer daemon.NotifyDaemon(daemon.DaemonStopping) defer close(exitSignal) defer handler.GracefullWait() select { case <-handler.Done(): log.Infoln("Service request to close this application") case err := <-handler.Error(): log.Errorf("%s", err) case sysSignal := <-exitSignal: log.Warnf("SYSCALL! %s", sysSignal.String()) } }