如何监控各个线程的资源占用情况?

嵌入式linux开发中 , 有时候为了定位问题 , 需要查看某个进程的各个线程的运行情况 。
例子multi_thread.c:
左右滑动查看全部代码>>>
#define _GNU_SOURCE#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>// 线程名称最大长度#define App_THREAD_NAME_MAX_LEN32// 线程索引typedef enum _app_thread_index{APP_THREAD_INDEX_TEST0,APP_THREAD_INDEX_TEST1,APP_THREAD_INDEX_TEST2,APP_THREAD_INDEX_TEST3,APP_THREAD_INDEX_TEST4,APP_THREAD_INDEX_TEST5,APP_THREAD_INDEX_MAX}app_thread_index_e;// 线程入口函数指针类型typedef void *(*p_thread_fun)(void *param);// 线程数据表typedef struct _app_thread{pthread_t thread_handle;p_thread_fun thread_entry;char name[APP_THREAD_NAME_MAX_LEN];}app_thread_s;static void *test0_thread_entry(void *param);static void *test1_thread_entry(void *param);static void *test2_thread_entry(void *param);static void *test3_thread_entry(void *param);static void *test4_thread_entry(void *param);static void *test5_thread_entry(void *param);// 线程表app_thread_s s_app_thread_table[APP_THREAD_INDEX_MAX] ={{0, test0_thread_entry, "test0_thread"},{0, test1_thread_entry, "test1_thread"},{0, test2_thread_entry, "test2_thread"},{0, test3_thread_entry, "test3_thread"},{0, test4_thread_entry, "test4_thread"},{0, test5_thread_entry, "test5_thread"}};static void *test0_thread_entry(void *param){printf("test0_thread running...n");while (1){usleep(2 * 1000);}return NULL;}static void *test1_thread_entry(void *param){printf("test1_thread running...n");while (1){usleep(2 * 1000);}return NULL;}static void *test2_thread_entry(void *param){printf("test2_thread running...n");while (1){usleep(2 * 1000);}return NULL;}static void *test3_thread_entry(void *param){printf("test3_thread running...n");while (1){usleep(2 * 1000);}return NULL;}static void *test4_thread_entry(void *param){printf("test4_thread running...n");while (1){usleep(2 * 1000);}return NULL;}static void *test5_thread_entry(void *param){printf("test5_thread running...n");while (1){usleep(2 * 1000);}return NULL;};static int create_all_app_thread(void){int ret = 0;for (int i = 0; i < APP_THREAD_INDEX_MAX; i++){ret = pthread_create(&s_app_thread_table[i].thread_handle, NULL, s_app_thread_table[i].thread_entry, NULL);if (0 != ret){printf("%s thread create error! thread_id = %ldn", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);return ret;}else{printf("%s thread create success! thread_id = %ldn", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);pthread_setname_np(s_app_thread_table[i].thread_handle, s_app_thread_table[i].name);}pthread_detach(s_app_thread_table[i].thread_handle);}return ret;}int main(int argc, char **argv){create_all_app_thread();while (1){usleep(2 * 1000);}return 0;}我们可以通过top命令来查看 。要在top输出中开启线程查看 , 请调用top命令的“-H”选项 , 该选项会列出所有Linux线程 。
这里我们指定查看multi_thread进程的各线程运行情况 , 命令:
top -H -p `pidof multi_thread`注意:
这里的 `号并不是单引号!!!
这里的 `号并不是单引号!!!
这里的 `号并不是单引号!!!
这个符号在键盘上感叹号!键的左边 。
我们先运行程序 , 再使用top命令查看 , 如:

如何监控各个线程的资源占用情况?

文章插图
 
注意 , 我们创建线程的时候需要使用 pthread_setname_np 函数设置线程的名字 , 否则top -H显示不出来具体的线程 。
假如我们把上例中的pthread_setname_np屏蔽掉 , 结果如:
如何监控各个线程的资源占用情况?

文章插图
 
可见 , 不调用pthread_setname_np设置线程名称的话 , top -H查看得到的各线程名称就是进程名 。
原文作者:ZhengNL
原文来源:嵌入式大杂烩
原文链接:
https://mp.weixin.qq.com/s/DURT5jhAWWFhsaHHB_qi4w

【如何监控各个线程的资源占用情况?】


    推荐阅读