Tianpu
V2EX  ›  问与答

/dev/shm 能不能当作一般文件系统使用?

  •  
  •   Tianpu · May 15, 2015 · 3001 views
    This topic created in 4047 days ago, the information mentioned may be changed or developed.
    有临时文件读写需求,写入频繁,无论是写入数据库、memcached、还是直接硬盘,都觉得不够快,这个能不能作为/tmp使用?

    不怕文件丢

    谢谢
    Supplement 1  ·  May 15, 2015
    简单测试下 结果很奇怪

    key value r/w memcached tmpfs
    32 33 10 2.6 1.6
    32 330 10 3.2 1.6
    32 3330 10 4.6 1.6

    tmpfs无论怎么测试 结果都是1.6ms 大概是内存响应速度吧
    Supplement 2  ·  May 15, 2015
    32 33330 10 21.6 3.3
    Supplement 3  ·  May 15, 2015
    测试代码:

    $k = md5(mt_rand());
    for($i=0;$i<10;$i++){
    $t.= md5(mt_rand()).PHP_EOL;
    }
    $s = microtime(true);
    for($i=0;$i<100;$i++){
    $memd->set($k,$t,100);
    $t = $memd->get($k);
    }
    echo $t;
    echo number_format((microtime(true)-$s)*1000,9).'ms'.PHP_EOL;
    $s = microtime(true);
    for($i=0;$i<100;$i++){
    file_put_contents(init_memdisk($k),$t);
    file_get_contents(init_memdisk($k));
    }
    echo $t;
    echo number_format((microtime(true)-$s)*1000,9).'ms'.PHP_EOL;
    Supplement 4  ·  May 15, 2015
    得把依赖memcached的部分数据迁移到tmpfs了 真快啊
    Supplement 5  ·  May 15, 2015
    系统响应时间降低 0.003s 效果比较显著
    7 replies    2015-05-18 07:55:58 +08:00
    Tiande
        1
    Tiande  
    PRO
       May 15, 2015
    move 到 /go/linux 节点比较好。

    当然可以直接当 内存盘 使用 ;)
    Tianpu
        2
    Tianpu  
    OP
       May 15, 2015
    多谢


    @dtdnqsb

    有无性能损失呢?相当于直接读内存,这样子的理解没问题吧?
    Septembers
        3
    Septembers  
       May 15, 2015
    Tianpu
        4
    Tianpu  
    OP
       May 15, 2015
    多谢

    那我能确定了 一堆计数器 定时写入数据库 即使是memcached也有一定的系统损耗 现在切换到/dev/shm的 以文件形式保存
    Tiande
        5
    Tiande  
    PRO
       May 15, 2015   ❤️ 1
    @Tianpu 性能损失我不懂,只知道快 ;)
    容量 inode 是有限制的,如果读的 文件数量 非常之多,可以重新 mount 一下:
    #mount -o size=6000M -o nr_inodes=4000000 -o noatime,nodiratime -o remount /dev/shm
    需要永久实现就改 /etc/fstab
    Tianpu
        6
    Tianpu  
    OP
       May 15, 2015
    @dtdnqsb 就几千个文件
    ryd994
        7
    ryd994  
       May 18, 2015 via Android
    memcached不是纯内存么?应该不比shm差啊?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1381 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 17:06 · PVG 01:06 · LAX 10:06 · JFK 13:06
    ♥ Do have faith in what you're doing.