taskstats: introduce version 12 with exe file info
RFC PATCH 2/2 taskstats: introduce version 12 with exe file info
From 4f5e7d46af70ed4b05261c6c097f7dd1c8a2268d Mon Sep 17 00:00:00 2001
From: "Dr. Thomas Orgis" <[email protected]>
Date: Mon, 10 Jan 2022 18:23:38 +0100
Subject: [PATCH 2/2] taskstats: introduce version 12 with exe file info
This adds fields to the taskstats struct that give device and inode info for the executable file associated with the process as a safer way to get that than polling /proc/$pid/exe before a process ends.
The getdelay example program is accompanied by a variant that just prints a stream of exiting tasks with the added info (as well es the recently added tgid field to identify multithreaded processes).
The idea is to be able to collect global statistics about use of certain programs installed on a system by associating process/task stats with an absolute executable path. The command field is unsuitable as it does not distinguish between /foo/bar/v1.2.3/bin/bar_exe and /foo/bar/v2.3.4/bin/bar_exe and is generally rather arbitrary.
I would like to just get the path communicated to userspace, but I presume the growth of the struct by PATH_MAX bytes would not be welcomed.
This version of the patch returns correct device IDs by copying what fs/stat.c does, despite the claim that the s_dev member of struct super_block is not supposed to be a search index and not a device ID (include/linux/fs.h).
I left a number of comments and humbly ask for confirmation of the approach or any treatment for my doubts. I am running variants of this patch on kernel 4.14.x since several years now on a 400-node cluster, but I am mostly cargo-coding in kernel space and might have gotten things horribly wrong.
I am open to alternatives for identifying the program executable belonging to certain tasks, but imagine that communicating the inode is the cheapest way for the kernel. The remaining style warnings of the added procacct.c example program are inherited from getdelays.c. I hope they don't distract from the core question.
--- include/uapi/linux/taskstats.h | 17 +- kernel/taskstats.c | 20 ++ tools/accounting/.gitignore | 1 + tools/accounting/Makefile | 2 +- tools/accounting/procacct.c | 418 +++++++++++++++++++++++++++++++++ 5 files changed, 456 insertions(+), 2 deletions(-) create mode 100644 tools/accounting/procacct.c
include/uapi/linux/taskstats.h +16 -1
@@ -34,7 +34,7 @@*/ -#define TASKSTATS_VERSION 11 +#define TASKSTATS_VERSION 12#define TS_COMM_LEN 32/* should be >= TASK_COMM_LEN * in linux/sched.h */
@@ -174,6 +174,21 @@ struct taskstats {__u64 ac_btime64;/* 64-bit begin time */ /* v11: thread group ID to identify process vs. (non-leader) thread */ __u32 ac_tgid; +/* v12: lightweight information to identify process binary files +* I would prefer the fully resolved path, but presume that would need +* growing of the struct by PATH_MAX bytes. A hash sum might be an +* alternative, but expensive again. Suggestions welcome. My client +* application maps device and inode to path in userspace, having +* possible failure modes (changes in mounts, filesystem operations), +* in mind. +* +* I kept both fields at 64 bits. Device ID usually has only 16 bits +* relevant for userspace resolving it to MAJOR and MINOR. Limiting +* to that preferred? Or store MAJOR:MINOR as two values here? +* Both values are zero if no executable file is present. +*/ +__u64 ac_exe_dev;/* program binary device ID */ +__u64 ac_exe_inode;/* program binary inode number */ };
kernel/taskstats.c +20 -0
@@ -152,6 +152,23 @@ static void send_cpu_listeners(struct sk_buff *skb,up_write (&listeners->sem); } +static void exe_add_tsk (struct taskstats *stats,struct task_struct *tsk) +{ +/* No idea if I'm allowed to access that here, now. */ +struct file *exe_file =get_task_exe_file (tsk); + +if (exe_file) { +/* Following cp_new_stat64() in stat.c . */ + stats->ac_exe_dev = +huge_encode_dev (exe_file->f_inode->i_sb->s_dev); + stats->ac_exe_inode = exe_file->f_inode->i_ino; +fput (exe_file); + }else { + stats->ac_exe_dev =0 ; + stats->ac_exe_inode =0 ; + } +} +static void fill_stats (struct user_namespace *user_ns,struct pid_namespace *pid_ns,struct task_struct *tsk,struct taskstats *stats)
@@ -174,6 +191,9 @@ static void fill_stats(struct user_namespace *user_ns,/* fill in extended acct fields */ xacct_add_tsk (stats, tsk); + +/* add executable info */ +exe_add_tsk (stats, tsk); }static int fill_stats_for_pid (pid_t pid,struct taskstats *stats)
tools/accounting/.gitignore +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only getdelays +procacct
tools/accounting/Makefile +1 -1
@@ -2,7 +2,7 @@ CC := $(CROSS_COMPILE)gcc CFLAGS := -I../../usr/include -PROGS := getdelays +PROGS := getdelays procacct all: $(PROGS)
tools/accounting/procacct.c new file +418 -0
418 lines not on this page · raw message
Signature · 7 lines
2.29.2 -- Dr. Thomas Orgis HPC @ Universität Hamburg