Skip to content

Commit 263dda2

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
selftests/proc: Assert clock_gettime(CLOCK_BOOTTIME) VS /proc/uptime monotonicity
The first field of /proc/uptime relies on the CLOCK_BOOTTIME clock which can also be fetched from clock_gettime() API. Improve the test coverage while verifying the monotonicity of CLOCK_BOOTTIME accross both interfaces. Suggested-by: Thomas Gleixner <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 270b2a6 commit 263dda2

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

tools/testing/selftests/proc/proc-uptime-001.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*/
16-
// Test that boottime value in /proc/uptime increments monotonically.
17-
// We don't test idle time monotonicity due to broken iowait task
18-
// counting, cf: comment above get_cpu_idle_time_us()
16+
// Test that boottime value in /proc/uptime and CLOCK_BOOTTIME increment
17+
// monotonically. We don't test idle time monotonicity due to broken iowait
18+
// task counting, cf: comment above get_cpu_idle_time_us()
1919
#undef NDEBUG
2020
#include <assert.h>
2121
#include <stdint.h>
@@ -27,18 +27,31 @@
2727

2828
int main(void)
2929
{
30-
uint64_t start, u0, u1;
30+
uint64_t start, u0, u1, c0, c1;
3131
int fd;
3232

3333
fd = open("/proc/uptime", O_RDONLY);
3434
assert(fd >= 0);
3535

3636
u0 = proc_uptime(fd);
3737
start = u0;
38+
c0 = clock_boottime();
39+
3840
do {
3941
u1 = proc_uptime(fd);
42+
c1 = clock_boottime();
43+
44+
/* Is /proc/uptime monotonic ? */
4045
assert(u1 >= u0);
46+
47+
/* Is CLOCK_BOOTTIME monotonic ? */
48+
assert(c1 >= c0);
49+
50+
/* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */
51+
assert(c0 >= u0);
52+
4153
u0 = u1;
54+
c0 = c1;
4255
} while (u1 - start < 100);
4356

4457
return 0;

tools/testing/selftests/proc/proc-uptime-002.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*/
16-
// Test that boottime value in /proc/uptime increments monotonically
17-
// while shifting across CPUs. We don't test idle time monotonicity
18-
// due to broken iowait task counting, cf: comment above get_cpu_idle_time_us()
16+
// Test that boottime value in /proc/uptime and CLOCK_BOOTTIME increment
17+
// monotonically while shifting across CPUs. We don't test idle time
18+
// monotonicity due to broken iowait task counting, cf: comment above
19+
// get_cpu_idle_time_us()
1920
#undef NDEBUG
2021
#include <assert.h>
2122
#include <errno.h>
@@ -43,10 +44,10 @@ static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned lo
4344

4445
int main(void)
4546
{
47+
uint64_t u0, u1, c0, c1;
4648
unsigned int len;
4749
unsigned long *m;
4850
unsigned int cpu;
49-
uint64_t u0, u1;
5051
int fd;
5152

5253
/* find out "nr_cpu_ids" */
@@ -62,6 +63,8 @@ int main(void)
6263
assert(fd >= 0);
6364

6465
u0 = proc_uptime(fd);
66+
c0 = clock_boottime();
67+
6568
for (cpu = 0; cpu < len * 8; cpu++) {
6669
memset(m, 0, len);
6770
m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long)));
@@ -70,8 +73,19 @@ int main(void)
7073
sys_sched_setaffinity(0, len, m);
7174

7275
u1 = proc_uptime(fd);
76+
c1 = clock_boottime();
77+
78+
/* Is /proc/uptime monotonic ? */
7379
assert(u1 >= u0);
80+
81+
/* Is CLOCK_BOOTTIME monotonic ? */
82+
assert(c1 >= c0);
83+
84+
/* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */
85+
assert(c0 >= u0);
86+
7487
u0 = u1;
88+
c0 = c1;
7589
}
7690

7791
return 0;

tools/testing/selftests/proc/proc-uptime.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@
1919
#include <string.h>
2020
#include <stdlib.h>
2121
#include <unistd.h>
22+
#include <time.h>
2223

2324
#include "proc.h"
2425

26+
static uint64_t clock_boottime(void)
27+
{
28+
struct timespec ts;
29+
int err;
30+
31+
err = clock_gettime(CLOCK_BOOTTIME, &ts);
32+
assert(err >= 0);
33+
34+
return (ts.tv_sec * 100) + (ts.tv_nsec / 10000000);
35+
}
36+
2537
static uint64_t proc_uptime(int fd)
2638
{
2739
uint64_t val1, val2;

0 commit comments

Comments
 (0)