xref: /titanic_52/usr/src/uts/common/syscall/rusagesys.c (revision d4bf385cfc51cf116daea06b7a3cbeb344e92efd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*d4bf385cSjohansen  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Implement fast getrusage call
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/resource.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static int
407c478bd9Sstevel@tonic-gate getrusage(void *user_rusage)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	struct rusage r;
437c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
447c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
457c478bd9Sstevel@tonic-gate 	hrtime_t snsecs, unsecs;
467c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
477c478bd9Sstevel@tonic-gate 
48*d4bf385cSjohansen 	bzero(&r, sizeof (struct rusage));
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (p->p_defunct > 0) {
537c478bd9Sstevel@tonic-gate 		r.ru_majflt	= p->p_ru.majflt;
547c478bd9Sstevel@tonic-gate 		r.ru_minflt	= p->p_ru.minflt;
557c478bd9Sstevel@tonic-gate 		r.ru_nswap	= p->p_ru.nswap;
567c478bd9Sstevel@tonic-gate 		r.ru_inblock	= p->p_ru.inblock;
577c478bd9Sstevel@tonic-gate 		r.ru_oublock	= p->p_ru.oublock;
587c478bd9Sstevel@tonic-gate 		r.ru_msgsnd	= p->p_ru.msgsnd;
597c478bd9Sstevel@tonic-gate 		r.ru_msgrcv	= p->p_ru.msgrcv;
607c478bd9Sstevel@tonic-gate 		r.ru_nsignals	= p->p_ru.nsignals;
617c478bd9Sstevel@tonic-gate 		r.ru_nvcsw	= p->p_ru.nvcsw;
627c478bd9Sstevel@tonic-gate 		r.ru_nivcsw	= p->p_ru.nivcsw;
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	unsecs = mstate_aggr_state(p, LMS_USER);
667c478bd9Sstevel@tonic-gate 	snsecs = mstate_aggr_state(p, LMS_SYSTEM);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	do {
697c478bd9Sstevel@tonic-gate 		if (t->t_proc_flag & TP_LWPEXIT)
707c478bd9Sstevel@tonic-gate 			continue;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		lwp = ttolwp(t);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		r.ru_majflt	+= lwp->lwp_ru.majflt;
757c478bd9Sstevel@tonic-gate 		r.ru_minflt	+= lwp->lwp_ru.minflt;
767c478bd9Sstevel@tonic-gate 		r.ru_nswap	+= lwp->lwp_ru.nswap;
777c478bd9Sstevel@tonic-gate 		r.ru_inblock	+= lwp->lwp_ru.inblock;
787c478bd9Sstevel@tonic-gate 		r.ru_oublock	+= lwp->lwp_ru.oublock;
797c478bd9Sstevel@tonic-gate 		r.ru_msgsnd	+= lwp->lwp_ru.msgsnd;
807c478bd9Sstevel@tonic-gate 		r.ru_msgrcv	+= lwp->lwp_ru.msgrcv;
817c478bd9Sstevel@tonic-gate 		r.ru_nsignals	+= lwp->lwp_ru.nsignals;
827c478bd9Sstevel@tonic-gate 		r.ru_nvcsw	+= lwp->lwp_ru.nvcsw;
837c478bd9Sstevel@tonic-gate 		r.ru_nivcsw	+= lwp->lwp_ru.nivcsw;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != curthread);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	hrt2tv(unsecs, &r.ru_utime);
907c478bd9Sstevel@tonic-gate 	hrt2tv(snsecs, &r.ru_stime);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
937c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
947c478bd9Sstevel@tonic-gate 		struct rusage32 r32;
957c478bd9Sstevel@tonic-gate 
96*d4bf385cSjohansen 		bzero(&r32, sizeof (struct rusage32));
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		r32.ru_utime.tv_sec  = r.ru_utime.tv_sec;
997c478bd9Sstevel@tonic-gate 		r32.ru_utime.tv_usec = r.ru_utime.tv_usec;
1007c478bd9Sstevel@tonic-gate 		r32.ru_stime.tv_sec  = r.ru_stime.tv_sec;
1017c478bd9Sstevel@tonic-gate 		r32.ru_stime.tv_usec = r.ru_stime.tv_usec;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		r32.ru_majflt	= (int32_t)r.ru_majflt;
1047c478bd9Sstevel@tonic-gate 		r32.ru_minflt	= (int32_t)r.ru_minflt;
1057c478bd9Sstevel@tonic-gate 		r32.ru_nswap	= (int32_t)r.ru_nswap;
1067c478bd9Sstevel@tonic-gate 		r32.ru_inblock	= (int32_t)r.ru_inblock;
1077c478bd9Sstevel@tonic-gate 		r32.ru_oublock	= (int32_t)r.ru_oublock;
1087c478bd9Sstevel@tonic-gate 		r32.ru_msgsnd	= (int32_t)r.ru_msgsnd;
1097c478bd9Sstevel@tonic-gate 		r32.ru_msgrcv	= (int32_t)r.ru_msgrcv;
1107c478bd9Sstevel@tonic-gate 		r32.ru_nsignals	= (int32_t)r.ru_nsignals;
1117c478bd9Sstevel@tonic-gate 		r32.ru_nvcsw	= (int32_t)r.ru_nvcsw;
1127c478bd9Sstevel@tonic-gate 		r32.ru_nivcsw	= (int32_t)r.ru_nivcsw;
1137c478bd9Sstevel@tonic-gate 		if (copyout(&r32, user_rusage, sizeof (r32)) != 0)
1147c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1157c478bd9Sstevel@tonic-gate 	} else
1167c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		if (copyout(&r, user_rusage, sizeof (r)) != 0)
1197c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int
1257c478bd9Sstevel@tonic-gate getrusage_chld(void *user_rusage)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct rusage r;
1287c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1297c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1307c478bd9Sstevel@tonic-gate 	hrtime_t snsecs, unsecs;
1317c478bd9Sstevel@tonic-gate 
132*d4bf385cSjohansen 	bzero(&r, sizeof (struct rusage));
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	unsecs = p->p_cacct[LMS_USER];
1377c478bd9Sstevel@tonic-gate 	snsecs = p->p_cacct[LMS_SYSTEM] + p->p_cacct[LMS_TRAP];
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	r.ru_majflt	= p->p_cru.majflt;
1407c478bd9Sstevel@tonic-gate 	r.ru_minflt	= p->p_cru.minflt;
1417c478bd9Sstevel@tonic-gate 	r.ru_nswap	= p->p_cru.nswap;
1427c478bd9Sstevel@tonic-gate 	r.ru_inblock	= p->p_cru.inblock;
1437c478bd9Sstevel@tonic-gate 	r.ru_oublock	= p->p_cru.oublock;
1447c478bd9Sstevel@tonic-gate 	r.ru_msgsnd	= p->p_cru.msgsnd;
1457c478bd9Sstevel@tonic-gate 	r.ru_msgrcv	= p->p_cru.msgrcv;
1467c478bd9Sstevel@tonic-gate 	r.ru_nsignals	= p->p_cru.nsignals;
1477c478bd9Sstevel@tonic-gate 	r.ru_nvcsw	= p->p_cru.nvcsw;
1487c478bd9Sstevel@tonic-gate 	r.ru_nivcsw	= p->p_cru.nivcsw;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	hrt2tv(unsecs, &r.ru_utime);
1537c478bd9Sstevel@tonic-gate 	hrt2tv(snsecs, &r.ru_stime);
1547c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1557c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
1567c478bd9Sstevel@tonic-gate 		struct rusage32 r32;
1577c478bd9Sstevel@tonic-gate 
158*d4bf385cSjohansen 		bzero(&r32, sizeof (struct rusage32));
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		r32.ru_utime.tv_sec  = r.ru_utime.tv_sec;
1617c478bd9Sstevel@tonic-gate 		r32.ru_utime.tv_usec = r.ru_utime.tv_usec;
1627c478bd9Sstevel@tonic-gate 		r32.ru_stime.tv_sec  = r.ru_stime.tv_sec;
1637c478bd9Sstevel@tonic-gate 		r32.ru_stime.tv_usec = r.ru_stime.tv_usec;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		r32.ru_majflt	= (int32_t)r.ru_majflt;
1667c478bd9Sstevel@tonic-gate 		r32.ru_minflt	= (int32_t)r.ru_minflt;
1677c478bd9Sstevel@tonic-gate 		r32.ru_nswap	= (int32_t)r.ru_nswap;
1687c478bd9Sstevel@tonic-gate 		r32.ru_inblock	= (int32_t)r.ru_inblock;
1697c478bd9Sstevel@tonic-gate 		r32.ru_oublock	= (int32_t)r.ru_oublock;
1707c478bd9Sstevel@tonic-gate 		r32.ru_msgsnd	= (int32_t)r.ru_msgsnd;
1717c478bd9Sstevel@tonic-gate 		r32.ru_msgrcv	= (int32_t)r.ru_msgrcv;
1727c478bd9Sstevel@tonic-gate 		r32.ru_nsignals	= (int32_t)r.ru_nsignals;
1737c478bd9Sstevel@tonic-gate 		r32.ru_nvcsw	= (int32_t)r.ru_nvcsw;
1747c478bd9Sstevel@tonic-gate 		r32.ru_nivcsw	= (int32_t)r.ru_nivcsw;
1757c478bd9Sstevel@tonic-gate 		if (copyout(&r32, user_rusage, sizeof (r32)) != 0)
1767c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1777c478bd9Sstevel@tonic-gate 	} else
1787c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		if (copyout(&r, user_rusage, sizeof (r)) != 0)
1817c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (0);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static int
1877c478bd9Sstevel@tonic-gate getrusage_lwp(void *user_rusage)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	struct rusage r;
1907c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1917c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
1927c478bd9Sstevel@tonic-gate 	hrtime_t snsecs, unsecs;
1937c478bd9Sstevel@tonic-gate 	struct mstate *ms;
1947c478bd9Sstevel@tonic-gate 
195*d4bf385cSjohansen 	bzero(&r, sizeof (struct rusage));
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	lwp = ttolwp(t);
1987c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
1997c478bd9Sstevel@tonic-gate 	unsecs = ms->ms_acct[LMS_USER];
2007c478bd9Sstevel@tonic-gate 	snsecs = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP];
2017c478bd9Sstevel@tonic-gate 	scalehrtime(&unsecs);
2027c478bd9Sstevel@tonic-gate 	scalehrtime(&snsecs);
2037c478bd9Sstevel@tonic-gate 	r.ru_majflt	= lwp->lwp_ru.majflt;
2047c478bd9Sstevel@tonic-gate 	r.ru_minflt	= lwp->lwp_ru.minflt;
2057c478bd9Sstevel@tonic-gate 	r.ru_nswap	= lwp->lwp_ru.nswap;
2067c478bd9Sstevel@tonic-gate 	r.ru_inblock	= lwp->lwp_ru.inblock;
2077c478bd9Sstevel@tonic-gate 	r.ru_oublock	= lwp->lwp_ru.oublock;
2087c478bd9Sstevel@tonic-gate 	r.ru_msgsnd	= lwp->lwp_ru.msgsnd;
2097c478bd9Sstevel@tonic-gate 	r.ru_msgrcv	= lwp->lwp_ru.msgrcv;
2107c478bd9Sstevel@tonic-gate 	r.ru_nsignals	= lwp->lwp_ru.nsignals;
2117c478bd9Sstevel@tonic-gate 	r.ru_nvcsw	= lwp->lwp_ru.nvcsw;
2127c478bd9Sstevel@tonic-gate 	r.ru_nivcsw	= lwp->lwp_ru.nivcsw;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	hrt2tv(unsecs, &r.ru_utime);
2157c478bd9Sstevel@tonic-gate 	hrt2tv(snsecs, &r.ru_stime);
2167c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2177c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
2187c478bd9Sstevel@tonic-gate 		struct rusage32 r32;
2197c478bd9Sstevel@tonic-gate 
220*d4bf385cSjohansen 		bzero(&r32, sizeof (struct rusage32));
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		r32.ru_utime.tv_sec  = r.ru_utime.tv_sec;
2237c478bd9Sstevel@tonic-gate 		r32.ru_utime.tv_usec = r.ru_utime.tv_usec;
2247c478bd9Sstevel@tonic-gate 		r32.ru_stime.tv_sec  = r.ru_stime.tv_sec;
2257c478bd9Sstevel@tonic-gate 		r32.ru_stime.tv_usec = r.ru_stime.tv_usec;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		r32.ru_majflt	= (int32_t)r.ru_majflt;
2287c478bd9Sstevel@tonic-gate 		r32.ru_minflt	= (int32_t)r.ru_minflt;
2297c478bd9Sstevel@tonic-gate 		r32.ru_nswap	= (int32_t)r.ru_nswap;
2307c478bd9Sstevel@tonic-gate 		r32.ru_inblock	= (int32_t)r.ru_inblock;
2317c478bd9Sstevel@tonic-gate 		r32.ru_oublock	= (int32_t)r.ru_oublock;
2327c478bd9Sstevel@tonic-gate 		r32.ru_msgsnd	= (int32_t)r.ru_msgsnd;
2337c478bd9Sstevel@tonic-gate 		r32.ru_msgrcv	= (int32_t)r.ru_msgrcv;
2347c478bd9Sstevel@tonic-gate 		r32.ru_nsignals	= (int32_t)r.ru_nsignals;
2357c478bd9Sstevel@tonic-gate 		r32.ru_nvcsw	= (int32_t)r.ru_nvcsw;
2367c478bd9Sstevel@tonic-gate 		r32.ru_nivcsw	= (int32_t)r.ru_nivcsw;
2377c478bd9Sstevel@tonic-gate 		if (copyout(&r32, user_rusage, sizeof (r32)) != 0)
2387c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2397c478bd9Sstevel@tonic-gate 	} else
2407c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		if (copyout(&r, user_rusage, sizeof (r)) != 0)
2437c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate int
2497c478bd9Sstevel@tonic-gate rusagesys(int code, void * arg)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	switch (code) {
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	case _RUSAGESYS_GETRUSAGE:
2547c478bd9Sstevel@tonic-gate 		return (getrusage(arg));
2557c478bd9Sstevel@tonic-gate 	case _RUSAGESYS_GETRUSAGE_CHLD:
2567c478bd9Sstevel@tonic-gate 		return (getrusage_chld(arg));
2577c478bd9Sstevel@tonic-gate 	case _RUSAGESYS_GETRUSAGE_LWP:
2587c478bd9Sstevel@tonic-gate 		return (getrusage_lwp(arg));
2597c478bd9Sstevel@tonic-gate 	default:
2607c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate }
263