Skip to content

Commit 0298396

Browse files
committed
Reintroduce rand()-based frequency selection in test-cdeque (useful on platforms where clock_gettime may trap into kernel mode)
1 parent 1c1b424 commit 0298396

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

libworkstream_df/benchmark-cdeque.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ if [ -z "$randfile" ]; then
5858
randfile=rand.$$
5959
awk -v ni=$niter -v mu=$mu '
6060
BEGIN {
61-
for (i = 1; i <= ni; ++i)
62-
print -log(rand()) * mu
61+
for (i = 1; i <= ni; ++i) {
62+
do
63+
x = -log(rand()) * mu
64+
while (mu <= 1.0 && x > 1.0)
65+
print x
66+
}
6367
}
6468
' >"$randfile"
6569
fi
@@ -71,7 +75,7 @@ for t in $tests; do
7175
done
7276
log=$t.$nthread.$b.$d.log
7377
while read x; do
74-
f=$(awk "BEGIN{print $x / ($nthread-1)}")
78+
f=$(awk "BEGIN{print $mu <= 1.0 ? $x : $x / ($nthread-1)}")
7579
echo $t $testargs -f $f >&2 \>\> $log
7680
[ $dry ] || ./$t $testargs -f $f | tail -n 1 >>$log
7781
done <"$randfile"

libworkstream_df/test-cdeque.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static void worker (struct state *, unsigned long);
4646
static void straight_worker (struct state *, unsigned long);
4747
static void *thief_main (void *);
4848
static bool do_steal (struct state *, double *);
49+
static bool do_rand_steal (struct state *);
4950

5051
static void *
5152
worker_main (void *data)
@@ -130,8 +131,16 @@ thief_main (void *data)
130131
lasttime = get_thread_cpu_time ();
131132
while (state->num_attempt < num_steal_per_thread)
132133
{
133-
if (!do_steal (state, &lasttime))
134-
break;
134+
if (steal_freq <= 1.0)
135+
{
136+
if (!do_rand_steal (state))
137+
break;
138+
}
139+
else
140+
{
141+
if (!do_steal (state, &lasttime))
142+
break;
143+
}
135144
}
136145

137146
END_TIME (&state->time);
@@ -167,6 +176,26 @@ do_steal (struct state *state, double *plasttime)
167176
return true;
168177
}
169178

179+
static bool
180+
do_rand_steal (struct state *state)
181+
{
182+
void *val;
183+
184+
if ((double) rand_r (&state->seed) / RAND_MAX >= steal_freq)
185+
return !atomic_load_explicit (&end, memory_order_acquire);
186+
val = cdeque_steal (worker_deque);
187+
if (val == NULL)
188+
++state->num_failed_attempt;
189+
if (atomic_load_explicit (&end, memory_order_acquire))
190+
{
191+
if (num_steal_per_thread == (unsigned long) -1)
192+
return false;
193+
}
194+
else
195+
++state->num_attempt;
196+
return true;
197+
}
198+
170199
int
171200
main (int argc, char *argv[])
172201
{

libworkstream_df/time-util.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef TIME_UTIL_H
22
#define TIME_UTIL_H
33

4+
#include <assert.h>
45
#include <stdatomic.h>
56
#include <time.h>
67

@@ -10,23 +11,23 @@ static inline int timespec_diff (struct timespec *,
1011
const struct timespec *,
1112
const struct timespec *);
1213

13-
#define BEGIN_TIME(p) \
14-
do \
15-
{ \
16-
atomic_thread_fence (memory_order_seq_cst); \
17-
clock_gettime (TIME_UTIL_CLOCK, (p)); \
18-
atomic_thread_fence (memory_order_seq_cst); \
19-
} \
14+
#define BEGIN_TIME(p) \
15+
do \
16+
{ \
17+
atomic_thread_fence (memory_order_seq_cst); \
18+
assert (clock_gettime (TIME_UTIL_CLOCK, (p)) == 0); \
19+
atomic_thread_fence (memory_order_seq_cst); \
20+
} \
2021
while (0)
21-
#define END_TIME(p) \
22-
do \
23-
{ \
24-
struct timespec _end_time_tv; \
25-
atomic_thread_fence (memory_order_seq_cst); \
26-
clock_gettime (TIME_UTIL_CLOCK, &_end_time_tv); \
27-
assert (timespec_diff ((p), &_end_time_tv, (p)) == 0); \
28-
atomic_thread_fence (memory_order_seq_cst); \
29-
} \
22+
#define END_TIME(p) \
23+
do \
24+
{ \
25+
struct timespec _end_time_tv; \
26+
atomic_thread_fence (memory_order_seq_cst); \
27+
assert (clock_gettime (TIME_UTIL_CLOCK, &_end_time_tv) == 0); \
28+
assert (timespec_diff ((p), &_end_time_tv, (p)) == 0); \
29+
atomic_thread_fence (memory_order_seq_cst); \
30+
} \
3031
while (0)
3132

3233
static inline double
@@ -35,7 +36,7 @@ get_thread_cpu_time (void)
3536
struct timespec _tv;
3637
double _t;
3738

38-
clock_gettime (CLOCK_THREAD_CPUTIME_ID, &_tv);
39+
assert (clock_gettime (CLOCK_THREAD_CPUTIME_ID, &_tv) == 0);
3940
_t = _tv.tv_sec;
4041
_t += _tv.tv_nsec * 1e-9;
4142
return _t;

0 commit comments

Comments
 (0)