[嵌入式Linux] 如何监控各个线程的资源占用情况?

[复制链接]
 楼主| 呐咯密密 发表于 2022-9-15 15:09 | 显示全部楼层 |阅读模式
例子

multi_thread.c:

  1. #define _GNU_SOURCE
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>

  6. // 线程名称最大长度
  7. #define APP_THREAD_NAME_MAX_LEN     32

  8. // 线程索引
  9. typedef enum _app_thread_index
  10. {
  11.     APP_THREAD_INDEX_TEST0,
  12.     APP_THREAD_INDEX_TEST1,
  13.     APP_THREAD_INDEX_TEST2,
  14.     APP_THREAD_INDEX_TEST3,
  15.     APP_THREAD_INDEX_TEST4,
  16.     APP_THREAD_INDEX_TEST5,
  17.     APP_THREAD_INDEX_MAX
  18. }app_thread_index_e;

  19. // 线程入口函数指针类型
  20. typedef void *(*p_thread_fun)(void *param);

  21. // 线程数据表
  22. typedef struct _app_thread
  23. {
  24.     pthread_t thread_handle;
  25.     p_thread_fun thread_entry;
  26.     char name[APP_THREAD_NAME_MAX_LEN];
  27. }app_thread_s;

  28. static void *test0_thread_entry(void *param);
  29. static void *test1_thread_entry(void *param);
  30. static void *test2_thread_entry(void *param);
  31. static void *test3_thread_entry(void *param);
  32. static void *test4_thread_entry(void *param);
  33. static void *test5_thread_entry(void *param);

  34. // 线程表
  35. app_thread_s s_app_thread_table[APP_THREAD_INDEX_MAX] =
  36. {
  37.     {0, test0_thread_entry, "test0_thread"},
  38.     {0, test1_thread_entry, "test1_thread"},
  39.     {0, test2_thread_entry, "test2_thread"},
  40.     {0, test3_thread_entry, "test3_thread"},
  41.     {0, test4_thread_entry, "test4_thread"},
  42.     {0, test5_thread_entry, "test5_thread"}
  43. };

  44. static void *test0_thread_entry(void *param)
  45. {
  46.     printf("test0_thread running...\n");

  47.     while (1)
  48.     {
  49.         usleep(2 * 1000);
  50.     }
  51.    
  52.     return NULL;
  53. }

  54. static void *test1_thread_entry(void *param)
  55. {
  56.     printf("test1_thread running...\n");

  57.     while (1)
  58.     {
  59.         usleep(2 * 1000);
  60.     }
  61.    
  62.     return NULL;
  63. }

  64. static void *test2_thread_entry(void *param)
  65. {
  66.     printf("test2_thread running...\n");

  67.     while (1)
  68.     {
  69.         usleep(2 * 1000);
  70.     }
  71.    
  72.     return NULL;
  73. }

  74. static void *test3_thread_entry(void *param)
  75. {
  76.     printf("test3_thread running...\n");

  77.     while (1)
  78.     {
  79.         usleep(2 * 1000);
  80.     }
  81.    
  82.     return NULL;
  83. }

  84. static void *test4_thread_entry(void *param)
  85. {
  86.     printf("test4_thread running...\n");

  87.     while (1)
  88.     {
  89.         usleep(2 * 1000);
  90.     }
  91.    
  92.     return NULL;
  93. }

  94. static void *test5_thread_entry(void *param)
  95. {
  96.     printf("test5_thread running...\n");

  97.     while (1)
  98.     {
  99.         usleep(2 * 1000);
  100.     }
  101.    
  102.     return NULL;
  103. };

  104. static int create_all_app_thread(void)
  105. {
  106.     int ret = 0;

  107.     for (int i = 0; i < APP_THREAD_INDEX_MAX; i++)
  108.     {
  109.         ret = pthread_create(&s_app_thread_table[i].thread_handle, NULL, s_app_thread_table[i].thread_entry, NULL);

  110.         if (0 != ret)
  111.         {
  112.             printf("%s thread create error! thread_id = %ld\n", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);
  113.             return ret;
  114.         }
  115.         else
  116.         {
  117.             printf("%s thread create success! thread_id = %ld\n", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);
  118.             pthread_setname_np(s_app_thread_table[i].thread_handle, s_app_thread_table[i].name);
  119.         }

  120.         pthread_detach(s_app_thread_table[i].thread_handle);
  121.     }

  122.     return ret;
  123. }

  124. int main(int argc, char **argv)
  125. {
  126.     create_all_app_thread();
  127.    
  128.     while (1)
  129.     {
  130.         usleep(2 * 1000);
  131.     }

  132.     return 0;
  133. }


 楼主| 呐咯密密 发表于 2022-9-15 15:10 | 显示全部楼层
我们可以通过top命令来查看。要在top输出中开启线程查看,请调用top命令的“-H”选项,该选项会列出所有Linux线程。

这里我们指定查看multi_thread进程的各线程运行情况,命令:

  1. top -H -p `pidof multi_thread`

注意:

这里的 `号并不是单引号!!!

这里的 `号并不是单引号!!!

这里的 `号并不是单引号!!!

这个符号在键盘上感叹号!键的左边。

我们先运行程序,再使用top命令查看,如:

注意,我们创建线程的时候需要使用 pthread_setname_np 函数设置线程的名字,否则top -H显示不出来具体的线程。

假如我们把上例中的pthread_setname_np屏蔽掉,结果如:

可见,不调用pthread_setname_np设置线程名称的话,top -H查看得到的各线程名称就是进程名。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:苏州澜宭自动化科技嵌入式工程师
简介:本人从事磁编码器研发工作,负责开发2500线增量式磁编码器以及17位、23位绝对值式磁编码器,拥有多年嵌入式开发经验,精通STM32、GD32、N32等多种品牌单片机,熟练使用单片机各种外设。

567

主题

4081

帖子

56

粉丝
快速回复 在线客服 返回列表 返回顶部
认证:苏州澜宭自动化科技嵌入式工程师
简介:本人从事磁编码器研发工作,负责开发2500线增量式磁编码器以及17位、23位绝对值式磁编码器,拥有多年嵌入式开发经验,精通STM32、GD32、N32等多种品牌单片机,熟练使用单片机各种外设。

567

主题

4081

帖子

56

粉丝
快速回复 在线客服 返回列表 返回顶部