github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/cmd/syncthing/gui_solaris.go (about) 1 // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file). 2 // All rights reserved. Use of this source code is governed by an MIT-style 3 // license that can be found in the LICENSE file. 4 5 //+build solaris 6 7 package main 8 9 import ( 10 "encoding/binary" 11 "fmt" 12 "os" 13 "time" 14 ) 15 16 type id_t int32 17 type ulong_t uint32 18 19 type timestruc_t struct { 20 Tv_sec int64 21 Tv_nsec int64 22 } 23 24 func (tv timestruc_t) Nano() int64 { 25 return tv.Tv_sec*1e9 + tv.Tv_nsec 26 } 27 28 type prusage_t struct { 29 Pr_lwpid id_t /* lwp id. 0: process or defunct */ 30 Pr_count int32 /* number of contributing lwps */ 31 Pr_tstamp timestruc_t /* real time stamp, time of read() */ 32 Pr_create timestruc_t /* process/lwp creation time stamp */ 33 Pr_term timestruc_t /* process/lwp termination time stamp */ 34 Pr_rtime timestruc_t /* total lwp real (elapsed) time */ 35 Pr_utime timestruc_t /* user level CPU time */ 36 Pr_stime timestruc_t /* system call CPU time */ 37 Pr_ttime timestruc_t /* other system trap CPU time */ 38 Pr_tftime timestruc_t /* text page fault sleep time */ 39 Pr_dftime timestruc_t /* data page fault sleep time */ 40 Pr_kftime timestruc_t /* kernel page fault sleep time */ 41 Pr_ltime timestruc_t /* user lock wait sleep time */ 42 Pr_slptime timestruc_t /* all other sleep time */ 43 Pr_wtime timestruc_t /* wait-cpu (latency) time */ 44 Pr_stoptime timestruc_t /* stopped time */ 45 Pr_minf ulong_t /* minor page faults */ 46 Pr_majf ulong_t /* major page faults */ 47 Pr_nswap ulong_t /* swaps */ 48 Pr_inblk ulong_t /* input blocks */ 49 Pr_oublk ulong_t /* output blocks */ 50 Pr_msnd ulong_t /* messages sent */ 51 Pr_mrcv ulong_t /* messages received */ 52 Pr_sigs ulong_t /* signals received */ 53 Pr_vctx ulong_t /* voluntary context switches */ 54 Pr_ictx ulong_t /* involuntary context switches */ 55 Pr_sysc ulong_t /* system calls */ 56 Pr_ioch ulong_t /* chars read and written */ 57 58 } 59 60 func solarisPrusage(pid int, rusage *prusage_t) error { 61 fd, err := os.Open(fmt.Sprintf("/proc/%d/usage", pid)) 62 if err != nil { 63 return err 64 } 65 err = binary.Read(fd, binary.LittleEndian, rusage) 66 fd.Close() 67 return err 68 } 69 70 func init() { 71 go trackCPUUsage() 72 } 73 74 func trackCPUUsage() { 75 var prevUsage int64 76 var prevTime = time.Now().UnixNano() 77 var rusage prusage_t 78 var pid = os.Getpid() 79 for _ = range time.NewTicker(time.Second).C { 80 err := solarisPrusage(pid, &rusage) 81 if err != nil { 82 l.Warnln(err) 83 continue 84 } 85 curTime := time.Now().UnixNano() 86 timeDiff := curTime - prevTime 87 curUsage := rusage.Pr_utime.Nano() + rusage.Pr_stime.Nano() 88 usageDiff := curUsage - prevUsage 89 cpuUsageLock.Lock() 90 copy(cpuUsagePercent[1:], cpuUsagePercent[0:]) 91 cpuUsagePercent[0] = 100 * float64(usageDiff) / float64(timeDiff) 92 cpuUsageLock.Unlock() 93 prevTime = curTime 94 prevUsage = curUsage 95 } 96 }