Android 系統文件限制小探

我們可以通過ulimit -a 用來顯示當前的各種資源限制。

rk3399_all:/ # ulimit -a
time(cpu-seconds)    unlimited
file(blocks)         unlimited
coredump(blocks)     unlimited
data(KiB)            unlimited
stack(KiB)           8192
lockedmem(KiB)       64
nofiles(descriptors) 1024
processes            15426
sigpending           15426
msgqueue(bytes)      819200
maxnice              40
maxrtprio            0
resident-set(KiB)    unlimited
address-space(KiB)   unlimited

在Linux系統中,每個進程可以使用的FD數量是有上限的,在Android中這個上限爲1024,表示每個進程可以創建的file descriptors 不能超多1024個。當系統某一文件的打開句柄數超過1024時,就會報錯:

"Too many open files"

那麼,這個值到底是哪裏定義的呢?

這邊根據descriptors的屬性,判斷應該在kernel的某個頭文件中,終於在:linux/posix_types.h 找到:

/*
 * This allows for 1024 file descriptors: if NR_OPEN is ever grown
 * beyond that you'll have to change this too. But 1024 fd's seem to be
 * enough even for such "real" unices like OSF/1, so hopefully this is
 * one limit that doesn't have to be changed [again].
 *
 * Note that POSIX wants the FD_CLEAR(fd,fdsetp) defines to be in
 * <sys/time.h> (and thus <linux/time.h>) - but this is a more logical
 * place for them. Solved by having dummy defines in <sys/time.h>.
 */

/*
 * This macro may have been defined in <gnu/types.h>. But we always
 * use the one here.
 */
#undef __FD_SETSIZE
#define __FD_SETSIZE    1024

這麼說來,是否我們直接在kernel中修改這個定義,就能生效。

興沖沖的修改了這個1024的值,編譯固件測試發現:Too yong too simple.

搜索研究一通,發現在bionic/libc/kernel$ vim README.TXT 有如下一段話:

Bionic comes with a processed set of all of the uapi Linux kernel headers that
can safely be included by userland applications and libraries.

These clean headers are automatically generated by several scripts located
in the 'bionic/kernel/tools' directory. The tools process the original
unmodified kernel headers in order to get rid of many annoying
declarations and constructs that usually result in compilation failure.

好吧,原來還要再經過一系列的獲取,然後通過bionic/libc/kernel/tools 裏面的腳本來生成。

這邊驗證修改:

external/kernel-headers/original/uapi$ vim linux/posix_types.h +22

#define __FD_SETSIZE    1036

然後執行工具裏:

./update_all.py

再來看看:

cc@ubuntu:~/7.1/external/kernel-headers/original/uapi$ gd .
diff --git a/original/uapi/linux/posix_types.h b/original/uapi/linux/posix_types.h
index 988f76e..44a2fc8 100644
--- a/original/uapi/linux/posix_types.h
+++ b/original/uapi/linux/posix_types.h
@@ -19,7 +19,7 @@
  * use the one here.
  */
 #undef __FD_SETSIZE
-#define __FD_SETSIZE   1024
+#define __FD_SETSIZE   1036
 

修改生效了。

總結:

      系統的FD  limit是在kernel中配置,但在android bionic這邊,依賴於kernel的配置值,但需要通過相關腳本更新。然後才能最終體現在編譯後的系統中。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章