
Profiling code with Caché Monitor

Not everyone knows that InterSystems Caché has a built-in code profiling tool called Caché Monitor.
Its main purpose (obviously) is to collect statistics on programs running in Caché. It can provide both general statistics for all programs, and detailed Line-by-Line statistics for each individual program.
Using Caché Monitor
Let's look at the Caché Monitor usage scenario and its main features. So, to start the profiler, you need to go to the area that you plan to monitor in the terminal and start the% SYS.MONLBL system routine:
zn ""
do ^%SYS.MONLBL
As a result of the last command, you will see the following:

There are only two points: the first item allows you to start the profiler itself, and the second allows you to simulate the launch to calculate the memory needed for monitoring. Note that monitoring a large number of programs at the same time may require quite a lot of memory. If you need this, you may need to increase the value of the gmheap parameter (you can increase it in the System Management Portal => System Administration => Configuration => Advanced Settings => Advanced Memory Settings . Changing this parameter will require a restart of Caché).
Let's select the first item. A screen will prompt you to enter a program name. A mask using "*" is supported. To view all already added programs, you must enter "? L".
In the example, I will monitor all the programs in my area, so I will simply enter "*".

The end of the input is the input of an empty string. As you can see, 246 programs were selected for monitoring.
Further, we are invited to select metrics for monitoring. In Caché Monitor, more than 50 metrics are available for tracking, for example:
- The number of global links
- Time spent executing a string
- Number of Caché Object Script Lines
- The number of rows executed
- The number of executions of a particular line
- Number of Lock Commands
- The number of successful lock commands, etc.
In most cases, the programmer needs only minimal metrics (number of lines, number of lines executed, number of times a particular line was executed, time to execute a specific line). These statistics will be collected when you select item 1 (default value). If you wish, you can also collect all statistics or select certain metrics. In my example, I will focus on the minimum metrics:

The next step will prompt you to select the processes to monitor. You can collect statistics for some specific processes (you will need to specify a list of their PIDs), for the current process, or for all. I will again focus on the default value.
In case of a successful start, you will see the message “Monitor started”. And by pressing Enter you will be in the monitor menu:

Let's go over the items.
Stop Monitor - full stop of the monitor. This will also erase all statistics collected.
Pause Monitor - pause the monitor with the ability to continue and save the collected statistics.
Clear Counters - clear statistics.
The following four points are responsible for the output of the collected statistics. The first two are most often used - the output of detailed Line-by-Line information on a specific program and the output of generalized statistics for all executed programs. When choosing these items, you will be prompted to enter a list of programs for which you want to display statistics, and the name of the file in which you want to write (if you do not specify a file, the statistics will be displayed directly in the terminal). In addition to the INT code, it is possible to include the source code (if any) in the output file, however, it is not collected for detailed statistics.
Everything is ready, we run the programs we need to execute, and then we look at the statistics.
Attention!Using Caché Monitor can bring significant overhead in terms of performance, so using it in production systems is highly discouraged. Please use it only during development and debugging.
Small bonus
For more convenient viewing of the results, I wrote a small web application that you can download from our GitHub repository: github.com/intersystems-ru/monlbl-viewer
At the moment, it can do the following:
- Start, Stop, Pause / Resume Caché Monitor.
- Track minimum software metrics.
- View general statistics on programs.
- View detailed Line-by-Line statistics for a specific program.
- Syntax highlighting.
- The transition from calling the class method or program (program label) to the code of this class method or program (if the code is available and monitored by the monitor).
What's in the plans:
- Provide the ability to select an area (Namespace) for monitoring.
- The transition from calling a method of an object and calling a program function to the code of this method or function.
- The mapping of the source lines of the CLS class to the lines of the intermediate code INT (if possible, without losing statistics).
A couple of screenshots



I note that the application is distributed under the MIT license (ie, distributed “as is”), as uses the undocumented features of Caché and its performance is not guaranteed on different versions due to the possibility of changing these most undocumented features from version to version.
To use it, you need to import the files downloaded from the repository into the area for which you want to collect statistics.