#2160·gopsutil

[磁盘][Windows]: IOCounters ReadTime 和 WriteTime 显示为秒而不是毫秒

作者: LudwigJMarx创建于 2026年9月24日更新于 2026年9月27日

描述错误

在 Windows 上,IOCountersWithContext 将 DISK_PERFORMANCE.ReadTime 和 WriteTime 转换为以下形式(disk_windows.go L349-L350):

go
ReadTime:  uint64(dPerformance.ReadTime / 10000 / 1000), // 将其转换为毫秒:https://GitHub.com/giampaolo/psutil/issues/1012
WriteTime:  uint64(dPerformance.WriteTime / 10000 / 1000),

这将其除以 10^7。如果原始值是以 100 ns 为单位的,则结果为秒。注释中提到的是毫秒,而 Linux、macOS 和 FreeBSD 的代码都返回了毫秒。Microsoft 对 DISK_PERFORMANCE 的文档(https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-disk_performance)中没有为 ReadTime 和 WriteTime 提供单位。我找到的唯一公开实现是 WDK 7.1 中的 diskperf 过滤器驱动示例(storage/filters/diskperf/diskperf.c)。它使用 KeQueryPerformanceCounter 来测量每个请求。IOCTL_DISK_PERFORMANCE 处理程序然后使用 DiskPerfAddCounters 将总和转换为:

c
TotalCounters->ReadTime.QuadPart +=
    NewCounters->ReadTime.QuadPart * 10000000 / Frequency.QuadPart;

这表示 100 ns 单位。如果没有性能计数器频率,则示例将使用 KeQuerySystemTime,该计数器也以 100 ns 为单位。该示例不是内置驱动,因此这证明了单位,而不是证明。

内容来源: shirou/gopsutil