Exemplo n.º 1
0
func trackCPUUsage() {
	handle, err := syscall.GetCurrentProcess()
	if err != nil {
		l.Warnln("Cannot track CPU usage:", err)
		return
	}

	var ctime, etime, ktime, utime syscall.Filetime
	err = syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime)
	if err != nil {
		l.Warnln("Cannot track CPU usage:", err)
		return
	}

	prevTime := ctime.Nanoseconds()
	prevUsage := ktime.Nanoseconds() + utime.Nanoseconds() // Always overflows

	for _ = range time.NewTicker(time.Second).C {
		err := syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime)
		if err != nil {
			continue
		}

		curTime := time.Now().UnixNano()
		timeDiff := curTime - prevTime
		curUsage := ktime.Nanoseconds() + utime.Nanoseconds()
		usageDiff := curUsage - prevUsage
		cpuUsageLock.Lock()
		copy(cpuUsagePercent[1:], cpuUsagePercent[0:])
		cpuUsagePercent[0] = 100 * float64(usageDiff) / float64(timeDiff)
		cpuUsageLock.Unlock()
		prevTime = curTime
		prevUsage = curUsage
	}
}
Exemplo n.º 2
0
// readFiletime reads a Windows Filetime struct and converts it to a
// time.Time value with a UTC timezone.
func readFiletime(reader io.Reader) (*time.Time, error) {
	var filetime syscall.Filetime
	err := binary.Read(reader, binary.LittleEndian, &filetime.LowDateTime)
	if err != nil {
		return nil, err
	}
	err = binary.Read(reader, binary.LittleEndian, &filetime.HighDateTime)
	if err != nil {
		return nil, err
	}
	t := time.Unix(0, filetime.Nanoseconds()).UTC()
	return &t, nil
}
Exemplo n.º 3
0
func toFileInfo(name string, fa, sizehi, sizelo uint32, ctime, atime, mtime syscall.Filetime) FileInfo {
	fs := new(FileStat)
	fs.mode = 0
	if fa&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
		fs.mode |= ModeDir
	}
	if fa&syscall.FILE_ATTRIBUTE_READONLY != 0 {
		fs.mode |= 0444
	} else {
		fs.mode |= 0666
	}
	fs.size = int64(sizehi)<<32 + int64(sizelo)
	fs.name = name
	fs.modTime = time.Unix(0, mtime.Nanoseconds())
	fs.Sys = &winTimes{atime, ctime}
	return fs
}
Exemplo n.º 4
0
// Collects the current raw data value for all counters in the specified query and
// updates the status code of each counter.
//
// Time stamp when the first counter value in the query was retrieved. The time is specified as FILETIME.
func PdhCollectQueryDataWithTime(queryHdl syscall.Handle) (*time.Time, error) {
	var TimeStamp syscall.Filetime

	r, _, err := procPdhCollectQueryDataWithTime.Call(
		uintptr(queryHdl),
		uintptr(unsafe.Pointer(&TimeStamp)))

	if r != 0 && err != nil {
		if r == uintptr(PDH_NO_DATA) {
			// Has not values
			return nil, errors.New(codeText[PDH_NO_DATA])
		}
		return nil, err
	}

	ts := time.Unix(0, TimeStamp.Nanoseconds())

	return &ts, nil
}
Exemplo n.º 5
0
func (c *Cpu) Get() error {
	var (
		idleTime   syscall.Filetime
		kernelTime syscall.Filetime // Includes kernel and idle time.
		userTime   syscall.Filetime
	)
	r1, _, e1 := syscall.Syscall(procGetSystemTimes.Addr(), 3,
		uintptr(unsafe.Pointer(&idleTime)),
		uintptr(unsafe.Pointer(&kernelTime)),
		uintptr(unsafe.Pointer(&userTime)),
	)
	if err := checkErrno(r1, e1); err != nil {
		return fmt.Errorf("GetSystemTimes: %s", err)
	}

	c.Idle = uint64(idleTime.Nanoseconds())
	c.Sys = uint64(kernelTime.Nanoseconds()) - c.Idle
	c.User = uint64(userTime.Nanoseconds())
	return nil
}
Exemplo n.º 6
0
func setFileInfo(fi *FileInfo, name string, fa, sizehi, sizelo uint32, ctime, atime, wtime syscall.Filetime) *FileInfo {
	fi.Mode = 0
	if fa&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
		fi.Mode = fi.Mode | syscall.S_IFDIR
	} else {
		fi.Mode = fi.Mode | syscall.S_IFREG
	}
	if fa&syscall.FILE_ATTRIBUTE_READONLY != 0 {
		fi.Mode = fi.Mode | 0444
	} else {
		fi.Mode = fi.Mode | 0666
	}
	fi.Size = int64(sizehi)<<32 + int64(sizelo)
	fi.Name = name
	fi.FollowedSymlink = false
	fi.Atime_ns = atime.Nanoseconds()
	fi.Mtime_ns = wtime.Nanoseconds()
	fi.Ctime_ns = ctime.Nanoseconds()
	return fi
}
Exemplo n.º 7
0
func mkModTime(mtime syscall.Filetime) time.Time {
	return time.Unix(0, mtime.Nanoseconds())
}
Exemplo n.º 8
0
func win32TimeToTar(ft syscall.Filetime) (string, time.Time) {
	return fmt.Sprintf("%d", uint64(ft.LowDateTime)+(uint64(ft.HighDateTime)<<32)), time.Unix(0, ft.Nanoseconds())
}
Exemplo n.º 9
0
func filetimeToDuration(ft *syscall.Filetime) time.Duration {
	ns := ft.Nanoseconds()
	return time.Duration(ns)
}