From 2318b5e69f4ff5dd62845079ac81c1f3e1fea175 Mon Sep 17 00:00:00 2001 From: Lomanic <5020919+Lomanic@users.noreply.github.com> Date: Tue, 3 May 2022 19:46:41 +0200 Subject: [PATCH] [disk][windows] Set EnableCounterForIoctl reg key to 1 in init() to automatically enable diskperf on Server editions Fixes #1094 --- disk/disk_windows.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/disk/disk_windows.go b/disk/disk_windows.go index 9106364c2..5fb9b5b42 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -12,6 +12,7 @@ import ( "github.com/shirou/gopsutil/v3/internal/common" "golang.org/x/sys/windows" + "golang.org/x/sys/windows/registry" ) var ( @@ -44,6 +45,14 @@ type diskPerformance struct { alignmentPadding uint32 // necessary for 32bit support, see https://github.com/elastic/beats/pull/16553 } +func init() { + // enable disk performance counters on Windows Server editions (needs to run as admin) + key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\PartMgr`, registry.SET_VALUE) + if err == nil { + key.SetDWordValue("EnableCounterForIoctl", 1) + } +} + func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { lpFreeBytesAvailable := int64(0) lpTotalNumberOfBytes := int64(0)