我的机器不应该有 /dev/ram0 文件吗?

fer*_*nad 12 memory linux ramdisk dev

我有一项大学作业,必须创建 RAM\xe2\x80\xafDisk。

\n

有人告诉我然后制作一个 C 程序,对放置在 RAM\xe2\x80\xafDisk 内的文件进行多次写入,然后对放置在硬盘上的文件执行相同的操作,以比较写入速度。

\n

为了创建 RAM\xe2\x80\xafDisk,我得到了以下脚本:

\n
#!/bin/bash\n# RAM Disk\nROOTUSER_NAME=root\nMOUNTPT=/tmp/ramdisk\nSIZE=2024 # 2K blocs\nBLOCKSIZE=1024 # block size: 1K (1024 bytes)\nDEVICE=/dev/ram0 # First RAM\xe2\x80\xafDisk\nusername=`id -nu`\n[ "$username" != "$ROOTUSER_NAME" ] && echo "not authorised" && exit 1\n[ ! -d "$MOUNTPT" ] && mkdir $MOUNTPT\ndd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE\n/sbin/mkfs -t ext4 $DEVICE \nmount $DEVICE $MOUNTPT # the mount\nchmod 777 $MOUNTPT\necho $MOUNTPT " ready"\nexit 0\n
Run Code Online (Sandbox Code Playgroud)\n

这里的问题是我的机器上好像没有/dev/ram0/dev目录;这是的输出ls /dev

\n
ferran@amsa:~/Desktop$ ls /dev\nautofs          lightnvm    sda     tty22  tty5     ttyS18   vcs3\nblock           log         sda1    tty23  tty50    ttyS19   vcs4\nbsg             loop0       sda2    tty24  tty51    ttyS2   vcs5\nbtrfs-control   loop1       sda3    tty25  tty52    ttyS20   vcs6\nbus             loop2       sda5    tty26  tty53    ttyS21   vcsa\ncdrom           loop3       sg0     tty27  tty54    ttyS22   vcsa1\ncdrw            loop4       sg1     tty28  tty55    ttyS23   vcsa2\nchar            loop5       shm     tty29  tty56    ttyS24   vcsa3\nconsole         loop6       snapshot  tty3   tty57      ttyS25   vcsa4\ncore            loop7       snd     tty30  tty58    ttyS26   vcsa5\ncpu             loop8       sr0     tty31  tty59    ttyS27   vcsa6\ncpu_dma_latency  loop9      stderr  tty32  tty6     ttyS28   vcsu\ncuse            loop-control  stdin     tty33  tty60    ttyS29   vcsu1\ndisk            mapper      stdout  tty34  tty61    ttyS3   vcsu2\ndma_heap        mcelog      tty     tty35  tty62    ttyS30   vcsu3\ndmmidi          mem         tty0    tty36  tty63    ttyS31   vcsu4\ndri             midi        tty1    tty37  tty7     ttyS4   vcsu5\ndvd             mqueue      tty10   tty38  tty8     ttyS5   vcsu6\necryptfs        net         tty11   tty39  tty9     ttyS6   vfio\nfb0             null        tty12   tty4   ttyprintk  ttyS7 vga_arbiter\nfd              nvram       tty13   tty40  ttyS0    ttyS8   vhci\nfull            port        tty14   tty41  ttyS1    ttyS9   vhost-net\nfuse            ppp         tty15   tty42  ttyS10   udmabuf  vhost-vsock\nhidraw0         psaux       tty16   tty43  ttyS11   uhid    vmci\nhpet            ptmx        tty17   tty44  ttyS12   uinput   zero\nhugepages       pts         tty18   tty45  ttyS13   urandom  zfs\nhwrng           random      tty19   tty46  ttyS14   userio\ninitctl         rfkill      tty2    tty47  ttyS15   vcs\ninput           rtc         tty20   tty48  ttyS16   vcs1\nkmsg            rtc0        tty21   tty49  ttyS17   vcs2\n
Run Code Online (Sandbox Code Playgroud)\n

这是输出fdisk -l

\n
ferran@amsa:~/Desktop$ sudo fdisk -l\n[sudo] password for ferran:\nDisk /dev/loop0: 4 KiB, 4096 bytes, 8 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop1: 55,45 MiB, 58130432 bytes, 113536 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop2: 65,22 MiB, 68378624 bytes, 133552 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop3: 55,51 MiB, 58191872 bytes, 113656 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop4: 65,1 MiB, 68259840 bytes, 133320 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop5: 50,98 MiB, 53432320 bytes, 104360 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop6: 32,3 MiB, 33865728 bytes, 66144 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop7: 32,45 MiB, 34017280 bytes, 66440 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors\nDisk model: VMware Virtual S\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\nDisklabel type: dos\nDisk identifier: 0x70d3f840\n\nDevice  Boot    Start       End   Sectors  Size Id Type\n/dev/sda1  *        2048 117186559 117184512 55,9G 83 Linux\n/dev/sda2       117188606 132810751  15622146  7,5G  5 Extended\n/dev/sda3       132810752 132812799     2048    1M 82 Linux swap / Solaris\n/dev/sda5       117188608 132810751  15622144  7,5G 82 Linux swap / Solaris\n\nPartition table entries are not in disk order.\n\n\nDisk /dev/loop8: 219 MiB, 229638144 bytes, 448512 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n
Run Code Online (Sandbox Code Playgroud)\n

对于写入速度测试,我使用以下 C 程序:

\n
ferran@amsa:~/Desktop$ ls /dev\nautofs          lightnvm    sda     tty22  tty5     ttyS18   vcs3\nblock           log         sda1    tty23  tty50    ttyS19   vcs4\nbsg             loop0       sda2    tty24  tty51    ttyS2   vcs5\nbtrfs-control   loop1       sda3    tty25  tty52    ttyS20   vcs6\nbus             loop2       sda5    tty26  tty53    ttyS21   vcsa\ncdrom           loop3       sg0     tty27  tty54    ttyS22   vcsa1\ncdrw            loop4       sg1     tty28  tty55    ttyS23   vcsa2\nchar            loop5       shm     tty29  tty56    ttyS24   vcsa3\nconsole         loop6       snapshot  tty3   tty57      ttyS25   vcsa4\ncore            loop7       snd     tty30  tty58    ttyS26   vcsa5\ncpu             loop8       sr0     tty31  tty59    ttyS27   vcsa6\ncpu_dma_latency  loop9      stderr  tty32  tty6     ttyS28   vcsu\ncuse            loop-control  stdin     tty33  tty60    ttyS29   vcsu1\ndisk            mapper      stdout  tty34  tty61    ttyS3   vcsu2\ndma_heap        mcelog      tty     tty35  tty62    ttyS30   vcsu3\ndmmidi          mem         tty0    tty36  tty63    ttyS31   vcsu4\ndri             midi        tty1    tty37  tty7     ttyS4   vcsu5\ndvd             mqueue      tty10   tty38  tty8     ttyS5   vcsu6\necryptfs        net         tty11   tty39  tty9     ttyS6   vfio\nfb0             null        tty12   tty4   ttyprintk  ttyS7 vga_arbiter\nfd              nvram       tty13   tty40  ttyS0    ttyS8   vhci\nfull            port        tty14   tty41  ttyS1    ttyS9   vhost-net\nfuse            ppp         tty15   tty42  ttyS10   udmabuf  vhost-vsock\nhidraw0         psaux       tty16   tty43  ttyS11   uhid    vmci\nhpet            ptmx        tty17   tty44  ttyS12   uinput   zero\nhugepages       pts         tty18   tty45  ttyS13   urandom  zfs\nhwrng           random      tty19   tty46  ttyS14   userio\ninitctl         rfkill      tty2    tty47  ttyS15   vcs\ninput           rtc         tty20   tty48  ttyS16   vcs1\nkmsg            rtc0        tty21   tty49  ttyS17   vcs2\n
Run Code Online (Sandbox Code Playgroud)\n

我调用了两次:

\n
    \n
  1. ./writeTest ~/Desktop/test
  2. \n
  3. ./writeTest /tmp/ramdisk/test
  4. \n
\n

而且结果\xe2\x80\x99不太有说服力,因为RAM\xe2\x80\xafDisk文件需要8s,硬盘文件需要11s,这似乎不是我应该得到的改进,对吧?

\n

当我运行 bash 脚本时,它/dev/ram0被创建,但我不知道这是否应该发生,并且写入速度的提高是否符合预期,或者我是否应该已经/dev/ram0在我的/dev目录中创建了一个。

\n

任何帮助,将不胜感激。

\n

编辑1:

\n

我按照@user1686所说做了一些更改。\xc2\xa0\n首先我加载了所需的模块modprobe brd,\n然后所有的都出现了。/dev/ram#

\n

然后,我尝试改进我的 C 程序以减少seek调用,尽管我知道以这种方式测试写入速度仍然不是最好的主意,但我被告知使用 C 程序来做到这一点(也许目标只是我们在 RAM 而不是硬盘上写入时看到了改进,而不是我们知道写入速度的确切增量)。

\n

为了减少seek调用,我使 RAM\xe2\x80\xafDisk 更大(它从 2\xe2\x80\xafMB 到 32\xe2\x80\xafMB),然后seek我不调用循环的每次迭代,而是近似需要多少个循环才能写出差不多 32\xe2\x80\xafMB,然后每次MAX迭代我都调用seek,最后重复外层循环LOOPS次数。

\n

该程序如下所示:

\n
ferran@amsa:~/Desktop$ sudo fdisk -l\n[sudo] password for ferran:\nDisk /dev/loop0: 4 KiB, 4096 bytes, 8 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop1: 55,45 MiB, 58130432 bytes, 113536 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop2: 65,22 MiB, 68378624 bytes, 133552 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop3: 55,51 MiB, 58191872 bytes, 113656 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop4: 65,1 MiB, 68259840 bytes, 133320 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop5: 50,98 MiB, 53432320 bytes, 104360 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop6: 32,3 MiB, 33865728 bytes, 66144 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/loop7: 32,45 MiB, 34017280 bytes, 66440 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n\n\nDisk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors\nDisk model: VMware Virtual S\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\nDisklabel type: dos\nDisk identifier: 0x70d3f840\n\nDevice  Boot    Start       End   Sectors  Size Id Type\n/dev/sda1  *        2048 117186559 117184512 55,9G 83 Linux\n/dev/sda2       117188606 132810751  15622146  7,5G  5 Extended\n/dev/sda3       132810752 132812799     2048    1M 82 Linux swap / Solaris\n/dev/sda5       117188608 132810751  15622144  7,5G 82 Linux swap / Solaris\n\nPartition table entries are not in disk order.\n\n\nDisk /dev/loop8: 219 MiB, 229638144 bytes, 448512 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical/physical): 512 bytes / 512 bytes\nI/O size (minimum/optimal): 512 bytes / 512 bytes\n
Run Code Online (Sandbox Code Playgroud)\n

在这个新版本中,主硬盘和 RAM\xe2\x80\xafDisk 安装了选项sync\nI\xc2\xa0manage,速度提高了 30\xc3\x97,\n而且似乎随着我的增加而增加增加 的值LOOPS

\n

use*_*686 32

\n

我的机器不应该有 /dev/ram0 文件吗?

\n
\n

是的,也不是,但大多数情况下不是。

\n

Linux 确实有一个可以提供设备的“ramdisk”驱动程序/dev/ram#,并且它可能在您的系统上可用,但默认情况下通常不会加载它,并且除非您首先加载该驱动程序,否则这些设备节点将不存在。为此,插入brd.ko内核模块,您应该获得 8 或 16 个ram设备:

\n
# modprobe brd\n
Run Code Online (Sandbox Code Playgroud)\n

如果驱动程序未加载,并且您的脚本盲目地使用创建了 /dev/ram0 dd,那么它不会以这种方式创建 ramdisk \xe2\x80\x93 它创建了一个文件。(你实际上应该在之前删除它(您实际上应该在加载“brd”驱动程序

\n

然而,幸运的是,即使 /dev/ram0 不可用,您的脚本最终仍能实现“更好的写入速度”的目标。这是因为在大多数 Linux 系统上,整个 /dev 并不存储在物理磁盘上,而是tmpfs安装了一个文件系统1

\n

“tmpfs”已经在内存中 \xe2\x80\x93 它有点像使用 ramdisk,除了 ramdisk 是固定大小的并且需要与另一个文件系统组合,而 tmpfs 是动态调整大小的。

\n

因此,您在 /dev \xe2\x80\x93 中(有时在 /tmp 2 \xe2\x80\x93 中)创建的任何新文件都将完全存储在内存中。即使您的脚本没有建立真正的旧式“ramdisk”,而只是创建了一个名为“/dev/ram0”的常规文件3,但它存储在 tmpfs 上的事实意味着您仍然可以获得预期的速度提升。4

\n

(但是您也可以通过将 ./writeTest 程序直接指向 /dev/testfile 来获得这一点。)

\n

简而言之,虽然 Linux 有 ramdisk,但 tmpfs 几乎在所有情况下都是 ramdisk 的现代替代品。您不需要决定它的大小,或者显式“mkfs”文件系统 \xe2\x80\x93 只需在需要的地方安装一个 tmpfs:

\n
# mount -t tmpfs horse /tmp/ramdisk\n
Run Code Online (Sandbox Code Playgroud)\n
\n

1(如今,在大多数Linux 系统上,/dev 使用“devtmpfs”,这是一种特殊的 tmpfs 变体,一旦驱动程序识别出设备,设备节点就会自动出现;例如,一旦加载“brd”驱动程序,/dev/ ram0 将完全独立出现。但除此之外,它的行为就像一个常规的 tmpfs。)

\n

2(使用findmntmount检查您的发行版是否确实在 /tmp 上安装了 tmpfs。)

\n

3(即使它是一个文件,您仍然可以像磁盘一样挂载它,因为该mount命令在 2011 年更新为自动设置“循环设备”,而无需-o loop指定。)

\n
\n
\n

结果\xe2\x80\x99不太有说服力,因为RAM文件需要8秒,硬盘文件需要11秒,这似乎不是我应该得到的改进,对吧?

\n
\n

您看到两个测试运行速度相同的原因之一是写入被缓冲在内存 \xe2\x80\x93 中,您的程序不会等待每个写入完全提交到磁盘,然后再继续下一个;相反,所有内容都会在短时间内写入磁盘。

\n

(因为你的程序会寻找回 0 并完全覆盖数据,所以可能没有那么多数据从缓冲区移动到磁盘。)

\n

您看到这两项测试运行速度很快的另一个原因是现代 HDD速度相当快,而 SSD 速度更快。同时,如果你的作业要求你设置一个 ramdisk,这会让我认为它是在 1999 年编写的,磁盘速度预计为数十 MB/s。

\n

  • “同时,如果你的作业要求你设置一个 ramdisk,这会让我认为它是在 1999 年编写的,预计磁盘速度为数十 MB/s。”与所有从未更新的大学课程一样......你会期望 compsci 至少尝试一下,但不......这就是为什么这些学位大多毫无用处。 (13认同)
  • @IanKemp:我发现(通过有意且相当广泛的采访)“comps sci 学位毫无用处”的立场往往是基于 comp sci = 编程的假设。很明显,这种等价是错误的,持有这种信念的人将其建立在对计算机科学是什么的错误概念之上。你相信这种错误的对等吗? (4认同)