2 * Copyright 2013 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
39 #include <xtypes/xlock.h>
43 unsigned long sum = 0;
46 void jobfn(void *q, void *arg)
48 unsigned long c = (unsigned long) arg;
52 int test1(unsigned long n)
56 xworkq_init(&wq, &lock, 0);
60 for (i = 0; i < n; i++) {
61 xworkq_enqueue(&wq, jobfn, (void *)1);
66 return ((sum == n)? 0 : -1);
75 void *thread_test(void *arg)
77 struct thread_arg *targ = (struct thread_arg *)arg;
78 unsigned long n = targ->n;
81 for (i = 0; i < n; i++) {
82 xworkq_enqueue(targ->wq, jobfn, (void *)targ->num);
89 int test2(unsigned long n, unsigned long nr_threads)
93 xworkq_init(&wq, &lock, 0);
97 struct thread_arg *targs = malloc(sizeof(struct thread_arg)*nr_threads * n);
98 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
100 for (i = 0; i < nr_threads; i++) {
105 for (i = 0; i < nr_threads; i++) {
106 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
108 fprintf(stderr, "error pthread_create\n");
113 for (i = 0; i < nr_threads; i++) {
114 pthread_join(threads[i], NULL);
123 unsigned long expected_sum = 0;
124 for (i = 0; i < nr_threads; i++) {
125 expected_sum += n*(i+1);
127 return ((sum == expected_sum) ? 0 : -1);
130 int test3(unsigned long n, unsigned long nr_threads)
134 xworkq_init(&wq, &lock, 0);
136 xlock_release(&lock);
138 struct thread_arg *targs = malloc(sizeof(struct thread_arg)*nr_threads * n);
139 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
141 for (i = 0; i < nr_threads; i++) {
146 for (i = 0; i < nr_threads; i++) {
147 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
149 fprintf(stderr, "error pthread_create\n");
154 for (i = 0; i < nr_threads; i++) {
155 pthread_join(threads[i], NULL);
164 unsigned long expected_sum = 0;
165 for (i = 0; i < nr_threads; i++) {
166 expected_sum += n*(i+1);
168 return ((sum == expected_sum) ? 0 : -1);
171 int main(int argc, const char *argv[])
173 struct timeval start, end, tv;
175 int n = atoi(argv[1]);
176 int t = atoi(argv[2]);
178 fprintf(stderr, "Running test1\n");
179 gettimeofday(&start, NULL);
182 fprintf(stderr, "Test1: FAILED\n");
185 gettimeofday(&end, NULL);
186 timersub(&end, &start, &tv);
187 fprintf(stderr, "Test1: PASSED\n");
188 fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec);
190 fprintf(stderr, "running test2\n");
191 gettimeofday(&start, NULL);
194 fprintf(stderr, "test2: failed\n");
197 gettimeofday(&end, NULL);
198 fprintf(stderr, "test2: passed\n");
199 timersub(&end, &start, &tv);
200 fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec);
202 fprintf(stderr, "running test3\n");
203 gettimeofday(&start, NULL);
206 fprintf(stderr, "test3: failed\n");
209 gettimeofday(&end, NULL);
210 fprintf(stderr, "test3: passed\n");
211 timersub(&end, &start, &tv);
212 fprintf(stderr, "Test time: %ds %dusec\n\n", (int)tv.tv_sec, (int)tv.tv_usec);