Beispiel #1
0
func showOneStats(handlers []*StatsHandler, delay string, path string) {

	d, err := time.ParseDuration(delay)
	if err != nil {
		fmt.Printf("Unrecognized duration '%s', using '1s' instead\n", delay)
		d = 1 * time.Second
	}

	var handler *StatsHandler
	for _, h := range handlers {
		if h.path == path {
			handler = h
			break
		}
	}
	if handler == nil {
		fmt.Printf("No such handler: %s\n", path)
		return
	}

	var prev, curr, diff Stats

	var w tabwriter.Writer
	w.Init(os.Stdout, 5, 0, 2, ' ', tabwriter.AlignRight)

	start := clock.Monotonic.Now()
	A, _ := linuxproc.ReadStat("/proc/stat")
	for {
		time.Sleep(d)
		curr.AtomicCopy(&handler.Stats)
		B, _ := linuxproc.ReadStat("/proc/stat")
		end := clock.Monotonic.Now()
		elapsed := end.Sub(start)
		start = end

		showstat(B, A)
		A = B

		diff = curr.Sub(prev)
		fmt.Printf("Load statistics for %s\n", path)
		fmt.Fprintf(&w, "\tops\tops/sec\tsec/op\terrs\t\n")
		fmt.Fprintf(&w, "\t")
		fmt.Fprintf(&w, "%d\t", diff.nok)
		fmt.Fprintf(&w, "%0.2f/sec\t", (float64)(diff.nok)/elapsed.Seconds())
		if diff.nok == 0 {
			fmt.Fprintf(&w, "%s\t", "-")
		} else {
			fmt.Fprintf(&w, "%s\t", time.Duration(diff.tok/diff.nok))
		}
		fmt.Fprintf(&w, "%d\t\n", diff.nerr)
		w.Flush()

		prev = curr
	}
}
func (csc contextSwitchChecker) count() uint64 {
	d, err := linuxproc.ReadStat(*hostPath + "/proc/stat")
	if err != nil {
		panic(fmt.Sprintf("Cannot read disk info of %s file system.", *hostPath+"/proc/stat"))
	}
	return d.ContextSwitches
}
Beispiel #3
0
// Report resource collection.
func (c *CPU) Report() {
	var prevTotal, prevIdle uint64
	prev := new(linux.Stat)

	for {
		select {
		case <-time.Tick(c.Interval):
			log.Info("cpu: reporting")

			stat, err := linux.ReadStat(c.Path)

			if err != nil {
				log.Error("cpu: %s", err)
				continue
			}

			c.client.Gauge("percent", int(percent(&prevIdle, &prevTotal, stat.CPUStatAll)))

			if c.Extended {
				c.client.IncrBy("blocked", int(stat.ProcsBlocked))
				c.client.IncrBy("interrupts", int(stat.Interrupts-prev.Interrupts))
				c.client.IncrBy("switches", int(stat.ContextSwitches-prev.ContextSwitches))
			}

			prev = stat
		case <-c.exit:
			log.Info("cpu: exiting")
			return
		}
	}
}
func GetCPU() {
	stat, err := linuxproc.ReadStat("/proc/stat")
	if err != nil {
		fmt.Errorf("stat read fail")
		return
	}

	unixTs := time.Now().Unix()
	if hostname == "" {
		hostname, _ = os.Hostname()
	}

	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	for i := 0; i < len(stat.CPUStats); i++ {
		s := stat.CPUStats[i]
		b := submit_cpu(i, "user", unixTs, s.User)
		b += submit_cpu(i, "nice", unixTs, s.Nice)
		b += submit_cpu(i, "system", unixTs, s.System)
		b += submit_cpu(i, "idle", unixTs, s.Idle)
		b += submit_cpu(i, "iowait", unixTs, s.IOWait)
		b += submit_cpu(i, "irq", unixTs, s.IRQ)
		b += submit_cpu(i, "softirq", unixTs, s.SoftIRQ)
		b += submit_cpu(i, "steal", unixTs, s.Steal)
		f.Write([]byte(b))
	}
}
func GetCPUPercent() {
	stat, err := linuxproc.ReadStat("/proc/stat")
	if err != nil {
		fmt.Errorf("stat read fail")
		return
	}

	unixTs := time.Now().Unix()
	if hostname == "" {
		hostname, _ = os.Hostname()
	}

	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()

	s := stat.CPUStatAll
	total := float64(s.User + s.Nice + s.System + s.Idle + s.IOWait + s.IRQ + s.SoftIRQ + s.Steal)

	b := submit_cpu_percent("user", unixTs, float64(s.User)/total)
	b += submit_cpu_percent("nice", unixTs, float64(s.Nice)/total)
	b += submit_cpu_percent("system", unixTs, float64(s.System)/total)
	b += submit_cpu_percent("idle", unixTs, float64(s.Idle)/total)
	b += submit_cpu_percent("iowait", unixTs, float64(s.IOWait)/total)
	b += submit_cpu_percent("irq", unixTs, float64(s.IRQ)/total)
	b += submit_cpu_percent("softirq", unixTs, float64(s.SoftIRQ)/total)
	b += submit_cpu_percent("steal", unixTs, float64(s.Steal)/total)

	f.Write([]byte(b))
}
Beispiel #6
0
func getSysTime() int64 {
	sys, err := procinfo.ReadStat("/proc/stat")
	if err != nil {
		panic(err)
	}
	stat := sys.CPUStatAll
	return int64(stat.User + stat.Nice + stat.System + stat.Idle + stat.IOWait)
}
Beispiel #7
0
// cpuStats reads /proc/stat to obtain CPU utilization statistics.
func cpuStats() linux.CPUStat {
	stat, err := linux.ReadStat("/proc/stat")
	if err != nil {
		log.Fatalf("Unable to process /proc/stat: %s", err)
	}

	return stat.CPUStatAll
}
Beispiel #8
0
func (p *ProcStat) Run() error {
	stat, err := linuxproc.ReadStat("/proc/stat")
	if err != nil {
		return err
	}

	p.Data = stat
	return nil
}
Beispiel #9
0
// GetDeltaTotalJiffies returns the number of jiffies that have passed since it
// was last called.  In that respect, it is side-effect-ful.
func GetDeltaTotalJiffies() (uint64, float64, error) {
	stat, err := linuxproc.ReadStat(host.ProcStat)
	if err != nil {
		return 0, 0.0, err
	}

	var (
		currentStat = stat.CPUStatAll
		prevTotal   = (previousStat.Idle + previousStat.IOWait + previousStat.User +
			previousStat.Nice + previousStat.System + previousStat.IRQ +
			previousStat.SoftIRQ + previousStat.Steal)
		currentTotal = (currentStat.Idle + currentStat.IOWait + currentStat.User +
			currentStat.Nice + currentStat.System + currentStat.IRQ +
			currentStat.SoftIRQ + currentStat.Steal)
	)
	previousStat = currentStat
	return currentTotal - prevTotal, float64(len(stat.CPUStats)) * 100., nil
}
Beispiel #10
0
//	https://github.com/c9s/goprocinfo
func cpuStat() {

	for {
		//http://stackoverflow.com/questions/6807590/how-to-stop-a-goroutine
		select {
		case <-H.quit:
			return
		default:

		}

		stat, err := linuxproc.ReadStat("/proc/stat")
		if err != nil {
			return
		}
		for _, s := range stat.CPUStats {

			total := s.User + s.System + s.Nice + s.Idle + s.IOWait + s.IRQ + s.SoftIRQ
			diff_idle := float64(s.Idle - cpu_prev_idle[s.Id])
			diff_total := float64(total - cpu_prev_total[s.Id])
			usage := 100 * (diff_total - diff_idle) / diff_total
			cpu_prev_total[s.Id] = total
			cpu_prev_idle[s.Id] = s.Idle

			// сглаживание колебаний
			if len(usage_bufer) < 10 {
				usage_bufer = append(usage_bufer, usage)
			} else {
				usage = 0
				for _, c := range usage_bufer {
					usage += c
				}
				usage_bufer = []float64{}

				msg, _ := json.Marshal(&map[string]interface{}{"cpu": s.Id, "usage": strconv.FormatFloat(usage/10, 'f', 2, 64)})
				H.broadcast <- []byte(msg)
			}
		}

		time.Sleep(time.Millisecond * time.Duration(cpu_update))
	}
}
Beispiel #11
0
func getStats() (idle, total uint64) {
	stat, err := linuxproc.ReadStat("/proc/stat")
	if err != nil {
		log.Fatal("stat read fail")
	}

	for _, s := range stat.CPUStats {
		// s.User
		// s.Nice
		// s.System
		// s.Idle
		// s.IOWait
		val := strconv.FormatUint(s.System, 10)
		//		if err != nil {
		//			fmt.Println("Error: ", err)
		//		}
		fmt.Printf(val)
	}
	return
}
Beispiel #12
0
		return 0, fmt.Errorf("invalid format: %s", string(buf))
	}

	uptime, err := strconv.ParseFloat(fields[0], 64)
	if err != nil {
		return 0, err
	}

	return time.Duration(uptime) * time.Second, nil
}

var previousStat = linuxproc.CPUStat{}

// GetCPUUsagePercent returns the percent cpu usage and max (ie #cpus * 100)
var GetCPUUsagePercent = func() (float64, float64) {
	stat, err := linuxproc.ReadStat(ProcStat)
	if err != nil {
		return 0.0, 0.0
	}

	// From http://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
	var (
		currentStat = stat.CPUStatAll
		prevIdle    = previousStat.Idle + previousStat.IOWait
		idle        = currentStat.Idle + currentStat.IOWait
		prevNonIdle = (previousStat.User + previousStat.Nice + previousStat.System +
			previousStat.IRQ + previousStat.SoftIRQ + previousStat.Steal)
		nonIdle = (currentStat.User + currentStat.Nice + currentStat.System +
			currentStat.IRQ + currentStat.SoftIRQ + currentStat.Steal)
		prevTotal = prevIdle + prevNonIdle
		total     = idle + nonIdle
Beispiel #13
0
func (c *CPUTime) Store() {
	c.last = c.actual
	stat, _ := linuxproc.ReadStat("/proc/stat")
	c.actual = stat.CPUStatAll
}