2 * Copyright 2012 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 <xseg/xseg.h>
42 uint64_t loops = 100000;
47 void test_all_funcs(){
48 xpool_index idx1, idx2, ret;
51 idx1 = xpool_add(&xp, (xpool_data) 5);
52 if (idx1 == NoSerial) {
53 printf("couldn't add idx1\n");
56 printf("added 5 in idx %llu\n", idx1);
57 idx2 = xpool_add(&xp, (xpool_data) 6);
58 if (idx2 == NoSerial) {
59 printf("couldn't add idx2\n");
62 printf("added 6 in idx %llu\n", idx2);
64 ret = xpool_peek(&xp, &data);
65 if (ret == NoSerial) {
66 printf("couldn't peek\n");
69 printf("peeked xpool (idx %llu) and took data %u\n", ret, data);
70 ret = xpool_peek_idx(&xp, idx2, &data);
72 printf("couldn't peek\n");
75 printf("peeked xpool in idx %llu and took data %u\n", idx2, data);
76 ret = xpool_peek_and_fwd(&xp, &data);
77 if (ret == NoSerial) {
78 printf("couldn't peek\n");
81 printf("peeked and fwd xpool (idx %llu) and took data %u\n", ret, data);
82 ret = xpool_peek_and_fwd(&xp, &data);
83 if (ret == NoSerial) {
84 printf("couldn't peek\n");
87 printf("peeked and fwd xpool (idx %llu) and took data %u\n", ret, data);
89 ret = xpool_remove(&xp, idx1, &data);
91 printf("couldn't remove idx1\n");
94 if (data != (xpool_data) 5) {
95 printf("idx1 returned wrong value (%u)\n", data);
98 printf("removed idx1 with data %u\n", data);
99 ret = xpool_remove(&xp, idx2, &data);
101 printf("couldn't remove idx2\n");
104 if (data != (xpool_data) 6) {
105 printf("idx2 returned wrong value (%u)\n", data);
108 printf("removed idx2 with data %u\n", data);
110 printf("test succesfull\n");
113 void *xpool_func(void *arg)
116 xpool_index idx, ret;
119 struct timeval start, end;
121 gettimeofday(&start, NULL);
122 for (i = 0; i < loops; i++) {
123 idx = xpool_add(&xp, (xpool_data) id);
124 if (idx == NoSerial) {
125 printf("couldn't add idx\n");
129 ret = xpool_peek_idx(&xp, idx, &data);
131 printf("couldn't peek\n");
134 if (data != (xpool_data) id){
135 printf("peekidx returned wrong value %u instead of %u\n",
136 data, (xpool_data) id);
139 ret = xpool_peek_and_fwd(&xp, &data);
140 if (ret == NoSerial) {
141 printf("couldn't peek and fwd\n");
145 ret = xpool_remove(&xp, idx, &data);
147 // printf("couldn't remove idx: %llu\n", idx);
150 // if (data != (xpool_data) id){
151 // printf("remove returned wrong value %u instead of %u\n",
152 // data, (xpool_data) id);
156 gettimeofday(&end, NULL);
157 timersub(&end, &start, &end);
158 printf("%d: test succesfull (%llu loops in %u.%us)\n", id, loops, end.tv_sec, end.tv_usec);
163 void *xq_func(void *arg)
168 struct timeval start, end;
170 gettimeofday(&start, NULL);
171 for (i = 0; i < loops; i++) {
173 idx = xq_append_tail(&xq, (xqindex) id);
174 if (idx == NoSerial) {
175 printf("couldn't add idx\n");
179 ret = xpool_peek_idx(&xp, idx, &data);
181 printf("couldn't peek\n");
184 if (data != (xpool_data) id){
185 printf("peekidx returned wrong value %u instead of %u\n",
186 data, (xpool_data) id);
189 ret = xpool_peek_and_fwd(&xp, &data);
190 if (ret == NoSerial) {
191 printf("couldn't peek and fwd\n");
195 ret = xq_pop_head(&xq);
197 // printf("couldn't remove idx: %llu\n", idx);
200 //if (ret != (xqindex) id){
201 // printf("remove returned wrong value %u instead of %u\n",
202 // ret, (xqindex) id);
206 gettimeofday(&end, NULL);
207 timersub(&end, &start, &end);
208 printf("%d: test succesfull (%llu loops in %u.%us)\n", id, loops, end.tv_sec, end.tv_usec);
214 int main(int argc, const char *argv[])
217 size = atoi(argv[1]);
218 loops = atoi(argv[2]);
220 struct xpool_node* mem = malloc(sizeof(struct xpool_node) * size);
221 xqindex* xqmem = malloc(sizeof(xqindex) * size);
222 pthread_t *thread, tid;
226 thread = malloc(sizeof(pthread_t)*size);
228 printf("Testing xpool\n");
229 xpool_init(&xp, size, mem);
230 for (i = 0; i < size; i++) {
231 pthread_create(thread+i, NULL, xpool_func, (void *) i);
234 for (i = 0; i < size; i++) {
235 pthread_join(thread[i], &ret);
238 printf("Testing xq\n");
239 xq_init_empty(&xq, size, xqmem);
240 for (i = 0; i < size; i++) {
241 pthread_create(thread+i, NULL, xq_func, (void *) i);
244 for (i = 0; i < size; i++) {
245 pthread_join(thread[i], &ret);