1cd844e7aSJohn Birrell /*
2cd844e7aSJohn Birrell  * CDDL HEADER START
3cd844e7aSJohn Birrell  *
4cd844e7aSJohn Birrell  * The contents of this file are subject to the terms of the
5cd844e7aSJohn Birrell  * Common Development and Distribution License (the "License").
6cd844e7aSJohn Birrell  * You may not use this file except in compliance with the License.
7cd844e7aSJohn Birrell  *
8cd844e7aSJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9cd844e7aSJohn Birrell  * or http://www.opensolaris.org/os/licensing.
10cd844e7aSJohn Birrell  * See the License for the specific language governing permissions
11cd844e7aSJohn Birrell  * and limitations under the License.
12cd844e7aSJohn Birrell  *
13cd844e7aSJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
14cd844e7aSJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15cd844e7aSJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
16cd844e7aSJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
17cd844e7aSJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
18cd844e7aSJohn Birrell  *
19cd844e7aSJohn Birrell  * CDDL HEADER END
20cd844e7aSJohn Birrell  */
21cd844e7aSJohn Birrell /*
2210b9d77bSPawel Jakub Dawidek  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
23cd844e7aSJohn Birrell  * Copyright 2017 RackTop Systems.
24cd844e7aSJohn Birrell  */
25cd844e7aSJohn Birrell 
26cd844e7aSJohn Birrell #ifndef	_SYS_CPUPART_H
27cd844e7aSJohn Birrell #define	_SYS_CPUPART_H
28cd844e7aSJohn Birrell 
29cd844e7aSJohn Birrell #include <sys/types.h>
30cd844e7aSJohn Birrell #include <sys/processor.h>
31cd844e7aSJohn Birrell #include <sys/cpuvar.h>
32cd844e7aSJohn Birrell #include <sys/disp.h>
33cd844e7aSJohn Birrell #include <sys/pset.h>
34cd844e7aSJohn Birrell #include <sys/lgrp.h>
35cd844e7aSJohn Birrell #include <sys/lgrp_user.h>
36cd844e7aSJohn Birrell #include <sys/pg.h>
37cd844e7aSJohn Birrell #include <sys/bitset.h>
38cd844e7aSJohn Birrell #include <sys/time.h>
39cd844e7aSJohn Birrell 
40cd844e7aSJohn Birrell #ifdef	__cplusplus
41cd844e7aSJohn Birrell extern "C" {
42cd844e7aSJohn Birrell #endif
43cd844e7aSJohn Birrell 
44cd844e7aSJohn Birrell #if defined(_KERNEL) || defined(_FAKE_KERNEL)
45cd844e7aSJohn Birrell 
46cd844e7aSJohn Birrell typedef int	cpupartid_t;
47cd844e7aSJohn Birrell 
48cd844e7aSJohn Birrell /*
49cd844e7aSJohn Birrell  * Special partition id.
50cd844e7aSJohn Birrell  */
51cd844e7aSJohn Birrell #define	CP_DEFAULT	0
52cd844e7aSJohn Birrell 
53cd844e7aSJohn Birrell /*
54cd844e7aSJohn Birrell  * Flags for cpupart_list()
55cd844e7aSJohn Birrell  */
56cd844e7aSJohn Birrell #define	CP_ALL		0		/* return all cpu partitions */
57cd844e7aSJohn Birrell #define	CP_NONEMPTY	1		/* return only non-empty ones */
58cd844e7aSJohn Birrell 
59cd844e7aSJohn Birrell typedef struct cpupart {
60cd844e7aSJohn Birrell 	disp_t		cp_kp_queue;	/* partition-wide kpreempt queue */
61cd844e7aSJohn Birrell 	cpupartid_t	cp_id;		/* partition ID */
62cd844e7aSJohn Birrell 	int		cp_ncpus;	/* number of online processors */
63cd844e7aSJohn Birrell 	struct cpupart	*cp_next;	/* next partition in list */
64cd844e7aSJohn Birrell 	struct cpupart	*cp_prev;	/* previous partition in list */
65cd844e7aSJohn Birrell 	struct cpu	*cp_cpulist;	/* processor list */
66cd844e7aSJohn Birrell 	struct kstat	*cp_kstat;	/* per-partition statistics */
67cd844e7aSJohn Birrell 
68cd844e7aSJohn Birrell 	/*
69cd844e7aSJohn Birrell 	 * cp_nrunnable and cp_nrunning are used to calculate load average.
70cd844e7aSJohn Birrell 	 */
71cd844e7aSJohn Birrell 	uint_t		cp_nrunnable;	/* current # of runnable threads */
72cd844e7aSJohn Birrell 	uint_t		cp_nrunning;	/* current # of running threads */
73cd844e7aSJohn Birrell 
74cd844e7aSJohn Birrell 	/*
75cd844e7aSJohn Birrell 	 * cp_updates, cp_nrunnable_cum, cp_nwaiting_cum, and cp_hp_avenrun
76cd844e7aSJohn Birrell 	 * are used to generate kstat information on an as-needed basis.
77cd844e7aSJohn Birrell 	 */
78cd844e7aSJohn Birrell 	uint64_t	cp_updates;	/* number of statistics updates */
79cd844e7aSJohn Birrell 	uint64_t	cp_nrunnable_cum; /* cum. # of runnable threads */
80cd844e7aSJohn Birrell 	uint64_t	cp_nwaiting_cum;  /* cum. # of waiting threads */
81cd844e7aSJohn Birrell 
82cd844e7aSJohn Birrell 	struct loadavg_s cp_loadavg;	/* cpupart loadavg */
83cd844e7aSJohn Birrell 
84cd844e7aSJohn Birrell 	klgrpset_t	cp_lgrpset;	/* set of lgroups on which this */
85cd844e7aSJohn Birrell 					/*    partition has cpus */
86cd844e7aSJohn Birrell 	lpl_t		*cp_lgrploads;	/* table of load averages for this  */
87cd844e7aSJohn Birrell 					/*    partition, indexed by lgrp ID */
88cd844e7aSJohn Birrell 	int		cp_nlgrploads;	/* size of cp_lgrploads table */
89cd844e7aSJohn Birrell 	uint64_t	cp_hp_avenrun[3]; /* high-precision load average */
90cd844e7aSJohn Birrell 	uint_t		cp_attr;	/* bitmask of attributes */
91cd844e7aSJohn Birrell 	lgrp_gen_t	cp_gen;		/* generation number */
92cd844e7aSJohn Birrell 	lgrp_id_t	cp_lgrp_hint;	/* last home lgroup chosen */
9310b9d77bSPawel Jakub Dawidek 	bitset_t	cp_cmt_pgs;	/* CMT PGs represented */
94cd844e7aSJohn Birrell 	bitset_t	cp_haltset;	/* halted CPUs */
95cd844e7aSJohn Birrell } cpupart_t;
96cd844e7aSJohn Birrell 
97cd844e7aSJohn Birrell typedef struct cpupart_kstat {
98cd844e7aSJohn Birrell 	kstat_named_t	cpk_updates;		/* number of updates */
99cd844e7aSJohn Birrell 	kstat_named_t	cpk_runnable;		/* cum # of runnable threads */
100cd844e7aSJohn Birrell 	kstat_named_t	cpk_waiting;		/* cum # waiting for I/O */
101cd844e7aSJohn Birrell 	kstat_named_t	cpk_ncpus;		/* current # of CPUs */
102cd844e7aSJohn Birrell 	kstat_named_t	cpk_avenrun_1min;	/* 1-minute load average */
103cd844e7aSJohn Birrell 	kstat_named_t	cpk_avenrun_5min;	/* 5-minute load average */
104cd844e7aSJohn Birrell 	kstat_named_t	cpk_avenrun_15min;	/* 15-minute load average */
105cd844e7aSJohn Birrell } cpupart_kstat_t;
106cd844e7aSJohn Birrell 
107cd844e7aSJohn Birrell /*
108cd844e7aSJohn Birrell  * Macro to obtain the maximum run priority for the global queue associated
109cd844e7aSJohn Birrell  * with given cpu partition.
110cd844e7aSJohn Birrell  */
111cd844e7aSJohn Birrell #define	CP_MAXRUNPRI(cp)	((cp)->cp_kp_queue.disp_maxrunpri)
112cd844e7aSJohn Birrell 
113cd844e7aSJohn Birrell /*
114cd844e7aSJohn Birrell  * This macro is used to determine if the given thread must surrender
115cd844e7aSJohn Birrell  * CPU to higher priority runnable threads on one of its dispatch queues.
116cd844e7aSJohn Birrell  * This should really be defined in <sys/disp.h> but it is not because
117cd844e7aSJohn Birrell  * including <sys/cpupart.h> there would cause recursive includes.
118cd844e7aSJohn Birrell  */
119cd844e7aSJohn Birrell #define	DISP_MUST_SURRENDER(t)				\
120cd844e7aSJohn Birrell 	((DISP_MAXRUNPRI(t) > DISP_PRIO(t)) ||		\
121cd844e7aSJohn Birrell 	(CP_MAXRUNPRI(t->t_cpupart) > DISP_PRIO(t)))
122cd844e7aSJohn Birrell 
123cd844e7aSJohn Birrell extern cpupart_t	cp_default;
124cd844e7aSJohn Birrell extern cpupart_t	*cp_list_head;
125cd844e7aSJohn Birrell extern uint_t		cp_numparts;
126cd844e7aSJohn Birrell extern uint_t		cp_numparts_nonempty;
12710b9d77bSPawel Jakub Dawidek 
12810b9d77bSPawel Jakub Dawidek /*
12910b9d77bSPawel Jakub Dawidek  * Each partition contains a bitset that indicates which CPUs are halted and
13010b9d77bSPawel Jakub Dawidek  * which ones are running. Given the growing number of CPUs in current and
13110b9d77bSPawel Jakub Dawidek  * future platforms, it's important to fanout each CPU within its partition's
13210b9d77bSPawel Jakub Dawidek  * haltset to prevent contention due to false sharing. The fanout factor
13310b9d77bSPawel Jakub Dawidek  * is platform specific, and declared accordingly.
13410b9d77bSPawel Jakub Dawidek  */
13510b9d77bSPawel Jakub Dawidek extern uint_t cp_haltset_fanout;
136cd844e7aSJohn Birrell 
137cd844e7aSJohn Birrell extern void	cpupart_initialize_default();
138cd844e7aSJohn Birrell extern cpupart_t *cpupart_find(psetid_t);
139cd844e7aSJohn Birrell extern int	cpupart_create(psetid_t *);
140cd844e7aSJohn Birrell extern int	cpupart_destroy(psetid_t);
141cd844e7aSJohn Birrell extern psetid_t	cpupart_query_cpu(cpu_t *);
142cd844e7aSJohn Birrell extern int	cpupart_attach_cpu(psetid_t, cpu_t *, int);
143cd844e7aSJohn Birrell extern int	cpupart_get_cpus(psetid_t *, processorid_t *, uint_t *);
144cd844e7aSJohn Birrell extern int	cpupart_bind_thread(kthread_id_t, psetid_t, int, void *,
145cd844e7aSJohn Birrell     void *);
146cd844e7aSJohn Birrell extern void	cpupart_kpqalloc(pri_t);
147cd844e7aSJohn Birrell extern int	cpupart_get_loadavg(psetid_t, int *, int);
148cd844e7aSJohn Birrell extern uint_t	cpupart_list(psetid_t *, uint_t, int);
149cd844e7aSJohn Birrell extern int	cpupart_setattr(psetid_t, uint_t);
150cd844e7aSJohn Birrell extern int	cpupart_getattr(psetid_t, uint_t *);
151cd844e7aSJohn Birrell 
152cd844e7aSJohn Birrell #endif	/* _KERNEL || _FAKE_KERNEL */
153cd844e7aSJohn Birrell 
154cd844e7aSJohn Birrell #ifdef	__cplusplus
155cd844e7aSJohn Birrell }
156cd844e7aSJohn Birrell #endif
157cd844e7aSJohn Birrell 
158 #endif	/* _SYS_CPUPART_H */
159