Skip to content

Commit c459654

Browse files
committed
syscalls: Add timer measurement library
This commit adds a timer measurement library, mostly based on changes done to the pselect01.c test and changes all tests that measure timer precision to use it. The timer testcases that measure timeouts now just define sampling function and optional setup and cleanup. The rest of the functionality is implemented in the lib/tst_timer_test.c library. This change not only removes fair amount of duplicated code but also allows us to tune thresholds and define testcases in a single place for all testcases. The timer measurement library also supports for passing sleep time and number of iterations as a command-line parameters, can print nifty frequency plot into the terminal, as well as save test measurements into a text file. Signed-off-by: Cyril Hrubis <[email protected]> Acked-by: Jan Stancek <[email protected]>
1 parent 62fe712 commit c459654

File tree

15 files changed

+790
-527
lines changed

15 files changed

+790
-527
lines changed

include/tst_test.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ struct tst_test {
145145
void (*test)(unsigned int test_nr);
146146
void (*test_all)(void);
147147

148+
/* Sampling function for timer measurement testcases */
149+
int (*sample)(int clk_id, long long usec);
150+
148151
/* NULL terminated array of resource file names */
149152
const char *const *resource_files;
150153
};
@@ -179,12 +182,12 @@ extern int TEST_ERRNO;
179182
const char *tst_strerrno(int err);
180183
const char *tst_strsig(int sig);
181184

185+
void tst_set_timeout(unsigned int timeout);
186+
182187
#ifndef TST_NO_DEFAULT_MAIN
183188

184189
static struct tst_test test;
185190

186-
void tst_set_timeout(unsigned int timeout);
187-
188191
int main(int argc, char *argv[])
189192
{
190193
tst_run_tcases(argc, argv, &test);

include/tst_timer.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ static inline struct timeval tst_us_to_timeval(long long us)
9292
return ret;
9393
}
9494

95+
/*
96+
* Converts ms to struct timespec
97+
*/
98+
static inline struct timespec tst_ms_to_timespec(long long us)
99+
{
100+
struct timespec ret;
101+
102+
ret.tv_sec = us / 1000;
103+
ret.tv_nsec = (us % 1000) * 1000000;
104+
105+
return ret;
106+
}
107+
108+
/*
109+
* Converts us to struct timespec
110+
*/
111+
static inline struct timespec tst_us_to_timespec(long long us)
112+
{
113+
struct timespec ret;
114+
115+
ret.tv_sec = us / 1000000;
116+
ret.tv_nsec = (us % 1000000) * 1000;
117+
118+
return ret;
119+
}
120+
95121
/*
96122
* Comparsions
97123
*/

include/tst_timer_test.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2017 Cyril Hrubis <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/*
19+
20+
Timer measuring library.
21+
22+
The test is supposed to define sampling function and set it in the tst_test
23+
structure the rest of the work is then done by the library.
24+
25+
int sample(int clk_id, long long usec)
26+
{
27+
// Any setup done here
28+
29+
tst_timer_start(clk_id);
30+
// Call that is being measured sleeps for usec
31+
tst_timer_stop();
32+
tst_timer_sample();
33+
34+
// Any cleanup done here
35+
36+
// Non-zero return exits the test
37+
}
38+
39+
struct tst_test test = {
40+
.tid = "syscall_name()",
41+
.sample = sample,
42+
};
43+
44+
*/
45+
46+
#ifndef TST_TIMER_TEST__
47+
#define TST_TIMER_TEST__
48+
49+
#include "tst_test.h"
50+
#include "tst_timer.h"
51+
52+
void tst_timer_sample(void);
53+
54+
# ifdef TST_NO_DEFAULT_MAIN
55+
struct tst_test *tst_timer_test_setup(struct tst_test *test);
56+
# endif /* TST_NO_DEFAULT_MAIN */
57+
#endif /* TST_TIMER_TEST__ */

lib/tst_test.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "tst_device.h"
3131
#include "lapi/futex.h"
3232
#include "tst_ansi_color.h"
33+
#include "tst_timer_test.h"
3334

3435
#include "old_resource.h"
3536
#include "old_device.h"
@@ -635,25 +636,44 @@ static const char *get_tid(char *argv[])
635636
static struct tst_device tdev;
636637
struct tst_device *tst_device;
637638

638-
static void do_setup(int argc, char *argv[])
639+
static void assert_test_fn(void)
639640
{
640-
if (!tst_test)
641-
tst_brk(TBROK, "No tests to run");
641+
int cnt = 0;
642642

643-
if (!tst_test->tid)
644-
tst_test->tid = get_tid(argv);
643+
if (tst_test->test)
644+
cnt++;
645645

646-
if (!tst_test->test && !tst_test->test_all)
646+
if (tst_test->test_all)
647+
cnt++;
648+
649+
if (tst_test->sample)
650+
cnt++;
651+
652+
if (!cnt)
647653
tst_brk(TBROK, "No test function speficied");
648654

649-
if (tst_test->test && tst_test->test_all)
650-
tst_brk(TBROK, "You can define either test() or test_all()");
655+
if (cnt != 1)
656+
tst_brk(TBROK, "You can define only one test function");
651657

652658
if (tst_test->test && !tst_test->tcnt)
653659
tst_brk(TBROK, "Number of tests (tcnt) must not be > 0");
654660

655-
if (tst_test->test_all && tst_test->tcnt)
656-
tst_brk(TBROK, "You can't define tcnt for test_all()");
661+
if (!tst_test->test && tst_test->tcnt)
662+
tst_brk(TBROK, "You can define tcnt only for test()");
663+
}
664+
665+
static void do_setup(int argc, char *argv[])
666+
{
667+
if (!tst_test)
668+
tst_brk(TBROK, "No tests to run");
669+
670+
assert_test_fn();
671+
672+
if (tst_test->sample)
673+
tst_test = tst_timer_test_setup(tst_test);
674+
675+
if (!tst_test->tid)
676+
tst_test->tid = get_tid(argv);
657677

658678
if (tst_test->needs_root && geteuid() != 0)
659679
tst_brk(TCONF, "Test needs to be run as root");

0 commit comments

Comments
 (0)