lowmemkiller 分析_lowmemorykiller怎么排查-程序员宅基地

Android是一个多任务系统,也就是说可以同时运行多个程序,这个大家应该很熟悉。一般来说,启动运行一个程序是有一定的时间开销的,因此为了加快运行速度,当你退出一个程序时,Android并不会立即杀掉它,这样下次再运行该程序时,可以很快的启动。随着系统中保留的程序越来越多,内存肯定会出现不足,lowmemorykiller就是在系统内存低于某值时,清除相关的程序,保障系统保持拥有一定数量的空闲内存。


lowmemorykiller 根据进程重要性和进程优先级来选择进程kill。


linux内核原始lowmemkiller实现比较简单,初始化的时候将lowmem_shrinker注册到
shrink_list中,然后在kernel kswapd中会周期执行lowmem_shrink函数,
这个函数中通过冒泡排序找到oom_adj大于等于min_adj的进程,如果有多个进程时进一步找到
占用内存更大的进程kill掉。


代码中设计两个数组:
kill进程时用来比较的adj值
static short lowmem_adj[6] = {
0,
1,
6,
12,
};
kill进程时空闲内存的警戒值,当系统可用内存少于某个警戒值时就kill掉比对应lowmem_adj大于等于的进程
static int lowmem_minfree[6] = {//以page数为单位
3 * 512, /* 6MB */
2 * 1024, /* 8MB */
4 * 1024, /* 16MB */
16 * 1024, /* 64MB */
};


具体实现代码可以找来linux标准实现。
高通针对这部分代码有一些修改,重点讲下高通平台上代码实现。
高通加入了内存压力管理,内存使用达到一定限度的时候,会调整kill时选择的oom_score_adj。
//初始化时除了注册了shrink_list,另外注册了vmpressure的notify函数,
//同样在内存回收时会计算当前vmpressure值,然后再lowmem_shrinker中调整oom_score_adj
static int __init lowmem_init(void)
{
register_shrinker(&lowmem_shrinker);
vmpressure_notifier_register(&lmk_vmpr_nb);
return 0;
}


/*
NR_FILE_PAGES=global_page_state(NR_FILE_PAGES) 来自 vmstat[NR_FILE_PAGES],表示所有的缓存页(page cache)的总和,它包括:
Cached
buffers
交换区缓存(swap cache)
other_file:buffer+cached 页面数
other_free:空闲内存页面数
*/


static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
{
struct task_struct *tsk;
struct task_struct *selected = NULL;
int rem = 0;
int tasksize;
int i;
int ret = 0;
short min_score_adj = OOM_SCORE_ADJ_MAX + 1;
int minfree = 0;
int selected_tasksize = 0;
short selected_oom_score_adj;
int array_size = ARRAY_SIZE(lowmem_adj);
int other_free;
int other_file;
unsigned long nr_to_scan = sc->nr_to_scan;


if (nr_to_scan > 0) {
if (mutex_lock_interruptible(&scan_mutex) < 0)
return 0;
}


other_free = global_page_state(NR_FREE_PAGES);


if (global_page_state(NR_SHMEM) + total_swapcache_pages() <
global_page_state(NR_FILE_PAGES))
other_file = global_page_state(NR_FILE_PAGES) -
global_page_state(NR_SHMEM) -
total_swapcache_pages();
else
other_file = 0; 


tune_lmk_param(&other_free, &other_file, sc);


if (lowmem_adj_size < array_size)
array_size = lowmem_adj_size;
if (lowmem_minfree_size < array_size)
array_size = lowmem_minfree_size;
for (i = 0; i < array_size; i++) {
minfree = lowmem_minfree[i];
if (other_free < minfree && other_file < minfree) {
min_score_adj = lowmem_adj[i];
break;
}
}
if (nr_to_scan > 0) {
ret = adjust_minadj(&min_score_adj);
lowmem_print(3, "lowmem_shrink %lu, %x, ofree %d %d, ma %hd\n",
nr_to_scan, sc->gfp_mask, other_free,
other_file, min_score_adj);
}
//rem LRU list 页面总数
rem = global_page_state(NR_ACTIVE_ANON) +
global_page_state(NR_ACTIVE_FILE) +
global_page_state(NR_INACTIVE_ANON) +
global_page_state(NR_INACTIVE_FILE);
if (nr_to_scan <= 0 || min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
lowmem_print(5, "lowmem_shrink %lu, %x, return %d\n",
    nr_to_scan, sc->gfp_mask, rem);


if (nr_to_scan > 0)
mutex_unlock(&scan_mutex);


if ((min_score_adj == OOM_SCORE_ADJ_MAX + 1) &&
(nr_to_scan > 0))
trace_almk_shrink(0, ret, other_free, other_file, 0);


return rem;
}
selected_oom_score_adj = min_score_adj;


rcu_read_lock();
for_each_process(tsk) {
struct task_struct *p;
short oom_score_adj;


if (tsk->flags & PF_KTHREAD)//kernel thread 不能kill
continue;


/* if task no longer has any memory ignore it */
//task mm_struct结构被释放,task不再占用内存
if (test_task_flag(tsk, TIF_MM_RELEASED))
continue;

//if jiffies <= lowmem_deathpending_timeout
if (time_before_eq(jiffies, lowmem_deathpending_timeout)) {
if (test_task_flag(tsk, TIF_MEMDIE)) {//task ±»oomɱµô
rcu_read_unlock();
/* give the system time to free up the memory */
msleep_interruptible(20);
mutex_unlock(&scan_mutex);
return 0;
}
}
//tsk 的mm_struct可能已经被释放,但是它的线程还是有效的,此时返回它的线程
p = find_lock_task_mm(tsk);
if (!p)
continue;

// /proc/PID/oom_score_adj 
//这个值表示kill时的优先级,值越大越容易被kill
oom_score_adj = p->signal->oom_score_adj;
if (oom_score_adj < min_score_adj) {
task_unlock(p);
continue;
}
tasksize = get_mm_rss(p->mm);//进程占用的文件页加匿名页的大小
task_unlock(p);
if (tasksize <= 0)
continue;
if (selected) {//冒泡排序找到占用内存最大的进程
if (oom_score_adj < selected_oom_score_adj)
continue;
if (oom_score_adj == selected_oom_score_adj &&
   tasksize <= selected_tasksize)
continue;
}
selected = p;
selected_tasksize = tasksize;
selected_oom_score_adj = oom_score_adj;
lowmem_print(3, "select '%s' (%d), adj %hd, size %d, to kill\n",
    p->comm, p->pid, oom_score_adj, tasksize);
}
if (selected) {
long cache_size = other_file * (long)(PAGE_SIZE / 1024);
long cache_limit = minfree * (long)(PAGE_SIZE / 1024);
long free = other_free * (long)(PAGE_SIZE / 1024);
trace_lowmemory_kill(selected, cache_size, cache_limit, free);
lowmem_print(1, "Killing '%s' (%d), adj %hd,\n" \
"   to free %ldkB on behalf of '%s' (%d) because\n" \
"   cache %ldkB is below limit %ldkB for oom_score_adj %hd\n" \
"   Free memory is %ldkB above reserved.\n" \
"   Free CMA is %ldkB\n" \
"   Total reserve is %ldkB\n" \
"   Total free pages is %ldkB\n" \
"   Total file cache is %ldkB\n" \
"   Slab Reclaimable is %ldkB\n" \
"   Slab UnReclaimable is %ldkB\n" \
"   Total Slab is %ldkB\n" \
"   GFP mask is 0x%x\n",
    selected->comm, selected->pid,
    selected_oom_score_adj,
    selected_tasksize * (long)(PAGE_SIZE / 1024),
    current->comm, current->pid,
    cache_size, cache_limit,
    min_score_adj,
    free ,
    global_page_state(NR_FREE_CMA_PAGES) *
(long)(PAGE_SIZE / 1024),
    totalreserve_pages * (long)(PAGE_SIZE / 1024),
    global_page_state(NR_FREE_PAGES) *
(long)(PAGE_SIZE / 1024),
    global_page_state(NR_FILE_PAGES) *
(long)(PAGE_SIZE / 1024),
    global_page_state(NR_SLAB_RECLAIMABLE) *
(long)(PAGE_SIZE / 1024),
    global_page_state(NR_SLAB_UNRECLAIMABLE) *
(long)(PAGE_SIZE / 1024),
    global_page_state(NR_SLAB_RECLAIMABLE) *
(long)(PAGE_SIZE / 1024) +
    global_page_state(NR_SLAB_UNRECLAIMABLE) *
(long)(PAGE_SIZE / 1024),
    sc->gfp_mask);


if (lowmem_debug_level >= 2 && selected_oom_score_adj == 0) {
show_mem(SHOW_MEM_FILTER_NODES);
dump_tasks(NULL, NULL);
show_mem_call_notifiers();
}


lowmem_deathpending_timeout = jiffies + HZ;
send_sig(SIGKILL, selected, 0);
set_tsk_thread_flag(selected, TIF_MEMDIE);
rem -= selected_tasksize;
rcu_read_unlock();
/* give the system time to free up the memory */
msleep_interruptible(20);
trace_almk_shrink(selected_tasksize, ret,
other_free, other_file, selected_oom_score_adj);
} else {
trace_almk_shrink(1, ret, other_free, other_file, 0);
rcu_read_unlock();
}


lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
    nr_to_scan, sc->gfp_mask, rem);
mutex_unlock(&scan_mutex);
return rem;
}


void tune_lmk_param(int *other_free, int *other_file, struct shrink_control *sc)
{
gfp_t gfp_mask;
struct zone *preferred_zone;
struct zonelist *zonelist;
enum zone_type high_zoneidx, classzone_idx;
unsigned long balance_gap;
int use_cma_pages;


gfp_mask = sc->gfp_mask;
adjust_gfp_mask(&gfp_mask);


zonelist = node_zonelist(0, gfp_mask);
high_zoneidx = gfp_zone(gfp_mask);
//找到第一个扫描的zone区
first_zones_zonelist(zonelist, high_zoneidx, NULL, &preferred_zone); 
classzone_idx = zone_idx(preferred_zone);
use_cma_pages = can_use_cma_pages(gfp_mask);


//获取zone low watermark页面数跟zone当前所有的页面数较小值
balance_gap = min(low_wmark_pages(preferred_zone),
 (preferred_zone->present_pages +
  KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
  KSWAPD_ZONE_BALANCE_GAP_RATIO);

//当前进程是kswap并且zone水位满足要求,即空闲页面大于mark值
if (likely(current_is_kswapd() && zone_watermark_ok(preferred_zone, 0,
 high_wmark_pages(preferred_zone) + SWAP_CLUSTER_MAX +
 balance_gap, 0, 0))) {
if (lmk_fast_run)//use this
tune_lmk_zone_param(zonelist, classzone_idx, other_free,
      other_file, use_cma_pages);
else
tune_lmk_zone_param(zonelist, classzone_idx, other_free,
      NULL, use_cma_pages);


if (zone_watermark_ok(preferred_zone, 0, 0, _ZONE, 0)) {
if (!use_cma_pages) {
*other_free -= min(
 preferred_zone->lowmem_reserve[_ZONE]
 + zone_page_state(
   preferred_zone, NR_FREE_CMA_PAGES),
 zone_page_state(
   preferred_zone, NR_FREE_PAGES));
} else {
*other_free -=
 preferred_zone->lowmem_reserve[_ZONE];
}
} else {
*other_free -= zone_page_state(preferred_zone,
     NR_FREE_PAGES);
}


lowmem_print(4, "lowmem_shrink of kswapd tunning for highmem "
    "ofree %d, %d\n", *other_free, *other_file);
} else {
tune_lmk_zone_param(zonelist, classzone_idx, other_free,
      other_file, use_cma_pages);


if (!use_cma_pages) {
*other_free -=
 zone_page_state(preferred_zone, NR_FREE_CMA_PAGES);
}


lowmem_print(4, "lowmem_shrink tunning for others ofree %d, "
    "%d\n", *other_free, *other_file);
}
}


//扫描zone区调整other_free other_file的值
void tune_lmk_zone_param(struct zonelist *zonelist, int classzone_idx,
int *other_free, int *other_file,
int use_cma_pages)
{
struct zone *zone;
struct zoneref *zoneref;
int zone_idx;


for_each_zone_zonelist(zone, zoneref, zonelist, MAX_NR_ZONES) {
zone_idx = zonelist_zone_idx(zoneref);
if (zone_idx == ZONE_MOVABLE) {
if (!use_cma_pages && other_free)
*other_free -=
   zone_page_state(zone, NR_FREE_CMA_PAGES);
continue;
}


if (zone_idx > classzone_idx) {
if (other_free != NULL)
*other_free -= zone_page_state(zone,
      NR_FREE_PAGES);
if (other_file != NULL)
*other_file -= zone_page_state(zone,
      NR_FILE_PAGES)
- zone_page_state(zone, NR_SHMEM)
- zone_page_state(zone, NR_SWAPCACHE);
} else if (zone_idx < classzone_idx) {
if (zone_watermark_ok(zone, 0, 0, classzone_idx, 0) &&
   other_free) {
if (!use_cma_pages) {
*other_free -= min(
 zone->lowmem_reserve[classzone_idx] +
 zone_page_state(
   zone, NR_FREE_CMA_PAGES),
 zone_page_state(
   zone, NR_FREE_PAGES));
} else {
*other_free -=
 zone->lowmem_reserve[classzone_idx];
}
} else {
if (other_free)
*other_free -=
 zone_page_state(zone, NR_FREE_PAGES);
}
}
}
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014089131/article/details/59117952

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签