前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >修复 Emacs 在 macOS 下最大文件数为 1024 的问题

修复 Emacs 在 macOS 下最大文件数为 1024 的问题

作者头像
飞驰的西瓜
发布2022-07-26 16:52:10
6750
发布2022-07-26 16:52:10
举报
文章被收录于专栏:EmacsTalk

不知道从哪个版本开始,macOS 最大文件数(max open files)改成了 1024,这对于使用 lsp 进行开发来说,显得有些小。而且这个问题并不能简单通过调大 ulimit 解决,在这个 reddit 帖子[1]里,rpluim 用户提到:

Emacs uses pselect, which is limited to FD_SETSIZE file descriptors, usually 1024. I suspect you've got one of the file-watching utilities enabled in emacs, which tends to use up a lot of file descriptors. … Increasing the maxfiles limit will not change the value of FD_SETSIZE compiled into emacs and the macOS libraries. Emacs would have to move to using poll or kqueue to fully solve this issue.

在 macOS 的开发者文档[2]里也能找到印证:

The default size FD_SETSIZE (currently 1024) is some-what somewhat what smaller than the current kernel limit to the number of open files. However, in order to accommodate programs which might potentially use a larger number of open files with select, it is possible to increase this size within a program by providing a larger definition of FD_SETSIZE before the inclusion of <sys/types.h>.

但是文档里没提到怎么改,搜了下找到了一个 erlang 的类似问题[3],里面有提到怎么修改:

代码语言:javascript
复制
CFLAGS="-DFD_SETSIZE=10000 -DDARWIN_UNLIMITED_SELECT"

Great!经过一番测试,成功将 emacs 的最大文件数改成 10000,这里总结下步骤:

  1. 1. 调大系统级别 ulimit 的限制,可参考这个 gist[4] 或 Mac OS X下的资源限制[5]。新建文件 limit.maxfiles.plist
代码语言:javascript
复制
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
            "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>Label</key>
        <string>limit.maxfiles</string>
        <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxfiles</string>
          <string>64000</string>
          <string>524288</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceIPC</key>
        <false/>
      </dict>
    </plist>

上面的 xml 含义是把最大文件打开数的 soft/hard 限制分别改成 64000 与 524288。

代码语言:javascript
复制
     sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
     sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
    # 加载plist文件
     sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
    # 确认已经生效
    launchctl limit maxfiles
    ulimit -n
  1. 1. 下载 emacs 源码,在 configure 时指定 CFLAGS 参数。参考命令:
代码语言:javascript
复制
    git clone https://git.savannah.gnu.org/emacs && cd emacs
    git checkout emacs-28
    git clean -xf
    
    ./autogen.sh
    ./configure "CFLAGS=-DFD_SETSIZE=10000 -DDARWIN_UNLIMITED_SELECT"
    
    export CPATH=`xcrun --show-sdk-path`/usr/include:`xcrun --show-sdk-path`/usr/include/libxml2
    
    make -j 4 && make install

之后打开新编译的 Emacs 进行测试:

代码语言:javascript
复制
    (shell-command-to-string "ulimit -n")
    ;; 10000
    
    (dotimes (i 2000)
      (make-process
       :name (format "Sleep-%s" i)
       :buffer nil
       :command '("sleep" "60000")
       :connection-type 'pipe))

上面的 dotimes 代码块创建了 2000 个进程,之后用 lsof -p ${emacs_pid} | wc -l 查看打开的文件数,可以看到是 4000 多个,应该是一个 process 会打开两个文件:stdout 与 stderr。

这样,Emacs 最大文件打开数就成功修改了!

引用链接

[1] 这个 reddit 帖子: https://www.reddit.com/r/emacs/comments/mq2znn/comment/gudivjv/?utm_source=share&utm_medium=web2x&context=3 [2] 开发者文档: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/select.2.html [3] 类似问题: https://github.com/Homebrew/legacy-homebrew/issues/6143 [4] 这个 gist: https://gist.github.com/skylock/0117ec637d468f91260927b43b816eda [5] Mac OS X下的资源限制: https://wudaijun.com/2017/02/max-osx-ulimit/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 EmacsTalk 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引用链接
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档