Back to index

glibc  2.9
tst-cputimer1.c
Go to the documentation of this file.
00001 /* Tests for POSIX timer implementation using process CPU clock.  */
00002 
00003 #include <unistd.h>
00004 
00005 #if _POSIX_THREADS && defined _POSIX_CPUTIME
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <fcntl.h>
00011 #include <time.h>
00012 #include <pthread.h>
00013 
00014 #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
00015 #define TEST_CLOCK_MISSING(clock) \
00016   (setup_test () ? "process CPU clock timer support" : NULL)
00017 
00018 /* This function is intended to rack up both user and system time.  */
00019 static void *
00020 chew_cpu (void *arg)
00021 {
00022   while (1)
00023     {
00024       static volatile char buf[4096];
00025       for (int i = 0; i < 100; ++i)
00026        for (size_t j = 0; j < sizeof buf; ++j)
00027          buf[j] = 0xaa;
00028       int nullfd = open ("/dev/null", O_WRONLY);
00029       for (int i = 0; i < 100; ++i)
00030        for (size_t j = 0; j < sizeof buf; ++j)
00031          buf[j] = 0xbb;
00032       write (nullfd, (char *) buf, sizeof buf);
00033       close (nullfd);
00034     }
00035 
00036   return NULL;
00037 }
00038 
00039 static int
00040 setup_test (void)
00041 {
00042   /* Test timers on our own process CPU clock by having a worker thread
00043      eating CPU.  First make sure we can make such timers at all.  */
00044 
00045   timer_t t;
00046   if (timer_create (TEST_CLOCK, NULL, &t) != 0)
00047     {
00048       printf ("timer_create: %m\n");
00049       return 1;
00050     }
00051   timer_delete (t);
00052 
00053   pthread_t th;
00054   int e = pthread_create (&th, NULL, chew_cpu, NULL);
00055   if (e != 0)
00056     {
00057       printf ("pthread_create: %s\n", strerror (e));
00058       exit (1);
00059     }
00060 
00061   return 0;
00062 }
00063 
00064 #else
00065 # define TEST_CLOCK_MISSING(clock) "process clocks"
00066 #endif
00067 
00068 #include "tst-timer4.c"