Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/lock-pushdown.stp

raw
1 #! stap -gp5
2
3
4 @define assert_lock(e,v) %( if (%{ c->locked %} != @v) { warn(sprintf("%s, locked mismatch, expected %d, actual %d", @e, @v, %{ c->locked %})) } %)
5
6 // for use in probes that don't lock at all
7 @define assert_never_locked(e) %( @assert_lock(@e,0) %)
8
9 // for use in probes that deal with globals
10 // PR26296: in version <= 4.3, this locking took place always in probe prologue/epilogue,
11 // so the c->locked value was always 1. In version >4.4, this locking is pushed down
12 // in the control flow to be as tight as possible.
13 @define assert_pre_locked(e) %( @assert_lock(@e, %( systemtap_v <= "4.3" %? 1 %: 0 %) ) %)
14 @define assert_locked(e) %( @assert_lock(@e, 1 ) %)
15 @define assert_un_locked(e) %( @assert_lock(@e, %( systemtap_v <= "4.3" %? 1 %: 2 %) ) %)
16
17
18 function used(v:long) %{ %} // prevent given value/variable from being optimized away
19
20
21
22 global a = 0
23 global b
24 global c = 0
25
26
27 probe any = timer.s(1) {}
28
29 probe timer.s(2) { exit() }
30 probe never { used(b[4]) } // so probe-end doesn't auto-print
31
32
33 probe begin // begin probes don't lock due to no concurrency
34 {
35 @assert_never_locked("begin.1")
36 a = 5
37 @assert_never_locked("begin.2")
38 b[5] = 0
39 @assert_never_locked("begin.3")
40 println("begin pass")
41 }
42
43
44 // test straight-through pushdown; it should enclose just middle few statements
45
46 probe any
47 {
48 localvar = 5
49 @assert_pre_locked("straight.1")
50 a = 5
51 @assert_locked("straight.2")
52 a = 6
53 @assert_un_locked("straight.3")
54 localvar = 6
55 used(localvar)
56 @assert_un_locked("straight.4")
57 println("straight pass")
58 }
59
60
61 // test if/else pushdown; it should enclose sections of the then/else blocks only
62
63 probe any
64 {
65 @assert_pre_locked("if.1")
66 if (uid() == 0)
67 {
68 @assert_pre_locked("if.2")
69 a++
70 @assert_locked("if.3")
71 a = 5
72 @assert_un_locked("if.4")
73 }
74 else
75 {
76 @assert_pre_locked("if.5")
77 b[a] = 4
78 @assert_un_locked("if.6")
79 }
80 @assert_un_locked("if.7")
81 println("if pass")
82 }
83
84
85
86 probe any
87 {
88 @assert_pre_locked("if2.1")
89 if (uid()==0) {
90 @assert_pre_locked("if2.2")
91 a++
92 @assert_un_locked("if2.3")
93 used(localvar++)
94 @assert_un_locked("if2.4")
95 } else {
96 used(localvar++)
97 @assert_pre_locked("if2.5")
98 }
99 // could be either un_locked or pre_locked at this point, depending on condition
100 println("if2 pass")
101 }
102
103
104 // test probe-conditions; it should keep locked through end of probe handler,
105 // for evaluation of dependent probe condition expressions
106 probe any
107 {
108 @assert_pre_locked("cond.1")
109 c = 1
110 @assert_locked("cond.2")
111 localvar = 4
112 @assert_locked("cond.3")
113 used(localvar)
114 println("cond pass")
115 }
116
117
118 probe any if (c)
119 {
120 }
121
122
123 // while loops; locked around the whole loop body, whether or not
124 // conditional uses globals
125
126 probe any
127 {
128 @assert_pre_locked("while.1")
129 while(i++ < 10) {
130 @assert_locked("while.2")
131 a ++
132 @assert_locked("while.3")
133 continue
134 }
135 @assert_un_locked("while.4")
136 println("while pass")
137 }
138
139 probe any
140 {
141 @assert_pre_locked("while2.1")
142 while(a++ < 10) {
143 used(localvar++)
144 @assert_locked("while2.2")
145 break
146 }
147 @assert_un_locked("while2.3")
148 println("while2 pass")
149 }
150
151
152 // for loops; locked around the whole loop body, whether or not
153 // conditional uses globals
154
155 probe any
156 {
157 @assert_pre_locked("for.1")
158 for(i=0; i<10; i++) {
159 @assert_locked("for.2")
160 a ++
161 @assert_locked("for.3")
162 continue
163 }
164 @assert_un_locked("for.4")
165 println("for pass")
166 }
167
168 probe any
169 {
170 @assert_pre_locked("for2.1")
171 for(a=0; a<10; a++) {
172 used(localvar++)
173 @assert_locked("for2.2")
174 break
175 }
176 @assert_un_locked("for2.3")
177 println("for2 pass")
178 }
179
180
181 // foreach loops; locked around the whole loop body; loop condition
182 // necessarily uses globals (iterating over a map)
183
184 probe any
185 {
186 @assert_pre_locked("foreach.1")
187 foreach (i in b) {
188 @assert_locked("foreach.2")
189 continue
190 }
191 @assert_un_locked("foreach.3")
192 println("foreach pass")
193 }
194
195
196 // try statement: no lock pushdown at all
197
198 probe any
199 {
200 @assert_pre_locked("try.1")
201 try {
202 @assert_locked("try.2")
203 a++
204 @assert_locked("try.3")
205 used(1/0)
206 } catch {
207 @assert_locked("try.4")
208 }
209 @assert_un_locked("try.5")
210 println("try pass")
211 }
212
213
214 // delete statement
215
216 probe any
217 {
218 @assert_pre_locked("delete.1")
219 delete b[0]
220 @assert_un_locked("delete.2")
221 println("delete pass")
222 }
223
224
225 // next statement
226
227 probe any
228 {
229 @assert_un_locked("next.1")
230 if(uid()!=0)
231 next;
232 @assert_un_locked("next.1")
233 println("next pass")
234 }
235
236 // next statement in function mucking with global
237
238 function any2()
239 {
240 @assert_locked("next2.0")
241 a++
242 next
243 }
244
245 probe any
246 {
247 @assert_pre_locked("next2.1")
248 try {
249 @assert_locked("next2.2")
250 any2()
251 } catch {
252 @assert_locked("next2.3")
253 }
254 @assert_un_locked("next2.4")
255 println("next2 pass")
256 }