不知道从哪个版本开始,macOS 最大文件数(max open files)改成了 1024,这对于使用 lsp 进行开发来说,显得有些小。而且这个问题并不能简单通过调大 ulimit 解决,在这个 reddit 帖子里,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 的开发者文档里也能找到印证:
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 的类似问题,里面有提到怎么修改:
|
|
Great!经过一番测试,成功将 emacs 的最大文件数改成 10000,这里总结下步骤:
调大系统级别 ulimit 的限制,可参考这个 gist 或 Mac OS X下的资源限制。新建文件
limit.maxfiles.plist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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。
1 2 3 4 5 6 7 8
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
从源码编译 Emacs,在 configure 时指定 CFLAGS 参数。参考命令:
1 2 3 4 5 6 7 8 9 10
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
这里指定
FD_SETSIZE
为 1w,需要小于 xml 中的 hard 限制。之后打开新编译的 Emacs 进行测试:1 2 3 4 5 6 7 8 9
(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 最大文件打开数就成功修改了!
签名
如果在启动 Emacs 时,遇到下面的错误:
|
|
可以尝试重新签名:
|
|