"Fossies" - the Fresh Open Source Software Archive

Member "chrony-3.5/test/kernel/adjtime.c" (10 May 2019, 3746 Bytes) of package /linux/misc/chrony-3.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright (C) Miroslav Lichvar  2015
    3  * 
    4  * This program is free software; you can redistribute it and/or modify
    5  * it under the terms of version 2 of the GNU General Public License as
    6  * published by the Free Software Foundation.
    7  * 
    8  * This program is distributed in the hope that it will be useful, but
    9  * WITHOUT ANY WARRANTY; without even the implied warranty of
   10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   11  * General Public License for more details.
   12  * 
   13  * You should have received a copy of the GNU General Public License along
   14  * with this program; if not, write to the Free Software Foundation, Inc.,
   15  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   16  */
   17 
   18 /* Test the system adjtime() function. Check the range of supported offset,
   19    support for readonly operation, and slew rate with different update
   20    intervals and offsets. */
   21 
   22 #include <errno.h>
   23 #include <stdio.h>
   24 #include <string.h>
   25 #include <sys/time.h>
   26 #include <unistd.h>
   27 
   28 static int
   29 diff_tv(struct timeval *tv1, struct timeval *tv2)
   30 {
   31   return 1000000 * (tv1->tv_sec - tv2->tv_sec) + (tv1->tv_usec - tv2->tv_usec);
   32 }
   33 
   34 static struct timeval
   35 usec_to_tv(int usec)
   36 {
   37   struct timeval tv;
   38 
   39   tv.tv_sec = usec / 1000000;
   40   tv.tv_usec = usec % 1000000;
   41 
   42   return tv;
   43 }
   44 
   45 static int
   46 try_adjtime(struct timeval *new, struct timeval *old)
   47 {
   48   int r;
   49 
   50   r = adjtime(new, old);
   51   if (r)
   52     printf("adjtime() failed : %s ", strerror(errno));
   53   return r;
   54 }
   55 
   56 static void
   57 reset_adjtime(void)
   58 {
   59   struct timeval tv;
   60 
   61   tv = usec_to_tv(0);
   62   try_adjtime(&tv, NULL);
   63 }
   64 
   65 static void
   66 test_range(void)
   67 {
   68   struct timeval tv;
   69   int i;
   70 
   71   printf("range:\n");
   72 
   73   for (i = 0; i < sizeof (time_t) * 8; i++) {
   74     tv.tv_usec = 0;
   75     tv.tv_sec = (1ULL << i) - 1;
   76     printf("%20lld s : ", (long long)tv.tv_sec);
   77     printf("%s\n", !try_adjtime(&tv, NULL) ? "ok" : "");
   78     tv.tv_sec = ~tv.tv_sec;
   79     printf("%20lld s : ", (long long)tv.tv_sec);
   80     printf("%s\n", !try_adjtime(&tv, NULL) ? "ok" : "");
   81   }
   82 }
   83 
   84 static void
   85 test_readonly(void)
   86 {
   87   struct timeval tv1, tv2;
   88   int i, r;
   89 
   90   printf("readonly:\n");
   91 
   92   for (i = 0; i <= 20; i++) {
   93     tv1 = usec_to_tv(1 << i);
   94 
   95     printf("%9d us : ", 1 << i);
   96     try_adjtime(&tv1, NULL);
   97     r = !try_adjtime(NULL, &tv2) && !diff_tv(&tv1, &tv2);
   98     printf("%s\n", r ? "ok" : "fail");
   99   }
  100 }
  101 
  102 static void
  103 test_readwrite(void)
  104 {
  105   struct timeval tv1, tv2, tv3;
  106   int i, r;
  107 
  108   printf("readwrite:\n");
  109 
  110   for (i = 0; i <= 20; i++) {
  111     tv1 = usec_to_tv(1 << i);
  112     tv3 = usec_to_tv(0);
  113 
  114     printf("%9d us : ", 1 << i);
  115     try_adjtime(&tv1, NULL);
  116     r = !try_adjtime(&tv3, &tv2) && !diff_tv(&tv1, &tv2);
  117     printf("%s\n", r ? "ok" : "fail");
  118   }
  119 }
  120 
  121 static void
  122 xusleep(int usec)
  123 {
  124   struct timeval tv;
  125 
  126   tv = usec_to_tv(usec);
  127   select(0, NULL, NULL, NULL, &tv);
  128 }
  129 
  130 static void
  131 test_slew(void)
  132 {
  133   struct timeval tv1, tv2, tv3;
  134   int i, j, k, diff, min, has_min;
  135 
  136   printf("slew:\n");
  137 
  138   for (i = 9; i <= 20; i++) {
  139     printf("%9d us : ", 1 << i);
  140     for (j = 4; j <= 20; j += 4) {
  141       for (min = has_min = 0, k = 4; k < 16; k += 2) {
  142 
  143         tv1 = usec_to_tv(1 << j);
  144         tv3 = usec_to_tv(0);
  145 
  146         xusleep(1 << i);
  147         reset_adjtime();
  148 
  149         xusleep(1 << i);
  150         if (try_adjtime(&tv1, NULL))
  151           continue;
  152 
  153         xusleep(1 << i);
  154         if (try_adjtime(&tv3, &tv2))
  155           continue;
  156 
  157         diff = diff_tv(&tv1, &tv2);
  158         if (!has_min || min > diff) {
  159           min = diff;
  160           has_min = 1;
  161         }
  162       }
  163 
  164       if (!has_min)
  165         continue;
  166 
  167       printf(" %5d (%d)", min, 1 << j);
  168       fflush(stdout);
  169     }
  170     printf("\n");
  171   }
  172 }
  173 
  174 int
  175 main()
  176 {
  177   test_range();
  178   test_readonly();
  179   test_readwrite();
  180   test_slew();
  181 
  182   reset_adjtime();
  183 
  184   return 0;
  185 }