~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/timers/clocksource-switch.c

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Clocksource change test
  2  *              by: john stultz (johnstul@us.ibm.com)
  3  *              (C) Copyright IBM 2012
  4  *              Licensed under the GPLv2
  5  *
  6  *  NOTE: This is a meta-test which quickly changes the clocksourc and
  7  *  then uses other tests to detect problems. Thus this test requires
  8  *  that the inconsistency-check and nanosleep tests be present in the
  9  *  same directory it is run from.
 10  *
 11  *  To build:
 12  *      $ gcc clocksource-switch.c -o clocksource-switch -lrt
 13  *
 14  *   This program is free software: you can redistribute it and/or modify
 15  *   it under the terms of the GNU General Public License as published by
 16  *   the Free Software Foundation, either version 2 of the License, or
 17  *   (at your option) any later version.
 18  *
 19  *   This program is distributed in the hope that it will be useful,
 20  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 21  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 22  *   GNU General Public License for more details.
 23  */
 24 
 25 
 26 #include <stdio.h>
 27 #include <unistd.h>
 28 #include <stdlib.h>
 29 #include <sys/time.h>
 30 #include <sys/timex.h>
 31 #include <time.h>
 32 #include <sys/types.h>
 33 #include <sys/stat.h>
 34 #include <fcntl.h>
 35 #include <string.h>
 36 #include <sys/wait.h>
 37 #ifdef KTEST
 38 #include "../kselftest.h"
 39 #else
 40 static inline int ksft_exit_pass(void)
 41 {
 42         exit(0);
 43 }
 44 static inline int ksft_exit_fail(void)
 45 {
 46         exit(1);
 47 }
 48 #endif
 49 
 50 
 51 int get_clocksources(char list[][30])
 52 {
 53         int fd, i;
 54         size_t size;
 55         char buf[512];
 56         char *head, *tmp;
 57 
 58         fd = open("/sys/devices/system/clocksource/clocksource0/available_clocksource", O_RDONLY);
 59 
 60         size = read(fd, buf, 512);
 61 
 62         close(fd);
 63 
 64         for (i = 0; i < 30; i++)
 65                 list[i][0] = '\0';
 66 
 67         head = buf;
 68         i = 0;
 69         while (head - buf < size) {
 70                 /* Find the next space */
 71                 for (tmp = head; *tmp != ' '; tmp++) {
 72                         if (*tmp == '\n')
 73                                 break;
 74                         if (*tmp == '\0')
 75                                 break;
 76                 }
 77                 *tmp = '\0';
 78                 strcpy(list[i], head);
 79                 head = tmp + 1;
 80                 i++;
 81         }
 82 
 83         return i-1;
 84 }
 85 
 86 int get_cur_clocksource(char *buf, size_t size)
 87 {
 88         int fd;
 89 
 90         fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_RDONLY);
 91 
 92         size = read(fd, buf, size);
 93 
 94         return 0;
 95 }
 96 
 97 int change_clocksource(char *clocksource)
 98 {
 99         int fd;
100         size_t size;
101 
102         fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_WRONLY);
103 
104         if (fd < 0)
105                 return -1;
106 
107         size = write(fd, clocksource, strlen(clocksource));
108 
109         if (size < 0)
110                 return -1;
111 
112         close(fd);
113         return 0;
114 }
115 
116 
117 int run_tests(int secs)
118 {
119         int ret;
120         char buf[255];
121 
122         sprintf(buf, "./inconsistency-check -t %i", secs);
123         ret = system(buf);
124         if (ret)
125                 return ret;
126         ret = system("./nanosleep");
127         return ret;
128 }
129 
130 
131 char clocksource_list[10][30];
132 
133 int main(int argv, char **argc)
134 {
135         char orig_clk[512];
136         int count, i, status;
137         pid_t pid;
138 
139         get_cur_clocksource(orig_clk, 512);
140 
141         count = get_clocksources(clocksource_list);
142 
143         if (change_clocksource(clocksource_list[0])) {
144                 printf("Error: You probably need to run this as root\n");
145                 return -1;
146         }
147 
148         /* Check everything is sane before we start switching asyncrhonously */
149         for (i = 0; i < count; i++) {
150                 printf("Validating clocksource %s\n", clocksource_list[i]);
151                 if (change_clocksource(clocksource_list[i])) {
152                         status = -1;
153                         goto out;
154                 }
155                 if (run_tests(5)) {
156                         status = -1;
157                         goto out;
158                 }
159         }
160 
161 
162         printf("Running Asyncrhonous Switching Tests...\n");
163         pid = fork();
164         if (!pid)
165                 return run_tests(60);
166 
167         while (pid != waitpid(pid, &status, WNOHANG))
168                 for (i = 0; i < count; i++)
169                         if (change_clocksource(clocksource_list[i])) {
170                                 status = -1;
171                                 goto out;
172                         }
173 out:
174         change_clocksource(orig_clk);
175 
176         if (status)
177                 return ksft_exit_fail();
178         return ksft_exit_pass();
179 }
180 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp