-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater_reader.rs
More file actions
210 lines (171 loc) · 6.08 KB
/
updater_reader.rs
File metadata and controls
210 lines (171 loc) · 6.08 KB
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
use clap::Parser;
use orx_concurrent_vec::*;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use std::{
sync::{Arc, Mutex},
time::Instant,
};
fn fmt(n: usize) -> String {
n.to_string()
.as_bytes()
.rchunks(3)
.rev()
.map(std::str::from_utf8)
.collect::<Result<Vec<&str>, _>>()
.unwrap()
.join(",")
}
/// Runs the reader & updater scenario using the lock-free ConcurrentVec<u64>.
fn run_with_con_vec(len: usize, num_readers: usize, num_updaters: usize, duration_seconds: u128) {
fn update(vec: &ConcurrentVec<u64>, seed: u64, duration_seconds: u128) -> usize {
let mut num_updates = 0;
let mut r = ChaCha8Rng::seed_from_u64(seed);
let instant = Instant::now();
while instant.elapsed().as_millis() < duration_seconds * 1000 {
let i = r.random_range(1..vec.len());
vec[i].set((i as u64 + 1) % 1000);
num_updates += 1;
}
num_updates
}
fn read(vec: &ConcurrentVec<u64>, seed: u64, duration_seconds: u128) -> usize {
let mut num_reads = 0;
let mut r = ChaCha8Rng::seed_from_u64(seed);
let len = vec.len();
let instant = Instant::now();
while instant.elapsed().as_millis() < duration_seconds * 1000 {
let i = r.random_range(1..len);
let value = vec.get_cloned(i).unwrap();
assert!(value < len as u64);
num_reads += 1;
}
num_reads
}
let vec = ConcurrentVec::new();
vec.extend((0..len).map(|i| i as u64));
let (num_updates, num_reads) = std::thread::scope(|s| {
let vec = &vec;
let mut update_handles = vec![];
for i in 0..num_updaters {
update_handles.push(s.spawn(move || update(&vec, i as u64, duration_seconds)));
}
let mut read_handles = vec![];
for i in 0..num_readers {
read_handles.push(s.spawn(move || read(&vec, i as u64, duration_seconds)));
}
let mut num_reads = 0;
for x in read_handles {
num_reads += x.join().unwrap();
}
let mut num_updates = 0;
for x in update_handles {
num_updates += x.join().unwrap();
}
(num_updates, num_reads)
});
println!(
"\nConcurrentVec<u64>\n* num-updates:\t{}\n* num-reads:\t{}\n* total-ops:\t{}\n",
fmt(num_updates),
fmt(num_reads),
fmt(num_reads + num_updates),
);
}
/// Runs the reader & updater scenario using the locking Arc<Mutex<Vec<u64>>>.
fn run_with_arc_mutex_vec(
len: usize,
num_readers: usize,
num_updaters: usize,
duration_seconds: u128,
) {
fn update(vec: Arc<Mutex<Vec<u64>>>, seed: u64, duration_seconds: u128) -> usize {
let mut num_updates = 0;
let vec_len = vec.lock().unwrap().len();
let mut r = ChaCha8Rng::seed_from_u64(seed);
let instant = Instant::now();
while instant.elapsed().as_millis() < duration_seconds * 1000 {
let i = r.random_range(1..vec_len);
vec.lock().unwrap()[i] = (i as u64 + 1) % 1000;
num_updates += 1;
}
num_updates
}
fn read(vec: Arc<Mutex<Vec<u64>>>, seed: u64, duration_seconds: u128) -> usize {
let mut num_reads = 0;
let mut r = ChaCha8Rng::seed_from_u64(seed);
let len = vec.lock().unwrap().len();
let instant = Instant::now();
while instant.elapsed().as_millis() < duration_seconds * 1000 {
let i = r.random_range(1..len);
let value = vec.lock().unwrap()[i];
assert!(value < len as u64);
num_reads += 1;
}
num_reads
}
let vec: Vec<_> = (0..len).map(|i| i as u64).collect();
let vec = Arc::new(Mutex::new(vec));
let (num_updates, num_reads) = std::thread::scope(|s| {
let vec = &vec;
let mut update_handles = vec![];
for i in 0..num_updaters {
update_handles.push(s.spawn(move || update(vec.clone(), i as u64, duration_seconds)));
}
let mut read_handles = vec![];
for i in 0..num_readers {
read_handles.push(s.spawn(move || read(vec.clone(), i as u64, duration_seconds)));
}
let mut num_reads = 0;
for x in read_handles {
num_reads += x.join().unwrap();
}
let mut num_updates = 0;
for x in update_handles {
num_updates += x.join().unwrap();
}
(num_updates, num_reads)
});
println!(
"\nArc<Mutex<Vec<u64>>>\n* num-updates:\t{}\n* num-reads:\t{}\n* total-ops:\t{}\n",
fmt(num_updates),
fmt(num_reads),
fmt(num_reads + num_updates),
);
}
/// Program to test and demonstrate the impact of locking and lock-free
/// approaches to update and read from a vector.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Final length of the vector to read and update concurrently.
#[arg(long, default_value_t = 10_000)]
len: usize,
/// Number of threads that will continuously read elements.
#[arg(long, default_value_t = 8)]
num_readers: usize,
/// Number of threads that will continuously update elements.
#[arg(long, default_value_t = 8)]
num_updaters: usize,
/// Total duration of the experiment.
#[arg(long, default_value_t = 4)]
duration_seconds: u128,
}
/// Program to test and demonstrate the impact of locking and lock-free
/// approaches to update and read from a vector.
fn main() {
let args = Args::parse();
println!("\nNumber of read & update operations that can be achieved within a fixed duration.");
println!("\n{:?}", args);
let (len, num_readers, num_updaters, duration_seconds) = (
args.len,
args.num_readers,
args.num_updaters,
args.duration_seconds,
);
assert!(len > 0);
assert!(num_readers > 0);
assert!(num_updaters > 0);
assert!(duration_seconds > 0);
run_with_arc_mutex_vec(len, num_readers, num_updaters, duration_seconds);
run_with_con_vec(len, num_readers, num_updaters, duration_seconds);
}