Debian Codegraph @

regressions/ck_hp/validate/ck_hp_stack.c

raw
1 /*
2 * Copyright 2010-2015 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <pthread.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <sys/time.h>
36
37 #include <ck_backoff.h>
38 #include <ck_cc.h>
39 #include <ck_pr.h>
40 #include <stdbool.h>
41 #include <stddef.h>
42 #include <ck_hp.h>
43 #include <ck_stack.h>
44 #include <ck_hp_stack.h>
45
46 #include "../../common.h"
47
48 static unsigned int threshold;
49 static unsigned int n_threads;
50 static unsigned int barrier;
51 static unsigned int e_barrier;
52
53 #ifndef PAIRS
54 #define PAIRS 5000000
55 #endif
56
57 struct node {
58 unsigned int value;
59 ck_hp_hazard_t hazard;
60 ck_stack_entry_t stack_entry;
61 };
62 static ck_stack_t stack = {NULL, NULL};
63 static ck_hp_t stack_hp;
64 CK_STACK_CONTAINER(struct node, stack_entry, stack_container)
65 static struct affinity a;
66
67 static void *
68 thread(void *unused CK_CC_UNUSED)
69 {
70 struct node **entry, *e;
71 unsigned int i;
72 ck_hp_record_t record;
73 void **pointers;
74 ck_stack_entry_t *s;
75
76 unused = NULL;
77 pointers = malloc(sizeof(void *));
78 ck_hp_register(&stack_hp, &record, pointers);
79
80 if (aff_iterate(&a)) {
81 perror("ERROR: failed to affine thread");
82 exit(EXIT_FAILURE);
83 }
84
85 entry = malloc(sizeof(struct node *) * PAIRS);
86 if (entry == NULL) {
87 ck_error("Failed allocation.\n");
88 }
89
90 for (i = 0; i < PAIRS; i++) {
91 entry[i] = malloc(sizeof(struct node));
92 if (entry == NULL) {
93 ck_error("Failed individual allocation\n");
94 }
95 }
96
97 ck_pr_inc_uint(&barrier);
98 while (ck_pr_load_uint(&barrier) < n_threads)
99 ck_pr_stall();
100
101 for (i = 0; i < PAIRS; i++) {
102 ck_hp_stack_push_mpmc(&stack, &entry[i]->stack_entry);
103 s = ck_hp_stack_pop_mpmc(&record, &stack);
104 e = stack_container(s);
105 ck_hp_free(&record, &e->hazard, e, s);
106 }
107
108 ck_pr_inc_uint(&e_barrier);
109 while (ck_pr_load_uint(&e_barrier) < n_threads)
110 ck_pr_stall();
111
112 fprintf(stderr, "Peak: %u (%2.2f%%)\nReclamations: %" PRIu64 "\n\n",
113 record.n_peak,
114 (double)record.n_peak / PAIRS * 100,
115 record.n_reclamations);
116
117 ck_hp_clear(&record);
118 ck_hp_purge(&record);
119
120 ck_pr_inc_uint(&e_barrier);
121 while (ck_pr_load_uint(&e_barrier) < (n_threads << 1));
122
123 if (record.n_pending != 0) {
124 ck_error("ERROR: %u pending, expecting none.\n",
125 record.n_pending);
126 }
127
128 return (NULL);
129 }
130
131 static void
132 destructor(void *p)
133 {
134
135 free(p);
136 return;
137 }
138
139 int
140 main(int argc, char *argv[])
141 {
142 unsigned int i;
143 pthread_t *threads;
144
145 if (argc != 4) {
146 ck_error("Usage: stack <threads> <threshold> <delta>\n");
147 }
148
149 n_threads = atoi(argv[1]);
150 threshold = atoi(argv[2]);
151 a.delta = atoi(argv[3]);
152 a.request = 0;
153
154 threads = malloc(sizeof(pthread_t) * n_threads);
155
156 ck_hp_init(&stack_hp, 1, threshold, destructor);
157
158 for (i = 0; i < n_threads; i++)
159 pthread_create(threads + i, NULL, thread, NULL);
160
161 for (i = 0; i < n_threads; i++)
162 pthread_join(threads[i], NULL);
163
164 return (0);
165 }