Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 24 Aug 2020 14:26:49 +0000 (UTC)
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r364688 - in stable/12/sys: kern sys
Message-ID:  <202008241426.07OEQn1h098378@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trasz
Date: Mon Aug 24 14:26:48 2020
New Revision: 364688
URL: https://svnweb.freebsd.org/changeset/base/364688

Log:
  MFC r357492 by dchagin:
  
  For code reuse in Linuxulator rename get_proccess_cputime()
  and get_thread_cputime() and add prototypes for it to <sys/syscallsubr.h>.
  
  As both functions become a public interface add process lock assert
  to ensure that the process is not exiting under it.
  
  Fix whitespace nit while here.

Modified:
  stable/12/sys/kern/kern_time.c
  stable/12/sys/sys/syscallsubr.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/kern/kern_time.c
==============================================================================
--- stable/12/sys/kern/kern_time.c	Mon Aug 24 14:17:25 2020	(r364687)
+++ stable/12/sys/kern/kern_time.c	Mon Aug 24 14:26:48 2020	(r364688)
@@ -243,7 +243,7 @@ sys_clock_gettime(struct thread *td, struct clock_gett
 	return (error);
 }
 
-static inline void 
+static inline void
 cputick2timespec(uint64_t runtime, struct timespec *ats)
 {
 	runtime = cputick2usec(runtime);
@@ -251,12 +251,15 @@ cputick2timespec(uint64_t runtime, struct timespec *at
 	ats->tv_nsec = runtime % 1000000 * 1000;
 }
 
-static void
-get_thread_cputime(struct thread *targettd, struct timespec *ats)
+void
+kern_thread_cputime(struct thread *targettd, struct timespec *ats)
 {
 	uint64_t runtime, curtime, switchtime;
+	struct proc *p;
 
 	if (targettd == NULL) { /* current thread */
+		p = curthread->td_proc;
+		PROC_LOCK_ASSERT(p, MA_OWNED);
 		critical_enter();
 		switchtime = PCPU_GET(switchtime);
 		curtime = cpu_ticks();
@@ -264,6 +267,8 @@ get_thread_cputime(struct thread *targettd, struct tim
 		critical_exit();
 		runtime += curtime - switchtime;
 	} else {
+		p = targettd->td_proc;
+		PROC_LOCK_ASSERT(p, MA_OWNED);
 		thread_lock(targettd);
 		runtime = targettd->td_runtime;
 		thread_unlock(targettd);
@@ -271,12 +276,13 @@ get_thread_cputime(struct thread *targettd, struct tim
 	cputick2timespec(runtime, ats);
 }
 
-static void
-get_process_cputime(struct proc *targetp, struct timespec *ats)
+void
+kern_process_cputime(struct proc *targetp, struct timespec *ats)
 {
 	uint64_t runtime;
 	struct rusage ru;
 
+	PROC_LOCK_ASSERT(targetp, MA_OWNED);
 	PROC_STATLOCK(targetp);
 	rufetch(targetp, &ru);
 	runtime = targetp->p_rux.rux_runtime;
@@ -301,14 +307,14 @@ get_cputime(struct thread *td, clockid_t clock_id, str
 		td2 = tdfind(tid, p->p_pid);
 		if (td2 == NULL)
 			return (EINVAL);
-		get_thread_cputime(td2, ats);
+		kern_thread_cputime(td2, ats);
 		PROC_UNLOCK(td2->td_proc);
 	} else {
 		pid = clock_id & CPUCLOCK_ID_MASK;
 		error = pget(pid, PGET_CANSEE, &p2);
 		if (error != 0)
 			return (EINVAL);
-		get_process_cputime(p2, ats);
+		kern_process_cputime(p2, ats);
 		PROC_UNLOCK(p2);
 	}
 	return (0);
@@ -361,11 +367,11 @@ kern_clock_gettime(struct thread *td, clockid_t clock_
 		ats->tv_nsec = 0;
 		break;
 	case CLOCK_THREAD_CPUTIME_ID:
-		get_thread_cputime(NULL, ats);
+		kern_thread_cputime(NULL, ats);
 		break;
 	case CLOCK_PROCESS_CPUTIME_ID:
 		PROC_LOCK(p);
-		get_process_cputime(p, ats);
+		kern_process_cputime(p, ats);
 		PROC_UNLOCK(p);
 		break;
 	default:

Modified: stable/12/sys/sys/syscallsubr.h
==============================================================================
--- stable/12/sys/sys/syscallsubr.h	Mon Aug 24 14:17:25 2020	(r364687)
+++ stable/12/sys/sys/syscallsubr.h	Mon Aug 24 14:26:48 2020	(r364688)
@@ -93,6 +93,8 @@ int	kern_clock_nanosleep(struct thread *td, clockid_t 
 	    const struct timespec *rqtp, struct timespec *rmtp);
 int	kern_clock_settime(struct thread *td, clockid_t clock_id,
 	    struct timespec *ats);
+void	kern_thread_cputime(struct thread *targettd, struct timespec *ats);
+void	kern_process_cputime(struct proc *targetp, struct timespec *ats);
 int	kern_close_range(struct thread *td, u_int lowfd, u_int highfd);
 int	kern_close(struct thread *td, int fd);
 int	kern_connectat(struct thread *td, int dirfd, int fd,



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202008241426.07OEQn1h098378>