cpuusage_windows.go 684 B

123456789101112131415161718192021222324252627
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //+build windows
  7. package syncthing
  8. import "syscall"
  9. import "time"
  10. func cpuUsage() time.Duration {
  11. handle, err := syscall.GetCurrentProcess()
  12. if err != nil {
  13. return 0
  14. }
  15. defer syscall.CloseHandle(handle)
  16. var ctime, etime, ktime, utime syscall.Filetime
  17. if err := syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime); err != nil {
  18. return 0
  19. }
  20. return time.Duration(ktime.Nanoseconds() + utime.Nanoseconds())
  21. }