1/*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _SYS_SFI_H_
30#define _SYS_SFI_H_
31
32#include <stdint.h>
33#include <sys/types.h>
34#include <mach/sfi_class.h>
35/*
36 * Selective Forced Idle (SFI) is a mechanism for doing
37 * phase-aligned modulation of runnable tasks
38 */
39
40/* Flags for use with sfi_process_set_flags() */
41#define SFI_PROCESS_SET_MANAGED 0x00000001
42#define SFI_PROCESS_SET_UNMANAGED 0x00000002
43
44#define SFI_PROCESS_SET_MANAGED_MASK 0x00000003
45
46#ifndef KERNEL
47/*
48 * The system_set_sfi_window() call can be used to set "Selective
49 * Forced Idle" window for the system. sfi_window_usec is the
50 * interval in microseconds and when the "Selective Forced Idle" is
51 * active its "off phase" starts every sfi_window_usec. A nonzero
52 * value lower than MIN_SFI_WINDOW_USEC will be automatically changed
53 * to MIN_SFI_WINDOW_USEC. The actual window size used is
54 * implementation dependent and may be longer.
55 *
56 * system_get_sfi_window() can be used to determine the actual value.
57 *
58 * A value of 0 for sfi_window_usec can be used to disable "Selective
59 * Forced Idle".
60 */
61int system_set_sfi_window(uint64_t sfi_window_usec);
62
63int system_get_sfi_window(uint64_t *sfi_window_usec);
64
65/*
66 * sfi_set_class_offtime() can be used to set the "off time" interval
67 * for all threads placed in a "Selective Forced Idle" class class_id.
68 * A thread placed in class_id will not execute for offtime_usec
69 * microseconds at the beginning of each "Selective Forced Idle"
70 * window. A nonzero value lower than MIN_OFFTIME_USEC will be
71 * automatically changed to MIN_OFFTIME_USEC. The actual "off time"
72 * used is implementation dependent and may be
73 * longer. sfi_get_class_offtime() can be used to determine the actual
74 * value.
75 *
76 * A value of 0 for offtime_usec can be used to disable "Selective
77 * Forced Idle" for all the threads placed in class_id.
78 *
79 * A value of offtime_usec greater than the value of system wide
80 * "Selective Forced Idle" window will cause failure.
81 */
82int sfi_set_class_offtime(sfi_class_id_t class_id, uint64_t offtime_usec);
83
84int sfi_get_class_offtime(sfi_class_id_t class_id, uint64_t *offtime_usec);
85
86/*
87 * The sfi_process_set_class() can be used to place all the threads in
88 * a process ID pid in the "Selective Forced Idle" managed class, or clear
89 * the managed state.
90 */
91
92int sfi_process_set_flags(pid_t pid, uint32_t flags);
93
94int sfi_process_get_flags(pid_t pid, uint32_t *flags);
95
96#endif /* !KERNEL */
97
98#if PRIVATE
99
100/* This is the private system call interface between Libsyscall and xnu */
101#define SFI_CTL_OPERATION_SFI_SET_WINDOW 0x00000001
102#define SFI_CTL_OPERATION_SFI_GET_WINDOW 0x00000002
103#define SFI_CTL_OPERATION_SET_CLASS_OFFTIME 0x00000003
104#define SFI_CTL_OPERATION_GET_CLASS_OFFTIME 0x00000004
105
106#define SFI_PIDCTL_OPERATION_PID_SET_FLAGS 0x00000001
107#define SFI_PIDCTL_OPERATION_PID_GET_FLAGS 0x00000002
108
109int __sfi_ctl(uint32_t operation, uint32_t sfi_class, uint64_t time, uint64_t *out_time);
110int __sfi_pidctl(uint32_t operation, pid_t pid, uint32_t sfi_flags, uint32_t *out_sfi_flags);
111
112#endif /* PRIVATE */
113
114#endif /* _SYS_SFI_H_ */
115