android修改线程名字长度

对于默认的linux系统中,当前可设置的线程名字长度被限制在16个字符。

在未修改前,通过ps -eT查看到的线程名如下:

u0_a6 1204 1214 192 14891352 113620 0 S HeapTaskDaemon

u0_a6 1204 1215 192 14891352 113620 0 S ReferenceQueueD

u0_a6 1204 1216 192 14891352 113620 0 S FinalizerDaemon

u0_a6 1204 1217 192 14891352 113620 0 S FinalizerWatchd

u0_a6 1204 1218 192 14891352 113620 0 S Binder:1204_1

u0_a6 1204 1221 192 14891352 113620 0 S Binder:1204_2

u0_a6 1204 1244 192 14891352 113620 0 S Profile Saver

可通过如下方法修改线程名字的限制

1:修改内核中线程名字长度限制。

如下修改,可更改linux 内核中对线程名字长度的限制。如果只修改到32个字符,修改下面两个点就可以了,超过还需修改其他地方。

diff --git a/include/linux/sched.h b/include/linux/sched.h

index a9427da2dfdd..608017184474 100644

--- a/include/linux/sched.h

+++ b/include/linux/sched.h

@@ -212,7 +212,7 @@ struct io_uring_task;

#endif

/* Task command name length: */

-#define TASK_COMM_LEN 16

+#define TASK_COMM_LEN 32

extern void scheduler_tick(void);

diff --git a/include/uapi/linux/cn_proc.h b/include/uapi/linux/cn_proc.h

index db210625cee8..9fc6295e25e2 100644

--- a/include/uapi/linux/cn_proc.h

+++ b/include/uapi/linux/cn_proc.h

@@ -110,7 +110,7 @@ struct proc_event {

struct comm_proc_event {

__kernel_pid_t process_pid;

__kernel_pid_t process_tgid;

  • char comm16;
  • char comm32;

} comm;

struct coredump_proc_event {

2:修改android 源码中对线程名字的限制

2.1 修改bionic目录下pthread对线程名字长度的限制

diff --git a/libc/bionic/pthread_setname_np.cpp b/libc/bionic/pthread_setname_np.cpp

index 5236e4dbe..b3e5b2102 100644

--- a/libc/bionic/pthread_setname_np.cpp

+++ b/libc/bionic/pthread_setname_np.cpp

@@ -41,7 +41,7 @@

#include "pthread_internal.h"

// This value is not exported by kernel headers.

-#define MAX_TASK_COMM_LEN 16

+#define MAX_TASK_COMM_LEN 32

static int __open_task_comm_fd(pthread_t t, int flags, const char* caller) {

char comm_name64;

2.2 修改art目录下虚拟机中对线程名字长度的限制

diff --git a/libartbase/base/utils.cc b/libartbase/base/utils.cc

index ba62f30bdc..81e568bebe 100644

--- a/libartbase/base/utils.cc

+++ b/libartbase/base/utils.cc

@@ -291,7 +291,7 @@ void SetThreadName(const char* thread_name) {

}

#if defined(linux) || defined(_WIN32)

// pthread_setname_np fails rather than truncating long strings.

  • char buf16; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
  • char buf32; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.

strncpy(buf, s, sizeof(buf)-1);

bufsizeof(buf)-1 = '\0';

errno = pthread_setname_np(pthread_self(), buf);

3:修改后的结果,通过ps -eT 查看到线程名如下:

system 1898 1903 187 15396108 104388 0 S Signal Catcher

system 1898 1904 187 15396108 104388 0 S perfetto_hprof_listener

system 1898 1905 187 15396108 104388 0 S ADB-JDWP Connection Control Th

system 1898 1906 187 15396108 104388 0 S Jit thread pool worker thread

system 1898 1907 187 15396108 104388 0 S HeapTaskDaemon

system 1898 1908 187 15396108 104388 0 S ReferenceQueueDaemon

system 1898 1909 187 15396108 104388 0 S FinalizerDaemon

system 1898 1910 187 15396108 104388 0 S FinalizerWatchdogDaemon

相关推荐
爱笑鱼36 分钟前
Binder(一):一次方法调用,究竟怎样跨进程执行?
android
壮哥_icon1 小时前
【Android 系统开发】使用 BAT 脚本高效自动化管理 /system/priv-app/ 系统应用(安装与卸载)
android·运维·自动化
用户69371750013843 小时前
Claude Code终端日志Token占用实测:一个过滤器砍掉60%-90%
android·前端·后端
蝉蜕日记3 小时前
AccumuPDF高级版 v2.57 | 视图文转换PDF,合并分割压缩
android·智能手机·pdf·生活·软件需求
吐了啊取名字太难3 小时前
美颜系统AI修图本地跑并支持Mac、win、安卓、iOS不卡顿
android·人工智能·windows·数码相机·mac·ai编程
阿pin5 小时前
Android随笔-Retrofit
android·retrofit
想取一个与众不同的名字好难5 小时前
安卓自定义颜色选择器
android
我命由我123455 小时前
Android 开发问题:java.lang.NoSuchMethodError:No virtual method readAllBytes()
android·java·java-ee·kotlin·android studio·android-studio·android runtime
李艺为5 小时前
展锐相机DreamCamera2模式精简
android·java
Coffeeee5 小时前
ios零基础的Android开发能否靠AI让老板省一笔人工费呢
android·ios·ai编程