Beispiel #1
0
int main(int argc, char **argv)
{
    int ret;
    test_issue_128();

    test_sleep(1, 0, 1, 0);
    
    test_nanosleep(0, 0, 0, 1000); /*if 0 time is passed it should take anyway 1 microsecond*/
    test_nanosleep(0, 1998, 0, 1998);

    test_select_timeout(0, 0, 0, 1000);
    test_select_timeout(0, 1000, 0, 1000);

    {
    	struct timespec ts, ts2;
    	struct timeval tv_inc, tv, tv2;

    	/*compare time before and after select, measure in nanoseconds*/
    	TEST_OPERATION_RESULT( clock_gettime(CLOCK_REALTIME, &ts), &ret, ret==0 );

    	/*select can only wait for timeout when no file descriptors specified*/
    	struct timeval timeout;
    	timeout.tv_sec = 1;
    	timeout.tv_usec = 1000;
    	TEST_OPERATION_RESULT( select(0, NULL, NULL,
    				      NULL, &timeout), &ret, ret==0);
    	timeout.tv_usec += 1; /*clock_gettime done +1 microsecond*/
    	TEST_OPERATION_RESULT( clock_gettime(CLOCK_REALTIME, &ts2), &ret, ret==0 );

    	TIMESPEC_TO_TIMEVAL(&tv, &ts);
    	TIMESPEC_TO_TIMEVAL(&tv2, &ts2);
    	timeradd(&tv, &timeout, &tv_inc);

    	TEST_OPERATION_RESULT( timercmp(&tv_inc, &tv2, <= ), &ret, ret==1);

    	/*select's synchronous multiplexing is supported now*/
    	fd_set readfds;
    	FD_ZERO (&readfds);
    	FD_SET (0, &readfds);
    	TEST_OPERATION_RESULT( select(FD_SETSIZE, &readfds, NULL,
    				      NULL, &timeout), &ret, errno!=ENOSYS&&ret!=0);
    }

    return 0;
}
/*! \brief Wait until a channel is not bridged */
static void wait_for_unbridged(struct ast_channel *channel)
{
	ast_channel_lock(channel);
	while (ast_channel_is_bridged(channel)) {
		ast_channel_unlock(channel);
		test_nanosleep(0, 1000000);
		ast_channel_lock(channel);
	}
	ast_channel_unlock(channel);
}
/* Need to post null frames periodically so DTMF emulation can work. */
static void stream_periodic_frames(struct ast_channel *chan, int ms, int interval_ms)
{
	long nanosecs;

	ast_assert(chan != NULL);
	ast_assert(0 < ms);
	ast_assert(0 < interval_ms);

	nanosecs = interval_ms * 1000000L;
	while (0 < ms) {
		ast_queue_frame(chan, &ast_null_frame);

		if (interval_ms < ms) {
			ms -= interval_ms;
		} else {
			nanosecs = ms * 1000000L;
			ms = 0;
		}
		test_nanosleep(0, nanosecs);
	}
}
Beispiel #4
0
static unsigned long long int
tsdiff (const struct timespec *before, const struct timespec *after)
{
  struct timespec diff = { .tv_sec = after->tv_sec - before->tv_sec,
			   .tv_nsec = after->tv_nsec - before->tv_nsec };
  while (diff.tv_nsec < 0)
    {
      --diff.tv_sec;
      diff.tv_nsec += 1000000000;
    }
  return diff.tv_sec * 1000000000ULL + diff.tv_nsec;
}

static unsigned long long int
test_nanosleep (clockid_t clock, const char *which,
		const struct timespec *before, int *bad)
{
  const struct timespec sleeptime = { .tv_nsec = 100000000 };
  int e = clock_nanosleep (clock, 0, &sleeptime, NULL);
  if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
    {
      printf ("clock_nanosleep not supported for %s CPU clock: %s\n",
	      which, strerror (e));
      return 0;
    }
  if (e != 0)
    {
      printf ("clock_nanosleep on %s CPU clock: %s\n", which, strerror (e));
      *bad = 1;
      return 0;
    }

  struct timespec after;
  if (clock_gettime (clock, &after) < 0)
    {
      printf ("clock_gettime on %s CPU clock %lx => %s\n",
	      which, (unsigned long int) clock, strerror (errno));
      *bad = 1;
      return 0;
    }

  unsigned long long int diff = tsdiff (before, &after);
  if (diff < sleeptime.tv_nsec || diff > sleeptime.tv_nsec * 2)
    {
      printf ("clock_nanosleep on %s slept %llu (outside reasonable range)\n",
	      which, diff);
      *bad = 1;
      return diff;
    }

  struct timespec sleeptimeabs = sleeptime;
  sleeptimeabs.tv_sec += after.tv_sec;
  sleeptimeabs.tv_nsec += after.tv_nsec;
  while (sleeptimeabs.tv_nsec >= 1000000000)
    {
      ++sleeptimeabs.tv_sec;
      sleeptimeabs.tv_nsec -= 1000000000;
    }
  e = clock_nanosleep (clock, TIMER_ABSTIME, &sleeptimeabs, NULL);
  if (e != 0)
    {
      printf ("absolute clock_nanosleep on %s CPU clock: %s\n",
	      which, strerror (e));
      *bad = 1;
      return diff;
    }

  struct timespec afterabs;
  if (clock_gettime (clock, &afterabs) < 0)
    {
      printf ("clock_gettime on %s CPU clock %lx => %s\n",
	      which, (unsigned long int) clock, strerror (errno));
      *bad = 1;
      return diff;
    }

  unsigned long long int sleepdiff = tsdiff (&sleeptimeabs, &afterabs);
  if (sleepdiff > sleeptime.tv_nsec)
    {
      printf ("\
absolute clock_nanosleep on %s %llu past target (outside reasonable range)\n",
	      which, sleepdiff);
      *bad = 1;
    }

  unsigned long long int diffabs = tsdiff (&after, &afterabs);
  if (diffabs < sleeptime.tv_nsec || diffabs > sleeptime.tv_nsec * 2)
    {
      printf ("\
absolute clock_nanosleep on %s slept %llu (outside reasonable range)\n",
	      which, diffabs);
      *bad = 1;
    }

  return diff + diffabs;
}



static int
do_test (void)
{
  int result = 0;
  clockid_t process_clock, th_clock, my_thread_clock;
  int e;
  pthread_t th;

  e = clock_getcpuclockid (0, &process_clock);
  if (e != 0)
    {
      printf ("clock_getcpuclockid on self => %s\n", strerror (e));
      return 1;
    }

  e = pthread_getcpuclockid (pthread_self (), &my_thread_clock);
  if (e != 0)
    {
      printf ("pthread_getcpuclockid on self => %s\n", strerror (e));
      return 1;
    }

  /* This is a kludge.  This test fails if the semantics of thread and
     process clocks are wrong.  The old code using hp-timing without kernel
     support has bogus semantics if there are context switches.  We don't
     fail to report failure when the proper functionality is not available
     in the kernel.  It so happens that Linux kernels without correct CPU
     clock support also lack CPU timer support, so we use use that to guess
     that we are using the bogus code and not test it.  */
  timer_t t;
  if (timer_create (my_thread_clock, NULL, &t) != 0)
    {
      printf ("timer_create: %m\n");
      puts ("No support for CPU clocks with good semantics, skipping test");
      return 0;
    }
  timer_delete (t);


  pthread_barrier_init (&barrier, NULL, 2);

  e = pthread_create (&th, NULL, chew_cpu, NULL);
  if (e != 0)
    {
      printf ("pthread_create: %s\n", strerror (e));
      return 1;
    }

  e = pthread_getcpuclockid (th, &th_clock);
  if (e == ENOENT || e == ENOSYS || e == ENOTSUP)
    {
      puts ("pthread_getcpuclockid does not support other threads");
      return 1;
    }

  pthread_barrier_wait (&barrier);

  struct timespec res;
  if (clock_getres (th_clock, &res) < 0)
    {
      printf ("clock_getres on live thread clock %lx => %s\n",
	      (unsigned long int) th_clock, strerror (errno));
      result = 1;
      return 1;
    }
  printf ("live thread clock %lx resolution %lu.%.9lu\n",
	  (unsigned long int) th_clock, res.tv_sec, res.tv_nsec);

  struct timespec process_before, process_after;
  if (clock_gettime (process_clock, &process_before) < 0)
    {
      printf ("clock_gettime on process clock %lx => %s\n",
	      (unsigned long int) process_clock, strerror (errno));
      return 1;
    }

  struct timespec before, after;
  if (clock_gettime (th_clock, &before) < 0)
    {
      printf ("clock_gettime on live thread clock %lx => %s\n",
	      (unsigned long int) th_clock, strerror (errno));
      return 1;
    }
  printf ("live thread before sleep => %lu.%.9lu\n",
	  before.tv_sec, before.tv_nsec);

  struct timespec me_before, me_after;
  if (clock_gettime (my_thread_clock, &me_before) < 0)
    {
      printf ("clock_gettime on self thread clock %lx => %s\n",
	      (unsigned long int) my_thread_clock, strerror (errno));
      return 1;
    }
  printf ("self thread before sleep => %lu.%.9lu\n",
	  me_before.tv_sec, me_before.tv_nsec);

  struct timespec sleeptime = { .tv_nsec = 500000000 };
  if (nanosleep (&sleeptime, NULL) != 0)
    {
      perror ("nanosleep");
      return 1;
    }

  if (clock_gettime (th_clock, &after) < 0)
    {
      printf ("clock_gettime on live thread clock %lx => %s\n",
	      (unsigned long int) th_clock, strerror (errno));
      return 1;
    }
  printf ("live thread after sleep => %lu.%.9lu\n",
	  after.tv_sec, after.tv_nsec);

  if (clock_gettime (process_clock, &process_after) < 0)
    {
      printf ("clock_gettime on process clock %lx => %s\n",
	      (unsigned long int) process_clock, strerror (errno));
      return 1;
    }

  if (clock_gettime (my_thread_clock, &me_after) < 0)
    {
      printf ("clock_gettime on self thread clock %lx => %s\n",
	      (unsigned long int) my_thread_clock, strerror (errno));
      return 1;
    }
  printf ("self thread after sleep => %lu.%.9lu\n",
	  me_after.tv_sec, me_after.tv_nsec);

  unsigned long long int th_diff = tsdiff (&before, &after);
  unsigned long long int pdiff = tsdiff (&process_before, &process_after);
  unsigned long long int my_diff = tsdiff (&me_before, &me_after);

  if (th_diff < 100000000 || th_diff > 600000000)
    {
      printf ("live thread before - after %llu outside reasonable range\n",
	      th_diff);
      result = 1;
    }

  if (my_diff > 100000000)
    {
      printf ("self thread before - after %llu outside reasonable range\n",
	      my_diff);
      result = 1;
    }

  if (pdiff < th_diff)
    {
      printf ("process before - after %llu outside reasonable range (%llu)\n",
	      pdiff, th_diff);
      result = 1;
    }

  process_after.tv_nsec += test_nanosleep (th_clock, "live thread",
					   &after, &result);
  process_after.tv_nsec += test_nanosleep (process_clock, "process",
					   &process_after, &result);
  test_nanosleep (CLOCK_PROCESS_CPUTIME_ID,
		  "PROCESS_CPUTIME_ID", &process_after, &result);

  pthread_cancel (th);

  e = clock_nanosleep (CLOCK_THREAD_CPUTIME_ID, 0, &sleeptime, NULL);
  if (e != EINVAL)
    {
      printf ("clock_nanosleep CLOCK_THREAD_CPUTIME_ID: %s\n",
	      strerror (e));
      result = 1;
    }

  return result;
}