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