From e16dd314450cb2b85e4984c17fa60403b662ef8c Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Wed, 16 Jul 2025 15:56:38 +0100 Subject: bitbake: cooker/process/utils: Create profiling common function to remove code duplication We have code duplication in the way we handle profiling of code sections. Create a common function in utils which covers this. The main loop and idle loop profile files were also reversed. Fix this and the naming, removing a couple of unused variables containing the profile log names in the process too. (Bitbake rev: b4f6bae97ac9607420fc49fd4c9e957d89c9a5f3) Signed-off-by: Richard Purdie --- bitbake/lib/bb/utils.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'bitbake/lib/bb/utils.py') diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 1cc74ed546..f688f7dd68 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py @@ -1418,6 +1418,34 @@ def cpu_count(): def nonblockingfd(fd): fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) +def profile_function(profile, function, output_fn, process=True): + """Common function to profile a code block and optionally process the + output using or processing function. + + Arguments: + + - ``profile``: a boolean saying whether to enable profiling or not + - ``function``: the function call to profile/run + - ``outputfn``: where to write the profiling data + - ``process``: whether to process the profiling data and write a report + + Returns the wrapped function return value + """ + if profile: + try: + import cProfile as profile + except: + import profile + prof = profile.Profile() + ret = profile.Profile.runcall(prof, function) + prof.dump_stats(output_fn) + if process: + process_profilelog(output_fn) + serverlog("Raw profiling information saved to %s and processed statistics to %s.processed" % (output_fn, output_fn)) + return ret + else: + return function() + def process_profilelog(fn, pout = None): # Either call with a list of filenames and set pout or a filename and optionally pout. if not pout: -- cgit v1.2.3-54-g00ecf