Skip to contentSkip to conversation

bitbake memory resident improvements

RFC PATCH New Jason Wessel 1 participant 6 messages Nov 25, 2013

lore
Share & tools
lore
#0 lore raw

cover bitbake memory resident improvements

The idea with this patch set is to improve the way bitbake is started up with the memory resident server. The following were the goals of this patch set.

1) Allow switching between the memres and non memres mode by sourcing the environment scripts. Example:
bash
. oe-init-build-env-memres
exit
bash
. oe-init-build-env
exit

2) Do not hard code a port number for the default memres setup. Instead allow the port number to be setup dynamically so that multiple memres servers can be started in multiple directories by multiple users on the same system.

3) Start the bitbake memory resident server as needed.
* This allows lazy start up
* Restarts the server if it should exit after an inactivity timeout
NOTE: This patch set does not implement the inactivity timeout
but it does implement the recovery.
* If the server dies it is started automatically

Cheers,
Jason

#1 lore raw

PATCH 1/5 bitbake: Add --status-only option

The --status-only option is purely to check the health of the server. The idea is to use it from the oe environment script so as to know if the server needs to be started or not.

---
 bin/bitbake |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
bin/bitbake +14 -1
index 50971a8..cca2b8d 100755
@@ -190,6 +190,9 @@ class BitBakeConfigParameters(cookerdata.ConfigParameters):
         parser.add_option("", "--observe-only", help = "Connect to a server as an observing-only client.",
                    action = "store_true", dest = "observe_only", default = False)
 
+        parser.add_option("", "--status-only", help = "Check the status of the remote bitbake server.",
+                   action = "store_true", dest = "status_only", default = False)
+
         options, targets = parser.parse_args(sys.argv)
 
         # some environmental variables set also configuration options
@@ -284,7 +287,9 @@ def main():
 
     # Ensure logging messages get sent to the UI as events
     handler = bb.event.LogHandler()
-    logger.addHandler(handler)
+    if not configParams.status_only:
+        # In status only mode there are no logs and no UI
+        logger.addHandler(handler)
 
     # Clear away any spurious environment variables while we stoke up the cooker
     cleanedvars = bb.utils.clean_environment()
@@ -302,6 +307,14 @@ def main():
         # Collect the feature set for the UI
         featureset = getattr(ui_module, "featureSet", [])
 
+        if configParams.status_only:
+            try:
+                server_connection = server.establishConnection(featureset)
+            except:
+                sys.exit(1)
+            server_connection.terminate()
+            sys.exit(0)
+
         # Setup a connection to the server (cooker)
         server_connection = server.establishConnection(featureset)
 
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/5 bitbake.lock: Add host:port to bitbake.lock for memres server

In reply to Jason Wessel

The idea is to build on the --status-only option for bitbake and expose a mechanism where the oe init scripts can easily switch between memres server and the non-memres server.

In the case of the standard oe init script the following can shut down the server:

if [ -z "$BBSERVER" ] && [ -f bitbake.lock ] ; then
grep ":" bitbake.lock > /dev/null && BBSERVER=cat bitbake.lock bitbake --status-only if [ $? = 0 ] ; then
echo "Shutting down bitbake memory resident server with bitbake -m" BBSERVER=cat bitbake.lock bitbake -m
fi
fi

A similar function can be used to automatically detect if the server is already running for the oe memres init script. This new functionality allows for the memres init script to be started in a new shell and connect up to an alaready running server without seeing the error of trying to start the server multiple times.

---
 bin/bitbake             |    2 ++
 lib/bb/cooker.py        |    8 ++++++++
 lib/bb/cookerdata.py    |    1 +
 lib/bb/server/xmlrpc.py |    7 +++++--
 4 files changed, 16 insertions(+), 2 deletions(-)
bin/bitbake +2 -0
index cca2b8d..21fe3ab 100755
@@ -208,8 +208,10 @@ def start_server(servermodule, configParams, configuration):
     if configParams.bind:
         (host, port) = configParams.bind.split(':')
         server.initServer((host, int(port)))
+        configuration.interface = [ server.serverImpl.host, server.serverImpl.port ]
     else:
         server.initServer()
+        configuration.interface = []
 
     try:
         configuration.setServerRegIdleCallback(server.getServerIdleCB())
lib/bb/cooker.py +8 -0
index 1f494ee..ae77f6f 100644
@@ -125,6 +125,14 @@ class BBCooker:
         self.lock = bb.utils.lockfile(lockfile, False, False)
         if not self.lock:
             bb.fatal("Only one copy of bitbake should be run against a build directory")
+        try:
+            self.lock.seek(0)
+            self.lock.truncate()
+            if len(configuration.interface) >= 2:
+                self.lock.write("%s:%s\n" % (configuration.interface[0], configuration.interface[1]));
+            self.lock.flush()
+        except:
+            pass
 
         # TOSTOP must not be set or our children will hang when they output
         fd = sys.stdout.fileno()
lib/bb/cookerdata.py +1 -0
index e640ed0..6200b0e 100644
@@ -127,6 +127,7 @@ class CookerConfiguration(object):
         self.dump_signatures = False
         self.dry_run = False
         self.tracking = False
+        self.interface = []
 
         self.env = {}
 
lib/bb/server/xmlrpc.py +5 -2
index 82c0e8d..54b0f88 100644
@@ -198,6 +198,11 @@ class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
         Constructor
         """
         BaseImplServer.__init__(self)
+        if (interface[1] == 0):     # anonymous port, not getting reused
+            self.single_use = True
+        # Use auto port configuration
+        if (interface[1] == -1):
+            interface = (interface[0], 0)
         SimpleXMLRPCServer.__init__(self, interface,
                                     requestHandler=BitBakeXMLRPCRequestHandler,
                                     logRequests=False, allow_none=True)
@@ -208,8 +213,6 @@ class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
         self.autoregister_all_functions(self.commands, "")
         self.interface = interface
         self.single_use = False
-        if (interface[1] == 0):     # anonymous port, not getting reused
-            self.single_use = True
 
     def addcooker(self, cooker):
         BaseImplServer.addcooker(self, cooker)
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH 3/5 bitbake, xmlrpc.py: Implement memory resident auto port configuration/restart

In reply to Jason Wessel

This patch adds the ability to dynamically select a port for the bitbake memory resident server when the BBSERVER port is set to -1. This allows for running multiple instances of the bitbake memory resident server on the same system in different build directories.

The client portion of the bitbake instance can also request that the server automatically start when using the auto port feature. This is to deal with a bitbake instance that eventually times out and exits or that has died for some unknown reason.

The new functionality allows for lazy startup of the server after sourcing the init script for the memory resident functionality.

---
 bin/bitbake             |   10 ++++++++++
 lib/bb/server/xmlrpc.py |   33 +++++++++++++++++++++++++++++----
 2 files changed, 39 insertions(+), 4 deletions(-)
bin/bitbake +10 -0
index 21fe3ab..5857b92 100755
@@ -304,6 +304,7 @@ def main():
         # we start a stub server that is actually a XMLRPClient that connects to a real server
         server = servermodule.BitBakeXMLRPCClient(configParams.observe_only)
         server.saveConnectionDetails(configParams.remote_server)
+        server.saveConnectionConfigParams(configParams)
 
     if not configParams.server_only:
         # Collect the feature set for the UI
@@ -314,11 +315,20 @@ def main():
                 server_connection = server.establishConnection(featureset)
             except:
                 sys.exit(1)
+            if not server_connection:
+                sys.exit(1)
             server_connection.terminate()
             sys.exit(0)
 
         # Setup a connection to the server (cooker)
         server_connection = server.establishConnection(featureset)
+        if not server_connection:
+            if configParams.kill_server:
+                bb.fatal("Server already killed")
+            configParams.bind = configParams.remote_server
+            start_server(servermodule, configParams, configuration)
+            bb.event.ui_queue = []
+            server_connection = server.establishConnection(featureset)
 
         # Restore the environment in case the UI needs it
         for k in cleanedvars:
lib/bb/server/xmlrpc.py +29 -4
index 54b0f88..46d074a 100644
@@ -341,13 +341,38 @@ class BitBakeXMLRPCClient(BitBakeBaseServer):
     def saveConnectionDetails(self, remote):
         self.remote = remote
 
+    def saveConnectionConfigParams(self, configParams):
+        self.configParams = configParams
+
     def establishConnection(self, featureset):
         # The format of "remote" must be "server:port"
         try:
             [host, port] = self.remote.split(":")
             port = int(port)
-        except:
-            return None
+        except Exception as e:
+            bb.fatal("Failed to read remote definition (%s)" % str(e))
+
+        # use automatic port if port set to -1, meaning read it from
+        # the bitbake.lock file
+        if port == -1:
+            lock_location = "%s/bitbake.lock" % self.configParams.environment.get('BUILDDIR')
+            lock = bb.utils.lockfile(lock_location, False, False)
+            if lock:
+                # This means there is no server running which we can
+                # connect to on the local system.
+                bb.utils.unlockfile(lock)
+                return None
+
+            try:
+                lf = open(lock_location, 'r')
+                remotedef = lf.readline()
+                [host, port] = remotedef.split(":")
+                port = int(port)
+                lf.close()
+                self.remote = remotedef
+            except Exception as e:
+                bb.fatal("Failed to read bitbake.lock (%s)" % str(e))
+
         # We need our IP for the server connection. We get the IP
         # by trying to connect with the server
         try:
@@ -355,8 +380,8 @@ class BitBakeXMLRPCClient(BitBakeBaseServer):
             s.connect((host, port))
             ip = s.getsockname()[0]
             s.close()
-        except:
-            return None
+        except Exception as e:
+            bb.fatal("Could not create socket for %s:%s (%s)" % (host, port, str(e)))
         try:
             self.serverImpl = XMLRPCProxyServer(host, port)
             self.connection = BitBakeXMLRPCServerConnection(self.serverImpl, (ip, 0), self.observer_only, featureset)
Signature · 1 line
1.7.9.5
#4 lore raw

PATCH 4/5 oe-init-build-env: Allow startup with and without memres

In reply to Jason Wessel

Use the bitbake --status-only and the fact that bitbake.lock will contain the host name and port to determine when to activate or shutdown the stay resident bitbake server.

This allows a end developer to cleanly switch between the two ways to use bitbake as well as enter the memres bitbake server from multiple shells without starting the server if it is already running.

---
 oe-init-build-env        |    9 +++++++++
 oe-init-build-env-memres |   21 ++++++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)
oe-init-build-env +9 -0
index de9692f..8ef32f3 100755
@@ -47,3 +47,12 @@ else
    unset BBPATH
 fi
 
+# Shutdown any bitbake server if the BBSERVER variable is not set
+if [ -z "$BBSERVER" ] && [ -f bitbake.lock ] ; then
+    grep ":" bitbake.lock > /dev/null && BBSERVER=`cat bitbake.lock` bitbake --status-only
+    if [ $? = 0 ] ; then
+	echo "Shutting down bitbake memory resident server with bitbake -m"
+	BBSERVER=`cat bitbake.lock` bitbake -m
+    fi
+fi
+
oe-init-build-env-memres +18 -3
index 79b91b7..687d5e1 100755
@@ -54,6 +54,21 @@ else
    unset OEROOT
    unset BBPATH
 fi
-bitbake --server-only -t xmlrpc -B localhost:$port
-export BBSERVER=localhost:$port
-echo "Bitbake server started on port $port, use bitbake -m to shut it down"
+res=1
+if [ -e bitbake.lock ] && grep : bitbake.lock > /dev/null ; then
+    BBSERVER=`cat bitbake.lock` bitbake --status-only
+    res=$?
+fi
+
+if [ $res != 0 ] ; then
+    bitbake --server-only -t xmlrpc -B localhost:$port
+fi
+
+export BBSERVER=`cat bitbake.lock`
+
+if [ $res = 0 ] ; then
+    echo "Using existing bitbake server at: $BBSERVER, use bitbake -m to shut it down"
+else
+    echo "Bitbake server started at: $BBSERVER, use bitbake -m to shut it down"
+fi
+unset res
Signature · 1 line
1.7.9.5
#5 lore raw

PATCH 5/5 oe-init-build-env-memres: Add auto port functionality

In reply to Jason Wessel

In order to run multiple bitbake memory resident servers on the same machine they must each use different ports.

This patch works in conjuction with bitbake to make the auto port selection and lazy server startup the default.

---
 oe-init-build-env-memres |   27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
oe-init-build-env-memres +16 -11
index 687d5e1..0007998 100755
@@ -26,8 +26,8 @@
 # to sourcing this script.
 #
 if [ -z "$1" ]; then
-    echo "No port specified, using 12345"
-    port=12345
+    echo "No port specified, using dynamically selected port"
+    port=-1
 else
     port=$1
     shift
@@ -60,15 +60,20 @@ if [ -e bitbake.lock ] && grep : bitbake.lock > /dev/null ; then
     res=$?
 fi
 
-if [ $res != 0 ] ; then
-    bitbake --server-only -t xmlrpc -B localhost:$port
-fi
+if [ $port = -1 ] ; then
+    export BBSERVER=localhost:-1
+    echo "Bitbake server started on demand as needed, use bitbake -m to shut it down"
+else
+    if [ $res != 0 ] ; then
+	bitbake --server-only -t xmlrpc -B localhost:$port
+    fi
 
-export BBSERVER=`cat bitbake.lock`
+    export BBSERVER=`cat bitbake.lock`
 
-if [ $res = 0 ] ; then
-    echo "Using existing bitbake server at: $BBSERVER, use bitbake -m to shut it down"
-else
-    echo "Bitbake server started at: $BBSERVER, use bitbake -m to shut it down"
+    if [ $res = 0 ] ; then
+	echo "Using existing bitbake server at: $BBSERVER, use bitbake -m to shut it down"
+    else
+	echo "Bitbake server started at: $BBSERVER, use bitbake -m to shut it down"
+    fi
+    unset res
 fi
-unset res
Signature · 1 line
1.7.9.5