]> git.saurik.com Git - apple/libpthread.git/blame - tests/wq_kevent_stress.c
libpthread-301.20.1.tar.gz
[apple/libpthread.git] / tests / wq_kevent_stress.c
CommitLineData
964d3577
A
1#include <assert.h>
2#include <errno.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7#include <sys/types.h>
8#include <sys/time.h>
9#include <sys/sysctl.h>
10#include <sys/qos.h>
11
12#include <dispatch/dispatch.h>
13
14#include "../private/workqueue_private.h"
15#include "../private/qos_private.h"
16
17#include "wq_kevent.h"
18
19static int rand_qos(){
20 switch (rand() % 5){
21 case 0: return QOS_CLASS_USER_INTERACTIVE;
22 case 1: return QOS_CLASS_USER_INITIATED;
23 case 2: return QOS_CLASS_DEFAULT;
24 case 3: return QOS_CLASS_UTILITY;
25 case 4: return QOS_CLASS_BACKGROUND;
26 }
27 return QOS_CLASS_UNSPECIFIED;
28}
29
30static void workqueue_func(pthread_priority_t priority){
31 fprintf(stderr, "WARNING: workqueue_func called.\n");
32}
33
34static void workqueue_func_kevent(void **buf, int *count){
35 pthread_priority_t p = (pthread_priority_t)pthread_getspecific(4);
36 fprintf(stderr, "\tthread with qos %s spawned (count: %d).\n", describe_pri(p), *count);
37
38 //struct timeval start, stop;
39 //gettimeofday(&start, NULL);
40
41 for (int i = 0; i < (rand() % 10000) * 1000 + 50000; i++){
42 burn_cpu();
43 }
44
45 //gettimeofday(&stop, NULL);
46 //fprintf(stderr, "\tthread exited %ld usec later\n", stop.tv_usec - start.tv_usec + (stop.tv_sec - start.tv_sec) * 1000000);
47}
48
49int main(int argc, char *argv[]){
50 int ret = 0;
51
52 ret = _pthread_workqueue_init_with_kevent(workqueue_func, workqueue_func_kevent, 0, 0);
53 assert(ret == 0);
54
55 int iteration = 0;
56 while (iteration++ < 1000){
57 switch (iteration % 5){
58 case 0:
59 // one event manager
60 bzero(requests, sizeof(requests));
61 requests[0].priority = _pthread_qos_class_encode(rand_qos(), 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG);
62 requests[0].count = 1;
63
64 if ((ret = do_req()) < 0) return ret;
65 break;
66
67 case 1:
68 // one constrained thread
69 bzero(requests, sizeof(requests));
70 requests[0].priority = _pthread_qos_class_encode(rand_qos(), 0, 0);
71 requests[0].count = rand() % 2;
72 if (requests[0].count > 0 && (ret = do_req()) < 0) return ret;
73 break;
74
75 case 2:
76 // one event manager
77 bzero(requests, sizeof(requests));
78 requests[0].priority = _pthread_qos_class_encode(rand_qos(), 0, _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG);
79 requests[0].count = 1;
80
81 if ((ret = do_req()) < 0) return ret;
82 break;
83
84 case 3:
85 // one overcommit thread
86 bzero(requests, sizeof(requests));
87 requests[0].priority = _pthread_qos_class_encode(rand_qos(), 0, _PTHREAD_PRIORITY_OVERCOMMIT_FLAG);
88 requests[0].count = rand() % 2;
89
90 if (requests[0].count > 0 && (ret = do_req()) < 0) return ret;
91 break;
92
93 case 4:
94 // varied constrained threads
95 bzero(requests, sizeof(requests));
96 requests[0].priority = _pthread_qos_class_encode(QOS_CLASS_USER_INTERACTIVE, 0, 0);
97 requests[0].count = rand() % 4;
98 requests[1].priority = _pthread_qos_class_encode(QOS_CLASS_USER_INITIATED, 0, 0);
99 requests[1].count = rand() % 4;
100 requests[2].priority = _pthread_qos_class_encode(QOS_CLASS_UTILITY, 0, 0);
101 requests[2].count = rand() % 4;
102 requests[3].priority = _pthread_qos_class_encode(QOS_CLASS_BACKGROUND, 0, 0);
103 requests[3].count = rand() % 4;
104 if ((requests[0].count + requests[1].count + requests[2].count + requests[3].count) > 0 && (ret = do_req()) < 0) return ret;
105 break;
106 }
107 usleep(rand() % 100000);
108 }
109
110 return 0;
111}