| 1 |
/* |
| 2 |
* Copyright 2008-2012 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 <math.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <strings.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <sys/time.h> |
| 38 |
|
| 39 |
#include <ck_pr.h> |
| 40 |
#include <ck_spinlock.h> |
| 41 |
|
| 42 |
#include "../../common.h" |
| 43 |
|
| 44 |
/* 8! = 40320, evenly divide 1 .. 8 processor workload. */ |
| 45 |
#define WORKLOAD (40320 * 2056) |
| 46 |
|
| 47 |
#ifndef ITERATE |
| 48 |
#define ITERATE 65536 |
| 49 |
#endif |
| 50 |
|
| 51 |
struct block { |
| 52 |
unsigned int tid; |
| 53 |
}; |
| 54 |
|
| 55 |
static struct affinity a; |
| 56 |
static unsigned int ready; |
| 57 |
|
| 58 |
struct counters { |
| 59 |
uint64_t value; |
| 60 |
} CK_CC_CACHELINE; |
| 61 |
|
| 62 |
static struct counters *count; |
| 63 |
static uint64_t nthr; |
| 64 |
static unsigned int barrier; |
| 65 |
|
| 66 |
int critical __attribute__((aligned(64))); |
| 67 |
|
| 68 |
LOCK_DEFINE; |
| 69 |
|
| 70 |
CK_CC_USED static void |
| 71 |
gen_lock(void) |
| 72 |
{ |
| 73 |
CK_CC_UNUSED int core = 0; |
| 74 |
#ifdef LOCK_STATE |
| 75 |
LOCK_STATE; |
| 76 |
#endif |
| 77 |
|
| 78 |
#ifdef LOCK |
| 79 |
LOCK; |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
CK_CC_USED static void |
| 84 |
gen_unlock(void) |
| 85 |
{ |
| 86 |
#ifdef LOCK_STATE |
| 87 |
LOCK_STATE; |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifdef UNLOCK |
| 91 |
UNLOCK; |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
static void * |
| 96 |
fairness(void *null) |
| 97 |
{ |
| 98 |
#ifdef LOCK_STATE |
| 99 |
LOCK_STATE; |
| 100 |
#endif |
| 101 |
struct block *context = null; |
| 102 |
unsigned int i = context->tid; |
| 103 |
volatile int j; |
| 104 |
long int base; |
| 105 |
unsigned int core; |
| 106 |
|
| 107 |
if (aff_iterate_core(&a, &core)) { |
| 108 |
perror("ERROR: Could not affine thread"); |
| 109 |
exit(EXIT_FAILURE); |
| 110 |
} |
| 111 |
|
| 112 |
while (ck_pr_load_uint(&ready) == 0); |
| 113 |
|
| 114 |
ck_pr_inc_uint(&barrier); |
| 115 |
while (ck_pr_load_uint(&barrier) != nthr); |
| 116 |
|
| 117 |
while (ready) { |
| 118 |
LOCK; |
| 119 |
|
| 120 |
count[i].value++; |
| 121 |
if (critical) { |
| 122 |
base = common_lrand48() % critical; |
| 123 |
for (j = 0; j < base; j++); |
| 124 |
} |
| 125 |
|
| 126 |
UNLOCK; |
| 127 |
} |
| 128 |
|
| 129 |
return (NULL); |
| 130 |
} |
| 131 |
|
| 132 |
int |
| 133 |
main(int argc, char *argv[]) |
| 134 |
{ |
| 135 |
uint64_t v, d; |
| 136 |
unsigned int i; |
| 137 |
pthread_t *threads; |
| 138 |
struct block *context; |
| 139 |
|
| 140 |
if (argc != 4) { |
| 141 |
ck_error("Usage: " LOCK_NAME " <number of threads> <affinity delta> <critical section>\n"); |
| 142 |
exit(EXIT_FAILURE); |
| 143 |
} |
| 144 |
|
| 145 |
nthr = atoi(argv[1]); |
| 146 |
if (nthr <= 0) { |
| 147 |
ck_error("ERROR: Number of threads must be greater than 0\n"); |
| 148 |
exit(EXIT_FAILURE); |
| 149 |
} |
| 150 |
|
| 151 |
#ifdef LOCK_INIT |
| 152 |
LOCK_INIT; |
| 153 |
#endif |
| 154 |
|
| 155 |
critical = atoi(argv[3]); |
| 156 |
if (critical < 0) { |
| 157 |
ck_error("ERROR: critical section cannot be negative\n"); |
| 158 |
exit(EXIT_FAILURE); |
| 159 |
} |
| 160 |
|
| 161 |
threads = malloc(sizeof(pthread_t) * nthr); |
| 162 |
if (threads == NULL) { |
| 163 |
ck_error("ERROR: Could not allocate thread structures\n"); |
| 164 |
exit(EXIT_FAILURE); |
| 165 |
} |
| 166 |
|
| 167 |
context = malloc(sizeof(struct block) * nthr); |
| 168 |
if (context == NULL) { |
| 169 |
ck_error("ERROR: Could not allocate thread contexts\n"); |
| 170 |
exit(EXIT_FAILURE); |
| 171 |
} |
| 172 |
|
| 173 |
a.delta = atoi(argv[2]); |
| 174 |
a.request = 0; |
| 175 |
|
| 176 |
count = malloc(sizeof(*count) * nthr); |
| 177 |
if (count == NULL) { |
| 178 |
ck_error("ERROR: Could not create acquisition buffer\n"); |
| 179 |
exit(EXIT_FAILURE); |
| 180 |
} |
| 181 |
memset(count, 0, sizeof(*count) * nthr); |
| 182 |
|
| 183 |
fprintf(stderr, "Creating threads (fairness)..."); |
| 184 |
for (i = 0; i < nthr; i++) { |
| 185 |
context[i].tid = i; |
| 186 |
if (pthread_create(&threads[i], NULL, fairness, context + i)) { |
| 187 |
ck_error("ERROR: Could not create thread %d\n", i); |
| 188 |
exit(EXIT_FAILURE); |
| 189 |
} |
| 190 |
} |
| 191 |
fprintf(stderr, "done\n"); |
| 192 |
|
| 193 |
ck_pr_store_uint(&ready, 1); |
| 194 |
common_sleep(10); |
| 195 |
ck_pr_store_uint(&ready, 0); |
| 196 |
|
| 197 |
fprintf(stderr, "Waiting for threads to finish acquisition regression..."); |
| 198 |
for (i = 0; i < nthr; i++) |
| 199 |
pthread_join(threads[i], NULL); |
| 200 |
fprintf(stderr, "done\n\n"); |
| 201 |
|
| 202 |
for (i = 0, v = 0; i < nthr; i++) { |
| 203 |
printf("%d %15" PRIu64 "\n", i, count[i].value); |
| 204 |
v += count[i].value; |
| 205 |
} |
| 206 |
|
| 207 |
printf("\n# total : %15" PRIu64 "\n", v); |
| 208 |
printf("# throughput : %15" PRIu64 " a/s\n", (v /= nthr) / 10); |
| 209 |
|
| 210 |
for (i = 0, d = 0; i < nthr; i++) |
| 211 |
d += (count[i].value - v) * (count[i].value - v); |
| 212 |
|
| 213 |
printf("# average : %15" PRIu64 "\n", v); |
| 214 |
printf("# deviation : %.2f (%.2f%%)\n\n", sqrt(d / nthr), (sqrt(d / nthr) / v) * 100.00); |
| 215 |
|
| 216 |
return (0); |
| 217 |
} |
| 218 |
|