summaryrefslogtreecommitdiffstats
path: root/test/kernel/adjtime.c
blob: 0ca8ff2fc7789c6fbf5c11146b2d966328f39dff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) Miroslav Lichvar  2015
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/* Test the system adjtime() function. Check the range of supported offset,
   support for readonly operation, and slew rate with different update
   intervals and offsets. */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

static int
diff_tv(struct timeval *tv1, struct timeval *tv2)
{
  return 1000000 * (tv1->tv_sec - tv2->tv_sec) + (tv1->tv_usec - tv2->tv_usec);
}

static struct timeval
usec_to_tv(int usec)
{
  struct timeval tv;

  tv.tv_sec = usec / 1000000;
  tv.tv_usec = usec % 1000000;

  return tv;
}

static int
try_adjtime(struct timeval *new, struct timeval *old)
{
  int r;

  r = adjtime(new, old);
  if (r)
    printf("adjtime() failed : %s ", strerror(errno));
  return r;
}

static void
reset_adjtime(void)
{
  struct timeval tv;

  tv = usec_to_tv(0);
  try_adjtime(&tv, NULL);
}

static void
test_range(void)
{
  struct timeval tv;
  int i;

  printf("range:\n");

  for (i = 0; i < sizeof (time_t) * 8; i++) {
    tv.tv_usec = 0;
    tv.tv_sec = (1ULL << i) - 1;
    printf("%20lld s : ", (long long)tv.tv_sec);
    printf("%s\n", !try_adjtime(&tv, NULL) ? "ok" : "");
    tv.tv_sec = ~tv.tv_sec;
    printf("%20lld s : ", (long long)tv.tv_sec);
    printf("%s\n", !try_adjtime(&tv, NULL) ? "ok" : "");
  }
}

static void
test_readonly(void)
{
  struct timeval tv1, tv2;
  int i, r;

  printf("readonly:\n");

  for (i = 0; i <= 20; i++) {
    tv1 = usec_to_tv(1 << i);

    printf("%9d us : ", 1 << i);
    try_adjtime(&tv1, NULL);
    r = !try_adjtime(NULL, &tv2) && !diff_tv(&tv1, &tv2);
    printf("%s\n", r ? "ok" : "fail");
  }
}

static void
test_readwrite(void)
{
  struct timeval tv1, tv2, tv3;
  int i, r;

  printf("readwrite:\n");

  for (i = 0; i <= 20; i++) {
    tv1 = usec_to_tv(1 << i);
    tv3 = usec_to_tv(0);

    printf("%9d us : ", 1 << i);
    try_adjtime(&tv1, NULL);
    r = !try_adjtime(&tv3, &tv2) && !diff_tv(&tv1, &tv2);
    printf("%s\n", r ? "ok" : "fail");
  }
}

static void
xusleep(int usec)
{
  struct timeval tv;

  tv = usec_to_tv(usec);
  select(0, NULL, NULL, NULL, &tv);
}

static void
test_slew(void)
{
  struct timeval tv1, tv2, tv3;
  int i, j, k, diff, min, has_min;

  printf("slew:\n");

  for (i = 9; i <= 20; i++) {
    printf("%9d us : ", 1 << i);
    for (j = 4; j <= 20; j += 4) {
      for (min = has_min = 0, k = 4; k < 16; k += 2) {

        tv1 = usec_to_tv(1 << j);
        tv3 = usec_to_tv(0);

        xusleep(1 << i);
        reset_adjtime();

        xusleep(1 << i);
        if (try_adjtime(&tv1, NULL))
          continue;

        xusleep(1 << i);
        if (try_adjtime(&tv3, &tv2))
          continue;

        diff = diff_tv(&tv1, &tv2);
        if (!has_min || min > diff) {
          min = diff;
          has_min = 1;
        }
      }

      if (!has_min)
        continue;

      printf(" %5d (%d)", min, 1 << j);
      fflush(stdout);
    }
    printf("\n");
  }
}

int
main()
{
  test_range();
  test_readonly();
  test_readwrite();
  test_slew();

  reset_adjtime();

  return 0;
}