我正准备发布一个只对普通硬盘有效的工具,而不是 SSD(固态硬盘)。事实上,它不应该与 SSD 一起使用,因为它会导致大量读取/写入而没有真正的效果。
任何人都知道一种检测给定驱动器是否为固态的方法?
我正准备发布一个只对普通硬盘有效的工具,而不是 SSD(固态硬盘)。事实上,它不应该与 SSD 一起使用,因为它会导致大量读取/写入而没有真正的效果。
任何人都知道一种检测给定驱动器是否为固态的方法?
终于找到可靠的解决方案了!其中两个,实际上!
检查 /sys/block/sdX/queue/rotational,其中sdX是驱动器名称。如果为 0,则表示您正在处理 SSD,而 1 表示普通的旧 HDD。
我无法确定引入它的 Linux 版本,但它存在于 Ubuntu 的 Linux 3.2 和 vanilla Linux 3.6 中,而不存在于 vanilla 2.6.38 中。Oracle 还将它反向移植到基于 2.6.32 的 Unbreakable Enterprise 内核 5.5。
还有一个 ioctl 来检查驱动器是否自 Linux 3.3 以来是旋转的,由this commit引入。不过,使用 sysfs 通常更方便。
You can actually fairly easily determine the rotational latency -- I did this once as part of a university project. It is described in this report. You'll want to skip to page 7 where you see some nice graphs of the latency. It goes from about 9.3 ms to 1.1 ms -- a drop of 8.2 ms. That corresponds directly to 60 s / 8.2 ms = 7317 RPM
.
It was done with simple C code -- here's the part that measures the between positions a
and b
in a scratch file. We did this with larger and larger b
values until we have been wandered all the way around a cylinder:
/* Measure the difference in access time between a and b. The result * is measured in nanoseconds. */ int measure_latency(off_t a, off_t b) { cycles_t ta, tb; overflow_disk_buffer(); lseek(work_file, a, SEEK_SET); read(work_file, buf, KiB/2); ta = get_cycles(); lseek(work_file, b, SEEK_SET); read(work_file, buf, KiB/2); tb = get_cycles(); int diff = (tb - ta)/cycles_per_ns; fprintf(stderr, "%i KiB to %i KiB: %i nsec\n", a / KiB, b / KiB, diff); return diff; }
此命令lsblk -d -o name,rota
列出您的驱动器,如果是旋转磁盘,则 ROTA 为 1,如果是 SSD,则为 0。示例输出:
名称罗塔 sda 1 数据库 0
Detecting SSDs is not as impossible as dseifert makes out. There is already some progress in linux's libata (http://linux.derkeiler.com/Mailing-Lists/Kernel/2009-04/msg03625.html), though it doesn't seem user-ready yet.
And I definitely understand why this needs to be done. It's basically the difference between a linked list and an array. Defragmentation and such is usually counter-productive on a SSD.
You could get lucky by running
smartctl -i sda
from Smartmontools. Almost all SSDs has SSD in the Model field. No guarantee though.
我的两分钱来回答这个古老但非常重要的问题......如果通过 SCSI 访问磁盘,那么您将(可能)能够使用 SCSI INQUIRY 命令来请求其旋转速率。VPD(重要产品数据)页面被调用Block Device Characteristics
并有一个数字0xB1
。本页的字节 4 和 5 包含一个具有含义的数字:
所以,SSD在这个领域必须有0001h
。T10.org
可以在此处找到有关此页面的文档。
但是,我不清楚该标准的实施情况。
我编写了以下 javascript 代码。我需要确定机器是否正在使用 SSD 驱动器以及它是否是引导驱动器。该解决方案使用 MSFT_PhysicalDisk WMI 接口。
function main()
{
var retval= false;
// MediaType - 0 Unknown, 3 HDD, 4 SSD
// SpindleSpeed - -1 has rotational speed, 0 has no rotational speed (SSD)
// DeviceID - 0 boot device
var objWMIService = GetObject("winmgmts:\\\\.\\root\\Microsoft\\Windows\\Storage");
var colItems = objWMIService.ExecQuery("select * from MSFT_PhysicalDisk");
var enumItems = new Enumerator(colItems);
for (; !enumItems.atEnd(); enumItems.moveNext())
{
var objItem = enumItems.item();
if (objItem.MediaType == 4 && objItem.SpindleSpeed == 0)
{
if (objItem.DeviceID ==0)
{
retval=true;
}
}
}
if (retval)
{
WScript.Echo("You have SSD Drive and it is your boot drive.");
}
else
{
WScript.Echo("You do not have SSD Drive");
}
return retval;
}
main();
SSD devices emulate a hard disk device interface, so they can just be used like hard disks. This also means that there is no general way to detect what they are.
You probably could use some characteristics of the drive (latency, speed, size), though this won't be accurate for all drives. Another possibility may be to look at the S.M.A.R.T. data and see whether you can determine the type of disk through this (by model name, certain values), however unless you keep a database of all drives out there, this is not gonna be 100% accurate either.
写文本文件
读文本文件
重复10000次...
10000/elapsed
对于 ssd 会更高,python3:
def ssd_test():
doc = 'ssd_test.txt'
start = time.time()
for i in range(10000):
with open(doc, 'w+') as f:
f.write('ssd test')
f.close()
with open(doc, 'r') as f:
ret = f.read()
f.close()
stop = time.time()
elapsed = stop - start
ios = int(10000/elapsed)
hd = 'HDD'
if ios > 6000: # ssd>8000; hdd <4000
hd = 'SSD'
print('detecting hard drive type by read/write speed')
print('ios', ios, 'hard drive type', hd)
return hd