Tool Recommendation! This ccusage Can Analyze C/C++ Source Code Call Hotspots and Identify Performance Bottlenecks!

Today, I want to share a small tool I recently discovered — <span>ccusage</span>. Although the project is not large, its functionality is indeed impressive! I tried it out, and it **directly analyzed tens of thousands of lines of C code in a project, producing results in seconds, and the performance skyrocketed!** It is fantastic for diagnosing code structural complexity, function coupling, and even hot path call graphs.

This tool is perfect for C projects that often have tens of thousands of lines, a bunch of macros, nested structures, and header file dependency hell.

Tool Recommendation! This ccusage Can Analyze C/C++ Source Code Call Hotspots and Identify Performance Bottlenecks!

Now, let’s take a look at how I got started with this tool and discuss some of its design concepts.

1) Project Overview: What is ccusage?

In one sentence: This is a source-level static analysis tool used to count the “usage frequency” and “occurrence location” of various C/C++ language elements in a project.

Specifically, it supports:

  • Counting the number of times functions are called and identifying the callers;
  • Analyzing the usage locations of macro definitions (define);
  • Frequency of structure member appearances;
  • Optionally generating DOT format call graphs (graphviz).

It’s somewhat like a lightweight version of clang-analyzer or cscope, but easier to use without complicated compilation parameters for import.

Moreover, it is based on <span>libclang</span>, so its accuracy is much higher than your own regex matching, avoiding a lot of false positives.

Tool Recommendation! This ccusage Can Analyze C/C++ Source Code Call Hotspots and Identify Performance Bottlenecks!

2) Installation & Dependencies

I am using macOS on an M1 chip, and to install this, you need the following dependencies:

  • Python 3.10+
  • <span>libclang</span> (install directly on mac using <span>brew install llvm</span>)
  • <span>graphviz</span> (optional: used for visualizing call graphs)

Installation steps are as follows:

git clone https://github.com/ryoppippi/ccusage.git
cd ccusage
pip install -r requirements.txt

Note! The path for <span>libclang</span> that this tool depends on may need to be configured manually. I set it up like this on my M1:

export LIBCLANG_PATH="/opt/homebrew/opt/llvm/lib"

Then you can run it.

Tool Recommendation! This ccusage Can Analyze C/C++ Source Code Call Hotspots and Identify Performance Bottlenecks!

3) How to Use? I Analyzed an Old Project with It!

I found an old embedded project in our company, written purely in C, with over 30,000 lines. I wanted to see which function is the “most core calling point”.

The command is straightforward:

python ccusage.py -r ./your_project_path -t function -o out/function_usage.json

The resulting JSON file looks something like this:

{
  "funcA": {
    "count": 12,
    "locations": [...],
    "called_by": [...],
    "calls": [...]
  },
  ...
}

I immediately noticed several functions called hundreds of times, and the code quality was concerning! There was a function called “util_parse_cmd” that was called by the UI layer, the hardware layer, and the network protocol all at once…It was a complete coupling monster!

Tool Recommendation! This ccusage Can Analyze C/C++ Source Code Call Hotspots and Identify Performance Bottlenecks!

Doesn’t it feel a bit like a Flamegraph? However, this is more low-level, allowing you to see the code-level calling relationships directly.

4) It Can Also Generate Graphs! Call Graphs Are Very Intuitive!

Next, I used its graphical feature to export a DOT graph file:

python ccusage.py -r ./your_project_path -t function -g out/callgraph.dot

Then I used <span>dot</span> to convert it to PNG:

dot -Tpng out/callgraph.dot -o callgraph.png

I included it in a PPT to show my leader, and we discovered that a function in one of our modules acted as the “core of the spider web”, with dozens of edges stemming from it!

He remarked, “No wonder this module explodes whenever it is modified”…

5) Summary of Use Cases: Who Is This Tool Suitable For?

I believe the following types of people would greatly benefit from this tool:

1) People Taking Over Old Projects — Want to quickly understand the code structure without relying on manually reading the source code; 2) Performance Analysts — Looking for hotspot functions to prioritize optimization; 3) Pre-refactoring Analysis — Identifying functions that are “overused” as candidates for splitting; 4) Teaching/Reading Source Code — Analyzing the function structure of open-source projects to aid learning; 5) Documentation Generation — Including call graphs in project documentation, perfect.

6) A Few Small Pitfalls to Share

From my experience, I found a few small pitfalls to share:

  • Some projects require importing compilation information using <span>compile_commands.json</span>, otherwise it will report missing header files;
  • If the structures are written very messily, it is recommended to first export and analyze using <span>-t struct</span> separately;
  • Some header files with nested macros can lead to inaccurate recognition by libclang, so use with caution;
  • Currently, it does not support template analysis (C++ projects heavily using templates are not very suitable);

However, the overall accuracy and analysis speed are indeed impressive, much stronger than writing your own scripts with regex!

If you are interested, you can give it a try. If you have ever taken over a project with tens of thousands of lines of messy code, this tool might just save you!

See you in the next article!

The project is open-source here:github.com/ryoppippi/ccusageTool Recommendation! This ccusage Can Analyze C/C++ Source Code Call Hotspots and Identify Performance Bottlenecks!

Additionally, you can check out my article “DeepSeek Local Deployment, It’s Insane!” for local setup.

Click the public account below and reply with the keyword:deepseek, to get resources related to DeepSeek!


Share

Collect

Like

View


Leave a Comment