~ubuntu-branches/ubuntu/utopic/libpthread-workqueue/utopic

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if !defined(_WIN32)
# if !defined(NO_CONFIG_H)
#  include "config.h"
# endif
#endif
#include "../../src/private.h"

#if HAVE_ERR_H
# include <err.h>
#else
# define err(rc,msg,...) do { perror(msg); exit(rc); } while (0)
# define errx(rc,msg,...) do { puts(msg); exit(rc); } while (0)
#endif

#include <pthread_workqueue.h>

static int work_cnt;

pthread_workqueue_t wq;

void additem(void (*func)(void *), 
             void * arg)
{

    int rv;
    
    rv = pthread_workqueue_additem_np(wq, *func, arg, NULL, NULL);
    if (rv != 0)
        errx(1, "unable to add item: %s", strerror(rv));
    puts("added item\n");
}

void
compute(void *arg)
{
    int *count = (int *) arg;
#define nval 5000
    int val[nval];
    int i,j;

    /* Do some useless computation */
    for (i = 0; i < nval; i++) {
        val[i] = INT_MAX;
    }
    for (j = 0; j < nval; j++) {
        for (i = 0; i < nval; i++) {
            val[i] /= 3;
            val[i] *= 2;
            val[i] /= 4;
            val[i] *= 5;
        }
    }

    if (count != NULL) 
        (*count)--;
}


void
sleepy(void *msg)
{
    printf("%s\n", (char *) msg);
    if (strcmp(msg, "done") == 0)
        exit(0);
    sleep(random() % 6);
}

void
lazy(void *arg)
{
    sleep(3);
    printf("item %lu complete\n", (unsigned long) arg);
	work_cnt--;
}

void
run_blocking_test(void)
{
	const int rounds = 50;
	long i = 0;
	work_cnt = rounds;
    for (i = 0; i < rounds; i++) {
        additem(lazy, (void *) i);
    }
	while (work_cnt > 0)
		sleep(1);
}

void
run_cond_wait_test(void)
{
	const int rounds = 10;
	long i = 0;

	sleep(3);	/* Allow time for the workers to enter pthread_cond_wait() */
	work_cnt = rounds;
    for (i = 0; i < rounds; i++) {
        additem(lazy, (void *) i);
		sleep(1);
    }
	while (work_cnt > 0)
		sleep(1);
}

void
run_load_test(void)
{
    char buf[16];
	int i = 0;
    for (i = 0; i < 1024; i++) {
        sprintf(buf, "%d", i);
        additem(sleepy, strdup(buf));
        additem(compute, NULL);
    }
    additem(sleepy, "done");
}

/* Try to overwhelm the CPU with computation requests */
void
run_stress_test(int rounds)
{
	int i = 0;
	work_cnt = rounds;
    for (i = 0; i < rounds; i++) {
        additem(compute, &work_cnt);
    }
	while (work_cnt > 0)
		sleep(1);
    puts("====== stress test complete =======");
}


int main() {
    int rv;

    printf("pthread_workqueue_create_np().. ");
    rv = pthread_workqueue_create_np(&wq, NULL);
    if (rv != 0)
        err(1, "failed");
    printf("ok\n");

    printf("stress test.. ");
    run_stress_test(25);
    printf("ok\n");

    //run_deadlock_test();
//    run_cond_wait_test();
//    run_blocking_test();
    //run_load_test();


	puts("All tests completed.\n");
    exit(0);
}