1 /*
2  * Copyright (c) 2025 Tenstorrent AI ULC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <sys/times.h>
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/util.h>
11 #include <zephyr/ztest.h>
12 
ZTEST(posix_multi_process,test_times)13 ZTEST(posix_multi_process, test_times)
14 {
15 	static const struct {
16 		const char *name;
17 		size_t offset;
18 	} fields[] = {
19 		{
20 			.name = "utime",
21 			.offset = offsetof(struct tms, tms_utime),
22 		},
23 		{
24 			.name = "stime",
25 			.offset = offsetof(struct tms, tms_stime),
26 		},
27 		{
28 			.name = "cutime",
29 			.offset = offsetof(struct tms, tms_cutime),
30 		},
31 		{
32 			.name = "cstime",
33 			.offset = offsetof(struct tms, tms_cstime),
34 		},
35 	};
36 	struct tms test_tms[2] = {};
37 	clock_t rtime[2];
38 
39 	rtime[0] = times(&test_tms[0]);
40 	k_msleep(MSEC_PER_SEC);
41 	rtime[1] = times(&test_tms[1]);
42 
43 	zexpect_not_equal(rtime[0], -1);
44 	zexpect_not_equal(rtime[1], -1);
45 
46 	printk("t0: rtime: %ld utime: %ld stime: %ld cutime: %ld cstime: %ld\n", rtime[0],
47 	       test_tms[0].tms_utime, test_tms[0].tms_stime, test_tms[0].tms_cutime,
48 	       test_tms[0].tms_cstime);
49 	printk("t1: rtime: %ld utime: %ld stime: %ld cutime: %ld cstime: %ld\n", rtime[1],
50 	       test_tms[1].tms_utime, test_tms[1].tms_stime, test_tms[1].tms_cutime,
51 	       test_tms[1].tms_cstime);
52 
53 	ARRAY_FOR_EACH(fields, i) {
54 		const char *name = fields[i].name;
55 		size_t offset = fields[i].offset;
56 
57 		clock_t t0 = *(clock_t *)((uint8_t *)&test_tms[0] + offset);
58 		clock_t t1 = *(clock_t *)((uint8_t *)&test_tms[1] + offset);
59 
60 		zexpect_true(t1 >= t0, "time moved backward for tms_%s: t0: %ld t1: %ld", name, t0,
61 			     t1);
62 	}
63 }
64