xref: /xnu-8020.121.3/osfmk/kperf/kptimer.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions /*
2*fdd8201dSApple OSS Distributions  * Copyright (c) 2011-2021 Apple Computer, Inc. All rights reserved.
3*fdd8201dSApple OSS Distributions  *
4*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*fdd8201dSApple OSS Distributions  *
6*fdd8201dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*fdd8201dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*fdd8201dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*fdd8201dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*fdd8201dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*fdd8201dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*fdd8201dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*fdd8201dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*fdd8201dSApple OSS Distributions  *
15*fdd8201dSApple OSS Distributions  * Please obtain a copy of the License at
16*fdd8201dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*fdd8201dSApple OSS Distributions  *
18*fdd8201dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*fdd8201dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*fdd8201dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*fdd8201dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*fdd8201dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*fdd8201dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*fdd8201dSApple OSS Distributions  * limitations under the License.
25*fdd8201dSApple OSS Distributions  *
26*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*fdd8201dSApple OSS Distributions  */
28*fdd8201dSApple OSS Distributions 
29*fdd8201dSApple OSS Distributions #ifndef KPERF_KPTIMER_H
30*fdd8201dSApple OSS Distributions #define KPERF_KPTIMER_H
31*fdd8201dSApple OSS Distributions 
32*fdd8201dSApple OSS Distributions /*
33*fdd8201dSApple OSS Distributions  * kptimer is responsible for managing the kperf's on-CPU timers.  These
34*fdd8201dSApple OSS Distributions  * timers sample threads that are running on CPUs at a cadence determined by a
35*fdd8201dSApple OSS Distributions  * specified period.  When they fire, a handler runs the specified action and
36*fdd8201dSApple OSS Distributions  * reprograms the timer to fire again.  To get everything started or stopped,
37*fdd8201dSApple OSS Distributions  * kptimer issues a broadcast IPI to modify kperf's multiplexed per-CPU timer,
38*fdd8201dSApple OSS Distributions  * stored in the machine-dependent per-CPU structure.
39*fdd8201dSApple OSS Distributions  *
40*fdd8201dSApple OSS Distributions  * On-CPU timers are disabled when the CPU they've been programmed for goes idle
41*fdd8201dSApple OSS Distributions  * to prevent waking up the idle CPU when it's not running anything interesting.
42*fdd8201dSApple OSS Distributions  * This logic lives in the platform code that's responsible for entering and
43*fdd8201dSApple OSS Distributions  * exiting idle.
44*fdd8201dSApple OSS Distributions  *
45*fdd8201dSApple OSS Distributions  * Traditional PET is configured here (since it's defined by identifying a timer
46*fdd8201dSApple OSS Distributions  * to use for PET) but its mechanism is in osfmk/kperf/pet.c.  Lightweight PET
47*fdd8201dSApple OSS Distributions  * does use kptimer to increment its generation count, however.
48*fdd8201dSApple OSS Distributions  */
49*fdd8201dSApple OSS Distributions 
50*fdd8201dSApple OSS Distributions /*
51*fdd8201dSApple OSS Distributions  * The minimum allowed timer period depends on the type of client (foreground vs.
52*fdd8201dSApple OSS Distributions  * background) and timer (on-CPU vs. PET).
53*fdd8201dSApple OSS Distributions  */
54*fdd8201dSApple OSS Distributions enum kptimer_period_limit {
55*fdd8201dSApple OSS Distributions 	KTPL_FG,
56*fdd8201dSApple OSS Distributions 	KTPL_BG,
57*fdd8201dSApple OSS Distributions 	KTPL_FG_PET,
58*fdd8201dSApple OSS Distributions 	KTPL_BG_PET,
59*fdd8201dSApple OSS Distributions 	KTPL_MAX,
60*fdd8201dSApple OSS Distributions };
61*fdd8201dSApple OSS Distributions 
62*fdd8201dSApple OSS Distributions /*
63*fdd8201dSApple OSS Distributions  * The minimum timer periods allowed by kperf.  There's no other mechanism
64*fdd8201dSApple OSS Distributions  * to prevent interrupt storms due to kptimer.
65*fdd8201dSApple OSS Distributions  */
66*fdd8201dSApple OSS Distributions extern const uint64_t kptimer_minperiods_ns[KTPL_MAX];
67*fdd8201dSApple OSS Distributions 
68*fdd8201dSApple OSS Distributions /*
69*fdd8201dSApple OSS Distributions  * Called from the kernel startup thread to set up kptimer.
70*fdd8201dSApple OSS Distributions  */
71*fdd8201dSApple OSS Distributions void kptimer_init(void);
72*fdd8201dSApple OSS Distributions 
73*fdd8201dSApple OSS Distributions /*
74*fdd8201dSApple OSS Distributions  * Return the minimum timer period in Mach time units.
75*fdd8201dSApple OSS Distributions  */
76*fdd8201dSApple OSS Distributions uint64_t kptimer_min_period_abs(bool pet);
77*fdd8201dSApple OSS Distributions 
78*fdd8201dSApple OSS Distributions /*
79*fdd8201dSApple OSS Distributions  * Return the number of timers available.
80*fdd8201dSApple OSS Distributions  */
81*fdd8201dSApple OSS Distributions unsigned int kptimer_get_count(void);
82*fdd8201dSApple OSS Distributions 
83*fdd8201dSApple OSS Distributions /*
84*fdd8201dSApple OSS Distributions  * Set the number of timers available to `count`.
85*fdd8201dSApple OSS Distributions  *
86*fdd8201dSApple OSS Distributions  * Returns 0 on success, and non-0 on error.
87*fdd8201dSApple OSS Distributions  */
88*fdd8201dSApple OSS Distributions int kptimer_set_count(unsigned int count);
89*fdd8201dSApple OSS Distributions 
90*fdd8201dSApple OSS Distributions /*
91*fdd8201dSApple OSS Distributions  * Return the period of the timer identified by `timerid` in `period_out`.
92*fdd8201dSApple OSS Distributions  *
93*fdd8201dSApple OSS Distributions  * Returns 0 on success, and non-0 on error.
94*fdd8201dSApple OSS Distributions  */
95*fdd8201dSApple OSS Distributions int kptimer_get_period(unsigned int timerid, uint64_t *period_out);
96*fdd8201dSApple OSS Distributions 
97*fdd8201dSApple OSS Distributions /*
98*fdd8201dSApple OSS Distributions  * Set the period of the timer identified by `timerid` to `period`.
99*fdd8201dSApple OSS Distributions  *
100*fdd8201dSApple OSS Distributions  * Returns non-zero on error, and zero otherwise.
101*fdd8201dSApple OSS Distributions  */
102*fdd8201dSApple OSS Distributions int kptimer_set_period(unsigned int timerid, uint64_t period);
103*fdd8201dSApple OSS Distributions 
104*fdd8201dSApple OSS Distributions /*
105*fdd8201dSApple OSS Distributions  * Return the action of the timer identified by `timerid` in
106*fdd8201dSApple OSS Distributions  * `actionid_out`.
107*fdd8201dSApple OSS Distributions  */
108*fdd8201dSApple OSS Distributions int kptimer_get_action(unsigned int timerid, uint32_t *actionid_out);
109*fdd8201dSApple OSS Distributions 
110*fdd8201dSApple OSS Distributions /*
111*fdd8201dSApple OSS Distributions  * Set the action of the timer identified by `timerid` to `actionid`.
112*fdd8201dSApple OSS Distributions  */
113*fdd8201dSApple OSS Distributions int kptimer_set_action(unsigned int timer, uint32_t actionid);
114*fdd8201dSApple OSS Distributions 
115*fdd8201dSApple OSS Distributions /*
116*fdd8201dSApple OSS Distributions  * Set the PET timer to the timer identified by `timerid`.
117*fdd8201dSApple OSS Distributions  */
118*fdd8201dSApple OSS Distributions int kptimer_set_pet_timerid(unsigned int timerid);
119*fdd8201dSApple OSS Distributions 
120*fdd8201dSApple OSS Distributions /*
121*fdd8201dSApple OSS Distributions  * Return the ID of the PET timer.
122*fdd8201dSApple OSS Distributions  */
123*fdd8201dSApple OSS Distributions unsigned int kptimer_get_pet_timerid(void);
124*fdd8201dSApple OSS Distributions 
125*fdd8201dSApple OSS Distributions /*
126*fdd8201dSApple OSS Distributions  * For PET to rearm its timer after its sampling thread took `sampledur_abs`
127*fdd8201dSApple OSS Distributions  * to sample.
128*fdd8201dSApple OSS Distributions  */
129*fdd8201dSApple OSS Distributions void kptimer_pet_enter(uint64_t sampledur_abs);
130*fdd8201dSApple OSS Distributions 
131*fdd8201dSApple OSS Distributions /*
132*fdd8201dSApple OSS Distributions  * Start all active timers.  The ktrace lock must be held.
133*fdd8201dSApple OSS Distributions  */
134*fdd8201dSApple OSS Distributions void kptimer_start(void);
135*fdd8201dSApple OSS Distributions 
136*fdd8201dSApple OSS Distributions /*
137*fdd8201dSApple OSS Distributions  * Stop all active timers, waiting for them to stop.  The ktrace lock must be held.
138*fdd8201dSApple OSS Distributions  */
139*fdd8201dSApple OSS Distributions void kptimer_stop(void);
140*fdd8201dSApple OSS Distributions 
141*fdd8201dSApple OSS Distributions /*
142*fdd8201dSApple OSS Distributions  * Cancel outstanding kperf timer for this CPU.
143*fdd8201dSApple OSS Distributions  */
144*fdd8201dSApple OSS Distributions void kptimer_stop_curcpu(void);
145*fdd8201dSApple OSS Distributions 
146*fdd8201dSApple OSS Distributions /*
147*fdd8201dSApple OSS Distributions  * Reconfigure this CPU's kptimer expiration when it's brought online
148*fdd8201dSApple OSS Distributions  */
149*fdd8201dSApple OSS Distributions void kptimer_curcpu_up(void);
150*fdd8201dSApple OSS Distributions 
151*fdd8201dSApple OSS Distributions /*
152*fdd8201dSApple OSS Distributions  * To indicate the next timer has expired.
153*fdd8201dSApple OSS Distributions  */
154*fdd8201dSApple OSS Distributions void kptimer_expire(processor_t processor, int cpuid, uint64_t now);
155*fdd8201dSApple OSS Distributions 
156*fdd8201dSApple OSS Distributions /*
157*fdd8201dSApple OSS Distributions  * Reset the kptimer system.
158*fdd8201dSApple OSS Distributions  */
159*fdd8201dSApple OSS Distributions void kptimer_reset(void);
160*fdd8201dSApple OSS Distributions 
161*fdd8201dSApple OSS Distributions #endif /* !defined(KPERF_KPTIMER_H) */
162