Skip to contentSkip to conversation

cooker: Track directories searched for bbappend/bb files

PATCH New Richard Purdie 1 participant 4 messages Aug 9, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 cooker: Track directories searched for bbappend/bb files

Some of the directories searched by BBFILES are not currently being added to the inotify watch list. This can mean that added append files are not noticed leading to misleading metadata results when using BB_SERVER_TIMEOUT != 0.

We use glob to expand the BBFILES references and without writing our own implentation, figuring out the directories it searches is hard. We use some horrible hacks here to intecept the listdir calls, I'm open to better ways to handle this but this does ensure we have the right watches set.

---
 lib/bb/cooker.py | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
lib/bb/cooker.py +21 -3
index 3740c61..ea4df26 100644
@@ -1192,7 +1192,7 @@ class BBCooker:
             bf = os.path.abspath(bf)
 
         self.collection = CookerCollectFiles(self.bbfile_config_priorities)
-        filelist, masked = self.collection.collect_bbfiles(self.data, self.data)
+        filelist, masked, searchdirs = self.collection.collect_bbfiles(self.data, self.data)
         try:
             os.stat(bf)
             bf = os.path.abspath(bf)
@@ -1482,7 +1482,11 @@ class BBCooker:
                     self.recipecaches[mc].ignored_dependencies.add(dep)
 
             self.collection = CookerCollectFiles(self.bbfile_config_priorities)
-            (filelist, masked) = self.collection.collect_bbfiles(self.data, self.data)
+            (filelist, masked, searchdirs) = self.collection.collect_bbfiles(self.data, self.data)
+
+            # Add inotify watches for directories searched for bb/bbappend files
+            for dirent in searchdirs:
+                self.add_filewatch([[dirent]])
 
             self.parser = CookerParser(self, filelist, masked)
             self.parsecache_valid = True
@@ -1654,6 +1658,18 @@ class CookerCollectFiles(object):
             collectlog.error("no recipe files to build, check your BBPATH and BBFILES?")
             bb.event.fire(CookerExit(), eventdata)
 
+        # We need to track where we look so that we can add inotify watches. There
+        # is no nice way to do this, this is horrid. We intercept the os.listdir()
+        # calls while we run glob().
+        origlistdir = os.listdir
+        searchdirs = []
+
+        def ourlistdir(d):
+            searchdirs.append(d)
+            return origlistdir(d)
+
+        os.listdir = ourlistdir
+
         # Can't use set here as order is important
         newfiles = []
         for f in files:
@@ -1671,6 +1687,8 @@ class CookerCollectFiles(object):
                     if g not in newfiles:
                         newfiles.append(g)
 
+        os.listdir = origlistdir
+
         bbmask = config.getVar('BBMASK')
 
         if bbmask:
@@ -1729,7 +1747,7 @@ class CookerCollectFiles(object):
                 topfile = bbfile_seen[base]
                 self.overlayed[topfile].append(f)
 
-        return (bbfiles, masked)
+        return (bbfiles, masked, searchdirs)
 
     def get_file_appends(self, fn):
         """
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/4 cooker: Ensure we handle inotify before running the next command

The inotify watch events are handled "at idle" which could in theory mean a command could run before any preceeding inotify events have been processed. This leads to a theoretical race window where those events may have a signficicant effect on the command.

Add a mechanism to allow us to ensure all pending events are processed before running commands.

---
 lib/bb/command.py |  2 ++
 lib/bb/cooker.py  | 17 ++++++++++-------
 2 files changed, 12 insertions(+), 7 deletions(-)
lib/bb/command.py +2 -0
index c44c7a6..a923245 100644
@@ -78,6 +78,7 @@ class Command:
                 if not hasattr(command_method, 'readonly') or False == getattr(command_method, 'readonly'):
                     return None, "Not able to execute not readonly commands in readonly mode"
             try:
+                self.cooker.process_inotify_updates()
                 if getattr(command_method, 'needconfig', False):
                     self.cooker.updateCacheSync()
                 result = command_method(self, commandline)
@@ -98,6 +99,7 @@ class Command:
 
     def runAsyncCommand(self):
         try:
+            self.cooker.process_inotify_updates()
             if self.cooker.state in (bb.cooker.state.error, bb.cooker.state.shutdown, bb.cooker.state.forceshutdown):
                 # updateCache will trigger a shutdown of the parser
                 # and then raise BBHandledException triggering an exit
lib/bb/cooker.py +10 -7
index ea4df26..77b7f3d 100644
@@ -205,15 +205,11 @@ class BBCooker:
 
         self.inotify_modified_files = []
 
-        def _process_inotify_updates(server, notifier_list, abort):
-            for n in notifier_list:
-                if n.check_events(timeout=0):
-                    # read notified events and enqeue them
-                    n.read_events()
-                    n.process_events()
+        def _process_inotify_updates(server, cooker, abort):
+            cooker.process_inotify_updates()
             return 1.0
 
-        self.configuration.server_register_idlecallback(_process_inotify_updates, [self.confignotifier, self.notifier])
+        self.configuration.server_register_idlecallback(_process_inotify_updates, self)
 
         # TOSTOP must not be set or our children will hang when they output
         try:
@@ -241,6 +237,13 @@ class BBCooker:
             os.write(readypipe, b"ready")
             os.close(readypipe)
 
+    def process_inotify_updates(self):
+        for n in [self.confignotifier, self.notifier]:
+            if n.check_events(timeout=0):
+                # read notified events and enqeue them
+                n.read_events()
+                n.process_events()
+
     def config_notifications(self, event):
         if event.maskname == "IN_Q_OVERFLOW":
             bb.warn("inotify event queue overflowed, invalidating caches.")
Signature · 1 line
2.7.4
#2 lore raw

PATCH 3/4 cooker: Improve inotify overflow handling

In reply to Richard Purdie

Add a proper function for clearing the mtime cache. Clean up the inotify event overflow case to err on the side of caution and clear any potentially now out of sync caches.

---
 lib/bb/cooker.py         | 5 ++++-
 lib/bb/parse/__init__.py | 4 ++++
 2 files changed, 8 insertions(+), 1 deletion(-)
lib/bb/cooker.py +4 -1
index 77b7f3d..b3d6cd9 100644
@@ -189,7 +189,7 @@ class BBCooker:
 
         # If being called by something like tinfoil, we need to clean cached data
         # which may now be invalid
-        bb.parse.__mtime_cache = {}
+        bb.parse.clear_cache()
         bb.parse.BBHandler.cached_statements = {}
 
         self.ui_cmdline = None
@@ -247,7 +247,9 @@ class BBCooker:
     def config_notifications(self, event):
         if event.maskname == "IN_Q_OVERFLOW":
             bb.warn("inotify event queue overflowed, invalidating caches.")
+            self.parsecache_valid = False
             self.baseconfig_valid = False
+            bb.parse.clear_cache()
             return
         if not event.pathname in self.configwatcher.bbwatchedfiles:
             return
@@ -259,6 +261,7 @@ class BBCooker:
         if event.maskname == "IN_Q_OVERFLOW":
             bb.warn("inotify event queue overflowed, invalidating caches.")
             self.parsecache_valid = False
+            bb.parse.clear_cache()
             return
         if not event.pathname in self.inotify_modified_files:
             self.inotify_modified_files.append(event.pathname)
lib/bb/parse/__init__.py +4 -0
index a2952ec..2fc4002 100644
@@ -84,6 +84,10 @@ def update_cache(f):
         logger.debug(1, "Updating mtime cache for %s" % f)
         update_mtime(f)
 
+def clear_cache():
+    global __mtime_cache
+    __mtime_cache = {}
+
 def mark_dependency(d, f):
     if f.startswith('./'):
         f = "%s/%s" % (os.getcwd(), f[2:])
Signature · 1 line
2.7.4
#3 lore raw

PATCH 4/4 cooker: Ignore common bitbake files for the parse cache invalidation

In reply to Richard Purdie

Writes to the cookerdaemon log and/or the lockfile were meaning the parser cache was always being invalidated and reparsed. This is unnecessary so spot accesses to these two common cases and ignore the files from a reparse perspective.

This doesn't remove many sources of reparse but does improve several common cases.

---
 lib/bb/cooker.py | 3 +++
 1 file changed, 3 insertions(+)
lib/bb/cooker.py +3 -0
index b3d6cd9..db034b9 100644
@@ -263,6 +263,9 @@ class BBCooker:
             self.parsecache_valid = False
             bb.parse.clear_cache()
             return
+        if event.pathname.endswith("bitbake-cookerdaemon.log") \
+                or event.pathname.endswith("bitbake.lock"):
+            return
         if not event.pathname in self.inotify_modified_files:
             self.inotify_modified_files.append(event.pathname)
         self.parsecache_valid = False
Signature · 1 line
2.7.4