Blame time/tst-cpuclock1.c

Packit 6c4009
/* Test program for process CPU clocks.
Packit 6c4009
   Copyright (C) 2004-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
/* This function is intended to rack up both user and system time.  */
Packit 6c4009
static void
Packit 6c4009
chew_cpu (void)
Packit 6c4009
{
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      static volatile char buf[4096];
Packit 6c4009
      for (int i = 0; i < 100; ++i)
Packit 6c4009
	for (size_t j = 0; j < sizeof buf; ++j)
Packit 6c4009
	  buf[j] = 0xaa;
Packit 6c4009
      int nullfd = open ("/dev/null", O_WRONLY);
Packit 6c4009
      for (int i = 0; i < 100; ++i)
Packit 6c4009
	for (size_t j = 0; j < sizeof buf; ++j)
Packit 6c4009
	  buf[j] = 0xbb;
Packit 6c4009
      write (nullfd, (char *) buf, sizeof buf);
Packit 6c4009
      close (nullfd);
Packit 6c4009
      if (getppid () == 1)
Packit 6c4009
	_exit (2);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  clockid_t cl;
Packit 6c4009
  int e;
Packit 6c4009
  pid_t dead_child, child;
Packit 6c4009
Packit 6c4009
  /* Fork a child and let it die, to give us a PID known not be valid
Packit 6c4009
     (assuming PIDs don't wrap around during the test).  */
Packit 6c4009
  {
Packit 6c4009
    dead_child = fork ();
Packit 6c4009
    if (dead_child == 0)
Packit 6c4009
      _exit (0);
Packit 6c4009
    if (dead_child < 0)
Packit 6c4009
      {
Packit 6c4009
	perror ("fork");
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
    int x;
Packit 6c4009
    if (wait (&x) != dead_child)
Packit 6c4009
      {
Packit 6c4009
	perror ("wait");
Packit 6c4009
	return 2;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* POSIX says we should get ESRCH for this.  */
Packit 6c4009
  e = clock_getcpuclockid (dead_child, &cl);
Packit 6c4009
  if (e != ENOSYS && e != ESRCH && e != EPERM)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_getcpuclockid on dead PID %d => %s\n",
Packit 6c4009
	      dead_child, strerror (e));
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now give us a live child eating up CPU time.  */
Packit 6c4009
  child = fork ();
Packit 6c4009
  if (child == 0)
Packit 6c4009
    {
Packit 6c4009
      chew_cpu ();
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (child < 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("fork");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  e = clock_getcpuclockid (child, &cl);
Packit 6c4009
  if (e == EPERM)
Packit 6c4009
    {
Packit 6c4009
      puts ("clock_getcpuclockid does not support other processes");
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
  if (e != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_getcpuclockid on live PID %d => %s\n",
Packit 6c4009
	      child, strerror (e));
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  const clockid_t child_clock = cl;
Packit 6c4009
  struct timespec res;
Packit 6c4009
  if (clock_getres (child_clock, &res) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_getres on live PID %d clock %lx => %s\n",
Packit 6c4009
	      child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
  printf ("live PID %d clock %lx resolution %ju.%.9ju\n",
Packit 6c4009
	  child, (unsigned long int) child_clock,
Packit 6c4009
	  (uintmax_t) res.tv_sec, (uintmax_t) res.tv_nsec);
Packit 6c4009
Packit 6c4009
  struct timespec before, after;
Packit 6c4009
  if (clock_gettime (child_clock, &before) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_gettime on live PID %d clock %lx => %s\n",
Packit 6c4009
	      child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
  /* Should be close to 0.0.  */
Packit 6c4009
  printf ("live PID %d before sleep => %ju.%.9ju\n",
Packit 6c4009
	  child, (uintmax_t) before.tv_sec, (uintmax_t) before.tv_nsec);
Packit 6c4009
Packit 6c4009
  struct timespec sleeptime = { .tv_nsec = 500000000 };
Packit 6c4009
  if (nanosleep (&sleeptime, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("nanosleep");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (clock_gettime (child_clock, &after) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_gettime on live PID %d clock %lx => %s\n",
Packit 6c4009
	      child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
  /* Should be close to 0.5.  */
Packit 6c4009
  printf ("live PID %d after sleep => %ju.%.9ju\n",
Packit 6c4009
	  child, (uintmax_t) after.tv_sec, (uintmax_t) after.tv_nsec);
Packit 6c4009
Packit 6c4009
  struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
Packit 6c4009
			   .tv_nsec = after.tv_nsec - before.tv_nsec };
Packit 6c4009
  if (diff.tv_nsec < 0)
Packit 6c4009
    {
Packit 6c4009
      --diff.tv_sec;
Packit 6c4009
      diff.tv_nsec += 1000000000;
Packit 6c4009
    }
Packit 6c4009
  if (diff.tv_sec != 0
Packit 6c4009
      || diff.tv_nsec > 600000000
Packit 6c4009
      || diff.tv_nsec < 100000000)
Packit 6c4009
    {
Packit 6c4009
      printf ("before - after %ju.%.9ju outside reasonable range\n",
Packit 6c4009
	      (uintmax_t) diff.tv_sec, (uintmax_t) diff.tv_nsec);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  sleeptime.tv_nsec = 100000000;
Packit 6c4009
  e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
Packit 6c4009
  if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_nanosleep not supported for other process clock: %s\n",
Packit 6c4009
	      strerror (e));
Packit 6c4009
    }
Packit 6c4009
  else if (e != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      struct timespec afterns;
Packit 6c4009
      if (clock_gettime (child_clock, &afterns) < 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("clock_gettime on live PID %d clock %lx => %s\n",
Packit 6c4009
		  child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
Packit 6c4009
				.tv_nsec = afterns.tv_nsec - after.tv_nsec };
Packit 6c4009
	  if (d.tv_nsec < 0)
Packit 6c4009
	    {
Packit 6c4009
	      --d.tv_sec;
Packit 6c4009
	      d.tv_nsec += 1000000000;
Packit 6c4009
	    }
Packit 6c4009
	  if (d.tv_sec > 0
Packit 6c4009
	      || d.tv_nsec < sleeptime.tv_nsec
Packit 6c4009
	      || d.tv_nsec > sleeptime.tv_nsec * 2)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("nanosleep time %ju.%.9ju outside reasonable range\n",
Packit 6c4009
		      (uintmax_t) d.tv_sec, (uintmax_t) d.tv_nsec);
Packit 6c4009
	      result = 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (kill (child, SIGKILL) != 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("kill");
Packit 6c4009
      result = 2;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Wait long enough to let the child finish dying.  */
Packit 6c4009
Packit 6c4009
  sleeptime.tv_nsec = 200000000;
Packit 6c4009
  if (nanosleep (&sleeptime, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("nanosleep");
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct timespec dead;
Packit 6c4009
  if (clock_gettime (child_clock, &dead) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_gettime on dead PID %d clock %lx => %s\n",
Packit 6c4009
	      child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
      result = 1;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
  /* Should be close to 0.6.  */
Packit 6c4009
  printf ("dead PID %d => %ju.%.9ju\n",
Packit 6c4009
	  child, (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
Packit 6c4009
Packit 6c4009
  diff.tv_sec = dead.tv_sec - after.tv_sec;
Packit 6c4009
  diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
Packit 6c4009
  if (diff.tv_nsec < 0)
Packit 6c4009
    {
Packit 6c4009
      --diff.tv_sec;
Packit 6c4009
      diff.tv_nsec += 1000000000;
Packit 6c4009
    }
Packit 6c4009
  if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
Packit 6c4009
    {
Packit 6c4009
      printf ("dead - after %ju.%.9ju outside reasonable range\n",
Packit 6c4009
	      (uintmax_t) diff.tv_sec, (uintmax_t) diff.tv_nsec);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now reap the child and verify that its clock is no longer valid.  */
Packit 6c4009
  {
Packit 6c4009
    int x;
Packit 6c4009
    if (waitpid (child, &x, 0) != child)
Packit 6c4009
      {
Packit 6c4009
	perror ("waitpid");
Packit 6c4009
	result = 1;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  if (clock_gettime (child_clock, &dead) == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_gettime on reaped PID %d clock %lx => %ju%.9ju\n",
Packit 6c4009
	      child, (unsigned long int) child_clock,
Packit 6c4009
	      (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (errno != EINVAL)
Packit 6c4009
	result = 1;
Packit 6c4009
      printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
Packit 6c4009
	      child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (clock_getres (child_clock, &dead) == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("clock_getres on reaped PID %d clock %lx => %ju%.9ju\n",
Packit 6c4009
	      child, (unsigned long int) child_clock,
Packit 6c4009
	      (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (errno != EINVAL)
Packit 6c4009
	result = 1;
Packit 6c4009
      printf ("clock_getres on reaped PID %d clock %lx => %s\n",
Packit 6c4009
	      child, (unsigned long int) child_clock, strerror (errno));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
Packit 6c4009
 done:
Packit 6c4009
  {
Packit 6c4009
    if (kill (child, SIGKILL) != 0 && errno != ESRCH)
Packit 6c4009
      {
Packit 6c4009
	perror ("kill");
Packit 6c4009
	return 2;
Packit 6c4009
      }
Packit 6c4009
    int x;
Packit 6c4009
    if (waitpid (child, &x, 0) != child && errno != ECHILD)
Packit 6c4009
      {
Packit 6c4009
	perror ("waitpid");
Packit 6c4009
	return 2;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TIMEOUT 5
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"