-
Notifications
You must be signed in to change notification settings - Fork 30
/
policy.c
113 lines (99 loc) · 2.7 KB
/
policy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <daniel@roe.ch>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
#include "sched.h"
#include <mach/mach.h>
#include <mach/thread_policy.h>
#include <sys/resource.h>
int
policy_task_sched_priority(void) {
#if 0
if (setpriority(PRIO_PROCESS, 0, -20) == -1) {
fprintf(stderr, "Failed to set process priority to -20: "
"%s (%i)\n", strerror(errno), errno);
rv = -1;
goto errout;
}
#endif
kern_return_t krv;
mach_port_t task = mach_task_self();
struct task_category_policy cp = {
TASK_FOREGROUND_APPLICATION
};
krv = task_policy_set(task,
TASK_CATEGORY_POLICY,
(task_policy_t)&cp,
TASK_CATEGORY_POLICY_COUNT);
if (krv != KERN_SUCCESS)
return -1;
/* the following requires Mavericks or later */
struct task_qos_policy qp = {
LATENCY_QOS_TIER_1,
THROUGHPUT_QOS_TIER_1
};
krv = task_policy_set(task,
TASK_OVERRIDE_QOS_POLICY,
(task_policy_t)&qp,
TASK_QOS_POLICY_COUNT);
if (krv != KERN_SUCCESS)
return -1;
return 0;
}
/*
* _terra pericolosa_
* Fiddling with scheduling priorities directly seems to have adversary
* effects more often than not. It seems preferable to let the QOS policy
* do its thing instead of tuning priorities.
*/
#if 0
int
policy_thread_sched_priority(int prio) {
kern_return_t krv;
struct thread_precedence_policy pp = {prio};
krv = thread_policy_set(mach_thread_self(),
THREAD_PRECEDENCE_POLICY,
(thread_policy_t)&pp,
THREAD_PRECEDENCE_POLICY_COUNT);
if (krv != KERN_SUCCESS) {
return -1;
}
return 0;
}
int
policy_thread_sched_standard(void) {
kern_return_t krv;
struct thread_standard_policy sp = {0};
krv = thread_policy_set(mach_thread_self(),
THREAD_STANDARD_POLICY,
(thread_policy_t)&sp,
THREAD_STANDARD_POLICY_COUNT);
if (krv != KERN_SUCCESS) {
return -1;
}
return 0;
}
#endif
int
policy_thread_diskio_important(void) {
return setiopolicy_np(IOPOL_TYPE_DISK,
IOPOL_SCOPE_THREAD,
IOPOL_IMPORTANT);
}
int
policy_thread_diskio_standard(void) {
return setiopolicy_np(IOPOL_TYPE_DISK,
IOPOL_SCOPE_THREAD,
IOPOL_STANDARD);
}
int
policy_thread_diskio_utility(void) {
return setiopolicy_np(IOPOL_TYPE_DISK,
IOPOL_SCOPE_THREAD,
IOPOL_UTILITY);
}