Skip to contentSkip to conversation

bitbake-worker: add header with length of message

PATCH In review [email protected] 2 participants 6 messages Sep 21, 2023 – Oct 4, 2023 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitbake-worker: add header with length of message

From: Etienne Cordonnier <[email protected]>

The IPC mechanism between runqueue.py and bitbake-worker is currently not scalable:

The data is sent with the format <tag>pickled-data</tag>, and bitbake-worker
has no information about the size of the message. Therefore, the bitbake-worker is calling select() and read() in a loop, and then calling "self.queue.find(b"</" + item + b">")" for each chunk received.

This does not scale, because queue.find has a linear complexity relative to the size of the queue, and workerdata messages get very big e.g. for builds which reference a lot of files in SRC_URI. The number of chunks varies, but on my test system a lot of chunks of 65536 bytes are sent, and each iteration takes 0.1 seconds, making the transfer of the "workerdata" data very slow (on my test setup 35 seconds before this fix, and 1.5 seconds after this fix).

This commit adds a 4 bytes header after <tag>, so that bitbake-worker knows how many bytes need to be received, and does not need to constantly search the whole queue for </tag>.

---
 bin/bitbake-worker | 34 +++++++++++++++++++++++-----------
 lib/bb/runqueue.py | 34 ++++++++++++++++++++++------------
 2 files changed, 45 insertions(+), 23 deletions(-)
bin/bitbake-worker +23 -11
index 451e6926..a4e78991 100755
@@ -433,18 +433,30 @@ class BitbakeWorker(object):
                 while self.process_waitpid():
                     continue
 
-
     def handle_item(self, item, func):
-        if self.queue.startswith(b"<" + item + b">"):
-            index = self.queue.find(b"</" + item + b">")
-            while index != -1:
-                try:
-                    func(self.queue[(len(item) + 2):index])
-                except pickle.UnpicklingError:
-                    workerlog_write("Unable to unpickle data: %s\n" % ":".join("{:02x}".format(c) for c in self.queue))
-                    raise
-                self.queue = self.queue[(index + len(item) + 3):]
-                index = self.queue.find(b"</" + item + b">")
+        opening_tag = b"<" + item + b">"
+        if not self.queue.startswith(opening_tag):
+            return
+
+        tag_len = len(opening_tag)
+        if len(self.queue) < tag_len + 4:
+            # we need to receive more data
+            return
+        header = self.queue[tag_len:tag_len + 4]
+        payload_len = int.from_bytes(header, 'big')
+        # closing tag has length (tag_len + 1)
+        if len(self.queue) < tag_len * 2 + 1 + payload_len:
+            # we need to receive more data
+            return
+
+        index = self.queue.find(b"</" + item + b">")
+        if index != -1:
+            try:
+                func(self.queue[(tag_len + 4):index])
+            except pickle.UnpicklingError:
+                workerlog_write("Unable to unpickle data: %s\n" % ":".join("{:02x}".format(c) for c in self.queue))
+                raise
+            self.queue = self.queue[(index + len(b"</") + len(item) + len(b">")):]
 
     def handle_cookercfg(self, data):
         self.cookercfg = pickle.loads(data)
lib/bb/runqueue.py +22 -12
index c88d7129..9afb899c 100644
@@ -1318,6 +1318,16 @@ class RunQueue:
         self.worker = {}
         self.fakeworker = {}
 
+    @staticmethod
+    def send_pickled_data(worker, data, name):
+        msg = bytearray()
+        msg.extend(b"<" + name.encode() + b">")
+        pickled_data = pickle.dumps(data)
+        msg.extend(len(pickled_data).to_bytes(4, 'big'))
+        msg.extend(pickled_data)
+        msg.extend(b"</" + name.encode() + b">")
+        worker.stdin.write(msg)
+
     def _start_worker(self, mc, fakeroot = False, rqexec = None):
         logger.debug("Starting bitbake-worker")
         magic = "decafbad"
@@ -1353,9 +1363,9 @@ class RunQueue:
             "umask" : self.cfgData.getVar("BB_DEFAULT_UMASK"),
         }
 
-        worker.stdin.write(b"<cookerconfig>" + pickle.dumps(self.cooker.configuration) + b"</cookerconfig>")
-        worker.stdin.write(b"<extraconfigdata>" + pickle.dumps(self.cooker.extraconfigdata) + b"</extraconfigdata>")
-        worker.stdin.write(b"<workerdata>" + pickle.dumps(workerdata) + b"</workerdata>")
+        RunQueue.send_pickled_data(worker, self.cooker.configuration, "cookerconfig")
+        RunQueue.send_pickled_data(worker, self.cooker.extraconfigdata, "extraconfigdata")
+        RunQueue.send_pickled_data(worker, workerdata, "workerdata")
         worker.stdin.flush()
 
         return RunQueueWorker(worker, workerpipe)
@@ -1365,7 +1375,7 @@ class RunQueue:
             return
         logger.debug("Teardown for bitbake-worker")
         try:
-           worker.process.stdin.write(b"<quit></quit>")
+           RunQueue.send_pickled_data(worker.process, b"", "quit")
            worker.process.stdin.flush()
            worker.process.stdin.close()
         except IOError:
@@ -1892,14 +1902,14 @@ class RunQueueExecute:
     def finish_now(self):
         for mc in self.rq.worker:
             try:
-                self.rq.worker[mc].process.stdin.write(b"<finishnow></finishnow>")
+                RunQueue.send_pickled_data(self.rq.worker[mc].process, b"", "finishnow")
                 self.rq.worker[mc].process.stdin.flush()
             except IOError:
                 # worker must have died?
                 pass
         for mc in self.rq.fakeworker:
             try:
-                self.rq.fakeworker[mc].process.stdin.write(b"<finishnow></finishnow>")
+                RunQueue.send_pickled_data(self.rq.fakeworker[mc].process, b"", "finishnow")
                 self.rq.fakeworker[mc].process.stdin.flush()
             except IOError:
                 # worker must have died?
@@ -2194,10 +2204,10 @@ class RunQueueExecute:
             if 'fakeroot' in taskdep and taskname in taskdep['fakeroot'] and not self.cooker.configuration.dry_run:
                 if not mc in self.rq.fakeworker:
                     self.rq.start_fakeworker(self, mc)
-                self.rq.fakeworker[mc].process.stdin.write(b"<runtask>" + pickle.dumps(runtask) + b"</runtask>")
+                RunQueue.send_pickled_data(self.rq.fakeworker[mc].process, runtask, "runtask")
                 self.rq.fakeworker[mc].process.stdin.flush()
             else:
-                self.rq.worker[mc].process.stdin.write(b"<runtask>" + pickle.dumps(runtask) + b"</runtask>")
+                RunQueue.send_pickled_data(self.rq.worker[mc].process, runtask, "runtask")
                 self.rq.worker[mc].process.stdin.flush()
 
             self.build_stamps[task] = bb.parse.siggen.stampfile_mcfn(taskname, taskfn, extrainfo=False)
@@ -2295,10 +2305,10 @@ class RunQueueExecute:
                         self.rq.state = runQueueFailed
                         self.stats.taskFailed()
                         return True
-                self.rq.fakeworker[mc].process.stdin.write(b"<runtask>" + pickle.dumps(runtask) + b"</runtask>")
+                RunQueue.send_pickled_data(self.rq.fakeworker[mc].process, runtask, "runtask")
                 self.rq.fakeworker[mc].process.stdin.flush()
             else:
-                self.rq.worker[mc].process.stdin.write(b"<runtask>" + pickle.dumps(runtask) + b"</runtask>")
+                RunQueue.send_pickled_data(self.rq.worker[mc].process, runtask, "runtask")
                 self.rq.worker[mc].process.stdin.flush()
 
             self.build_stamps[task] = bb.parse.siggen.stampfile_mcfn(taskname, taskfn, extrainfo=False)
@@ -2500,9 +2510,9 @@ class RunQueueExecute:
 
         if changed:
             for mc in self.rq.worker:
-                self.rq.worker[mc].process.stdin.write(b"<newtaskhashes>" + pickle.dumps(bb.parse.siggen.get_taskhashes()) + b"</newtaskhashes>")
+                RunQueue.send_pickled_data(self.rq.worker[mc].process, bb.parse.siggen.get_taskhashes(), "newtaskhashes")
             for mc in self.rq.fakeworker:
-                self.rq.fakeworker[mc].process.stdin.write(b"<newtaskhashes>" + pickle.dumps(bb.parse.siggen.get_taskhashes()) + b"</newtaskhashes>")
+                RunQueue.send_pickled_data(self.rq.fakeworker[mc].process, bb.parse.siggen.get_taskhashes(), "newtaskhashes")
 
             hashequiv_logger.debug(pprint.pformat("Tasks changed:\n%s" % (changed)))
 
Signature · 1 line
2.36.1.vfs.0.0
#1 lore raw
Quoted 16 lines · Etienne Cordonnier via lists.yoctoproject.org

From: Etienne Cordonnier <[email protected]>

The IPC mechanism between runqueue.py and bitbake-worker is currently not scalable:

The data is sent with the format <tag>pickled-data</tag>, and bitbake-worker
has no information about the size of the message. Therefore, the bitbake-worker is calling select() and read() in a loop, and then calling "self.queue.find(b"</" + item + b">")" for each chunk received.

This does not scale, because queue.find has a linear complexity relative to the size of the queue, and workerdata messages get very big e.g. for builds which reference a lot of files in SRC_URI. The number of chunks varies, but on my test system a lot of chunks of 65536 bytes are sent, and each iteration takes 0.1 seconds, making the transfer of the "workerdata" data very slow (on my test setup 35 seconds before this fix, and 1.5 seconds after this fix).

Quoted 8 lines

This commit adds a 4 bytes header after <tag>, so that bitbake-worker knows how many bytes need to be received, and does not need to constantly search the whole queue for </tag>.

---
 bin/bitbake-worker | 34 +++++++++++++++++++++++-----------
 lib/bb/runqueue.py | 34 ++++++++++++++++++++++------------
 2 files changed, 45 insertions(+), 23 deletions(-)

This does look interesting. The IPC mechanism was never intended to carry large data and it has changed over time in ways it was never really designed for.

I'm guessing just one of the objects is particularly large? You mention lots of files in SRC_URI causing the issue, I'd like to better understand that.

I'm just wondering whether instead of improving the IPC, we could avoid some of the data in the first place?

Cheers,

Richard

#2 lore raw

Hi Richard,
we have a use-case similar to the use-case for the externalsrc class, where we add whole directories containing many source-code files to SRC_URI of various recipes (instead of pointing to a tarball or to a git repository like poky recipes do). For that reason, we transmit 180MB of "workerdata" for one build instead of 18MB of workerdata e.g. for core-image-sato. The workerdata contains the signatures of many files and is therefore quite big (the "sigdata" field of the workerdata dictionary).
However with this fix the transmission is quite fast (about 1.5 seconds for the call to "RunQueue.send_pickled_data(worker, workerdata, "workerdata")", and could probably be optimized even more by passing the length-header first in the IPC and doing only one blocking read for all the data).

Here is an analysis of the workerdata payload in my test setup (I dumped workerdata to a file, then used awk '{ print length }' file.txt to count each line):
'taskdeps': 16.520.856
'fakerootenv': 10.318.907
'fakerootdirs': 884.364
'fakerootnoenv': 569.865
'sigdata': 160.786.962
'logdefaultlevel': 23
'build_verbose_shell': 30
'build_verbose_stdout': 31
'logdefaultdomain': 24
'prhost': 16
'buildname': 31
'date': 20
'time': 18
'hashservaddr': 22
'umask': 17

Reducing the size of the data is maybe an option (I don't know enough about bitbake architecture to judge whether it's possible or not), but I would improve the IPC anyway, since there is no drawback in making it more efficient.

Etienne

Quoted 51 lines · Richard Purdie
Quoted 6 lines · Etienne Cordonnier via lists.yoctoproject.org

From: Etienne Cordonnier <[email protected]>

The IPC mechanism between runqueue.py and bitbake-worker is currently not scalable:

The data is sent with the format <tag>pickled-data</tag>, and

bitbake-worker

Quoted 1 line

has no information about the size of the message. Therefore, the

bitbake-worker

Quoted 1 line

is calling select() and read() in a loop, and then calling

"self.queue.find(b"</" + item + b">")"

Quoted 3 lines

for each chunk received.

This does not scale, because queue.find has a linear complexity relative

to the size of the queue,

Quoted 1 line

and workerdata messages get very big e.g. for builds which reference a

lot of files in SRC_URI.

Quoted 1 line

The number of chunks varies, but on my test system a lot of chunks of

65536 bytes are sent, and each

Quoted 1 line

iteration takes 0.1 seconds, making the transfer of the "workerdata"

data very slow (on my test setup

Quoted 3 lines

35 seconds before this fix, and 1.5 seconds after this fix).

This commit adds a 4 bytes header after <tag>, so that bitbake-worker

knows how many bytes need to be

Quoted 1 line

received, and does not need to constantly search the whole queue for

</tag>.

Quoted 6 lines
---
 bin/bitbake-worker | 34 +++++++++++++++++++++++-----------
 lib/bb/runqueue.py | 34 ++++++++++++++++++++++------------
 2 files changed, 45 insertions(+), 23 deletions(-)

This does look interesting. The IPC mechanism was never intended to carry large data and it has changed over time in ways it was never really designed for.

I'm guessing just one of the objects is particularly large? You mention lots of files in SRC_URI causing the issue, I'd like to better understand that.

I'm just wondering whether instead of improving the IPC, we could avoid some of the data in the first place?

Cheers,

Richard

#4 lore raw
In reply to a message not in the archive

Is the patch OK? I think it should be merged independently of whether the amount of data sent through the IPC gets reduced or not.

--
Etienne

Quoted 110 lines · Etienne Cordonnier via lists.openembedded.org

Hi Richard,
we have a use-case similar to the use-case for the externalsrc class, where we add whole directories containing many source-code files to SRC_URI of various recipes (instead of pointing to a tarball or to a git repository like poky recipes do). For that reason, we transmit 180MB of "workerdata" for one build instead of 18MB of workerdata e.g. for core-image-sato. The workerdata contains the signatures of many files and is therefore quite big (the "sigdata" field of the workerdata dictionary).
However with this fix the transmission is quite fast (about 1.5 seconds for the call to "RunQueue.send_pickled_data(worker, workerdata, "workerdata")", and could probably be optimized even more by passing the length-header first in the IPC and doing only one blocking read for all the data).

Here is an analysis of the workerdata payload in my test setup (I dumped workerdata to a file, then used awk '{ print length }' file.txt to count each line):
'taskdeps': 16.520.856
'fakerootenv': 10.318.907
'fakerootdirs': 884.364
'fakerootnoenv': 569.865
'sigdata': 160.786.962
'logdefaultlevel': 23
'build_verbose_shell': 30
'build_verbose_stdout': 31
'logdefaultdomain': 24
'prhost': 16
'buildname': 31
'date': 20
'time': 18
'hashservaddr': 22
'umask': 17

Reducing the size of the data is maybe an option (I don't know enough about bitbake architecture to judge whether it's possible or not), but I would improve the IPC anyway, since there is no drawback in making it more efficient.

Etienne

Quoted 46 lines · Richard Purdie

On Thu, 2023-09-21 at 09:56 +0200, Etienne Cordonnier via lists.yoctoproject.org
<https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.yoctoproject.org&d=DwMFaQ&c=ncDTmphkJTvjIDPh0hpF_4vCHvabgGkICC2epckfdiw&r=AhkbNonVuMIGRfPx_Qj9TsRih1DULJTKUkSGa66m67E&m=11AFtkuAPLmyyZbu73s_NJ98bO45wx63XixN5V-1juUffwxOK25p5bXA2b9tA-UX&s=lNJwDNb9Qjcyy3HwPW3pD8ENHA3amzROyreHrTzs7xE&e=>

Quoted 6 lines · wrote:

From: Etienne Cordonnier <[email protected]>

The IPC mechanism between runqueue.py and bitbake-worker is currently not scalable:

The data is sent with the format <tag>pickled-data</tag>, and

bitbake-worker

Quoted 1 line

has no information about the size of the message. Therefore, the

bitbake-worker

Quoted 1 line

is calling select() and read() in a loop, and then calling

"self.queue.find(b"</" + item + b">")"

Quoted 3 lines

for each chunk received.

This does not scale, because queue.find has a linear complexity

relative to the size of the queue,

Quoted 1 line

and workerdata messages get very big e.g. for builds which reference a

lot of files in SRC_URI.

Quoted 1 line

The number of chunks varies, but on my test system a lot of chunks of

65536 bytes are sent, and each

Quoted 1 line

iteration takes 0.1 seconds, making the transfer of the "workerdata"

data very slow (on my test setup

Quoted 3 lines

35 seconds before this fix, and 1.5 seconds after this fix).

This commit adds a 4 bytes header after <tag>, so that bitbake-worker

knows how many bytes need to be

Quoted 1 line

received, and does not need to constantly search the whole queue for

</tag>.

Quoted 6 lines
---
 bin/bitbake-worker | 34 +++++++++++++++++++++++-----------
 lib/bb/runqueue.py | 34 ++++++++++++++++++++++------------
 2 files changed, 45 insertions(+), 23 deletions(-)

This does look interesting. The IPC mechanism was never intended to carry large data and it has changed over time in ways it was never really designed for.

I'm guessing just one of the objects is particularly large? You mention lots of files in SRC_URI causing the issue, I'd like to better understand that.

Quoted 7 lines

I'm just wondering whether instead of improving the IPC, we could avoid some of the data in the first place?

Cheers,

Richard

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#15102):
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.openembedded.org_g_bitbake-2Ddevel_message_15102&d=DwIFaQ&c=ncDTmphkJTvjIDPh0hpF_4vCHvabgGkICC2epckfdiw&r=AhkbNonVuMIGRfPx_Qj9TsRih1DULJTKUkSGa66m67E&m=11AFtkuAPLmyyZbu73s_NJ98bO45wx63XixN5V-1juUffwxOK25p5bXA2b9tA-UX&s=35XolLdHiGgz7p5y-cijay8jWjCYklOw3Ao5BblsE_g&e=
Mute This Topic:
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.openembedded.org_mt_101496046_7048771&d=DwIFaQ&c=ncDTmphkJTvjIDPh0hpF_4vCHvabgGkICC2epckfdiw&r=AhkbNonVuMIGRfPx_Qj9TsRih1DULJTKUkSGa66m67E&m=11AFtkuAPLmyyZbu73s_NJ98bO45wx63XixN5V-1juUffwxOK25p5bXA2b9tA-UX&s=27u-Kj3KVuMWX7dENA5Cp4uFi1bzjhHiTJK9Jrliirk&e=
Group Owner: [email protected] Unsubscribe:
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.openembedded.org_g_bitbake-2Ddevel_unsub&d=DwIFaQ&c=ncDTmphkJTvjIDPh0hpF_4vCHvabgGkICC2epckfdiw&r=AhkbNonVuMIGRfPx_Qj9TsRih1DULJTKUkSGa66m67E&m=11AFtkuAPLmyyZbu73s_NJ98bO45wx63XixN5V-1juUffwxOK25p5bXA2b9tA-UX&s=Dp6Bh7DLleq67c9kK9wfJeNF6n8TE0PgJa092Me7eww&e=
[[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

#5 lore raw
Quoted 2 lines · Etienne Cordonnier

Is the patch OK? I think it should be merged independently of whether the amount of data sent through the IPC gets reduced or not.

With this one, I'm not sure. We're in our feature freeze and there are a ton of problematic issues which we're struggling to get to grips with. Given I'm not 100% convinced that we should change this, I don't feel it is a good time to change this.

I really don't know whether it will/should go in after feature freeze or not either. I appreciate you'd like answers on that but I'm struggling with the bandwidth to give everything the time they need :(

Cheers,

Richard

#6 lore raw

Hi Richard,
OK, no problem, I just wanted to understand the status of the patch.

--
Etienne

Quoted 19 lines · Richard Purdie
Quoted 2 lines · Etienne Cordonnier

Is the patch OK? I think it should be merged independently of whether the amount of data sent through the IPC gets reduced or not.

With this one, I'm not sure. We're in our feature freeze and there are a ton of problematic issues which we're struggling to get to grips with. Given I'm not 100% convinced that we should change this, I don't feel it is a good time to change this.

I really don't know whether it will/should go in after feature freeze or not either. I appreciate you'd like answers on that but I'm struggling with the bandwidth to give everything the time they need :(

Cheers,

Richard