From db57c6582bf295e7fc6615401befbc208846f780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=99=93=E9=B9=8F?= Date: Fri, 27 Dec 2024 14:50:48 +0800 Subject: [PATCH] =?UTF-8?q?system=5Fprocs=20=E6=B7=BB=E5=8A=A0=E5=AE=B9?= =?UTF-8?q?=E5=99=A8=E5=86=85=E8=BF=9B=E7=A8=8Bid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨晓鹏 --- .../system_infos.probe/system_proc.meta | 5 +++ src/probes/system_infos.probe/system_procs.c | 38 ++++++++++++++++++- src/probes/system_infos.probe/system_procs.h | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/probes/system_infos.probe/system_proc.meta b/src/probes/system_infos.probe/system_proc.meta index d1848294..755c4dbe 100644 --- a/src/probes/system_infos.probe/system_proc.meta +++ b/src/probes/system_infos.probe/system_proc.meta @@ -12,6 +12,11 @@ measurements: type: "key", name: "tgid", }, + { + description: "in container id of process", + type: "key", + name: "con_tgid", + }, { description: "process group id", type: "label", diff --git a/src/probes/system_infos.probe/system_procs.c b/src/probes/system_infos.probe/system_procs.c index b3f46093..775e90d0 100644 --- a/src/probes/system_infos.probe/system_procs.c +++ b/src/probes/system_infos.probe/system_procs.c @@ -405,6 +405,35 @@ out: return 0; } +static int get_proc_con_tgid(u32 pid, proc_info_t *proc_info){ + FILE *f = NULL; + char fname[PATH_LEN]; + char line[LINE_BUF_LEN]; + + snprintf(fname, sizeof(fname), "/proc/%u/task/%u/status", pid, pid); + + f = fopen(fname, "r"); + if (f == NULL){ + return -1; + } + + while (fgets(line, LINE_BUF_LEN, f) != NULL){ + if (strncmp(line, "NStgid:", 7) == 0){ + u32 host_pid, con_pid; + if (sscanf(line + 7, "%u %u", &host_pid, &con_pid) == 2){ + if (host_pid == pid){ + proc_info->con_tgid = con_pid; + fclose(f); + return 0; + } + } + } + } + + fclose(f); + return 0; +} + static int update_proc_infos(u32 pid, proc_info_t *proc_info) { int ret = 0; @@ -435,6 +464,12 @@ static int update_proc_infos(u32 pid, proc_info_t *proc_info) return -1; } + ret = get_proc_con_tgid(pid, proc_info); + if (ret < 0) { + DEBUG("[SYSTEM_PROC] failed to get container tgid info\n"); + return -1; + } + return 0; } @@ -459,9 +494,10 @@ static void output_proc_infos(proc_hash_t *one_proc, unsigned int period) (period * sys_clock_ticks) * FULL_PER; nprobe_fprintf(stdout, - "|%s|%lu|%d|%d|%u|%.2f|%llu|%llu|%u|%u|%llu|%llu|%llu|%lu|%lu|%lu|%lu|%lu|%lu|%lu|%lu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%.2f|%llu|%.2f|%.2f|%.2f|\n", + "|%s|%lu|%d|%d|%d|%u|%.2f|%llu|%llu|%u|%u|%llu|%llu|%llu|%lu|%lu|%lu|%lu|%lu|%lu|%lu|%lu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%llu|%.2f|%llu|%.2f|%.2f|%.2f|\n", METRICS_PROC_NAME, one_proc->key.pid, + one_proc->info.con_tgid, one_proc->info.pgid, one_proc->info.ppid, one_proc->info.fd_count, diff --git a/src/probes/system_infos.probe/system_procs.h b/src/probes/system_infos.probe/system_procs.h index dbe7a682..395ba8c8 100644 --- a/src/probes/system_infos.probe/system_procs.h +++ b/src/probes/system_infos.probe/system_procs.h @@ -79,6 +79,7 @@ typedef struct { char comm[PROC_NAME_MAX]; int pgid; int ppid; + int con_tgid; u64 proc_start_time; // FROM same as proc_stat_min_flt u32 fd_count; // FROM '/usr/bin/ls -l /proc/[PID]/fd | wc -l' u32 max_fd_limit; // FROM 'cat /proc/[PID]/limits | grep -w "MAX open files"' -- Gitee