gh-116622: Android logging fixes (#122698) · python/cpython@b0c48b8 · GitHub
Skip to content

Commit b0c48b8

Browse files
authored
gh-116622: Android logging fixes (#122698)
Modifies the handling of stdout/stderr redirection on Android to accomodate the rate and buffer size limits imposed by Android's logging infrastructure.
1 parent 35ae4aa commit b0c48b8

3 files changed

Lines changed: 213 additions & 48 deletions

File tree

Lib/_android_support.py

Lines changed: 100 additions & 28 deletions

Lib/test/test_android.py

Lines changed: 112 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import io
12
import platform
23
import queue
34
import re
45
import subprocess
56
import sys
67
import unittest
8+
from _android_support import TextLogStream
79
from array import array
8-
from contextlib import contextmanager
10+
from contextlib import ExitStack, contextmanager
911
from threading import Thread
1012
from test.support import LOOPBACK_TIMEOUT
11-
from time import time
13+
from time import sleep, time
14+
from unittest.mock import patch
1215

1316

1417
if sys.platform != "android":
@@ -81,18 +84,39 @@ def unbuffered(self, stream):
8184
finally:
8285
stream.reconfigure(write_through=False)
8386

87+
# In --verbose3 mode, sys.stdout and sys.stderr are captured, so we can't
88+
# test them directly. Detect this mode and use some temporary streams with
89+
# the same properties.
90+
def stream_context(self, stream_name, level):
91+
# https://developer.android.com/ndk/reference/group/logging
92+
prio = {"I": 4, "W": 5}[level]
93+
94+
stack = ExitStack()
95+
stack.enter_context(self.subTest(stream_name))
96+
stream = getattr(sys, stream_name)
97+
if isinstance(stream, io.StringIO):
98+
stack.enter_context(
99+
patch(
100+
f"sys.{stream_name}",
101+
TextLogStream(
102+
prio, f"python.{stream_name}", errors="backslashreplace"
103+
),
104+
)
105+
)
106+
return stack
107+
84108
def test_str(self):
85109
for stream_name, level in [("stdout", "I"), ("stderr", "W")]:
86-
with self.subTest(stream=stream_name):
110+
with self.stream_context(stream_name, level):
87111
stream = getattr(sys, stream_name)
88112
tag = f"python.{stream_name}"
89113
self.assertEqual(f"<TextLogStream '{tag}'>", repr(stream))
90114

91-
self.assertTrue(stream.writable())
92-
self.assertFalse(stream.readable())
115+
self.assertIs(stream.writable(), True)
116+
self.assertIs(stream.readable(), False)
93117
self.assertEqual("UTF-8", stream.encoding)
94-
self.assertTrue(stream.line_buffering)
95-
self.assertFalse(stream.write_through)
118+
self.assertIs(stream.line_buffering, True)
119+
self.assertIs(stream.write_through, False)
96120

97121
# stderr is backslashreplace by default; stdout is configured
98122
# that way by libregrtest.main.
@@ -147,6 +171,13 @@ def write(s, lines=None, *, write_len=None):
147171
write("f\n\ng", ["exxf", ""])
148172
write("\n", ["g"])
149173

174+
# Since this is a line-based logging system, line buffering
175+
# cannot be turned off, i.e. a newline always causes a flush.
176+
stream.reconfigure(line_buffering=False)
177+
self.assertIs(stream.line_buffering, True)
178+
179+
# However, buffering can be turned off completely if you want a
180+
# flush after every write.
150181
with self.unbuffered(stream):
151182
write("\nx", ["", "x"])
152183
write("\na\n", ["", "a"])
@@ -209,30 +240,30 @@ def __str__(self):
209240
# (MAX_BYTES_PER_WRITE).
210241
#
211242
# ASCII (1 byte per character)
212-
write(("foobar" * 700) + "\n",
213-
[("foobar" * 666) + "foob", # 4000 bytes
214-
"ar" + ("foobar" * 33)]) # 200 bytes
243+
write(("foobar" * 700) + "\n", # 4200 bytes in
244+
[("foobar" * 666) + "foob", # 4000 bytes out
245+
"ar" + ("foobar" * 33)]) # 200 bytes out
215246

216247
# "Full-width" digits 0-9 (3 bytes per character)
217248
s = "\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18\uff19"
218-
write((s * 150) + "\n",
219-
[s * 100, # 3000 bytes
220-
s * 50]) # 1500 bytes
249+
write((s * 150) + "\n", # 4500 bytes in
250+
[s * 100, # 3000 bytes out
251+
s * 50]) # 1500 bytes out
221252

222253
s = "0123456789"
223-
write(s * 200, [])
224-
write(s * 150, [])
225-
write(s * 51, [s * 350]) # 3500 bytes
226-
write("\n", [s * 51]) # 510 bytes
254+
write(s * 200, []) # 2000 bytes in
255+
write(s * 150, []) # 1500 bytes in
256+
write(s * 51, [s * 350]) # 510 bytes in, 3500 bytes out
257+
write("\n", [s * 51]) # 0 bytes in, 510 bytes out
227258

228259
def test_bytes(self):
229260
for stream_name, level in [("stdout", "I"), ("stderr", "W")]:
230-
with self.subTest(stream=stream_name):
261+
with self.stream_context(stream_name, level):
231262
stream = getattr(sys, stream_name).buffer
232263
tag = f"python.{stream_name}"
233264
self.assertEqual(f"<BinaryLogStream '{tag}'>", repr(stream))
234-
self.assertTrue(stream.writable())
235-
self.assertFalse(stream.readable())
265+
self.assertIs(stream.writable(), True)
266+
self.assertIs(stream.readable(), False)
236267

237268
def write(b, lines=None, *, write_len=None):
238269
if write_len is None:
@@ -330,3 +361,64 @@ def write(b, lines=None, *, write_len=None):
330361
fr"{type(obj).__name__}"
331362
):
332363
stream.write(obj)
364+
365+
def test_rate_limit(self):
366+
# https://cs.android.com/android/platform/superproject/+/android-14.0.0_r1:system/logging/liblog/include/log/log_read.h;l=39
367+
PER_MESSAGE_OVERHEAD = 28
368+
369+
# https://developer.android.com/ndk/reference/group/logging
370+
ANDROID_LOG_DEBUG = 3
371+
372+
# To avoid flooding the test script output, use a different tag rather
373+
# than stdout or stderr.
374+
tag = "python.rate_limit"
375+
stream = TextLogStream(ANDROID_LOG_DEBUG, tag)
376+
377+
# Make a test message which consumes 1 KB of the logcat buffer.
378+
message = "Line {:03d} "
379+
message += "." * (
380+
1024 - PER_MESSAGE_OVERHEAD - len(tag) - len(message.format(0))
381+
) + "\n"
382+
383+
# See _android_support.py. The default values of these parameters work
384+
# well across a wide range of devices, but we'll use smaller values to
385+
# ensure a quick and reliable test that doesn't flood the log too much.
386+
MAX_KB_PER_SECOND = 100
387+
BUCKET_KB = 10
388+
with (
389+
patch("_android_support.MAX_BYTES_PER_SECOND", MAX_KB_PER_SECOND * 1024),
390+
patch("_android_support.BUCKET_SIZE", BUCKET_KB * 1024),
391+
):
392+
# Make sure the token bucket is full.
393+
sleep(BUCKET_KB / MAX_KB_PER_SECOND)
394+
line_num = 0
395+
396+
# Write BUCKET_KB messages, and return the rate at which they were
397+
# accepted in KB per second.
398+
def write_bucketful():
399+
nonlocal line_num
400+
start = time()
401+
max_line_num = line_num + BUCKET_KB
402+
while line_num < max_line_num:
403+
stream.write(message.format(line_num))
404+
line_num += 1
405+
return BUCKET_KB / (time() - start)
406+
407+
# The first bucketful should be written with minimal delay. The
408+
# factor of 2 here is not arbitrary: it verifies that the system can
409+
# write fast enough to empty the bucket within two bucketfuls, which
410+
# the next part of the test depends on.
411+
self.assertGreater(write_bucketful(), MAX_KB_PER_SECOND * 2)
412+
413+
# Write another bucketful to empty the token bucket completely.
414+
write_bucketful()
415+
416+
# The next bucketful should be written at the rate limit.
417+
self.assertAlmostEqual(
418+
write_bucketful(), MAX_KB_PER_SECOND,
419+
delta=MAX_KB_PER_SECOND * 0.1
420+
)
421+
422+
# Once the token bucket refills, we should go back to full speed.
423+
sleep(BUCKET_KB / MAX_KB_PER_SECOND)
424+
self.assertGreater(write_bucketful(), MAX_KB_PER_SECOND * 2)
Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)