1 /*
2  *  Test clock resolution for KVM guests that have kvm-clock as clock source
3  *
4  *  Copyright (c) 2010 Red Hat, Inc
5  *  Author: Lucas Meneghel Rodrigues <lmr@redhat.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <stdio.h>
21 #include <time.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
main(void)25 int main(void) {
26 	struct timespec res;
27 	int clock_return = clock_getres(CLOCK_MONOTONIC, &res);
28 	char clocksource[50];
29 	char line[80];
30 	FILE *fr;
31 	if ((fr = fopen(
32 			"/sys/devices/system/clocksource/clocksource0/current_clocksource",
33 			"rt")) == NULL) {
34 		perror("fopen");
35 		return EXIT_FAILURE;
36 	}
37 	while (fgets(line, 80, fr) != NULL) {
38 		sscanf(line, "%s", &clocksource);
39 	}
40 	fclose(fr);
41 	if (!strncmp(clocksource, "kvm-clock", strlen("kvm-clock"))) {
42 		if (clock_return == 0) {
43 			if (res.tv_sec > 1 || res.tv_nsec > 100) {
44 				printf("FAIL: clock_getres returned bad clock resolution\n");
45 				return EXIT_FAILURE;
46 			} else {
47 				printf("PASS: check successful\n");
48 				return EXIT_SUCCESS;
49 			}
50 		} else {
51 			printf("FAIL: clock_getres failed\n");
52 			return EXIT_FAILURE;
53 		}
54 	} else {
55 		printf("FAIL: invalid clock source: %s\n", clocksource);
56 		return EXIT_FAILURE;
57 	}
58 }
59