The libkos
library provides an API that lets you receive information about CPU time and memory usage. This API is defined in the header file sysroot-*-kos/include/coresrv/stat/stat_api.h
from the KasperskyOS SDK.
To get information about CPU time and memory usage and other statistical data, you need to build a solution with a KasperskyOS kernel version that supports performance counters. For more details, refer to "Image library".
Receiving information about CPU time
CPU uptime is counted from the startup of the KasperskyOS kernel.
To receive information about CPU time, 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.
Information about CPU time
Function |
Value of the |
Obtained value |
---|---|---|
|
|
CPU uptime in kernel mode |
|
|
CPU uptime in user mode |
|
|
CPU uptime in idle mode |
|
|
CPU uptime spent on process execution |
|
|
CPU uptime spent on process execution in user mode |
The CPU uptime obtained by calling the KnGroupStatGetParam()
or KnTaskStatGetParam()
function is presented in nanoseconds.
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.
Information about memory usage
Function |
Value of the |
Obtained value |
---|---|---|
|
|
Total size of all installed RAM |
|
|
Size of free RAM |
|
|
Size of RAM used by the process |
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 amount of RAM 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.
Enumerating processes
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
CPU load can be indicated as a percentage of total CPU load or as a percentage of CPU load by each process. 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 percentage of total CPU load is calculated as follows:
(TK+TU)/(TK+TU+TIDLE).
The percentage of CPU load by the ith process is calculated as follows:
Ti/(TK+TU+TIDLE).
Receiving additional information about processes
In addition to information about CPU time and memory usage, the KnGroupStatGetParam()
and KnTaskStatGetParam()
functions also let you obtain the following information:
The KnTaskStatGetId()
function gets the process ID (PID).