1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // away_fair.cfa -- Test that spinning doesn't cause submissions to get stuck. |
---|
8 | // This test should work without io_uring but isn't very useful without |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Wed Mar 2 12:56:51 2022 |
---|
12 | // Last Modified By : |
---|
13 | // Last Modified On : |
---|
14 | // Update Count : |
---|
15 | // |
---|
16 | |
---|
17 | #include <bits/defs.hfa> |
---|
18 | #include <fstream.hfa> |
---|
19 | #include <kernel.hfa> |
---|
20 | #include <thread.hfa> |
---|
21 | #include <iofwd.hfa> |
---|
22 | |
---|
23 | Duration default_preemption() { |
---|
24 | return 0; |
---|
25 | } |
---|
26 | |
---|
27 | enum { TIMES = 1000 }; |
---|
28 | |
---|
29 | volatile unsigned counter = 0; |
---|
30 | |
---|
31 | // ----- Spinner ----- |
---|
32 | // spins trying to prevent other threads from getting to this processor |
---|
33 | thread Spinner {}; |
---|
34 | void ^?{}(Spinner &mutex ) {} |
---|
35 | void main(Spinner &) { |
---|
36 | unsigned last = 0; |
---|
37 | for() { |
---|
38 | unsigned curr = __atomic_load_n(&counter, __ATOMIC_SEQ_CST); |
---|
39 | |
---|
40 | if(curr >= TIMES) return; |
---|
41 | |
---|
42 | if(last == curr) { |
---|
43 | Pause(); |
---|
44 | continue; |
---|
45 | } |
---|
46 | |
---|
47 | last = curr; |
---|
48 | yield(); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | // ----- Submitter ----- |
---|
53 | // try to submit io but yield so that it's likely we are moved to the slow path |
---|
54 | thread Submitter {}; |
---|
55 | void ^?{}(Submitter &mutex ) {} |
---|
56 | void main(Submitter & this) { |
---|
57 | for(TIMES) { |
---|
58 | #if CFA_HAVE_LINUX_IO_URING_H |
---|
59 | io_future_t f; |
---|
60 | struct io_uring_sqe * sqe; |
---|
61 | __u32 idx; |
---|
62 | struct io_context$ * ctx = cfa_io_allocate(&sqe, &idx, 1); |
---|
63 | |
---|
64 | zero_sqe(sqe); |
---|
65 | sqe->opcode = IORING_OP_NOP; |
---|
66 | sqe->user_data = (uintptr_t)&f; |
---|
67 | #endif |
---|
68 | |
---|
69 | yield( prng( this, 15 ) ); |
---|
70 | |
---|
71 | #if CFA_HAVE_LINUX_IO_URING_H |
---|
72 | // Submit everything |
---|
73 | asm volatile("": : :"memory"); |
---|
74 | cfa_io_submit( ctx, &idx, 1, false ); |
---|
75 | #endif |
---|
76 | |
---|
77 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); |
---|
78 | if(0 == (i % 100)) sout | i; |
---|
79 | |
---|
80 | #if CFA_HAVE_LINUX_IO_URING_H |
---|
81 | wait( f ); |
---|
82 | #endif |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | // ----- Yielder ----- |
---|
87 | // Add some chaos into the mix |
---|
88 | thread Yielder {}; |
---|
89 | void ^?{}(Yielder &mutex ) {} |
---|
90 | void main(Yielder&) { |
---|
91 | while(TIMES > __atomic_load_n(&counter, __ATOMIC_SEQ_CST)) { |
---|
92 | yield(); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | int main() { |
---|
98 | processor p; |
---|
99 | sout | "starting"; |
---|
100 | { |
---|
101 | Yielder y; |
---|
102 | Spinner s; |
---|
103 | Submitter io; |
---|
104 | } |
---|
105 | sout | "done"; |
---|
106 | } |
---|