162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any
562306a36Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
662306a36Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
962306a36Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1062306a36Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1162306a36Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1262306a36Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1362306a36Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1462306a36Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci// Test that boottime value in /proc/uptime and CLOCK_BOOTTIME increment
1762306a36Sopenharmony_ci// monotonically. We don't test idle time monotonicity due to broken iowait
1862306a36Sopenharmony_ci// task counting, cf: comment above get_cpu_idle_time_us()
1962306a36Sopenharmony_ci#undef NDEBUG
2062306a36Sopenharmony_ci#include <assert.h>
2162306a36Sopenharmony_ci#include <stdint.h>
2262306a36Sopenharmony_ci#include <sys/types.h>
2362306a36Sopenharmony_ci#include <sys/stat.h>
2462306a36Sopenharmony_ci#include <fcntl.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include "proc-uptime.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciint main(void)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	uint64_t start, u0, u1, c0, c1;
3162306a36Sopenharmony_ci	int fd;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	fd = open("/proc/uptime", O_RDONLY);
3462306a36Sopenharmony_ci	assert(fd >= 0);
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	u0 = proc_uptime(fd);
3762306a36Sopenharmony_ci	start = u0;
3862306a36Sopenharmony_ci	c0 = clock_boottime();
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	do {
4162306a36Sopenharmony_ci		u1 = proc_uptime(fd);
4262306a36Sopenharmony_ci		c1 = clock_boottime();
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci		/* Is /proc/uptime monotonic ? */
4562306a36Sopenharmony_ci		assert(u1 >= u0);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci		/* Is CLOCK_BOOTTIME monotonic ? */
4862306a36Sopenharmony_ci		assert(c1 >= c0);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci		/* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */
5162306a36Sopenharmony_ci		assert(c0 >= u0);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci		u0 = u1;
5462306a36Sopenharmony_ci		c0 = c1;
5562306a36Sopenharmony_ci	} while (u1 - start < 100);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	return 0;
5862306a36Sopenharmony_ci}
59