joy keeps flowin'

验证suspend all threads

xx
目次

验证suspend all threads #

对应KOOM实现流程, 结合对比几种场景, 验证KOOM中fork前suspend和resume后作用.

suspend和resume #

suspendAndFork

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
pid_t HprofDump::SuspendAndFork() {
  KCHECKI(init_done_)
  if (android_api_ < __ANDROID_API_R__) {
    suspend_vm_fnc_();
  } else if (android_api_ <= __ANDROID_API_S__) {
    void *self = __get_tls()[TLS_SLOT_ART_THREAD_SELF];
    sgc_constructor_fnc_((void *)sgc_instance_.get(), self, kGcCauseHprof,
                         kCollectorTypeHprof);
    ssa_constructor_fnc_((void *)ssa_instance_.get(), LOG_TAG, true);
    // avoid deadlock with child process
    exclusive_unlock_fnc_(*mutator_lock_ptr_, self);
    sgc_destructor_fnc_((void *)sgc_instance_.get());
  }
  pid_t pid = fork();
  if (pid == 0) {
    // Set timeout for child process
    alarm(60);
    prctl(PR_SET_NAME, "forked-dump-process");
  }
  return pid;
}

resumeAndWait

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool HprofDump::ResumeAndWait(pid_t pid) {
  KCHECKB(init_done_)
  if (android_api_ < __ANDROID_API_R__) {
    resume_vm_fnc_();
  } else if (android_api_ <= __ANDROID_API_S__) {
    void *self = __get_tls()[TLS_SLOT_ART_THREAD_SELF];
    exclusive_lock_fnc_(*mutator_lock_ptr_, self);
    ssa_destructor_fnc_((void *)ssa_instance_.get());
  }
  int status;
  for (;;) {
    if (waitpid(pid, &status, 0) != -1) {
      if (!WIFEXITED(status)) {
        ALOGE("Child process %d exited with status %d, terminated by signal %d",
              pid, WEXITSTATUS(status), WTERMSIG(status));
        return false;
      }
      return true;
    }
    // if waitpid is interrupted by the signal,just call it again
    if (errno == EINTR){
      continue;
    }
    return false;
  }
}

suspend验证 #

运行环境:一部虚拟机Android 9,一部物理机器Android 13

除Android13标题下的, 环境均为Android9的虚拟机.

运行KOOM Demo #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2024-02-27 11:17:25.346 12889-12889 OOMMonitor              com.kwai.koom.demo                   I  startLoop()
2024-02-27 11:17:30.412 12889-12940 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  ----OOM Monitor Memory----
2024-02-27 11:17:30.412 12889-12940 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  [java] max:536870912 used ratio:1%
2024-02-27 11:17:30.412 12889-12940 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  [proc] VmSize:2695044kB VmRss:135440kB Threads:715
2024-02-27 11:17:30.412 12889-12940 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  [meminfo] MemTotal:2042232kB MemFree:352144kB MemAvailable:881440kB
2024-02-27 11:17:30.412 12889-12940 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  avaliable ratio:43% CmaTotal:303104kB ION_heap:0kB
2024-02-27 11:17:30.412 12889-12940 OOMMonitor...OOMTracker com.kwai.koom.demo                   I  [meet condition] overThresholdCount:1, threadCount: 715
2024-02-27 11:17:30.413 12889-12940 OOMMonitor...OOMTracker com.kwai.koom.demo                   I  over threshold dumpThreadIfNeed
2024-02-27 11:17:30.833 12889-12940 OOMMonitor...OOMTracker com.kwai.koom.demo                   I  threadNames = [.kwai.koom.demo, Jit thread pool, Signal Catcher, ADB-JDWP Connec, HeapTaskDaemon, ReferenceQueueD, FinalizerDaemon, FinalizerWatchd, Binder:12889_1, Binder:12889_2, Binder:12889_3, Profile Saver, RenderThread, queued-work-loo, LoopThread, Thread-2, Thread-3, Thread-4, Thread-5, Thread-6, Thread-7, Thread-8, Thread-9, Thread-10, Thread-11, Thread-12, Thread-13, Thread-14, Thread-15, Thread-16, Thread-17, Thread-18, Thread-19, Thread-20, Thread-21, Thread-22, Thread-23, Thread-24, Thread-25, Thread-26, Thread-27, Thread-28, Thread-29, Thread-30, Thread-31, Thread-32, Thread-33, Thread-34, Thread-35, Thread-36, Thread-37, Thread-38, Thread-39, Thread-40, Thread-41, Thread-42, Thread-43, Thread-44, Thread-45, Thread-46, Thread-47, Thread-48, Thread-49, Thread-50, Thread-51, Thread-52, Thread-53, Thread-54, Thread-55, Thread-56, Thread-57, Thread-58, Thread-59, Thread-60, Thread-61, Thread-62, Thread-63, Thread-64, Thread-65, Thread-66, Thread-67, Thread-68, Thread-69, Thread-70, Thread-71, Thread-72, Thread-73, Thread-74, Thread-75, Thread-76, Thread-77, Thread-78, Thread-79, Thread-80, Thread-81, Thread-82, Thread-83, Thread-84, Thread-85, Thread-86, Thread-87, Thread-88, Thread-89, Thread-90, Thread-91, Thread-92, Thread-93, Thread-94, Thread-95, Thread-96, Thread-97, Thread-98, Thread-99, Thread-100, Thread-101, Thread-102, Thread-103, Thread-104, Thread-105, Thread-106, Thread-107, Thread-108, Thread-109, Thread-110, Thread-111, Thread-112, Thread-113, Thread-114, Thread-115, Thread-116, Thread-117, Thread-118, Thread-119, Thread-120, Thread-121, Thread-122, Thread-123, Thread-124, Thread-125, Thread-126, Thread-127, Thread-128, Thread-129, Thread-130, Thread-131, Thread-132, Thread-133, Thread-134, Thread-135, Thread-136, Thread-137, Thread-138, Thread-139, Thread-140, Thread-141, Thread-142, Thread-143, Thread-144, Thread-145, Thread-146, Thread-147, Thread-148, Thread-149, Thread-150, Thread-151, Thread-152, Thread-153, Thread-154, Thread-155, Thread-156, Thread-157, Thread-158, Thread-159, Thread-160, Thread-161, Thread-162, Thread-163, Thread-164, Thread-165, Thread-166, Thread-167, Thread-168, Thread-169, Thread-170, Thread-171, Thread-172, Thread-173, Thread-174, Thread-175, Thread-176, Thread-177, Thread-178, Thread-179, Thread-180, Thread-181, Thread-182, Thread-183, Thread-184, Thread-185, Thread-186, Thread-187, Thread-188, Thread-189, Thread-190, Thread-191, Thread-192, Thread-193, Thread-194, Thread-195, Thread-196, Thread-197, Thread-198, Thread-199, Thread-200, Thread-201, Thread-202, Thread-203, Thread-204, Thread-205, Thread-206, Thread-207, Thread-208, Thread-209, Thread-210, Thread-211, Thread-212, Thread-213, Thread-214, Thread-215, Thread-216, Thread-217, Thread-218, Thread-219, Thread-220, Thread-221, Thread-222, Thread-223, Thread-224, Thread-225, Thread-226, Thread-227, Thread-228, Thread-229, Thread-230, Thread-231, Thread-232, Thread-233, Thread-234, Thread-235, Thread-236, Thread-237, Thread-238, Thread-239, Thread-240, Thread-241, Thread-242, Thread-243, Thread-244, Thread-245, Thread-246, Thread-247, Thread-248, Thread-249, Thread-250, Thread-251, Thread-252, Thread-253, Thread-254, Thread-255, Thread-256, Thread-257, Thread-258, Thread-259, Thread-260, Thread-261, Thread-262, Thread-263, Thread-264, Thread-265, Thread-266, Thread-267, Thread-268, Thread-269, Thread-270, Thread-271, Thread-272, Thread-273, Thread-274, Thread-275, Thread-276, Thread-277, Thread-278, Thread-279, Thread-280, Thread-281, Thread-282, Thread-283, Thread-284, Thread-285, Thread-286, Thread-287, Thread-288, Thread-289, Thread-290, Thread-291, Thread-292, Thread-293, Thread-294, Thread-295, Thread-296, Thread-297, Thread-298, Thread-299, Thread-300, Thread-301, Thread-302, Thread-303, Thread-304, Thread-305, Thread-306, Thread-307, Thread-308, Thread-309, Thread-310, Thread-311, Thread-312, Thread-313, Thread-314, Thread-315, Thread-316, Thread-317, Thread-318, Thread-319, Thread-320, Thread-321, Thread-322, Thread-323, Thread-324, Thread-325, T
2024-02-27 11:17:30.847 12889-12940 OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709003366019
2024-02-27 11:17:30.847 12889-12940 OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:4
2024-02-27 11:17:30.849 12889-13644 OOMMonitor              com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-02-27 11:17:30.850 12889-13644 OOMMonitor              com.kwai.koom.demo                   I  dumpAndAnalysis
2024-02-27 11:17:30.852 12889-13645 OOMMonitor              com.kwai.koom.demo                   I  processHprofFile
2024-02-27 11:17:30.852 12889-13645 OOMMonitor              com.kwai.koom.demo                   I  delete other version files thread
2024-02-27 11:17:30.863 12889-13645 OOMMonitor              com.kwai.koom.demo                   I  delete old files
2024-02-27 11:17:30.871 12889-13644 OOMMonitor              com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-02-27 11:17:30.872 12889-13644 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-02-27_11-17-30_851.hprof
2024-02-27 11:17:30.894 12889-13644 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-02-27 11:17:30.954 12889-13645 OOMMonitor              com.kwai.koom.demo                   I  delete other version files fd
---------------------------- PROCESS STARTED (13646) for package com.kwai.koom.demo ----------------------------
2024-02-27 11:17:32.199 13646-13646 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump, spend:1305
2024-02-27 11:17:32.206 12889-13644 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  parent process wait time: 1312
2024-02-27 11:17:32.206 12889-13644 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump true, notify from pid 13646
2024-02-27 11:17:32.206 12889-13644 OOMMonitor              com.kwai.koom.demo                   I  end hprof dump
---------------------------- PROCESS ENDED (13646) for package com.kwai.koom.demo ----------------------------

多线程环境 #

主线程fork #

Java创建线程调用C++ pthread创建

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Java创建线程
static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
                                jboolean daemon) {
  ...
  Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
}






void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) {
  CHECK(java_peer != NULL);
  ...
  pthread_t new_pthread;
  pthread_attr_t attr;
  CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
  CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED");
  CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size);
  int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread);
  CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
  ...
}

子线程fork #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <chrono>
#include <thread>


void *func1(void *args)
{
    while (1)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));


        printf("func1 is printing.\n");
    }
}


int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, func1, NULL);
    sleep(3);
    pid_t pid = fork();
    // 子进程
    if (pid == 0)
    {
        int t = 5;
        while (t-- > 0)
        {
            sleep(1);
            printf("child process\n");
        }
    }
    return 0;
}

打印信息:

1
2
3
4
5
6
7
8
9
func1 is printing.
func1 is printing.
func1 is printing.
func1 is printing.
child process
child process
child process
child process
child process

子线程执行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <chrono>
#include <thread>


void *func1(void *args)
{
    // fork()出一个子进程
    pid_t pid = fork();
    if (pid < 0)
    {
        // 创建子进程失败
        printf("Failed to fork a child process\n");
        return NULL;
    }
    else if (pid == 0)
    {
        // 子进程
        printf("Child process pid = %d\n", getpid());
        while (1)
        {
            sleep(1);
            printf("func1: my pid = %d\n", getpid());
        }
    }
    else
    {
        // 父进程
        printf("Parent process pid = %d, child pid = %d\n", getpid(), pid);
        return NULL;
    }
}


int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, func1, NULL);
    // 主线程等待一段时间
    sleep(20);
    while (1)
    {
        sleep(1);
        printf("main : my pid = %d\n", getpid());
    }
    return 0;
}

打印日志

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Child process pid = 56330
func1: my pid = 56330
func1: my pid = 56330
func1: my pid = 56330
func1: my pid = 56330
func1: my pid = 56330
func1: my pid = 56330
func1: my pid = 56330
func1: my pid = 56330
...

不执行suspend和resume #

没有子线程运行(Running) #

注释C++调用suspend和resume方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2024-02-27 11:13:42.395 10991-10991 OOMMonitor              com.kwai.koom.demo                   I  startLoop()
2024-02-27 11:13:47.452 10991-11040 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  ----OOM Monitor Memory----
2024-02-27 11:13:47.452 10991-11040 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  [java] max:536870912 used ratio:1%
2024-02-27 11:13:47.452 10991-11040 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  [proc] VmSize:2685572kB VmRss:138468kB Threads:715
2024-02-27 11:13:47.452 10991-11040 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  [meminfo] MemTotal:2042232kB MemFree:368948kB MemAvailable:893380kB
2024-02-27 11:13:47.453 10991-11040 OOMMonitor_SystemInfo   com.kwai.koom.demo                   I  avaliable ratio:43% CmaTotal:303104kB ION_heap:0kB
2024-02-27 11:13:47.453 10991-11040 OOMMonitor...OOMTracker com.kwai.koom.demo                   I  [meet condition] overThresholdCount:1, threadCount: 715
2024-02-27 11:13:47.453 10991-11040 OOMMonitor...OOMTracker com.kwai.koom.demo                   I  over threshold dumpThreadIfNeed
2024-02-27 11:13:47.731 10991-11040 OOMMonitor...OOMTracker com.kwai.koom.demo                   I  threadNames = [.kwai.koom.demo, Jit thread pool, Signal Catcher, ADB-JDWP Connec, HeapTaskDaemon, ReferenceQueueD, FinalizerDaemon, FinalizerWatchd, Binder:10991_1, Binder:10991_2, Binder:10991_3, Profile Saver, RenderThread, queued-work-loo, LoopThread, Thread-2, Thread-3, Thread-4, Thread-5, Thread-6, Thread-7, Thread-8, Thread-9, Thread-10, Thread-11, Thread-12, Thread-13, Thread-14, Thread-15, Thread-16, Thread-17, Thread-18, Thread-19, Thread-20, Thread-21, Thread-22, Thread-23, Thread-24, Thread-25, Thread-26, Thread-27, Thread-28, Thread-29, Thread-30, Thread-31, Thread-32, Thread-33, Thread-34, Thread-35, Thread-36, Thread-37, Thread-38, Thread-39, Thread-40, Thread-41, Thread-42, Thread-43, Thread-44, Thread-45, Thread-46, Thread-47, Thread-48, Thread-49, Thread-50, Thread-51, Thread-52, Thread-53, Thread-54, Thread-55, Thread-56, Thread-57, Thread-58, Thread-59, Thread-60, Thread-61, Thread-62, Thread-63, Thread-64, Thread-65, Thread-66, Thread-67, Thread-68, Thread-69, Thread-70, Thread-71, Thread-72, Thread-73, Thread-74, Thread-75, Thread-76, Thread-77, Thread-78, Thread-79, Thread-80, Thread-81, Thread-82, Thread-83, Thread-84, Thread-85, Thread-86, Thread-87, Thread-88, Thread-89, Thread-90, Thread-91, Thread-92, Thread-93, Thread-94, Thread-95, Thread-96, Thread-97, Thread-98, Thread-99, Thread-100, Thread-101, Thread-102, Thread-103, Thread-104, Thread-105, Thread-106, Thread-107, Thread-108, Thread-109, Thread-110, Thread-111, Thread-112, Thread-113, Thread-114, Thread-115, Thread-116, Thread-117, Thread-118, Thread-119, Thread-120, Thread-121, Thread-122, Thread-123, Thread-124, Thread-125, Thread-126, Thread-127, Thread-128, Thread-129, Thread-130, Thread-131, Thread-132, Thread-133, Thread-134, Thread-135, Thread-136, Thread-137, Thread-138, Thread-139, Thread-140, Thread-141, Thread-142, Thread-143, Thread-144, Thread-145, Thread-146, Thread-147, Thread-148, Thread-149, Thread-150, Thread-151, Thread-152, Thread-153, Thread-154, Thread-155, Thread-156, Thread-157, Thread-158, Thread-159, Thread-160, Thread-161, Thread-162, Thread-163, Thread-164, Thread-165, Thread-166, Thread-167, Thread-168, Thread-169, Thread-170, Thread-171, Thread-172, Thread-173, Thread-174, Thread-175, Thread-176, Thread-177, Thread-178, Thread-179, Thread-180, Thread-181, Thread-182, Thread-183, Thread-184, Thread-185, Thread-186, Thread-187, Thread-188, Thread-189, Thread-190, Thread-191, Thread-192, Thread-193, Thread-194, Thread-195, Thread-196, Thread-197, Thread-198, Thread-199, Thread-200, Thread-201, Thread-202, Thread-203, Thread-204, Thread-205, Thread-206, Thread-207, Thread-208, Thread-209, Thread-210, Thread-211, Thread-212, Thread-213, Thread-214, Thread-215, Thread-216, Thread-217, Thread-218, Thread-219, Thread-220, Thread-221, Thread-222, Thread-223, Thread-224, Thread-225, Thread-226, Thread-227, Thread-228, Thread-229, Thread-230, Thread-231, Thread-232, Thread-233, Thread-234, Thread-235, Thread-236, Thread-237, Thread-238, Thread-239, Thread-240, Thread-241, Thread-242, Thread-243, Thread-244, Thread-245, Thread-246, Thread-247, Thread-248, Thread-249, Thread-250, Thread-251, Thread-252, Thread-253, Thread-254, Thread-255, Thread-256, Thread-257, Thread-258, Thread-259, Thread-260, Thread-261, Thread-262, Thread-263, Thread-264, Thread-265, Thread-266, Thread-267, Thread-268, Thread-269, Thread-270, Thread-271, Thread-272, Thread-273, Thread-274, Thread-275, Thread-276, Thread-277, Thread-278, Thread-279, Thread-280, Thread-281, Thread-282, Thread-283, Thread-284, Thread-285, Thread-286, Thread-287, Thread-288, Thread-289, Thread-290, Thread-291, Thread-292, Thread-293, Thread-294, Thread-295, Thread-296, Thread-297, Thread-298, Thread-299, Thread-300, Thread-301, Thread-302, Thread-303, Thread-304, Thread-305, Thread-306, Thread-307, Thread-308, Thread-309, Thread-310, Thread-311, Thread-312, Thread-313, Thread-314, Thread-315, Thread-316, Thread-317, Thread-318, Thread-319, Thread-320, Thread-321, Thread-322, Thread-323, Thread-324, Thread-325, T
2024-02-27 11:13:47.738 10991-11040 OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709003366019
2024-02-27 11:13:47.738 10991-11040 OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:2
2024-02-27 11:13:47.741 10991-11746 OOMMonitor              com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-02-27 11:13:47.741 10991-11746 OOMMonitor              com.kwai.koom.demo                   I  dumpAndAnalysis
2024-02-27 11:13:47.743 10991-11747 OOMMonitor              com.kwai.koom.demo                   I  processHprofFile
2024-02-27 11:13:47.743 10991-11747 OOMMonitor              com.kwai.koom.demo                   I  delete other version files thread
2024-02-27 11:13:47.752 10991-11747 OOMMonitor              com.kwai.koom.demo                   I  delete old files
2024-02-27 11:13:47.753 10991-11746 OOMMonitor              com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-02-27 11:13:47.754 10991-11746 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-02-27_11-13-47_742.hprof
2024-02-27 11:13:47.762 10991-11747 OOMMonitor              com.kwai.koom.demo                   I  delete other version files fd
2024-02-27 11:13:47.771 10991-11746 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
---------------------------- PROCESS STARTED (11748) for package com.kwai.koom.demo ----------------------------
2024-02-27 11:13:48.920 11748-11748 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump, spend:1149
2024-02-27 11:13:48.927 10991-11746 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  parent process wait time: 1156
2024-02-27 11:13:48.927 10991-11746 OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump true, notify from pid 11748
2024-02-27 11:13:48.927 10991-11746 OOMMonitor              com.kwai.koom.demo                   I  end hprof dump
---------------------------- PROCESS ENDED (11748) for package com.kwai.koom.demo ----------------------------

父进程fork前有子线程在运行,子进程dump #

fork前启动了一个线程,死循环执行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
2024-02-29 16:40:02.053  2594-2657  OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709191024825
2024-02-29 16:40:02.053  2594-2657  OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:1
2024-02-29 16:40:02.056  2594-3570  OOMMonitor              com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-02-29 16:40:02.056  2594-3570  OOMMonitor              com.kwai.koom.demo                   I  dumpAndAnalysis
2024-02-29 16:40:02.061  2594-3571  OOMMonitor              com.kwai.koom.demo                   I  processHprofFile
2024-02-29 16:40:02.061  2594-3571  OOMMonitor              com.kwai.koom.demo                   I  delete other version files thread
2024-02-29 16:40:02.071  2594-3571  OOMMonitor              com.kwai.koom.demo                   I  create json file and then start service
2024-02-29 16:40:02.073  2594-3571  OOMMonitor              com.kwai.koom.demo                   I  hprof file size 0
2024-02-29 16:40:02.079  2594-3570  OOMMonitor              com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-02-29 16:40:02.080  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-02-29_16-40-02_060.hprof
2024-02-29 16:40:02.090  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 712
2024-02-29 16:40:02.090  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-02-29 16:40:02.126  3577-3577  OOMMonitor...HeapDumper pid-3577                             I  child process
---------------------------- PROCESS STARTED (3577) for package com.kwai.koom.demo ----------------------------
2024-02-29 16:41:02.162  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  t1:RUNNABLE
2024-02-29 16:41:02.162  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  t2:TERMINATED
2024-02-29 16:41:02.162  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  t3:TERMINATED
2024-02-29 16:41:02.162  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump false, notify from pid 3577
2024-02-29 16:41:02.163  2594-3570  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  parent process wait time: 60029
2024-02-29 16:41:02.163  2594-3570  OOMMonitor              com.kwai.koom.demo                   I  end hprof dump

结果: 多了一个正在运行的线程

image子进程执行dump到60s结束

父进程fork前有子线程在运行,子进程suspend #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
2024-03-01 14:05:41.932 18915-18915 OOMMonitor               com.kwai.koom.demo                   I  startLoop()
2024-03-01 14:05:47.540 18915-19076 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709191024825
2024-03-01 14:05:47.541 18915-19076 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:4
2024-03-01 14:05:47.548 18915-19780 OOMMonitor               com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-03-01 14:05:47.548 18915-19780 OOMMonitor               com.kwai.koom.demo                   I  dumpAndAnalysis
2024-03-01 14:05:47.551 18915-19781 OOMMonitor               com.kwai.koom.demo                   I  processHprofFile
2024-03-01 14:05:47.552 18915-19781 OOMMonitor               com.kwai.koom.demo                   I  delete other version files thread
2024-03-01 14:05:47.575 18915-19780 OOMMonitor               com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-03-01 14:05:47.578 18915-19780 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-03-01_14-05-47_549.hprof
2024-03-01 14:05:47.600 18915-19780 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 711
2024-03-01 14:05:47.600 18915-19780 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-03-01 14:05:47.684 19783-19783 OOMMonitor_...HeapDumper pid-19783                            I  child process
---------------------------- PROCESS STARTED (19783) for package com.kwai.koom.demo ----------------------------
2024-03-01 14:06:47.752 18915-19780 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump false, notify from pid 19783
2024-03-01 14:06:47.752 18915-19780 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process wait: 60070
2024-03-01 14:06:47.753 18915-19780 OOMMonitor               com.kwai.koom.demo                   I  end hprof dump
---------------------------- PROCESS ENDED (19783) for package com.kwai.koom.demo ----------------------------

父进程有正在运行线程,fork前suspend,子进程dump #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2024-03-01 14:22:24.453 20998-20998 OOMMonitor               com.kwai.koom.demo                   I  startLoop()
2024-03-01 14:22:29.986 20998-21049 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709191024825
2024-03-01 14:22:29.986 20998-21049 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:4
2024-03-01 14:22:29.990 20998-21757 OOMMonitor               com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-03-01 14:22:29.990 20998-21757 OOMMonitor               com.kwai.koom.demo                   I  dumpAndAnalysis
2024-03-01 14:22:29.999 20998-21758 OOMMonitor               com.kwai.koom.demo                   I  processHprofFile
2024-03-01 14:22:30.000 20998-21758 OOMMonitor               com.kwai.koom.demo                   I  delete other version files thread
2024-03-01 14:22:30.015 20998-21757 OOMMonitor               com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-03-01 14:22:30.015 20998-21758 OOMMonitor               com.kwai.koom.demo                   I  create json file and then start service
2024-03-01 14:22:30.018 20998-21757 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-03-01_14-22-29_992.hprof
2024-03-01 14:22:30.018 20998-21758 OOMMonitor               com.kwai.koom.demo                   I  hprof file size 0
2024-03-01 14:22:30.036 20998-21757 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 710
2024-03-01 14:22:30.037 20998-21757 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-03-01 14:22:30.147 21760-21760 OOMMonitor_...HeapDumper pid-21760                            I  parent process suspend time: 40
2024-03-01 14:22:30.147 21760-21760 OOMMonitor_...HeapDumper pid-21760                            I  child process
2024-03-01 14:22:30.148 21760-21760 OOMMonitor_...HeapDumper pid-21760                            I  child process active thread count: 710
2024-03-01 14:22:30.148 21760-21760 OOMMonitor_...HeapDumper pid-21760                            I  suspend all thread, spend time: 1
2024-03-01 14:22:30.152 20998-21757 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process suspend time: 40
---------------------------- PROCESS STARTED (21760) for package com.kwai.koom.demo ----------------------------
2024-03-01 14:22:32.005 20998-21757 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump true, notify from pid 21760
2024-03-01 14:22:32.006 20998-21757 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process wait: 1854
2024-03-01 14:22:32.006 20998-21757 OOMMonitor               com.kwai.koom.demo                   I  end hprof dump
---------------------------- PROCESS ENDED (21760) for package com.kwai.koom.demo ----------------------------

结论: 子进程耗时没有卡住,dump的第一个步骤是suspend,长耗时是suspend造成的

Android 13 #

alarm改成180 #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
2024-02-29 17:26:28.568  7764-7764  OOMMonitor              com.kwai.koom.demo                   I  startLoop()
2024-02-29 17:26:34.506  7764-7822  OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709191024825
2024-02-29 17:26:34.506  7764-7822  OOMMonitor              com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:2
2024-02-29 17:26:34.509  7764-8534  OOMMonitor              com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-02-29 17:26:34.509  7764-8534  OOMMonitor              com.kwai.koom.demo                   I  dumpAndAnalysis
2024-02-29 17:26:34.512  7764-8535  OOMMonitor              com.kwai.koom.demo                   I  processHprofFile
2024-02-29 17:26:34.516  7764-8535  OOMMonitor              com.kwai.koom.demo                   I  delete other version files thread
2024-02-29 17:26:34.525  7764-8535  OOMMonitor              com.kwai.koom.demo                   I  delete old files
2024-02-29 17:26:34.540  7764-8535  OOMMonitor              com.kwai.koom.demo                   I  delete other version files fd
2024-02-29 17:26:34.777  7764-8534  OOMMonitor              com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-02-29 17:26:34.778  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-02-29_17-26-34_512.hprof
2024-02-29 17:26:34.800  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 712
2024-02-29 17:26:34.801  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-02-29 17:26:34.838  8539-8539  OOMMonitor...HeapDumper pid-8539                             I  child process
---------------------------- PROCESS STARTED (8539) for package com.kwai.koom.demo ----------------------------
2024-02-29 17:29:39.794  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  t1:RUNNABLE
2024-02-29 17:29:39.794  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  t2:TERMINATED
2024-02-29 17:29:39.794  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  t3:TERMINATED
2024-02-29 17:29:39.795  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  dump false, notify from pid 8539
2024-02-29 17:29:39.795  7764-8534  OOMMonitor...HeapDumper com.kwai.koom.demo                   I  parent process wait time: 184958
2024-02-29 17:29:39.795  7764-8534  OOMMonitor              com.kwai.koom.demo                   I  end hprof dump

结论: Android 13不fork没有子线程运行dump(KOOM fork前suspend方法不生效)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
2024-03-01 10:24:38.321 19711-19711 OOMMonitor               com.kwai.koom.demo                   I  startLoop()
2024-03-01 10:24:43.472 19711-19853 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709201524073
2024-03-01 10:24:43.472 19711-19853 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:3
2024-03-01 10:24:43.473 19711-20567 OOMMonitor               com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-03-01 10:24:43.473 19711-20567 OOMMonitor               com.kwai.koom.demo                   I  dumpAndAnalysis
2024-03-01 10:24:43.473 19711-20568 OOMMonitor               com.kwai.koom.demo                   I  processHprofFile
2024-03-01 10:24:43.473 19711-20568 OOMMonitor               com.kwai.koom.demo                   I  delete other version files thread
2024-03-01 10:24:43.475 19711-20568 OOMMonitor               com.kwai.koom.demo                   I  create json file and then start service
2024-03-01 10:24:43.478 19711-20568 OOMMonitor_...sisService com.kwai.koom.demo                   I  startAnalysisService
2024-03-01 10:24:43.482 19711-20567 OOMMonitor               com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-03-01 10:24:43.482 19711-20567 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-03-01_10-24-43_473.hprof
2024-03-01 10:24:43.485 19711-20568 OOMMonitor_...sisService com.kwai.koom.demo                   I  startAnalysisService get Pss:85973
2024-03-01 10:24:43.485 19711-20567 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 716
2024-03-01 10:24:43.485 19711-20567 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-03-01 10:24:45.092 19711-20567 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent dump wait: 1607
2024-03-01 10:24:45.093 19711-20567 OOMMonitor               com.kwai.koom.demo                   I  end hprof dump

不fork有子线程运行dump #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
2024-03-01 15:19:17.857  4939-4939  OOMMonitor               com.kwai.koom.demo                   I  startLoop()
2024-03-01 15:19:23.091  4939-5012  OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709277563090
2024-03-01 15:19:23.091  4939-5012  OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:0
2024-03-01 15:19:23.092  4939-5746  OOMMonitor               com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-03-01 15:19:23.092  4939-5746  OOMMonitor               com.kwai.koom.demo                   I  dumpAndAnalysis
2024-03-01 15:19:23.093  4939-5747  OOMMonitor               com.kwai.koom.demo                   I  processHprofFile
2024-03-01 15:19:23.093  4939-5747  OOMMonitor               com.kwai.koom.demo                   I  delete other version files thread
2024-03-01 15:19:23.100  4939-5746  OOMMonitor               com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-03-01 15:19:23.100  4939-5746  OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-03-01_15-19-23_092.hprof
2024-03-01 15:19:23.107  4939-5746  OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 716
2024-03-01 15:19:24.924  4939-5746  OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump spend time: 1817
2024-03-01 15:19:24.925  4939-5746  OOMMonitor               com.kwai.koom.demo                   I  end hprof dump

父进程没有其他线程运行时,子进程dump #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
2024-03-01 10:32:23.682 23437-23437 OOMMonitor               com.kwai.koom.demo                   I  startLoop()
2024-03-01 10:32:28.974 23437-23527 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709201524073
2024-03-01 10:32:28.974 23437-23527 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:7
2024-03-01 10:32:28.976 23437-24239 OOMMonitor               com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-03-01 10:32:28.976 23437-24239 OOMMonitor               com.kwai.koom.demo                   I  dumpAndAnalysis
2024-03-01 10:32:28.977 23437-24240 OOMMonitor               com.kwai.koom.demo                   I  processHprofFile
2024-03-01 10:32:28.978 23437-24240 OOMMonitor               com.kwai.koom.demo                   I  delete other version files thread
2024-03-01 10:32:28.985 23437-24240 OOMMonitor               com.kwai.koom.demo                   I  delete old files
2024-03-01 10:32:28.986 23437-24239 OOMMonitor               com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-03-01 10:32:28.986 23437-24239 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-03-01_10-32-28_977.hprof
2024-03-01 10:32:28.990 23437-24239 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 716
2024-03-01 10:32:28.990 23437-24239 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-03-01 10:32:29.007 24241-24241 OOMMonitor_...HeapDumper pid-24241                            I  child process
2024-03-01 10:32:29.009 23437-24240 OOMMonitor               com.kwai.koom.demo                   I  delete other version files fd
---------------------------- PROCESS STARTED (24241) for package com.kwai.koom.demo ----------------------------
2024-03-01 10:32:31.214 23437-24239 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump false, notify from pid 24241
2024-03-01 10:32:31.214 23437-24239 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process wait time: 2208
2024-03-01 10:32:31.214 23437-24239 OOMMonitor               com.kwai.koom.demo                   I  end hprof dump
---------------------------- PROCESS ENDED (24241) for package com.kwai.koom.demo ----------------------------

结论: dump正常结果

Android13父进程有线程运行时,子进程dump #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
2024-03-01 10:34:19.954 25380-25380 OOMMonitor               com.kwai.koom.demo                   I  startLoop()
2024-03-01 10:34:25.139 25380-25433 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getFirstAnalysisTime():1709201524073
2024-03-01 10:34:25.139 25380-25433 OOMMonitor               com.kwai.koom.demo                   I  OOMPreferenceManager.getAnalysisTimes:9
2024-03-01 10:34:25.141 25380-26166 OOMMonitor               com.kwai.koom.demo                   I  mTrackReasons:[reason_thread_oom]
2024-03-01 10:34:25.141 25380-26166 OOMMonitor               com.kwai.koom.demo                   I  dumpAndAnalysis
2024-03-01 10:34:25.142 25380-26167 OOMMonitor               com.kwai.koom.demo                   I  processHprofFile
2024-03-01 10:34:25.142 25380-26167 OOMMonitor               com.kwai.koom.demo                   I  delete other version files thread
2024-03-01 10:34:25.152 25380-26167 OOMMonitor               com.kwai.koom.demo                   I  create json file and then start service
2024-03-01 10:34:25.154 25380-26166 OOMMonitor               com.kwai.koom.demo                   I  hprof analysis dir:/storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly
2024-03-01 10:34:25.154 25380-26167 OOMMonitor               com.kwai.koom.demo                   I  hprof file size 0
2024-03-01 10:34:25.155 25380-26166 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump /storage/emulated/0/Android/data/com.kwai.koom.demo/files/performance/oom/memory/hprof-aly/1.0.0_2024-03-01_10-34-25_142.hprof
2024-03-01 10:34:25.156 25380-26167 OOMMonitor               com.kwai.koom.demo                   I  last analysis isn't succeed, delete file
2024-03-01 10:34:25.160 25380-26166 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process active thread count: 717
2024-03-01 10:34:25.160 25380-26166 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  before suspend and fork.
2024-03-01 10:34:25.177 26169-26169 OOMMonitor_...HeapDumper pid-26169                            I  child process
---------------------------- PROCESS STARTED (26169) for package com.kwai.koom.demo ----------------------------
2024-03-01 10:35:25.191 25380-26166 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  dump false, notify from pid 26169
2024-03-01 10:35:25.191 25380-26166 OOMMonitor_...HeapDumper com.kwai.koom.demo                   I  parent process wait time: 60015
2024-03-01 10:35:25.191 25380-26166 OOMMonitor               com.kwai.koom.demo                   I  end hprof dump
---------------------------- PROCESS ENDED (26169) for package com.kwai.koom.demo ----------------------------

结论: 子进程卡在dump

标签:
Categories: