xref: /xnu-10002.81.5/tests/recount/recount_test_utils.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1 // Copyright (c) 2021-2022 Apple Inc.  All rights reserved.
2 
3 #pragma once
4 
5 #include <mach/time_value.h>
6 #include <os/base.h>
7 #include <pthread/pthread.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 
11 #define ARRAY_COUNT(_a) (sizeof((_a)) / sizeof((_a[0])))
12 
13 #define REQUIRE_RECOUNT_PMCS \
14     T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1)
15 #define REQUIRE_RECOUNT_ENERGY \
16     T_META_REQUIRES_SYSTCL_EQ("kern.pervasive_energy", 1)
17 #define REQUIRE_MULTIPLE_PERF_LEVELS \
18     T_META_REQUIRES_SYSCTL_EQ("hw.nperflevels", 2)
19 #define SET_THREAD_BIND_BOOTARG \
20     T_META_BOOTARGS_SET("enable_skstb=1")
21 
22 // Returns true if the system implicitly tracks CPI.
23 bool has_cpi(void);
24 
25 // Returns true if precise user kernel (system) times are being tracked,
26 // and false otherwise.
27 bool has_user_system_times(void);
28 
29 // Returns true if the system can track energy usage.
30 bool has_energy(void);
31 
32 // Bind the current thread to the given cluster.
33 void bind_to_cluster(char type);
34 
35 // Returns the number of perf-levels on the system.
36 unsigned int perf_level_count(void);
37 
38 // Returns the name of the specified perf-level.
39 const char *perf_level_name(unsigned int perf_level);
40 
41 // Returns the index of the named perf-level.
42 unsigned int perf_level_index(const char *name);
43 
44 // Run periodically on all perf levels -- must have `SET_THREAD_BIND_BOOTARG`.
45 void run_on_all_perf_levels(void);
46 
47 // Return the nanoseconds represented by a Mach time.
48 uint64_t ns_from_mach(uint64_t mach_time);
49 
50 // Return the nanoseconds represented by a timeval.
51 uint64_t ns_from_timeval(struct timeval tv);
52 
53 // Return the timeval represented by nanoseconds.
54 struct timeval timeval_from_ns(uint64_t ns);
55 
56 // Return the nanoseconds represented by a Mach time_value.
57 uint64_t ns_from_time_value(struct time_value tv);
58 
59 // Return the Mach time_value represented by nanoseconds.
60 struct time_value time_value_from_ns(uint64_t ns);
61 
62 // What an actor should do when it's running.
63 __enum_decl(role_t, uint32_t, {
64 	ROLE_NONE,
65 	ROLE_SPIN,
66 	ROLE_WAIT,
67 });
68 
69 // A thread doing work according to a script.
70 struct actor {
71 	pthread_t act_thread;
72 	role_t act_role;
73 	void *act_context;
74 };
75 
76 struct scene {
77 	unsigned int scn_actor_count;
78 	uintptr_t scn_spin_sync;
79 	void *scn_wait_sync;
80 	struct actor scn_actors[];
81 };
82 
83 // Start `n` threads that follow a given pattern of scripts.
84 struct scene *scene_start(unsigned int n, role_t *roles);
85 
86 // Stop and destroy previously-started actors.
87 void scene_end(struct scene *scene);
88 
89 // Launch a `T_HELPER_DECL`-based helper.
90 pid_t launch_helper(char *name);
91