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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Copyright (c) 2013-2016 by Farsight Security, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <assert.h>
#include <inttypes.h>
#include <locale.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "libmy/my_alloc.h"
#include "libmy/my_time.h"
#include "libmy/my_memory_barrier.h"
#include "libmy/my_queue.h"
#ifdef MY_HAVE_MEMORY_BARRIERS
extern const struct my_queue_ops my_queue_mb_ops;
#endif
extern const struct my_queue_ops my_queue_mutex_ops;
const struct my_queue_ops *queue_ops;
struct producer_stats {
uint64_t count_producer_full;
uint64_t count_producer;
uint64_t checksum_producer;
uint64_t count_insert_calls;
};
struct consumer_stats {
uint64_t count_consumer_empty;
uint64_t count_consumer;
uint64_t count_remove_calls;
uint64_t checksum_consumer;
};
enum wait_type {
wt_spin,
wt_slow_producer,
wt_slow_consumer,
};
static volatile bool shut_down;
static enum wait_type wtype;
static const char *wtype_s;
static unsigned seconds;
static struct my_queue *q;
static int size;
static inline void
maybe_wait_producer(int64_t i)
{
if (wtype != wt_slow_producer)
return;
if ((i % 128) == 0) {
struct timespec ts_wait = {
.tv_sec = 0, .tv_nsec = 1
};
my_nanosleep(&ts_wait);
}
}
static inline void
maybe_wait_consumer(int64_t i)
{
if (wtype != wt_slow_consumer)
return;
if ((i % 128) == 0) {
struct timespec ts_wait = {
.tv_sec = 0, .tv_nsec = 1
};
my_nanosleep(&ts_wait);
}
}
static void *
thr_producer(__attribute__((unused)) void *arg)
{
bool res;
unsigned space = 0;
struct producer_stats *s;
s = my_calloc(1, sizeof(*s));
for (unsigned loops = 1; ; loops++) {
for (int64_t i = 1; i <= 1000000; i++) {
if (shut_down)
goto out;
res = queue_ops->insert(q, &i, &space);
s->count_insert_calls++;
if (res) {
s->count_producer++;
s->checksum_producer += i;
} else {
s->count_producer_full++;
}
maybe_wait_producer(i);
}
}
out:
fprintf(stderr, "%s: producer thread shutting down\n", __func__);
fprintf(stderr, "%s: count_producer= %" PRIu64 "\n", __func__, s->count_producer);
fprintf(stderr, "%s: count_producer_full= %" PRIu64 "\n", __func__, s->count_producer_full);
fprintf(stderr, "%s: count_insert_calls= %" PRIu64 "\n", __func__, s->count_insert_calls);
fprintf(stderr, "%s: checksum_producer= %" PRIu64 "\n", __func__, s->checksum_producer);
return (s);
}
static void *
thr_consumer(__attribute__((unused)) void *arg)
{
bool res;
unsigned count = 0;
struct consumer_stats *s;
s = my_calloc(1, sizeof(*s));
for (unsigned loops = 1; ; loops++) {
for (int64_t i = 1; i <= 1000000; i++) {
res = queue_ops->remove(q, &i, &count);
s->count_remove_calls++;
if (res) {
if (i == 0) {
fprintf(stderr, "%s: received shutdown message\n", __func__);
goto out;
}
s->checksum_consumer += i;
s->count_consumer++;
} else {
s->count_consumer_empty++;
}
maybe_wait_consumer(i);
}
}
out:
fprintf(stderr, "%s: count_consumer= %" PRIu64 "\n", __func__, s->count_consumer);
fprintf(stderr, "%s: count_consumer_empty= %" PRIu64 "\n", __func__, s->count_consumer_empty);
fprintf(stderr, "%s: count_remove_calls= %" PRIu64 "\n", __func__, s->count_remove_calls);
fprintf(stderr, "%s: checksum_consumer= %" PRIu64 "\n", __func__, s->checksum_consumer);
return (s);
}
static void
send_shutdown_message(struct my_queue *my_q)
{
int64_t i = 0;
while (!queue_ops->insert(my_q, &i, NULL));
}
static int
check_stats(struct producer_stats *ps, struct consumer_stats *cs)
{
if (ps->checksum_producer != cs->checksum_consumer) {
fprintf(stderr,
"FATAL ERROR: producer checksum != consumer checksum "
"(%" PRIu64 " != %" PRIu64 ")\n",
ps->checksum_producer,
cs->checksum_consumer
);
return EXIT_FAILURE;
}
if (ps->count_producer != cs->count_consumer) {
fprintf(stderr, "FATAL ERROR: producer count != consumer count "
"(%" PRIu64 " != %" PRIu64 ")\n",
ps->count_producer,
cs->count_consumer
);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static void
print_stats(struct timespec *a, struct timespec *b,
struct producer_stats *ps, struct consumer_stats *cs)
{
double dur;
dur = my_timespec_to_double(b) - my_timespec_to_double(a);
fprintf(stderr, "%s: ran for %'.4f seconds in %s mode\n", __func__, dur, wtype_s);
fprintf(stderr, "%s: producer: %'.0f iter/sec [%d nsec/iter] (%.2f%% full)\n",
__func__,
ps->count_insert_calls / dur,
(int) (1E9 * dur / ps->count_insert_calls),
100.0 * ps->count_producer_full / ps->count_insert_calls
);
fprintf(stderr, "%s: consumer: %'.0f iter/sec [%d nsec/iter] (%.2f%% empty)\n",
__func__,
cs->count_remove_calls / dur,
(int) (1E9 * dur / cs->count_remove_calls),
100.0 * cs->count_consumer_empty / cs->count_remove_calls
);
}
static int
run_test(void)
{
int res;
struct timespec ts_a, ts_b;
struct producer_stats *ps;
struct consumer_stats *cs;
struct timespec ts = { .tv_sec = seconds, .tv_nsec = 0 };
q = queue_ops->init(size, sizeof(int64_t));
if (q == NULL) {
fprintf(stderr, "queue_ops->init() failed, size too small or not a power-of-2?\n");
return (EXIT_FAILURE);
}
fprintf(stderr, "queue implementation type: %s\n", queue_ops->impl_type());
fprintf(stderr, "queue size: %d entries\n", size);
fprintf(stderr, "running for %d seconds\n", seconds);
pthread_t thr_p;
pthread_t thr_c;
#if HAVE_CLOCK_GETTIME
const clockid_t clock = CLOCK_MONOTONIC;
#else
const int clock = -1;
#endif
my_gettime(clock, &ts_a);
pthread_create(&thr_p, NULL, thr_producer, NULL);
pthread_create(&thr_c, NULL, thr_consumer, NULL);
my_nanosleep(&ts);
shut_down = true;
pthread_join(thr_p, (void **) &ps);
send_shutdown_message(q);
pthread_join(thr_c, (void **) &cs);
my_gettime(clock, &ts_b);
res = check_stats(ps, cs);
print_stats(&ts_a, &ts_b, ps, cs);
free(ps);
free(cs);
queue_ops->destroy(&q);
return res;
}
int
main(int argc, char **argv)
{
int res;
setlocale(LC_ALL, "");
if (argc != 4) {
fprintf(stderr, "Usage: %s <slow_producer | slow_consumer | spin> <QUEUE SIZE> <RUN SECONDS>\n", argv[0]);
return (EXIT_FAILURE);
}
if (strcasecmp(argv[1], "slow_producer") == 0) {
wtype = wt_slow_producer;
wtype_s = "slow producer";
} else if (strcasecmp(argv[1], "slow_consumer") == 0) {
wtype = wt_slow_consumer;
wtype_s = "slow consumer";
} else if (strcasecmp(argv[1], "spin") == 0) {
wtype = wt_spin;
wtype_s = "spin";
} else {
fprintf(stderr, "Error: invalid wait type '%s'\n", argv[1]);
return (EXIT_FAILURE);
}
size = atoi(argv[2]);
seconds = atoi(argv[3]);
#ifdef MY_HAVE_MEMORY_BARRIERS
queue_ops = &my_queue_mb_ops;
res = run_test();
if (res != EXIT_SUCCESS)
return res;
#endif
shut_down = false;
queue_ops = &my_queue_mutex_ops;
res = run_test();
if (res != EXIT_SUCCESS)
return res;
return EXIT_SUCCESS;
}
|