Back to index

glibc  2.9
tst-clock1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <pthread.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 #include <unistd.h>
00026 
00027 
00028 #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
00029 static pthread_barrier_t b2;
00030 static pthread_barrier_t bN;
00031 
00032 
00033 static void *
00034 tf (void *arg)
00035 {
00036   int e = pthread_barrier_wait (&b2);
00037   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00038     {
00039       puts ("barrier_wait failed");
00040       exit (1);
00041     }
00042 
00043   e = pthread_barrier_wait (&bN);
00044   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00045     {
00046       puts ("barrier_wait failed");
00047       exit (1);
00048     }
00049 
00050   return NULL;
00051 }
00052 #endif
00053 
00054 
00055 int
00056 do_test (void)
00057 {
00058 #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
00059 # define N 10
00060 
00061 # if _POSIX_THREAD_CPUTIME == 0
00062   if (sysconf (_SC_THREAD_CPUTIME) < 0)
00063     {
00064       puts ("_POSIX_THREAD_CPUTIME option not available");
00065       return 0;
00066     }
00067 # endif
00068 
00069   if (pthread_barrier_init (&b2, NULL, 2) != 0
00070       || pthread_barrier_init (&bN, NULL, N + 1) != 0)
00071     {
00072       puts ("barrier_init failed");
00073       return 1;
00074     }
00075 
00076   struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
00077   TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
00078 
00079   pthread_t th[N + 1];
00080   clockid_t cl[N + 1];
00081 # ifndef CLOCK_THREAD_CPUTIME_ID
00082   if (pthread_getcpuclockid (pthread_self (), &cl[0]) != 0)
00083     {
00084       puts ("own pthread_getcpuclockid failed");
00085       return 1;
00086     }
00087 # else
00088   cl[0] = CLOCK_THREAD_CPUTIME_ID;
00089 # endif
00090 
00091   pthread_attr_t at;
00092 
00093   if (pthread_attr_init (&at) != 0)
00094     {
00095       puts ("attr_init failed");
00096       return 1;
00097     }
00098 
00099   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
00100     {
00101       puts ("attr_setstacksize failed");
00102       return 1;
00103     }
00104 
00105   int i;
00106   int e;
00107   for (i = 0; i < N; ++i)
00108     {
00109       if (pthread_create (&th[i], &at, tf, NULL) != 0)
00110        {
00111          puts ("create failed");
00112          return 1;
00113        }
00114 
00115       e = pthread_barrier_wait (&b2);
00116       if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
00117        {
00118          puts ("barrier_wait failed");
00119          return 1;
00120        }
00121 
00122       ts.tv_sec = 0;
00123       ts.tv_nsec = 100000000;
00124       TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
00125 
00126       if (pthread_getcpuclockid (th[i], &cl[i + 1]) != 0)
00127        {
00128          puts ("pthread_getcpuclockid failed");
00129          return 1;
00130        }
00131     }
00132 
00133   if (pthread_attr_destroy (&at) != 0)
00134     {
00135       puts ("attr_destroy failed");
00136       return 1;
00137     }
00138 
00139   struct timespec t[N + 1];
00140   for (i = 0; i < N + 1; ++i)
00141     if (clock_gettime (cl[i], &t[i]) != 0)
00142       {
00143        printf ("clock_gettime round %d failed\n", i);
00144        return 1;
00145       }
00146 
00147   for (i = 0; i < N; ++i)
00148     {
00149       struct timespec diff;
00150 
00151       diff.tv_sec = t[i].tv_sec - t[i + 1].tv_sec;
00152       diff.tv_nsec = t[i].tv_nsec - t[i + 1].tv_nsec;
00153       if (diff.tv_nsec < 0)
00154        {
00155          diff.tv_nsec += 1000000000;
00156          --diff.tv_sec;
00157        }
00158 
00159       if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec < 100000000))
00160        {
00161          printf ("\
00162 difference between thread %d and %d too small (%ld.%09ld)\n",
00163                 i, i + 1, (long int) diff.tv_sec, (long int) diff.tv_nsec);
00164          return 1;
00165        }
00166 
00167       printf ("diff %d->%d: %ld.%09ld\n",
00168              i, i + 1, (long int) diff.tv_sec, (long int) diff.tv_nsec);
00169     }
00170 
00171   ts.tv_sec = 0;
00172   ts.tv_nsec = 0;
00173   for (i = 0; i < N + 1; ++i)
00174     if (clock_settime (cl[i], &ts) != 0)
00175       {
00176        printf ("clock_settime(%d) round %d failed\n", cl[i], i);
00177        return 1;
00178       }
00179 
00180   for (i = 0; i < N + 1; ++i)
00181     {
00182       if (clock_gettime (cl[i], &ts) != 0)
00183        {
00184          puts ("clock_gettime failed");
00185          return 1;
00186        }
00187 
00188       if (ts.tv_sec > t[i].tv_sec
00189          || (ts.tv_sec == t[i].tv_sec && ts.tv_nsec > t[i].tv_nsec))
00190        {
00191          puts ("clock_settime didn't reset clock");
00192          return 1;
00193        }
00194     }
00195 #endif
00196 
00197   return 0;
00198 }
00199 
00200 
00201 #define TEST_FUNCTION do_test ()
00202 #include "../test-skeleton.c"