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 | #include <io/types.hfa> |
---|
23 | |
---|
24 | Duration default_preemption() { |
---|
25 | return 0; |
---|
26 | } |
---|
27 | |
---|
28 | enum { TIMES = 1000 }; |
---|
29 | |
---|
30 | volatile unsigned counter = 0; |
---|
31 | |
---|
32 | // ----- Spinner ----- |
---|
33 | // spins trying to prevent other threads from getting to this processor |
---|
34 | thread Spinner {}; |
---|
35 | void ^?{}(Spinner &mutex ) {} |
---|
36 | void main(Spinner &) { |
---|
37 | unsigned last = 0; |
---|
38 | for() { |
---|
39 | unsigned curr = __atomic_load_n(&counter, __ATOMIC_SEQ_CST); |
---|
40 | |
---|
41 | if(curr >= TIMES) return; |
---|
42 | |
---|
43 | if(last == curr) { |
---|
44 | Pause(); |
---|
45 | continue; |
---|
46 | } |
---|
47 | |
---|
48 | last = curr; |
---|
49 | yield(); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | // ----- Spinner ----- |
---|
54 | // try to submit io but yield so that it's likely we are moved to the slow path |
---|
55 | thread Submitter {}; |
---|
56 | void ^?{}(Submitter &mutex ) {} |
---|
57 | void main(Submitter & this) { |
---|
58 | for(TIMES) { |
---|
59 | #if CFA_HAVE_LINUX_IO_URING_H |
---|
60 | io_future_t f; |
---|
61 | struct io_uring_sqe * sqe; |
---|
62 | __u32 idx; |
---|
63 | struct $io_context * ctx = cfa_io_allocate(&sqe, &idx, 1); |
---|
64 | |
---|
65 | zero_sqe(sqe); |
---|
66 | sqe->opcode = IORING_OP_NOP; |
---|
67 | sqe->user_data = (uintptr_t)&f; |
---|
68 | #endif |
---|
69 | |
---|
70 | yield( prng( this, 15 ) ); |
---|
71 | |
---|
72 | #if CFA_HAVE_LINUX_IO_URING_H |
---|
73 | // Submit everything |
---|
74 | asm volatile("": : :"memory"); |
---|
75 | cfa_io_submit( ctx, &idx, 1, false ); |
---|
76 | #endif |
---|
77 | |
---|
78 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); |
---|
79 | if(0 == (i % 100)) sout | i; |
---|
80 | |
---|
81 | #if CFA_HAVE_LINUX_IO_URING_H |
---|
82 | wait( f ); |
---|
83 | #endif |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | // ----- Yielder ----- |
---|
88 | // Add some chaos into the mix |
---|
89 | thread Yielder {}; |
---|
90 | void ^?{}(Yielder &mutex ) {} |
---|
91 | void main(Yielder&) { |
---|
92 | while(TIMES > __atomic_load_n(&counter, __ATOMIC_SEQ_CST)) { |
---|
93 | yield(); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | int main() { |
---|
99 | processor p; |
---|
100 | sout | "starting"; |
---|
101 | { |
---|
102 | Yielder y; |
---|
103 | Spinner s; |
---|
104 | Submitter io; |
---|
105 | } |
---|
106 | sout | "done"; |
---|
107 | } |
---|