技术专栏
如何监控各个线程的资源占用情况?
嵌入式Linux开发中,有时候为了定位问题,需要查看某个进程的各个线程的运行情况。
例子
multi_thread.c:
#define _GNU_SOURCE
#include
#include
#include
#include
// 线程名称最大长度
#define APP_THREAD_NAME_MAX_LEN 32
// 线程索引
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 = %ld\n", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);
return ret;
}
else
{
printf("%s thread create success! thread_id = %ld\n", 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;
}
- 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
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
<
我们可以通过top命令来查看。要在top输出中开启线程查看,请调用top命令的“-H”选项,该选项会列出所有Linux线程。
这里我们指定查看multi_thread进程的各线程运行情况,命令:
top -H -p `pidof multi_thread`
- 1
注意:
这里的 `号并不是单引号!!!
这里的 `号并不是单引号!!!
这里的 `号并不是单引号!!!
这个符号在键盘上感叹号!键的左边。
我们先运行程序,再使用top命令查看,如:
注意,我们创建线程的时候需要使用 pthread_setname_np
函数设置线程的名字,否则top -H显示不出来具体的线程。
假如我们把上例中的pthread_setname_np屏蔽掉,结果如:
可见,不调用pthread_setname_np设置线程名称的话,top -H查看得到的各线程名称就是进程名。
本文转载自公众号全栈芯片工程师,版权归原作者所有,不代表本站观点,如有侵权请与本站联系,本站将第一时间删除
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:3882次2021-04-09 10:33:59
-
浏览量:2894次2020-02-29 12:04:47
-
浏览量:1822次2020-08-07 17:02:17
-
浏览量:1569次2022-09-19 17:38:48
-
浏览量:2713次2020-10-27 09:17:56
-
浏览量:6614次2020-10-28 23:03:59
-
浏览量:4313次2020-10-28 23:08:53
-
浏览量:4916次2021-09-16 13:47:50
-
浏览量:1829次2019-12-05 16:34:01
-
浏览量:1535次2019-12-05 16:50:26
-
浏览量:10531次2020-11-08 17:15:55
-
浏览量:939次2023-12-06 09:40:25
-
浏览量:2102次2020-08-07 18:54:44
-
浏览量:1843次2020-03-25 20:08:11
-
浏览量:1741次2020-08-10 09:42:52
-
浏览量:1335次2022-10-10 11:44:39
-
浏览量:1145次2022-12-05 09:41:24
-
浏览量:1332次2022-10-10 10:18:49
-
浏览量:1952次2020-09-09 19:02:25
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者

南宫醉
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注