Skip to content

Commit

Permalink
Add disable performance counters functionality for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
narph committed Apr 19, 2019
1 parent 1119a28 commit 82acba3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
39 changes: 37 additions & 2 deletions metricbeat/module/system/diskio/diskstat_windows_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
)

const (
errorSuccess syscall.Errno = 0
ioctlDiskPerformance = 0x70020
errorSuccess syscall.Errno = 0
ioctlDiskPerformance = 0x70020
ioctlDiskPerformanceOff = 0x70060
)

var (
Expand Down Expand Up @@ -144,6 +145,40 @@ func enablePerformanceCounters() error {
return nil
}

// disablePerformanceCounters will disable performance counters using the IOCTL_DISK_PERFORMANCE_OFF IOCTL control code
func disablePerformanceCounters(path string) error {
utfPath, err := syscall.UTF16PtrFromString(path)
if err != nil {
return err
}
hFile, err := syscall.CreateFile(utfPath,
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_FLAG_BACKUP_SEMANTICS,
0)

if err != nil {
return err
}
defer syscall.CloseHandle(hFile)
var diskPerformanceSize uint32
err = syscall.DeviceIoControl(hFile,
ioctlDiskPerformanceOff,
nil,
0,
nil,
0,
&diskPerformanceSize,
nil)
if err != nil {
return err
}
return nil

}

// getLogicalDriveStrings calls the syscall GetLogicalDriveStrings in order to get the list of logical drives
func getLogicalDriveStrings() ([]logicalDrive, error) {
lpBuffer := make([]byte, 254)
Expand Down
9 changes: 8 additions & 1 deletion metricbeat/module/system/diskio/diskstat_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func TestCDriveFilterOnWindowsTestEnv(t *testing.T) {
assert.True(t, writeCount > 0)
assert.True(t, writeBytes > 0)
assert.True(t, writeTime > 0)

err := disablePerformanceCounters(`\\.\C:`)
assert.NoError(t, err)
}

func TestAllDrivesOnWindowsTestEnv(t *testing.T) {
Expand All @@ -72,4 +73,10 @@ func TestAllDrivesOnWindowsTestEnv(t *testing.T) {
data, errs := mbtest.ReportingFetchV2(f)
assert.Empty(t, errs)
assert.True(t, len(data) >= 1)
drives, err := getLogicalDriveStrings()
assert.NoError(t, err)
for _, drive := range drives {
err := disablePerformanceCounters(drive.UNCPath)
assert.NoError(t, err)
}
}

0 comments on commit 82acba3

Please sign in to comment.