We're hiring!
*

Implementing a performance boosting algorithm in Coccinelle

Jaskaran Singh avatar

Jaskaran Singh
January 21, 2021

Share this post:

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.

What is Coccinelle?

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.

How it works

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.

Implementing the algorithm

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:

  • While parsing a C file, Parse its included header files recursively. Parse each header file only once.
  • While parsing a header file, figure out types of relevant C constructs (struct fields, typedefs) and store the names of each in a cache. Map a name to the file its declared in and the type associated to the name.
  • While parsing a header file, create a dependency graph to figure out what header file is reachable from which C file.
  • When parsing a C file, and encountering a variable with an unknown type:
    • Lookup the name cache for the variable.
    • Determine reachability of the header file that variable is declared in using the dependency graph.
    • Grab the type of the reachable file's variable.

Result

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!

Comments (0)


Add a Comment






Allowed tags: <b><i><br>Add a new comment:


Search the newsroom

Latest Blog Posts

Quick hack: Patching kernel modules using DKMS

05/05/2021

DKMS is a framework that is mostly used to build and install external kernel modules. It can also be used to install a specific patch to…

Build your own application with GTK 4 as a Meson subproject!

29/04/2021

Building GTK 4 as a Meson subproject for your own application is not only useful for Windows builds, but also for many Linux distributions…

Profiling virtualized GPU acceleration with Perfetto

22/04/2021

Recently, we have been using Perfetto to successfully profile Apitrace traces in crosvm through VirGL renderer. We have now added perfetto…

Continuous 3D Hand Pose Tracking using Machine Learning & Monado OpenXR

20/04/2021

As part of a project backed by INVEST-AI, a program managed by IVADO Labs, we have developed a multi-stage neural network-based solution…

An easy to use MTP implementation for your next embedded Linux project

13/04/2021

Did you know you could run a permissively-licensed MTP implementation with minimal dependencies on an embedded device? Here's a step-by-step…

Rust: integrating LLVM source-base code coverage with GitLab

24/03/2021

Earlier this year, the Rust compiler gained support for LLVM source-base code coverage. In this post we'll explain how to setup a CI job…

Open Since 2005 logo

We use cookies on this website to ensure that you get the best experience. By continuing to use this website you are consenting to the use of these cookies. To find out more please follow this link.

Collabora Ltd © 2005-2021. All rights reserved. Privacy Notice. Sitemap.

Collabora Limited is registered in England and Wales. Company Registration number: 5513718. Registered office: The Platinum Building, St John's Innovation Park, Cambridge, CB4 0DS, United Kingdom. VAT number: 874 1630 19.