Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/procfs_maxsize.exp

raw
1 # Handle varying sizes of procfs buffers.
2
3 set test "PROCFS_BUFFER"
4 if {![installtest_p]} { untested $test; return }
5
6 proc proc_read_value { test path} {
7 set value "<unknown>"
8 if [catch {open $path RDONLY} channel] {
9 fail "$test $channel"
10 } else {
11 set value [read -nonewline $channel]
12 close $channel
13 pass "$test read $value"
14 }
15 return $value
16 }
17
18 proc proc_write_value { test path value } {
19 if [catch {open $path WRONLY} channel] {
20 fail "$test $channel"
21 } else {
22 puts -nonewline $channel $value
23 close $channel
24 pass "$test wrote $value"
25 }
26 }
27
28 set fullstring "1bcdefghijklmno2BCDEFGHIJKLMNO3qrstuvwxyz12344QRSTUVWXYZ12345bcdefghijklmno6BCDEFGHIJKLMNO7qrstuvwxyz12348QRSTUVWXYZ12340BCDEFGHIJKLMNO1qrstuvwxyz12342QRSTUVWXYZ1234"
29
30 proc proc_read_write_counted { test maxsize } {
31 global fullstring
32 set path "/proc/systemtap/$test/command"
33
34 # Read the file
35 set value [proc_read_value $test $path]
36
37 set value2 [string range $fullstring 0 [expr $maxsize - 2]]
38 if { $value == $value2 } {
39 pass "$test received correct initial value maxsize: $maxsize"
40 } else {
41 fail "$test received incorrect initial value: $value ($value2)"
42 }
43
44 # Write a new value.
45 proc_write_value $test $path $fullstring
46
47 return 0;
48 }
49
50 # Expect 80 byte return from reading procfs file.
51 proc proc_read_write_80 {} {
52 global test
53 proc_read_write_counted $test 80
54 }
55
56 # Expect 64 byte return from reading procfs file.
57 proc proc_read_write_64 {} {
58 global test
59 proc_read_write_counted $test 64
60 }
61
62 set script1 {
63 global values[10]
64 global idx = 0
65
66 probe procfs.read {
67 try {
68 $value = "1bcdefghijklmno"
69 $value .= "2BCDEFGHIJKLMNO"
70 $value .= "3qrstuvwxyz1234"
71 $value .= "4QRSTUVWXYZ1234"
72 $value .= "5bcdefghijklmno"
73 $value .= "6BCDEFGHIJKLMNO"
74 $value .= "7qrstuvwxyz1234"
75 $value .= "8QRSTUVWXYZ1234"
76 $value .= "9bcdefghijklmno"
77 $value .= "0BCDEFGHIJKLMNO"
78 $value .= "1qrstuvwxyz1234"
79 $value .= "2QRSTUVWXYZ1234"
80 } catch {
81 }
82 }
83 probe procfs.write {
84 values[idx] = $value
85 idx++
86 }
87
88 probe begin {
89 printf("systemtap starting probe\n")
90 }
91 probe end {
92 printf("systemtap ending probe\n")
93 for (i = 0; i < idx; i++) {
94 printf("value%d=%s\n", i, values[i])
95 }
96 }
97 }
98
99 # Output strings in 64 and 80 byte chunks.
100 set output_string_64 "value0=1bcdefghijklmno2BCDEFGHIJKLMNO3qrstuvwxyz12344QRSTUVWXYZ12345bc\r\nvalue1=defghijklmno6BCDEFGHIJKLMNO7qrstuvwxyz12348QRSTUVWXYZ12340BCDEF\r\nvalue2=GHIJKLMNO1qrstuvwxyz12342QRSTUVWXYZ1234\r\n"
101
102 set output_string_80 "value0=1bcdefghijklmno2BCDEFGHIJKLMNO3qrstuvwxyz12344QRSTUVWXYZ12345bcdefghijklmno6BCD\r\nvalue1=EFGHIJKLMNO7qrstuvwxyz12348QRSTUVWXYZ12340BCDEFGHIJKLMNO1qrstuvwxyz12342QRSTUVW\r\nvalue2=XYZ1234\r\n"
103
104 # By default, setting MAXSTRINGLEN should set the size of
105 # procfs read and write buffers.
106 set test "PROCFS_BUFFER1"
107 stap_run $test proc_read_write_80 $output_string_80 -DMAXSTRINGLEN=80 -e $script1 -m $test
108 exec /bin/rm -f ${test}.ko
109
110 # Try with MAXSTRINGLEN=80 and STP_PROCFS_BUFSIZE=64
111 set test "PROCFS_BUFFER2"
112 stap_run $test proc_read_write_64 $output_string_64 -DMAXSTRINGLEN=80 -DSTP_PROCFS_BUFSIZE=64 -e $script1 -m $test
113 exec /bin/rm -f ${test}.ko
114
115 # Try with MAXSTRINGLEN=64 and STP_PROCFS_BUFSIZE=80. Note that in
116 # this case, even though the procfs buffer is 80 bytes in size, since
117 # strings can only hold 64 bytes, the expected output is the same as
118 # in the previous test.
119 set test "PROCFS_BUFFER3"
120 stap_run $test proc_read_write_80 $output_string_64 -DMAXSTRINGLEN=64 -DSTP_PROCFS_BUFSIZE=80 -e $script1 -m $test
121 exec /bin/rm -f ${test}.ko
122
123 # script2 has a '.maxsize(80). override
124 set script2 {
125 global values[10]
126 global idx = 0
127
128 probe procfs.read.maxsize(80) {
129 try {
130 $value = "1bcdefghijklmno"
131 $value .= "2BCDEFGHIJKLMNO"
132 $value .= "3qrstuvwxyz1234"
133 $value .= "4QRSTUVWXYZ1234"
134 $value .= "5bcdefghijklmno"
135 $value .= "6BCDEFGHIJKLMNO"
136 $value .= "7qrstuvwxyz1234"
137 $value .= "8QRSTUVWXYZ1234"
138 $value .= "9bcdefghijklmno"
139 $value .= "0BCDEFGHIJKLMNO"
140 $value .= "1qrstuvwxyz1234"
141 $value .= "2QRSTUVWXYZ1234"
142 } catch {
143 }
144 }
145 probe procfs.write {
146 values[idx] = $value
147 idx++
148 }
149
150 probe begin {
151 printf("systemtap starting probe\n")
152 }
153 probe end {
154 printf("systemtap ending probe\n")
155 for (i = 0; i < idx; i++) {
156 printf("value%d=%s\n", i, values[i])
157 }
158 }
159 }
160
161 # By default, setting MAXSTRINGLEN should set the size of
162 # procfs read and write buffers. But script2 has a '.maxsize(80)'
163 # override.
164 set test "PROCFS_BUFFER4"
165 stap_run $test proc_read_write_80 $output_string_64 -DMAXSTRINGLEN=64 -e $script2 -m $test
166 exec /bin/rm -f ${test}.ko
167
168 # The script2 '.maxsize(80)' override should override even setting
169 # STP_PROCFS_BUFSIZE.
170 set test "PROCFS_BUFFER5"
171 stap_run $test proc_read_write_80 $output_string_64 -DMAXSTRINGLEN=64 -DSTP_PROCFS_BUFSIZE=80 -e $script2 -m $test
172 exec /bin/rm -f ${test}.ko
173
174 # script3 has a maxsize override and a default size file
175 set script3 {
176 global values[10]
177 global idx = 0
178
179 probe procfs("default").read {
180 try {
181 $value = "1bcdefghijklmno"
182 $value .= "2BCDEFGHIJKLMNO"
183 $value .= "3qrstuvwxyz1234"
184 $value .= "4QRSTUVWXYZ1234"
185 $value .= "5bcdefghijklmno"
186 $value .= "6BCDEFGHIJKLMNO"
187 $value .= "7qrstuvwxyz1234"
188 $value .= "8QRSTUVWXYZ1234"
189 $value .= "9bcdefghijklmno"
190 $value .= "0BCDEFGHIJKLMNO"
191 $value .= "1qrstuvwxyz1234"
192 $value .= "2QRSTUVWXYZ1234"
193 } catch {
194 }
195 }
196 probe procfs.read.maxsize(80) {
197 try {
198 $value = "1bcdefghijklmno"
199 $value .= "2BCDEFGHIJKLMNO"
200 $value .= "3qrstuvwxyz1234"
201 $value .= "4QRSTUVWXYZ1234"
202 $value .= "5bcdefghijklmno"
203 $value .= "6BCDEFGHIJKLMNO"
204 $value .= "7qrstuvwxyz1234"
205 $value .= "8QRSTUVWXYZ1234"
206 $value .= "9bcdefghijklmno"
207 $value .= "0BCDEFGHIJKLMNO"
208 $value .= "1qrstuvwxyz1234"
209 $value .= "2QRSTUVWXYZ1234"
210 } catch {
211 }
212 }
213 probe procfs.write {
214 values[idx] = $value
215 idx++
216 }
217
218 probe begin {
219 printf("systemtap starting probe\n")
220 }
221 probe end {
222 printf("systemtap ending probe\n")
223 for (i = 0; i < idx; i++) {
224 printf("value%d=%s\n", i, values[i])
225 }
226 }
227 }
228
229 proc proc_read_write_default_80 {} {
230 global test
231 global fullstring
232
233 set path "/proc/systemtap/$test/default"
234 set value2 [string range $fullstring 0 62]
235
236 # Read the file
237 set value [proc_read_value $test $path]
238 if { $value == $value2 } {
239 pass "$test received correct initial value"
240 } else {
241 fail "$test received incorrect initial value: $value ($value2)"
242 }
243
244 proc_read_write_counted $test 80
245 }
246
247 set test "PROCFS_BUFFER6"
248 stap_run $test proc_read_write_default_80 $output_string_64 -DMAXSTRINGLEN=64 -e $script3 -m $test
249 exec /bin/rm -f ${test}.ko
250
251 set script4 {
252 global large_strings[10]
253
254 # Put more data than we can really handle into $value
255 probe procfs.read.maxsize(2048) {
256 try {
257 $value = large_strings[0]
258 for (i = 1; i < 10; i+=1) {
259 $value .= large_strings[i]
260 }
261 } catch {
262 }
263 }
264
265 probe begin {
266 # build up several maximum length strings
267 max = 512
268 for (i = 0; i < max/64; i+=1) {
269 for (j = 0; j < 10; j++) {
270 if (i < (max/64 - 1)) {
271 large_strings[j] .= sprintf("%3d:12345678901234567890123456789012345678901234567890123456789\n",
272 i + (j * (max/64)))
273 }
274 else {
275 large_strings[j] .= sprintf("%3d:1234567890123456789012345678901234567890123456789012345678\n",
276 i + (j * (max/64)))
277 }
278 }
279 }
280 printf("systemtap starting probe\n")
281 }
282 probe end {
283 printf("systemtap ending probe\n")
284 }
285 }
286
287 proc proc_read_big {} {
288 global test
289 global test_string
290
291 set path "/proc/systemtap/$test/command"
292
293 # Read the file
294 set value [proc_read_value $test $path]
295
296 # set value2 [string range $fullstring 0 [expr $maxsize - 2]]
297 if { $value == $test_string } {
298 pass "$test received correct initial value"
299 } else {
300 fail "$test received incorrect initial value: $value ($test_string)"
301 }
302 return 0;
303 }
304
305 set test_string \
306 " 0:12345678901234567890123456789012345678901234567890123456789
307 1:12345678901234567890123456789012345678901234567890123456789
308 2:12345678901234567890123456789012345678901234567890123456789
309 3:12345678901234567890123456789012345678901234567890123456789
310 4:12345678901234567890123456789012345678901234567890123456789
311 5:12345678901234567890123456789012345678901234567890123456789
312 6:12345678901234567890123456789012345678901234567890123456789
313 7:1234567890123456789012345678901234567890123456789012345678
314 8:12345678901234567890123456789012345678901234567890123456789
315 9:12345678901234567890123456789012345678901234567890123456789
316 10:12345678901234567890123456789012345678901234567890123456789
317 11:12345678901234567890123456789012345678901234567890123456789
318 12:12345678901234567890123456789012345678901234567890123456789
319 13:12345678901234567890123456789012345678901234567890123456789
320 14:12345678901234567890123456789012345678901234567890123456789
321 15:1234567890123456789012345678901234567890123456789012345678
322 16:12345678901234567890123456789012345678901234567890123456789
323 17:12345678901234567890123456789012345678901234567890123456789
324 18:12345678901234567890123456789012345678901234567890123456789
325 19:12345678901234567890123456789012345678901234567890123456789
326 20:12345678901234567890123456789012345678901234567890123456789
327 21:12345678901234567890123456789012345678901234567890123456789
328 22:12345678901234567890123456789012345678901234567890123456789
329 23:1234567890123456789012345678901234567890123456789012345678
330 24:12345678901234567890123456789012345678901234567890123456789
331 25:12345678901234567890123456789012345678901234567890123456789
332 26:12345678901234567890123456789012345678901234567890123456789
333 27:12345678901234567890123456789012345678901234567890123456789
334 28:12345678901234567890123456789012345678901234567890123456789
335 29:12345678901234567890123456789012345678901234567890123456789
336 30:12345678901234567890123456789012345678901234567890123456789
337 31:1234567890123456789012345678901234567890123456789012345678
338 32"
339
340 # Test outputting 2K worth of data in one shot.
341 set test "PROCFS_BUFFER7"
342 stap_run $test proc_read_big "" -DMAXSTRINGLEN=512 -e $script4 -m $test
343 exec /bin/rm -f ${test}.ko
344
345 # Expect 72 byte return from reading procfs file.
346 proc proc_read_write_72 {} {
347 global test
348 proc_read_write_counted $test 72
349 }
350
351 set script5 {
352 global error
353
354 probe procfs.read {
355 try {
356 $value = "1bcdefghijklmno"
357 $value .= "2BCDEFGHIJKLMNO"
358 $value .= "3qrstuvwxyz1234"
359 $value .= "4QRSTUVWXYZ1234"
360 $value .= "5bcdefghijk"
361 } catch (err) {
362 error = err
363 }
364 }
365 probe procfs.write {
366 next
367 }
368
369 probe begin {
370 printf("systemtap starting probe\n")
371 }
372 probe end {
373 printf("systemtap ending probe\n")
374 println(error)
375 }
376 }
377
378 set overflow_string "buffer size exceeded, consider .maxsize\\(72\\).\r\n"
379
380 # Buffer too small
381 set test "PROCFS_BUFFER8"
382 stap_run $test proc_read_write_64 $overflow_string -DSTP_PROCFS_BUFSIZE=64 -e $script5 -m $test
383 exec /bin/rm -f ${test}.ko
384
385 # Buffer is exactly large enough to hold contents
386 set test "PROCFS_BUFFER9"
387 stap_run $test proc_read_write_72 "\r\n" -DSTP_PROCFS_BUFSIZE=72 -e $script5 -m $test
388 exec /bin/rm -f ${test}.ko