Multiplexed task log tail and dynamic log levels for knotty
cover Multiplexed task log tail and dynamic log levels for knotty
This patch set is intended to provide a semi real time aspect to the logging to stdout. Originally I was just interested in the output of the do_compile and I will still consider adding that capability, but I ended up using the logger generically. It provides the ability to see all the task logs multiplexed to stdout by PID.
More recently I decided I wanted to be able to toggle the logs on and off while bitbake was already running, so that was added into the series as well. After seeing how that worked, I also decided I wanted to be able to run the dev shell in a single terminal window. The last patch in the series is for the OE-Core, but since I made reference to this patch I included in this series for completeness.
As a foot note, I have actually been using a more primitive version of this patch set for the last 4 weeks that was controlled only by the local.conf setting and used it to diagnose quite a few different problems as well as to just understand what it is that bitbake is doing at times.
Thanks,
Jason.
PATCH 1/6 process.py: Fix log truncation problems
There are two problems with the _logged_communicate that are both caused as a result of buffering I/O, instead of flushing it out to the log files as it arrives.
1) log truncation when python dumps
I have seen the task logs missing data that was not flushed when bitbake crashes.
2) While a bitbake task is running it is impossible to see what is going on if it is only writing a small incremental log that is smaller than the buffer, or you get only a partial log, up until the task exists. It is worse in the case that stderr and stdout are separate file handles, because previous code blocks on the read of stdout and then stderr, serially.
The right approach is simply to use select() to determine if there is data available and then to flush the log buffers as they arrive. This still makes use of buffering in the cases where there is more than 1 byte of data, but the buffers are much more dynamic.
--- lib/bb/process.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
lib/bb/process.py +27 -2
@@ -1,6 +1,9 @@ import logging import signal import subprocess +import fcntl +import errno +import select logger = logging.getLogger('BitBake.Process')
@@ -70,18 +73,40 @@ def _logged_communicate(pipe, log, input): bufsize = 512 outdata, errdata = [], [] + rin = [] + + if pipe.stdout is not None: + fd = pipe.stdout.fileno() + fl = fcntl.fcntl(fd, fcntl.F_GETFL) + fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) + rin.append(pipe.stdout) + if pipe.stderr is not None: + fd = pipe.stderr.fileno() + fl = fcntl.fcntl(fd, fcntl.F_GETFL) + fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) + rin.append(pipe.stderr) + while pipe.poll() is None: - if pipe.stdout is not None: + rlist = rin + try: + r,w,e = select.select (rlist, [], []) + except OSError, e: + if e.errno != errno.EINTR: + raise + + if pipe.stdout in r: data = pipe.stdout.read(bufsize) if data is not None: outdata.append(data) log.write(data) + log.flush() - if pipe.stderr is not None: + if pipe.stderr in r: data = pipe.stderr.read(bufsize) if data is not None: errdata.append(data) log.write(data) + log.flush() return ''.join(outdata), ''.join(errdata) def run(cmd, input=None, log=None, **options):
Signature · 1 line
1.7.10
PATCH 2/6 knotty: Extend knotty to support "real time" log tail for tasks
In reply to Jason WesselThis patch provides the capability to knotty to have a higher log level where the individual tasks logs can be multiplexed to stdio. This is highly desirable for several different use cases.
1) You can visually spot "hot spots" in building parallel tasks
2) The relative order an sequence of events can be determined in difficult to diagnose inter-related library problems.
3) It provides a nice way to see what all is actually going on and getting logged by various tasks such as a kernel compile where you might want to actually look at the output in a build over build scenario.
4) There are most certainly other aspects where this is useful.
The primary control is to set the BB_RT_LOGLEVEL = "2" in your local.conf. Additional capabilities will follow in successive patches. When parallel tasks are enabled, the logs will be multiplexed and prefixed with the PID of the task. In the non-parallel case there is no PID prefix as everything is linear.
--- lib/bb/build.py | 4 ++ lib/bb/ui/crumbs/multilogtail.py | 135 ++++++++++++++++++++++++++++++++++++++ lib/bb/ui/knotty.py | 46 +++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 lib/bb/ui/crumbs/multilogtail.py
lib/bb/build.py +4 -0
@@ -88,6 +88,10 @@ class TaskBase(event.Event): class TaskStarted(TaskBase): """Task execution started""" + def __init__(self, t, d): + self.logfile = d.getVar("BB_LOGFILE", True) or ""; + super(TaskStarted, self).__init__(t, d) + class TaskSucceeded(TaskBase): """Task execution completed"""
lib/bb/ui/crumbs/multilogtail.py new file +135 -0
135 lines not on this page · raw message
lib/bb/ui/knotty.py +46 -0
85 lines not on this page · raw message
Signature · 1 line
1.7.10
PATCH 3/6 knotty: Add the ability to dynamically select loglevel from stdin
In reply to Jason WesselIf stdin is a controlling terminal, make it possible to dynamically select a log level with the keys 1 and 2.
1 = normal bitbake knotty logs
2 = realtime multiple tail of task logs
--- lib/bb/ui/knotty.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
lib/bb/ui/knotty.py +32 -0
@@ -108,6 +108,32 @@ class TerminalFilter(object): def finish(self): return +class StdinMgr: + def __init__(self): + self.stdinbackup = None + self.fd = None + if sys.stdin.isatty(): + self.fd = sys.stdin.fileno() + self.stdinbackup = termios.tcgetattr(self.fd) + new = termios.tcgetattr(self.fd) + new[3] = new[3] & ~termios.ICANON & ~termios.ECHO + termios.tcsetattr(self.fd, termios.TCSANOW, new) + + def poll(self): + if not self.stdinbackup: + return False + return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []) + + def restore(self): + if self.stdinbackup: + termios.tcsetattr(self.fd, termios.TCSADRAIN, self.stdinbackup) + self.stdinbackup = None + # Force echo back on "just in case" something went haywire with an exception + if sys.stdin.isatty(): + new = termios.tcgetattr(self.fd) + new[3] = new[3] | termios.ECHO + termios.tcsetattr(self.fd, termios.TCSANOW, new) + class RtLogLevel: def __init__(self, mlt): self.displaytail = False
@@ -184,11 +210,15 @@ def main(server, eventHandler, tf = TerminalFilter): taskfailures = [] termfilter = tf(main, helper, console, format) + stdin_mgr = StdinMgr() while True: try: termfilter.updateFooter() event = eventHandler.waitEvent(0.25) + if stdin_mgr.poll(): + keyinput = sys.stdin.read(1) + rtloglevel.setLevel(keyinput, True) # Always try printing any accumulated log files first rtloglevel.displayLogs() if event is None:
@@ -364,6 +394,7 @@ def main(server, eventHandler, tf = TerminalFilter): if ioerror.args[0] == 4: pass except KeyboardInterrupt: + stdin_mgr.restore() termfilter.clearFooter() if main.shutdown == 1: print("\nSecond Keyboard Interrupt, stopping...\n")
@@ -375,6 +406,7 @@ def main(server, eventHandler, tf = TerminalFilter): main.shutdown = main.shutdown + 1 pass + stdin_mgr.restore() summary = "" if taskfailures: summary += pluralise("\nSummary: %s task failed:",
Signature · 1 line
1.7.10
PATCH 4/6 msg.py, knotty.py: Allow dynamic toggle of the debug log level
In reply to Jason WesselIf stdin is a controlling tty, make it possible to have log levels 1-4 where you can press a key to dynamically change the log level, while bitbake is running to see what is going on.
1 = default log level
2 = multiple log tail of bitbake tasks
3 = bit bake debug messages
4 = bitbake debug messages + multiple lot tail of bitbake tasks
In order to make this possible a slight modification to the msg logfilter was needed to make it possible to get an object so as to change the filter log level.
--- lib/bb/msg.py | 9 ++++++++- lib/bb/ui/knotty.py | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-)
lib/bb/msg.py +8 -1
@@ -86,6 +86,13 @@ class BBLogFilter(object): handler.setLevel(loglevel) handler.addFilter(self) + def setFiltLevel(self, handler, level): + self.stdlevel = level + handler.setLevel(level) + + def getFiltLevel(self): + return self.stdlevel + def filter(self, record): if record.levelno >= self.stdlevel: return True
@@ -131,7 +138,7 @@ def addDefaultlogFilter(handler): dlevel = len(tuple(iterator)) debug_domains["BitBake.%s" % domainarg] = logging.DEBUG - dlevel + 1 - BBLogFilter(handler, level, debug_domains) + return BBLogFilter(handler, level, debug_domains) # # Message handling functions
lib/bb/ui/knotty.py +18 -3
@@ -135,8 +135,11 @@ class StdinMgr: termios.tcsetattr(self.fd, termios.TCSANOW, new) class RtLogLevel: - def __init__(self, mlt): + def __init__(self, handler, logfilter, mlt): self.displaytail = False + self.handler = handler + self.logfilter = logfilter + self.defaultLevel = logfilter.getFiltLevel() self.mlt = mlt def displayLogs(self):
@@ -147,10 +150,22 @@ class RtLogLevel: if input == "1": if verbose: print "NOTE: Turning off real time log tail" + self.logfilter.setFiltLevel(self.handler, self.defaultLevel) self.displaytail = False elif input == "2": if verbose: print "NOTE: Turning on real time log tail" + self.logfilter.setFiltLevel(self.handler, self.defaultLevel) + self.displaytail = True + elif input == "3": + if verbose: + print "NOTE: Turning on DEBUG logging" + self.logfilter.setFiltLevel(self.handler, logging.DEBUG) + self.displaytail = False + elif input == "4": + if verbose: + print "NOTE: Turning on DEBUG logging + real time log tail" + self.logfilter.setFiltLevel(self.handler, logging.DEBUG) self.displaytail = True def main(server, eventHandler, tf = TerminalFilter):
@@ -171,8 +186,8 @@ def main(server, eventHandler, tf = TerminalFilter): console = logging.StreamHandler(sys.stdout) format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") - bb.msg.addDefaultlogFilter(console) - rtloglevel = RtLogLevel(mlt) + logfilter = bb.msg.addDefaultlogFilter(console) + rtloglevel = RtLogLevel(console, logfilter, mlt) bb_rt_loglevel = server.runCommand(["getVariable", "BB_RT_LOGLEVEL"]) if bb_rt_loglevel and bb_rt_loglevel != "": rtloglevel.setLevel(bb_rt_loglevel, False)
Signature · 1 line
1.7.10
PATCH 5/6 knotty.py: Add the ability to spawn screen directly from knotty with OE_TERMINAL=screen_inline
In reply to Jason WesselThe oe-core has a change pending to add the screen_inline class. This change allows the simple use case of directly using a devshell from the controlling terminal that started bitbake which is a significant convience for some use cases.
--- lib/bb/ui/knotty.py | 11 +++++++++++ 1 file changed, 11 insertions(+)
lib/bb/ui/knotty.py +11 -0
@@ -174,6 +174,11 @@ def main(server, eventHandler, tf = TerminalFilter): includelogs = server.runCommand(["getVariable", "BBINCLUDELOGS"]) loglines = server.runCommand(["getVariable", "BBINCLUDELOGS_LINES"]) consolelogfile = server.runCommand(["getVariable", "BB_CONSOLELOG"]) + oeterminal = server.runCommand(["getVariable", "OE_TERMINAL"]) + if oeterminal == "screen_inline" and sys.stdin.isatty() and sys.stdout.isatty(): + runscreen = True + else: + runscreen = False # MultiTail affected varialbles numthreads = server.runCommand(["getVariable", "BB_NUMBER_THREADS"])
@@ -260,6 +265,12 @@ def main(server, eventHandler, tf = TerminalFilter): errors = errors + 1 return_value = 1 elif event.levelno == format.WARNING: + msg = event.getMessage() + if runscreen and msg.startswith("Screen Connect Command: "): + msg = msg[24:] + print "Starting terminal with: %s" % msg + os.system(msg) + continue warnings = warnings + 1 # For "normal" logging conditions, don't show note logs from tasks # but do show them if the user has changed the default log level to
Signature · 1 line
1.7.10
PATCH 6/6 meta/lib/oe/terminal.py: Add an additional reference to screen called screen_inline
In reply to Jason WesselThis is a new mode for screen with the explicit purpose of invoking screen on the same terminal that you started bitbake. The bitbake knotty will attempt to launch screen directly when using this setting for OE_TERMINAL.
--- meta/lib/oe/terminal.py | 7 +++++++ 1 file changed, 7 insertions(+)
meta/lib/oe/terminal.py +7 -0
@@ -100,6 +100,13 @@ class Screen(Terminal): logger.warn('Screen started. Please connect in another terminal with ' '"screen -r devshell"') +class Screen_inline(Terminal): + command = 'screen -D -m -t "{title}" -S devshell {command}' + + def __init__(self, command, title=None, env=None): + Terminal.__init__(self, command, title, env) + logger.warn('Screen Connect Command: screen -r devshell') + def prioritized(): return Registry.prioritized()
Signature · 1 line
1.7.10