Jaskaran Singh
January 21, 2021
Reading time:
Last year, from June to September, I worked on the kernel development tool Coccinelle under Collabora. I implemented a performance boosting algorithm for one of Coccinelle's use cases. Here's a look at this work.
Coccinelle is a tool used to refactor C source code. It's used for development in the Linux Kernel. You write an abstract patch (called a Semantic patch in Coccinelle terms), basically to remove a few lines of code and add some, to make a tree-wide change.
Coccinelle uses the semantic patch language for this purpose. Following is a basic example of a semantic patch:
@@ expression E; constant c; type T; @@ -kzalloc(c * sizeof(T), E) +kcalloc(c, sizeof(T), E)
When applied to the tree, the above semantic patch replaces every instance of kzalloc with kcalloc.
For more information, check out this page.
On the inside, Coccinelle has a semantic patch parser and a C parser. When fed a semantic patch and a C file, Coccinelle parses the semantic patch to create an AST, and parses the C file to create an AST as well.
Following this, it compares the semantic patch AST with the C AST. If matches are found, the changes detailed in the semantic patch are made to the C file.
During my work on Coccinelle, I implemented a performance boosting algorithm to speed up recursive parsing of header files in the Linux Kernel.
Coccinelle has an option to parse included header files recursively to figure out types of certain C constructs such as struct fields and typedefs. This is necessary in some cases, as Coccinelle can only look at one C file at a time.
Initially, this recursive parsing would take close to 7 hours for the entire Linux Kernel. Since the target userbase of Coccinelle is kernel developers, 7 hours wasn't a very good benchmark.
Implementation of the performance boosting algorithm resulted in that time coming down to 45 minutes. For the curious, following is the algorithm:
The algorithm isn't perfect, as it still takes 45 minutes to get everything done. There's a lot more that could be done, like leveraging multiprocessing (a whole other can of worms), or conditionally parsing the files based on the semantic patch's matches. However, it works relatively fine on a moderately fast PC.
Thank you Collabora for financially supporting this project!
27/06/2024
With each board running a mainline-first Linux software stack and tested in a CI loop with the LAVA test framework, the Farm showcased Collabora's…
26/06/2024
WirePlumber 0.5 arrived recently with many new and essential features including the Smart Filter Policy, enabling audio filters to automatically…
12/06/2024
Part 3 of the cmtp-responder series with a focus on USB gadgets explores several new elements including a unified build environment with…
06/06/2024
The final installment of a series explaining how Collabora is helping shape the video virtualization story for Chromebooks with a focus…
05/06/2024
Last week I attended the GStreamer spring hackfest in Thessaloniki to work on the PipeWire GStreamer elements and connect with the community.
28/05/2024
The world of AI has made leaps and bounds from what It once was, but there are still some adjustments required for the optimal outcome.…
Comments (0)
Add a Comment