• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #ifndef _SCHED_H_
29 #define _SCHED_H_
30 
31 #include <sys/cdefs.h>
32 #include <sys/time.h>
33 
34 __BEGIN_DECLS
35 
36 #define SCHED_NORMAL            0
37 #define SCHED_OTHER             0
38 #define SCHED_FIFO              1
39 #define SCHED_RR                2
40 
41 struct sched_param {
42     int sched_priority;
43 };
44 
45 extern int sched_setscheduler(pid_t, int, const struct sched_param *);
46 extern int sched_getscheduler(pid_t);
47 extern int sched_yield(void);
48 extern int sched_get_priority_max(int policy);
49 extern int sched_get_priority_min(int policy);
50 extern int sched_setparam(pid_t, const struct sched_param *);
51 extern int sched_getparam(pid_t, struct sched_param *);
52 extern int sched_rr_get_interval(pid_t pid, struct timespec *tp);
53 
54 #define CLONE_VM             0x00000100
55 #define CLONE_FS             0x00000200
56 #define CLONE_FILES          0x00000400
57 #define CLONE_SIGHAND        0x00000800
58 #define CLONE_PTRACE         0x00002000
59 #define CLONE_VFORK          0x00004000
60 #define CLONE_PARENT         0x00008000
61 #define CLONE_THREAD         0x00010000
62 #define CLONE_NEWNS          0x00020000
63 #define CLONE_SYSVSEM        0x00040000
64 #define CLONE_SETTLS         0x00080000
65 #define CLONE_PARENT_SETTID  0x00100000
66 #define CLONE_CHILD_CLEARTID 0x00200000
67 #define CLONE_DETACHED       0x00400000
68 #define CLONE_UNTRACED       0x00800000
69 #define CLONE_CHILD_SETTID   0x01000000
70 #define CLONE_STOPPED        0x02000000
71 
72 #ifdef _GNU_SOURCE
73 extern int clone(int (*fn)(void *), void *child_stack, int flags, void*  arg, ...);
74 #endif
75 
76 /* Support for cpu thread affinity */
77 #ifdef _GNU_SOURCE
78 
79 extern int sched_getcpu(void);
80 
81 
82 /* Our implementation supports up to 32 independent CPUs, which is also
83  * the maximum supported by the kernel at the moment. GLibc uses 1024 by
84  * default.
85  *
86  * If you want to use more than that, you should use CPU_ALLOC() / CPU_FREE()
87  * and the CPU_XXX_S() macro variants.
88  */
89 #define CPU_SETSIZE   32
90 
91 #define __CPU_BITTYPE    unsigned long int  /* mandated by the kernel  */
92 #define __CPU_BITSHIFT   5                  /* should be log2(BITTYPE) */
93 #define __CPU_BITS       (1 << __CPU_BITSHIFT)
94 #define __CPU_ELT(x)     ((x) >> __CPU_BITSHIFT)
95 #define __CPU_MASK(x)    ((__CPU_BITTYPE)1 << ((x) & (__CPU_BITS-1)))
96 
97 typedef struct {
98     __CPU_BITTYPE  __bits[ CPU_SETSIZE / __CPU_BITS ];
99 } cpu_set_t;
100 
101 extern int sched_setaffinity(pid_t pid, size_t setsize, const cpu_set_t* set);
102 
103 extern int sched_getaffinity(pid_t pid, size_t setsize, cpu_set_t* set);
104 
105 /* Provide optimized implementation for 32-bit cpu_set_t */
106 #if CPU_SETSIZE == __CPU_BITS
107 
108 #  define CPU_ZERO(set_)   \
109     do{ \
110         (set_)->__bits[0] = 0; \
111     }while(0)
112 
113 #  define CPU_SET(cpu_,set_) \
114     do {\
115         size_t __cpu = (cpu_); \
116         if (__cpu < CPU_SETSIZE) \
117             (set_)->__bits[0] |= __CPU_MASK(__cpu); \
118     }while (0)
119 
120 #  define CPU_CLR(cpu_,set_) \
121     do {\
122         size_t __cpu = (cpu_); \
123         if (__cpu < CPU_SETSIZE) \
124             (set_)->__bits[0] &= ~__CPU_MASK(__cpu); \
125     }while (0)
126 
127 #  define CPU_ISSET(cpu_, set_) \
128     (__extension__({\
129         size_t  __cpu = (cpu_); \
130         (cpu_ < CPU_SETSIZE) \
131             ? ((set_)->__bits[0] & __CPU_MASK(__cpu)) != 0 \
132             : 0; \
133     }))
134 
135 #  define CPU_EQUAL(set1_, set2_) \
136     ((set1_)->__bits[0] == (set2_)->__bits[0])
137 
138 #  define __CPU_OP(dst_, set1_, set2_, op_) \
139     do { \
140         (dst_)->__bits[0] = (set1_)->__bits[0] op_ (set2_)->__bits[0]; \
141     } while (0)
142 
143 #  define CPU_COUNT(set_)  __builtin_popcountl((set_)->__bits[0])
144 
145 #else /* CPU_SETSIZE != __CPU_BITS */
146 
147 #  define CPU_ZERO(set_)          CPU_ZERO_S(sizeof(cpu_set_t), set_)
148 #  define CPU_SET(cpu_,set_)      CPU_SET_S(cpu_,sizeof(cpu_set_t),set_)
149 #  define CPU_CLR(cpu_,set_)      CPU_CLR_S(cpu_,sizeof(cpu_set_t),set_)
150 #  define CPU_ISSET(cpu_,set_)    CPU_ISSET_S(cpu_,sizeof(cpu_set_t),set_)
151 #  define CPU_COUNT(set_)         CPU_COUNT_S(sizeof(cpu_set_t),set_)
152 #  define CPU_EQUAL(set1_,set2_)  CPU_EQUAL_S(sizeof(cpu_set_t),set1_,set2_)
153 
154 #  define __CPU_OP(dst_,set1_,set2_,op_)  __CPU_OP_S(sizeof(cpu_set_t),dst_,set1_,set2_,op_)
155 
156 #endif /* CPU_SETSIZE != __CPU_BITS */
157 
158 #define CPU_AND(set1_,set2_)   __CPU_OP(set1_,set2_,&)
159 #define CPU_OR(set1_,set2_)    __CPU_OP(set1_,set2_,|)
160 #define CPU_XOR(set1_,set2_)   __CPU_OP(set1_,set2_,^)
161 
162 /* Support for dynamically-allocated cpu_set_t */
163 
164 #define CPU_ALLOC_SIZE(count) \
165     __CPU_ELT((count) + (__CPU_BITS-1))*sizeof(__CPU_BITTYPE)
166 
167 #define CPU_ALLOC(count)   __sched_cpualloc((count));
168 #define CPU_FREE(set)      __sched_cpufree((set))
169 
170 extern cpu_set_t* __sched_cpualloc(size_t count);
171 extern void       __sched_cpufree(cpu_set_t* set);
172 
173 #define CPU_ZERO_S(setsize_,set_)  \
174     do { \
175         size_t __nn = 0; \
176         size_t __nn_max = (setsize_)/sizeof(__CPU_BITTYPE); \
177         for (; __nn < __nn_max; __nn++) \
178             (set_)->__bits[__nn] = 0; \
179     } while (0)
180 
181 #define CPU_SET_S(cpu_,setsize_,set_) \
182     do { \
183         size_t __cpu = (cpu_); \
184         if (__cpu < 8*(setsize_)) \
185             (set_)->__bits[__CPU_ELT(__cpu)] |= __CPU_MASK(__cpu); \
186     } while (0)
187 
188 #define CPU_CLR_S(cpu_,setsize_,set_) \
189     do { \
190         size_t __cpu = (cpu_); \
191         if (__cpu < 8*(setsize_)) \
192             (set_)->__bits[__CPU_ELT(__cpu)] &= ~__CPU_MASK(__cpu); \
193     } while (0)
194 
195 #define CPU_ISSET_S(cpu_, setsize_, set_) \
196     (__extension__ ({ \
197         size_t __cpu = (cpu_); \
198         (__cpu < 8*(setsize_)) \
199           ? ((set_)->__bits[__CPU_ELT(__cpu)] & __CPU_MASK(__cpu)) != 0 \
200           : 0; \
201     }))
202 
203 #define CPU_EQUAL_S(setsize_, set1_, set2_) \
204     (__extension__ ({ \
205         __const __CPU_BITTYPE* __src1 = (set1_)->__bits; \
206         __const __CPU_BITTYPE* __src2 = (set2_)->__bits; \
207         size_t __nn = 0, __nn_max = (setsize_)/sizeof(__CPU_BITTYPE); \
208         for (; __nn < __nn_max; __nn++) { \
209             if (__src1[__nn] != __src2[__nn]) \
210                 break; \
211         } \
212         __nn == __nn_max; \
213     }))
214 
215 #define __CPU_OP_S(setsize_, dstset_, srcset1_, srcset2_, op) \
216     do { \
217         cpu_set_t* __dst = (dstset); \
218         const __CPU_BITTYPE* __src1 = (srcset1)->__bits; \
219         const __CPU_BITTYPE* __src2 = (srcset2)->__bits; \
220         size_t __nn = 0, __nn_max = (setsize_)/sizeof(__CPU_BITTYPE); \
221         for (; __nn < __nn_max; __nn++) \
222             (__dst)->__bits[__nn] = __src1[__nn] op __src2[__nn]; \
223     } while (0)
224 
225 #define CPU_COUNT_S(setsize_, set_) \
226     __sched_cpucount((setsize_), (set_))
227 
228 extern int __sched_cpucount(size_t setsize, cpu_set_t* set);
229 
230 #endif /* _GNU_SOURCE */
231 
232 __END_DECLS
233 
234 #endif /* _SCHED_H_ */
235