xref: /illumos-gate/usr/src/uts/common/sys/cpucaps.h (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_CPUCAPS_H
28 #define	_SYS_CPUCAPS_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/zone.h>
36 #include <sys/project.h>
37 #include <sys/time.h>
38 #include <sys/rctl.h>
39 
40 /*
41  * CPU caps provide an absolute hard CPU usage limit which is enforced even if
42  * some CPUs are idle. It can be enforced at project or zone level.
43  */
44 
45 #ifdef _KERNEL
46 
47 /*
48  * Valid caps values go from 1 to MAXCAP - 1. Specifying the MAXCAP as the cap
49  * value is equivalent to disabling the cap.
50  */
51 #define	MAXCAP		UINT_MAX
52 
53 /*
54  * cpucaps_enabled is used to quickly check whether any CPU caps specific code
55  * should be invoked. Users outside CPU Caps framework should use CPUCAPS_ON()
56  * and CPUCAPS_OFF() macros.
57  */
58 extern boolean_t cpucaps_enabled;
59 
60 #define	CPUCAPS_ON()	cpucaps_enabled
61 #define	CPUCAPS_OFF()	(!cpucaps_enabled)
62 
63 /*
64  * Initialize the CPU caps framework.
65  */
66 extern void cpucaps_init(void);
67 
68 /*
69  * Notify caps framework of a new project coming in or existing project
70  * going away
71  */
72 extern void cpucaps_project_add(kproject_t *);
73 extern void cpucaps_project_remove(kproject_t *);
74 
75 /*
76  * Notify caps framework when a zone is going away.
77  */
78 extern void cpucaps_zone_remove(zone_t *);
79 
80 /*
81  * Set project/zone cap to specified value. Value of MAXCAP should disable caps.
82  */
83 extern int cpucaps_project_set(kproject_t *, rctl_qty_t);
84 extern int cpucaps_zone_set(zone_t *, rctl_qty_t);
85 
86 /*
87  * Get current CPU usage for a project/zone.
88  */
89 extern rctl_qty_t cpucaps_project_get(kproject_t *);
90 extern rctl_qty_t cpucaps_zone_get(zone_t *);
91 
92 /*
93  * Scheduling class hooks into CPU caps framework.
94  */
95 
96 /*
97  * CPU caps specific data for each scheduling class.
98  *
99  * There is a small amount of accounting data that should be kept by each
100  * scheduling class for each thread which is only used by CPU caps code. This
101  * data is kept in the caps_sc structure which is transparent for all scheduling
102  * classes. The fields in the structure are:
103  *
104  *     csc_cputime -  Total time spent on CPU during thread lifetime, obtained
105  *                    as the sum of user, system and trap time, reported by
106  *                    microstate accounting.
107  */
108 typedef struct caps_sc {
109 	hrtime_t	csc_cputime;
110 } caps_sc_t;
111 
112 /*
113  * Initialize per-thread cpu-caps specific data.
114  */
115 extern void cpucaps_sc_init(caps_sc_t *);
116 
117 /*
118  * Modus operandi for cpucaps_charge() function.
119  *
120  *   CPUCAPS_CHARGE_ENFORCE - charge a thread for its CPU time and
121  *				flag it to be placed on wait queue.
122  *
123  *   CPUCAPS_CHARGE_ONLY    - charge a thread for its CPU time.
124  */
125 typedef enum {
126 	CPUCAPS_CHARGE_ENFORCE,
127 	CPUCAPS_CHARGE_ONLY
128 } cpucaps_charge_t;
129 
130 /*
131  * Add accumulated CPU usage of a thread to its cap.
132  * Return True if thread should be placed on waitq.
133  */
134 extern boolean_t cpucaps_charge(kthread_t *, caps_sc_t *, cpucaps_charge_t);
135 #define	CPUCAPS_CHARGE(t, scp, flag) \
136 	(CPUCAPS_ON() && cpucaps_charge(t, scp, flag))
137 
138 /*
139  * Request a thread to be placed on a wait queue because the cap is exceeded
140  */
141 extern boolean_t cpucaps_enforce(kthread_t *);
142 #define	CPUCAPS_ENFORCE(t) (CPUCAPS_ON() && cpucaps_enforce(t))
143 
144 /*
145  * CPU Caps hook into clock().
146  */
147 extern void (*cpucaps_clock_callout)(void);
148 
149 #endif	/* _KERNEL */
150 
151 #ifdef	__cplusplus
152 }
153 #endif
154 
155 #endif	/* _SYS_CPUCAPS_H */
156