[LTP] [PATCH v3] syscalls/nice05: Add testcase for nice() syscall

zhaogongyi zhaogongyi@huawei.com
Tue Aug 23 13:23:25 CEST 2022


Hi Cyril,

> Hi!
> > +static void verify_nice(void)
> > +{
> > +	intptr_t nice_inc_high = -1;
> > +	intptr_t nice_inc_low = -2;
> > +	char buf[4096] = {0};
> > +	float sum_exec_runtime[2];
> > +	pthread_t nice_low, nice_high;
> > +	pid_t pid;
> > +
> > +	pid = SAFE_FORK();
> > +	if (!pid) {
> > +		TEST(pthread_barrier_init(&barrier, NULL, 3));
> > +		if (TST_RET != 0) {
> > +			tst_brk(TBROK | TTERRNO,
> > +				"pthread_barrier_init() failed");
> > +		}
> > +
> > +		SAFE_PTHREAD_CREATE(&nice_high, NULL, nice_high_thread,
> > +			(void *)nice_inc_high);
> > +		SAFE_PTHREAD_CREATE(&nice_low, NULL, nice_low_thread,
> > +			(void *)nice_inc_low);
> > +
> > +		TEST(pthread_barrier_wait(&barrier));
> > +		if (TST_RET != 0 && TST_RET !=
> PTHREAD_BARRIER_SERIAL_THREAD) {
> > +			tst_brk(TBROK | TTERRNO,
> > +				"pthread_barrier_wait() failed");
> > +		}
> > +
> > +		while (1)
> > +			pause();
> > +	}
> > +
> > +	sleep(tst_remaining_runtime());
> > +	SAFE_KILL(pid, SIGSTOP);
> > +
> > +	sprintf(buf, "/proc/%d/sched", child_tid[0]);
> > +	SAFE_FILE_SCANF(buf, "%*s %*s %*s %*s %*s"
> > +			"%*s %*s %*f %*s %*s %*f %*s %*s %f",
> > +			&sum_exec_runtime[0]);
> > +
> > +	sprintf(buf, "/proc/%d/sched", child_tid[1]);
> > +	SAFE_FILE_SCANF(buf, "%*s %*s %*s %*s %*s"
> > +			"%*s %*s %*f %*s %*s %*f %*s %*s %f",
> > +			&sum_exec_runtime[1]);
> 
> The /proc/$PID/sched file is unfortunatelly only present if
> CONFIG_SCHED_DEBUG was set in the kernel, which is not always the case,
> and the test TBROKs on some of my testing targets.
> 
> Thinking of alternatives we can probably do clock_getcpuclockid() for the
> threads and read the counters with clock_gettime().

The kernel does not support obtaining the CPU-time clock of another process according to man 3,and it would retuen the error number of EFAULT.

And, pthread_getcpuclockid() also can not get the CPU-time of the thread belong to another process.

So, it looks a little tricky.

> 
> --
> Cyril Hrubis
> chrubis@suse.cz


More information about the ltp mailing list