1 #define TARGET_OS_EMBEDDED 1
9 #include <sys/sysctl.h>
11 #import <Foundation/Foundation.h>
13 #include <mach/message.h>
14 #include <libproc_internal.h>
16 #define MAX_THREADS 100
24 int percentage = 95, interval = 600;
26 int wakemon_rate = 150;
28 int limit = 0; // Worker thread should apply per-thread limit to self?
29 int limit_period = 5000;
32 printf("usage: monitor_stress [ -c nthreads ] [ -w nthreads ] \n");
33 printf("\t-c: number of CPU usage monitor stress threads to use (default: 2\n");
34 printf("\t-w: number of wakeups monitor stress threads to use (default: 0\n");
35 printf("\t-e: exit after this many seconds (default: run forever)\n");
36 printf("\t-p: act on this pid (default: self)\n");
39 void *perthr_limit_thread(void *arg)
41 int percent = 90, refill_period = 30; // time unit is milliseconds
46 cpupercent = percent | (refill_period << 8);
48 if ((err = sysctlbyname("kern.setthread_cpupercent", 0, 0,
49 &cpupercent, sizeof (int))) != 0) {
50 printf("kern.setthread_cpupercent: error %d\n", err);
56 void *cpumon_stress_thread(void *arg)
59 if (proc_set_cpumon_params(pid, percentage, interval) != 0) {
60 perror("proc_set_cpumon_params");
63 if (proc_disable_cpumon(pid) != 0) {
64 perror("proc_disable_cpumon");
70 void *wakemon_stress_thread(void *arg)
73 if (proc_set_wakemon_params(pid, wakemon_rate, 0) != 0) {
74 perror("proc_set_wakemon_params");
77 if (proc_disable_wakemon(pid) != 0) {
78 perror("proc_disable_wakemon");
84 void *exit_thread(void *arg)
87 printf("...exiting.\n");
93 int main(int argc, char *argv[])
97 int cpumon_threads = 2;
98 int wakemon_threads = 0;
102 pname = basename(argv[0]);
105 while ((ch = getopt(argc, argv, "c:w:e:p:h?")) != -1) {
108 cpumon_threads = atoi(optarg);
111 wakemon_threads = atoi(optarg);
114 exit_after = atoi(optarg);
134 if ((cpumon_threads <= 0) || (cpumon_threads > MAX_THREADS) ||
135 (wakemon_threads < 0) || (wakemon_threads > MAX_THREADS)) {
136 printf("%s: %d/%d threads too many (max is %d)\n", pname,
137 cpumon_threads, wakemon_threads, MAX_THREADS);
141 printf("%s: creating %d CPU usage monitor stress threads (1 will be main thread), ", pname, cpumon_threads);
142 if (wakemon_threads > 0) {
143 printf( "%d wakeups monitor stress threads, ", wakemon_threads);
145 printf("and 1 per-thread CPU limit stress thread.\n");
147 if (pthread_create(&thr_id, NULL, perthr_limit_thread, NULL) != 0) {
148 perror("pthread_create");
152 for (i = 0; i < wakemon_threads; i++) {
153 if (pthread_create(&thr_id, NULL, wakemon_stress_thread, NULL) != 0) {
154 perror("pthread_create");
159 // main thread will be used as stress thread too, so start count at 1
160 for (i = 1; i < cpumon_threads; i++) {
161 if (pthread_create(&thr_id, NULL, cpumon_stress_thread, NULL) != 0) {
162 perror("pthread_create");
167 if (exit_after >= 0) {
168 printf("%s: will exit after %d seconds\n", pname, exit_after);
169 if (pthread_create(&thr_id, NULL, exit_thread, NULL) != 0) {
170 perror("pthread_create");
175 cpumon_stress_thread(NULL);