all repos — honk @ v1.2.1

my fork of honk

sensors.go (view raw)

 1//
 2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
 3//
 4// Permission to use, copy, modify, and distribute this software for any
 5// purpose with or without fee is hereby granted, provided that the above
 6// copyright notice and this permission notice appear in all copies.
 7//
 8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19	"runtime/debug"
20	"syscall"
21	"time"
22)
23
24func init() {
25	if softwareVersion != "develop" {
26		return
27	}
28	bi, ok := debug.ReadBuildInfo()
29	if !ok {
30		return
31	}
32	var vcs, rev, mod string
33	for _, bs := range bi.Settings {
34		if bs.Key == "vcs" {
35			vcs = "/" + bs.Value
36		}
37		if bs.Key == "vcs.revision" {
38			rev = bs.Value
39			if len(rev) > 12 {
40				rev = rev[:12]
41			}
42			rev = "-" + rev
43		}
44		if bs.Key == "vcs.modified" && bs.Value == "true" {
45			mod = "+"
46		}
47	}
48	softwareVersion += vcs + rev + mod
49}
50
51type Sensors struct {
52	Memory float64
53	Uptime float64
54	CPU    float64
55}
56
57var boottime = time.Now()
58
59func getSensors() Sensors {
60	var usage syscall.Rusage
61	syscall.Getrusage(syscall.RUSAGE_SELF, &usage)
62
63	now := time.Now()
64
65	var sensors Sensors
66	sensors.Memory = float64(usage.Maxrss) / 1024.0
67	sensors.Uptime = now.Sub(boottime).Seconds()
68	sensors.CPU = time.Duration(usage.Utime.Nano()).Seconds()
69
70	return sensors
71}
72
73func setLimits() error {
74	var limit syscall.Rlimit
75	limit.Cur = 2 * 1024 * 1024 * 1024
76	limit.Max = 2 * 1024 * 1024 * 1024
77	return syscall.Setrlimit(syscall.RLIMIT_DATA, &limit)
78}