]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/darwintests/perf_exit_proc.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / tools / tests / darwintests / perf_exit_proc.c
CommitLineData
813fb2f6
A
1#include <pthread.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <unistd.h>
5
6#include <mach/mach.h>
7#include <mach/mach_vm.h>
8
9static void* loop(__attribute__ ((unused)) void *arg) {
10 while (1) {
11
12 }
13}
14
15
16static int run_additional_threads(int nthreads) {
17 for (int i = 0; i < nthreads; i++) {
18 pthread_t pthread;
19 int err;
20
21 err = pthread_create(&pthread, NULL, loop, NULL);
22 if (err) {
23 return err;
24 }
25 }
26
27 return 0;
28}
29
30static int allocate_and_wire_memory(mach_vm_size_t size) {
31 int err;
32 task_t task = mach_task_self();
33 mach_vm_address_t addr;
34
35 if (size <= 0)
36 return 0;
37
38 err = mach_vm_allocate(task, &addr, size, VM_FLAGS_ANYWHERE);
39 if (err != KERN_SUCCESS) {
40 printf("mach_vm_allocate returned non-zero: %s\n", mach_error_string(err));
41 return err;
42 }
43 err = mach_vm_protect(task, addr, size, 0, VM_PROT_READ | VM_PROT_WRITE);;
44 if (err != KERN_SUCCESS) {
45 printf("mach_vm_protect returned non-zero: %s\n", mach_error_string(err));
46 return err;
47 }
48 host_t host_priv_port;
49 err = host_get_host_priv_port(mach_host_self(), &host_priv_port);
50 if (err != KERN_SUCCESS) {
51 printf("host_get_host_priv_port retruned non-zero: %s\n", mach_error_string(err));
52 return err;
53 }
54 err = mach_vm_wire(host_priv_port, task, addr, size, VM_PROT_READ | VM_PROT_WRITE);
55 if (err != KERN_SUCCESS) {
56 printf("mach_vm_wire returned non-zero: %s\n", mach_error_string(err));
57 return err;
58 }
59
60 return 0;
61}
62
63static int set_thread_priority(int priority) {
64 struct sched_param param;
65 int policy;
66
67 int err = pthread_getschedparam(pthread_self(), &policy, &param);
68 if (err) return err;
69
70 param.sched_priority = priority;
71
72 err = pthread_setschedparam(pthread_self(), policy, &param);
73 if (err) return err;
74
75 return 0;
76}
77
78int main(int argc, char *argv[]) {
79 int priority = 47, nthreads = 0;
80 int err;
81 mach_vm_size_t wired_mem = 0;
82
83 if (argc > 1) {
84 priority = (int)strtoul(argv[1], NULL, 10);
85 }
86 if (argc > 2) {
87 nthreads = (int)strtoul(argv[2], NULL, 10);
88 }
89 if (argc > 3) {
90 wired_mem = (mach_vm_size_t)strtoul(argv[3], NULL, 10);
91 }
92
93 err = allocate_and_wire_memory(wired_mem);
94 if (err) {
95 return err;
96 }
97
98 err = set_thread_priority(priority);
99 if (err) {
100 return err;
101 }
102
103 err = run_additional_threads(nthreads);
104 if (err) {
105 return err;
106 }
107
108 return 0;
109}