Creating Klocwork Metrics Reports using Built-in Metrics


What are Klocwork Metrics Reports?

In this article, we will go through the steps necessary to use Klocwork’s powerful built-in metrics analysis to produce interactive reports that will let you quickly spot code that needs refactoring.

Here is an example of a report based on methods exceeding a cyclomatic complexity value of 20. If you want to know how to create reports like these, then read on!

Cyclomatic Complexity Graph

What information do you need?

The custom reports that we will generate will depend on the metrics.xml file located in the directory

<projects_root>/config/

In our example, we will configure four new report types based on the following metrics:

  • The lines of code per file
  • The number of times a global variable is used for all routines per function
  • The cyclomatic complexity per method over 20
  • The number of writes to global variables per method over 2

All the information we need can be found on support.roguewave.com website here and here.

The corresponding Klocwork metrics respectively are

  • LOC_FILE
  • RNOACCTOGLOB
  • CYCLOMATIC
  • WRITETGLOBAL

Each metric has an ID. This is what you will add to the <kids></kids> element in the metrics.xml file. They respectively are

  • 7
  • 35
  • 135
  • 161

Finally, we need to know the entity to which these metrics apply. For example does the report apply the metric for each file, class, method, function, or interface? This is what you will add to the <entityKids></entityKids> element in the metrics.xml file.

In our case, the ID of the relevant entities respectively are:

  • 1 // FILE
  • 5 // FUNCTION
  • 48 // CLASS-METHOD
  • 48 // CLASS-METHOD

We now have all the data we need to edit the metrics.xml file.

Setting up the metrics.xml file

  1. Back up <projects_root>/config/metrics.xml
  2. Open the original metrics.xml for editing
  3. By default the file should look similar to this:

Metrics XML File

Notice how there are two categories: TotalMetricValue and EntityCountOverThreshold

TotalMetricValue is a category for metrics where you want it to report only the metric value e.g. Lines of Code.

EntityCountOverThreshold is slightly different because there is a threshold. This means Klocwork will report the metric only if the value is over a certain threshold. For example if you wanted Klocwork to report lines of code per method that exceeded 200 lines of code, then you would define the metric in this category.

Here is how the TotalMetricValue category should like

Metrics XML File

And this is how the EntityCountOverThreshold category should look like

Metrics XML File

Save the metrics.xml file.

Creating the report

Once you have saved your metrics.xml file, go ahead and restart the Klocwork servers by running the command:

kwservice -r <projects_root> restart

Once all services have restarted, go to the Klocwork web portal and click on the Reports tab, then click Create a new report.

Create Report Button

 

Scroll to the bottom and click on show more

Show more Reports

You should see the metrics defined in metrics.xml here

Metrics Report Types

Now you can click and see the reports. For example, click on Writes to Global Variables per Method.

Global Variable Metric Report

And if you click on the bar charts, you can get a better breakdown of where the metric is being reported

Global Variables per Method Graph

And that’s it! For any questions or if you need further clarification, please send us a message using the contact form below.



Get in touch

Full Name (required)

Work Email (required)

Title

Company

Contact number

Priority
HighResearch

Product
KlocworkUnderstandPerforceLattixSecurity InnovationFlexNet Code InsightSemiosCloneTracker

Platform
CC++C#JavaPythonPerlPHPOther Language

Certificate Standard(s)

Additional information