![NMVe SSD Fedora 吞吐量测试过高](https://linux22.com/image/122722/NMVe%20SSD%20Fedora%20%E5%90%9E%E5%90%90%E9%87%8F%E6%B5%8B%E8%AF%95%E8%BF%87%E9%AB%98.png)
我正在测试使用 NVMe 协议的三星 950 Pro SSD 卡的吞吐量。我当前的测试方法是在分区上挂载文件系统,并将大小为X字节的文件写入文件系统。通过记录执行此操作所需的时间,可以计算字节/秒。
在我的测试中,我有一个 while 循环,它将以可变块大小写入 X 字节,一次一个块,由更高级别的 for 循环指定。除此之外,我还有另一个循环,它将并行运行 N 个应用程序,每个应用程序写入 SSD 的不同分区。
目前,我看到的读取和写入速度略快于三星 950 Pro 数据表指定的理论最大传输速度。三星指定 950 Pro 的最大顺序写入速度为 1.5 GB/s,最大顺序读取速度为 2.5 GB/s。
以下是 bash 脚本的主要功能,该脚本循环遍历要运行的应用程序数量和块大小:
appInstances=1
while [ $appInstances -le 4 ]
do
for blocksize in 4096 32768 131072 524288 1048576 67108864 268435456 1073741824
do
# Run the test
datetime
echo "[$datetime_v]: Test blocksize: $blocksize appInstances: $appInstances"
run_single_perf_test $blocksize
done
appInstances=`expr $appInstances \* 2`
done
exit 0
这是 run_perf_test 的写入部分。这部分之后还有一个读取部分,其中包括写入吞吐速度测试。在测试之间,我卸载了 SSD 的所有分区并重新安装它们,以允许所有 NVMe 事务完成并防止写入操作的任何缓存影响读取操作的吞吐量测量。
instCnt=1
childpids=""
while [ $instCnt -le $appInstances ]
do
fsrw -w $blocksize /fsmnt/fs${instCnt}/usernumber1/j.j &
# Save the process ID
childpids="$childpids $!"
# Increment the instace count.
instCnt=`expr $instCnt + 1`
done
fsrw 是一个 C++ 应用程序,它基于第一个参数“-r”或“-w”、第二个参数(块大小)和第三个参数(SSD 分区上的文件名)构建一个字符串并尝试打开 SSD 分区上的文件并将字符串写入其中。这是 write 函数的实现,当提供“-w”作为第一个参数时调用该函数。
/*! \fn perform_writeop()
* \brief The function returns true when the write operation completes successfully.
*
* The function will run until the read is complete or a 35 second timeout is reached.
* It will record the time before the write begins, then also record the time afterward.
* If the timeout is reached this should be about 35 seconds
*/
bool perform_writeop ()
{
// File descriptor.
int32_t fd = -1;
// Function status.
bool status = false;
// Zero writes
int zero_writes = 0;
// Buffer fill index.
int32_t bfidx = 0;
// Character value.
int8_t cv = 33;
// Fill the buffer with printable characters.
for (; bfidx < blocksize; bfidx++, cv++)
{
// Verify the character value is in range.
if (cv >= 127)
{
cv = 33;
}
else
{
// Add to the buffer.
buf[bfidx] = cv;
}
}
// Open the file.
fd = open (fname.c_str (), O_WRONLY | O_CREAT, 0660);
// Verify the file has been opened.
if (fd == -1)
{
cout << get_datetime_string() << "Write open of " << fname
<< " failed. Errno: " << errno << endl;
}
else
{
// Total bytes written.
uint64_t written = 0;
// Notify the start of the test.
cout << get_datetime_string() << "Write test started" << endl;
// Elapsed time.
struct timeval tv = { 0 };
get_elapsed_time (&tv);
struct timeval write_tv = tv;
// Run until it is time for the test to stop.
while (written < READ_LIMIT && zero_writes < 10)
{
ssize_t writesize = write (fd, &buf[0], blocksize);
if (writesize == -1)
{
cout << get_datetime_string << "Write failure. Errno: " << errno << endl;
zero_writes = 10;
}
else if (0 == writesize)
{
cout << get_datetime_string() << "Zero bytes written" << endl;
zero_writes++;
}
else
{
written += writesize;
}
}
string flush_command = "nvme flush /dev/nvme0n1p";
flush_command += fname[9];
system(flush_command.c_str());
// Get the elapsed time.
get_elapsed_time (&write_tv);
// Report the number of bytes written.
cout << get_datetime_string() << "Write " << written << " bytes in "
<< write_tv.tv_sec << "." << write_tv.tv_usec
<< " seconds" << endl;
// Close the file.
close (fd);
// Get the elapsed time.
get_elapsed_time (&tv);
// Report the number of bytes read.
cout << get_datetime_string() << "Write closed. " << written
<< " Bytes written in " << tv.tv_sec << "." << tv.tv_usec
<< " seconds" << endl;
// Report the number of bytes per second.
cout << get_datetime_string() << "Bytes per second "
<< bytes_per_second (&tv, written) << endl;
// Report the cache flush time.
struct timeval flush_tv = { 0 };
timersub (&tv, &write_tv, &flush_tv);
cout << get_datetime_string() << "System cache flush completed in "
<< flush_tv.tv_sec << "." << flush_tv.tv_usec << "seconds" << endl;
// Set the function return status when all write operations have
// been successful.
if (zero_writes < 10)
{
status = true;
}
}
return status;
}
这些数字接近三星 950 Pro 的最大理论吞吐量,但有些数字太高,这让我很困扰。为什么我得到的数字可能高于 Samsung 950 Pro 的理论最大吞吐量?