xref: /linux/kernel/tsacct.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tsacct.c - System accounting over taskstats interface
4  *
5  * Copyright (C) Jay Lan,	<jlan@sgi.com>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/sched/signal.h>
10 #include <linux/sched/mm.h>
11 #include <linux/sched/cputime.h>
12 #include <linux/tsacct_kern.h>
13 #include <linux/acct.h>
14 #include <linux/jiffies.h>
15 #include <linux/mm.h>
16 
17 /*
18  * fill in basic accounting fields
19  */
20 void bacct_add_tsk(struct user_namespace *user_ns,
21 		   struct pid_namespace *pid_ns,
22 		   struct taskstats *stats, struct task_struct *tsk)
23 {
24 	const struct cred *tcred;
25 	u64 utime, stime, utimescaled, stimescaled;
26 	u64 now_ns, delta;
27 	struct timespec64 btime;
28 
29 	BUILD_BUG_ON(TS_COMM_LEN < TASK_COMM_LEN);
30 
31 	/* calculate task elapsed time in nsec */
32 	now_ns = ktime_get_ns();
33 	/* store whole group time first */
34 	delta = now_ns - tsk->group_leader->start_time;
35 	/* Convert to micro seconds */
36 	do_div(delta, NSEC_PER_USEC);
37 	stats->ac_tgetime = delta;
38 	delta = now_ns - tsk->start_time;
39 	do_div(delta, NSEC_PER_USEC);
40 	stats->ac_etime = delta;
41 	/* Convert to seconds for btime (note y2106 limit) */
42 	getboottime64(&btime);
43 	btime = timespec64_add(btime, ns_to_timespec64(tsk->start_boottime));
44 	stats->ac_btime = clamp_t(time64_t, btime.tv_sec, 0, U32_MAX);
45 	/* Setting the clock back after suspend can make btime negative. */
46 	stats->ac_btime64 = max_t(time64_t, btime.tv_sec, 0);
47 
48 	if (tsk->flags & PF_EXITING)
49 		stats->ac_exitcode = tsk->exit_code;
50 	if (thread_group_leader(tsk) && (tsk->flags & PF_FORKNOEXEC))
51 		stats->ac_flag |= AFORK;
52 	if (tsk->flags & PF_SUPERPRIV)
53 		stats->ac_flag |= ASU;
54 	if (tsk->flags & PF_DUMPCORE)
55 		stats->ac_flag |= ACORE;
56 	if (tsk->flags & PF_SIGNALED)
57 		stats->ac_flag |= AXSIG;
58 	stats->ac_nice	 = task_nice(tsk);
59 	stats->ac_sched	 = tsk->policy;
60 	stats->ac_pid	 = task_pid_nr_ns(tsk, pid_ns);
61 	stats->ac_tgid   = task_tgid_nr_ns(tsk, pid_ns);
62 	stats->ac_ppid	 = task_ppid_nr_ns(tsk, pid_ns);
63 	rcu_read_lock();
64 	tcred = __task_cred(tsk);
65 	stats->ac_uid	 = from_kuid_munged(user_ns, tcred->uid);
66 	stats->ac_gid	 = from_kgid_munged(user_ns, tcred->gid);
67 	rcu_read_unlock();
68 
69 	task_cputime(tsk, &utime, &stime);
70 	stats->ac_utime = div_u64(utime, NSEC_PER_USEC);
71 	stats->ac_stime = div_u64(stime, NSEC_PER_USEC);
72 
73 	task_cputime_scaled(tsk, &utimescaled, &stimescaled);
74 	stats->ac_utimescaled = div_u64(utimescaled, NSEC_PER_USEC);
75 	stats->ac_stimescaled = div_u64(stimescaled, NSEC_PER_USEC);
76 
77 	stats->ac_minflt = tsk->min_flt;
78 	stats->ac_majflt = tsk->maj_flt;
79 
80 	strscpy_pad(stats->ac_comm, tsk->comm);
81 }
82 
83 
84 #ifdef CONFIG_TASK_XACCT
85 
86 #define KB 1024
87 #define MB (1024*KB)
88 #define KB_MASK (~(KB-1))
89 /*
90  * fill in extended accounting fields
91  */
92 void xacct_add_tsk(struct taskstats *stats, struct task_struct *p)
93 {
94 	struct mm_struct *mm;
95 
96 	/* convert pages-nsec/1024 to Mbyte-usec, see __acct_update_integrals */
97 	stats->coremem = p->acct_rss_mem1 * PAGE_SIZE;
98 	do_div(stats->coremem, 1000 * KB);
99 	stats->virtmem = p->acct_vm_mem1 * PAGE_SIZE;
100 	do_div(stats->virtmem, 1000 * KB);
101 	mm = get_task_mm(p);
102 	if (mm) {
103 		/* adjust to KB unit */
104 		stats->hiwater_rss   = get_mm_hiwater_rss(mm) * PAGE_SIZE / KB;
105 		stats->hiwater_vm    = get_mm_hiwater_vm(mm)  * PAGE_SIZE / KB;
106 		mmput(mm);
107 	}
108 	stats->read_char	= p->ioac.rchar & KB_MASK;
109 	stats->write_char	= p->ioac.wchar & KB_MASK;
110 	stats->read_syscalls	= p->ioac.syscr & KB_MASK;
111 	stats->write_syscalls	= p->ioac.syscw & KB_MASK;
112 #ifdef CONFIG_TASK_IO_ACCOUNTING
113 	stats->read_bytes	= p->ioac.read_bytes & KB_MASK;
114 	stats->write_bytes	= p->ioac.write_bytes & KB_MASK;
115 	stats->cancelled_write_bytes = p->ioac.cancelled_write_bytes & KB_MASK;
116 #else
117 	stats->read_bytes	= 0;
118 	stats->write_bytes	= 0;
119 	stats->cancelled_write_bytes = 0;
120 #endif
121 }
122 #undef KB
123 #undef MB
124 
125 static void __acct_update_integrals(struct task_struct *tsk,
126 				    u64 utime, u64 stime)
127 {
128 	u64 time, delta;
129 
130 	if (unlikely(!tsk->mm || (tsk->flags & PF_KTHREAD)))
131 		return;
132 
133 	time = stime + utime;
134 	delta = time - tsk->acct_timexpd;
135 
136 	if (delta < TICK_NSEC)
137 		return;
138 
139 	tsk->acct_timexpd = time;
140 	/*
141 	 * Divide by 1024 to avoid overflow, and to avoid division.
142 	 * The final unit reported to userspace is Mbyte-usecs,
143 	 * the rest of the math is done in xacct_add_tsk.
144 	 */
145 	tsk->acct_rss_mem1 += delta * get_mm_rss(tsk->mm) >> 10;
146 	tsk->acct_vm_mem1 += delta * READ_ONCE(tsk->mm->total_vm) >> 10;
147 }
148 
149 /**
150  * acct_update_integrals - update mm integral fields in task_struct
151  * @tsk: task_struct for accounting
152  */
153 void acct_update_integrals(struct task_struct *tsk)
154 {
155 	u64 utime, stime;
156 	unsigned long flags;
157 
158 	local_irq_save(flags);
159 	task_cputime(tsk, &utime, &stime);
160 	__acct_update_integrals(tsk, utime, stime);
161 	local_irq_restore(flags);
162 }
163 
164 /**
165  * acct_account_cputime - update mm integral after cputime update
166  * @tsk: task_struct for accounting
167  */
168 void acct_account_cputime(struct task_struct *tsk)
169 {
170 	__acct_update_integrals(tsk, tsk->utime, tsk->stime);
171 }
172 
173 /**
174  * acct_clear_integrals - clear the mm integral fields in task_struct
175  * @tsk: task_struct whose accounting fields are cleared
176  */
177 void acct_clear_integrals(struct task_struct *tsk)
178 {
179 	tsk->acct_timexpd = 0;
180 	tsk->acct_rss_mem1 = 0;
181 	tsk->acct_vm_mem1 = 0;
182 }
183 #endif
184