背景:
在编译新版本的aosp15对应的lineage版本时候发现,经常在brunch gemini(手机目标名字),会出现无故退出,或者terminal闪退的情况。
通过dmesg查看日志可以看到如下的报错:
bash
[393958.626463] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice/user-1000.slice/[email protected],task=Isolated Web Co,pid=695778,uid=1000
[393958.626505] Out of memory: Killed process 695778 (Isolated Web Co) total-vm:3728956kB, anon-rss:474468kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:4628kB oom_score_adj:167
即使用内存过多,导致oom了。那么针对上面的出现oom问题到底应该如何解决呢?
解决方法:
1、物理解决方法,直接增加内存条,把内存变成64G,这种方式可能最简单啥也不需要改变,不过很多同学可能也有条件限制不一定可以,比如内存插槽不够,或者是内存条贵。
2、增加swap虚拟内存,这种方法也是可以的,但是也需要消耗相关的硬盘空间等,需要重新扩展swap
3、不增加任何物理和swap虚拟内存,减少编译的线程
大部分同学都可能觉得最方便简单就是第3种方法,但是lineage os编译一般采用是brunch 命令,这个命令正常是不可以和make命令一样自由指定相关的线程数量,如果指定会导致无法编译报错如下:
bash
test@test:~/disk_2T/aosp15_xiaomi$ brunch -j 8 gemini
In file included from build/make/core/config.mk:394:
In file included from build/make/core/envsetup.mk:351:
build/make/core/product_config.mk:226: error: Cannot locate config makefile for product "lineage_".
15:59:26 dumpvars failed with: exit status 1
Device not found. Attempting to retrieve device repository from LineageOS Github (http://github.com/LineageOS).
Repository for not found in the LineageOS Github repository list. If this is in error, you may need to manually add it to your local_manifests/roomservice.xml.
In file included from build/make/core/config.mk:394:
In file included from build/make/core/envsetup.mk:351:
build/make/core/product_config.mk:226: error: Cannot locate config makefile for product "lineage_".
15:59:30 dumpvars failed with: exit status 1
In file included from build/make/core/config.mk:394:
In file included from build/make/core/envsetup.mk:351:
build/make/core/product_config.mk:226: error: Cannot locate config makefile for product "lineage_".
15:59:32 dumpvars failed with: exit status 1
** Don't have a product spec for: 'lineage_'
** Do you have the right repo manifest?
No such item in brunch menu. Try 'breakfast'
那么到底该怎么在编译lineage os减少编译线程呢?
方法1
原理:因为breakfast,brunch其实属于lineage os自定义的函数帮助我们包装了一些操作而已,本身aosp是没有的,lineage os本质还是基于aosp的,所以自然aosp的所有编译命令也可以正常使用。基于这个背景那么就可以采用如下几个步骤:
步骤1:breakfast gemini
步骤2:make -j n //这里的n就是线程数量
因为make只是make了相关的img文件,正常如果模拟器的话make就结束了,但img文件不是我们实体机打包的zip文件,所以要做出对应zip文件还是需要再使用brunch命令
步骤3:brunch gemini
因为真正耗费内存时候都是在make img文件时候,make img都成功后只剩下打包工作了就完全可以交给brunch来做。
当然上面3个命令也可以封装写入一个脚本一起执行哈。
方法2
原理:因为brunch本质只是个shell脚本的函数,所以完全可以考虑到shell文件中修改这个函数,从而在函数中指定编译的线程数量
看看brunch的函数源码:
注意brunch属于非aosp自带的函数,所以不在build/envsetup.sh文件中,一般在vendor路径的envsetup.sh有定义:
定义路径:vendor/lineage/build/envsetup.sh
先看看brunch方法
bash
function brunch()
{
breakfast $*
if [ $? -eq 0 ]; then
mka bacon
else
echo "No such item in brunch menu. Try 'breakfast'"
return 1
fi
return $?
}
可以看出来最后调用也是调用 mka bacon,注意 mka也是一个定义函数,bacon代表一个目标叫做培根,大家就理解为升级包的目标吧
那么看看mka方法:
cpp
function mka() {
m "$@"
}
这里可以看到mka实际就是调用了m命令,这里m命令我们还是比较熟悉,m命令完全可以指定线程进行编译,所以修改如下:
cpp
function mka() {
m -j 4 "$@"
}
变成指定4线程进行brunch
然后在回到更目录一定要重新进行env操作,否则不生效
bash
. build/envsetup.sh
brunch gemini
上面执行完就可以发现已经变成4线程编译了。