The libkos library provides an API that obtains statistical data on CPU time and memory usage, and info on processes and threads. This API is defined in the header file sysroot-*-kos/include/coresrv/stat/stat_api.h from the KasperskyOS SDK.
The API includes KnGroupStatGetParam(), KnCpuStatGetParam(), KnTaskStatGetParam() functions that request the values of performance counters. To use these functions, you need to build a solution with a KasperskyOS kernel version that supports the full set of performance counters (for details, see image library). An exception to this rule includes calls of the KnGroupStatGetParam() function with the GROUP_PARAM_MEM_FREE and GROUP_PARAM_MEM_TOTAL values in the param parameter (see the table containing information about memory usage below) that can be successfully executed without a special kernel version.
Receiving information about CPU time
Uptime of CPUs (processor cores) is counted from the startup of the KasperskyOS kernel.
To obtain information about CPU time usage, use the KnGroupStatGetParam(), KnTaskStatGetParam() and KnCpuStatGetParam() functions. The values provided in the table below need to be passed in the param parameter of these functions. The CPU time obtained by calling the KnGroupStatGetParam(), KnTaskStatGetParam() or KnCpuStatGetParam() function is presented in nanoseconds. The CPU index (enumeration starts with zero) is the input parameter of the KnCpuStatGetParam() function. To get the total number of processors (computing cores) on the hardware platform, call the KnHalGetCpuCount() function from the hal_env.h API.
Information about CPU time
Function |
Value of the |
Obtained value |
|---|---|---|
|
|
Total uptime of all processors in kernel mode |
|
|
Total uptime of all processors in user mode |
|
|
Total uptime of all processors in idle mode |
|
|
Total uptime of all processors used for execution of the defined process |
|
|
Total uptime of all processors used for execution of the defined process in user mode |
|
|
Uptime of the defined processor in idle mode |
|
|
Uptime of the defined processor in user mode |
|
|
Uptime of the defined processor in kernel mode |
Receiving information about memory usage
To receive information about memory usage, you need to use the KnGroupStatGetParam() and KnTaskStatGetParam() functions. The values provided in the table below need to be passed in the param parameter of these functions. The memory size obtained by calling the KnGroupStatGetParam() or KnTaskStatGetParam() function is presented as the number of memory pages. The size of a memory page is 4 KB for all hardware platforms supported by KasperskyOS.
The size of physical memory used by a process refers only to the memory allocated directly for this process. For example, if the memory of a process is mapped to an MDL buffer created by another process, the size of this buffer is not included in this value.
Information about memory usage
Function |
Value of the |
Obtained value |
|---|---|---|
|
|
Size of all installed physical memory |
|
|
Size of free physical memory |
|
|
Size of physical memory with the "execution access" attribute |
|
|
Size of physical memory used as shared memory |
|
|
Size of physical memory used by the defined process |
|
|
Size of virtual memory of the defined process |
|
|
Size of virtual memory of the defined process mapped to shared physical memory |
|
|
Size of the page table of the defined process |
|
|
Average size of free virtual memory regions of the defined process |
|
|
Maximum size of free virtual memory regions of the defined process |
Obtaining information on processes and threads
In addition to information about CPU time and memory usage, the KnGroupStatGetParam() and KnTaskStatGetParam() functions also let you obtain information on processes and threads. To receive this information, the values provided in the table below need to be passed through the param parameter of these functions.
Information on processes and threads
Function |
Value of the |
Obtained value |
|---|---|---|
|
|
Number of processes (excluding the kernel process) |
|
|
Number of threads (including kernel threads) |
|
|
ID of the parent process of the defined process (PPID) |
|
|
Priority of the initial thread of the defined process |
|
|
State of the defined process (according to the list of |
|
|
Image size of the program loaded into the memory of the defined process (in bytes) |
|
|
Time (in nanoseconds) between startup of the kernel and startup of the defined process |
|
|
Number of handles owned by the defined process |
|
|
Number of threads in the defined process |
In addition to the KnGroupStatGetParam() and KnTaskStatGetParam() functions, information on processes and threads can also be obtained by using the following functions:
KnTaskStatGetName() – gets the name of the defined process.KnTaskStatGetPath() – gets the name of the executable file in ROMFS that was used to start the defined process.This function can be used only if the process was started from an executable file in ROMFS. Otherwise, the function call will result in an empty string.
KnTaskStatGetId() – gets the ID of the defined process (PID).KnTaskGetIdByName() gets the ID of the process (PID) with the defined name.Obtaining information on CPU time and memory usage by each process
To get information about CPU time and memory usage by each process, do the following:
KnGroupStatGetTaskList() function.KnTaskStatGetTasksCount() function.KnTaskStatEnumTaskList() function.KnTaskStatGetName() function.This is necessary to identify the process for which the information about CPU time and memory usage will be received.
KnTaskStatGetParam() function.To verify that the process was not terminated, you need to call the KnTaskStatGetParam() function, using the param parameter to pass the TASK_PARAM_STATE value. A value other than TaskStateTerminated should be received.
KnTaskStatCloseTask() function.KnTaskStatCloseTaskList() function.Calculating CPU load
Indicators of load on CPUs (processor cores) may be the following values:
These indicators are calculated for a specific time interval, at the start and end of which the information about CPU time utilization was received. (For example, CPU load can be monitored with periodic receipt of information about CPU time utilization.) The values obtained at the start of the interval need to be subtracted from the values obtained at the end of the interval. In other words, the following increments need to be obtained for the interval:
The percent load of all processors is calculated as follows:
(TK+TU)/(TK+TU+TIDLE).
The percent load of the ith processor is calculated as follows:
(TKi+TUi)/(TKi+TUi+TIDLEi).
The percent load of all processors caused by the jth process is calculated as follows:
Tj/(TK+TU+TIDLE).
Page top