varch/doc/cpul.en.md

2.3 KiB

Introduction

cpul is a simple CPU load (cpuload) module for the C language. It defines a series of data structures, macros, and function interfaces related to CPU load management, aiming to facilitate developers to control and monitor CPU load in C language projects.

Interfaces

Functions

cpul_init Function
int cpul_init(CPUL *cpul);

Function Description: This function is used to initialize an instance of CPUL. It initializes the CPUL structure to its default values and checks the validity of the passed-in pointer. For the initialization, a function method for obtaining the original load needs to be provided. For the Linux cpuload, you can refer to test_cpul.c.

cpul_get Function
int cpul_get(CPUL *cpul, uint16_t *load);

Function Description: This function retrieves the current CPU load value from the CPUL instance. It also checks the validity of the parameters and the status.

cpul_set Function
int cpul_set(CPUL *cpul, uint16_t load);

Function Description: This function is used to set the target CPU load value for the CPUL instance. It checks the validity of the passed-in parameter.

cpul_task Function
int cpul_task(CPUL *cpul);

Function Description: This function executes a CPU load management task. It manages the CPU load based on the current load value and the target load value.

Usage Example:

void *loadgen_entry(void *ptr)
{
    #define PERIOD 10
    const int coreid = *(int *)ptr;
    CPUL cpul;
    int ret = 0;
    uint32_t count = 0;
    uint16_t load = 0;

    cpul.coreid = coreid;
    cpul.resolution = 10;
    cpul.raw = cpul_raw;
    ret = cpul_init(&cpul);

    ret = cpul_set(&cpul, 5000);

    while(1)
    {
        count += PERIOD;
        if (count >= 25200000) count = 0;

        if (count % 1000 == 0)
        {
            ret = cpul_get(&cpul, &load);
            printf("cpul_get<%d> %2d.%02d%%, %d, %d\r\n", coreid, (uint16_t)(load / 100), load % 100, cpul.ctrl.tload, cpul.ctrl.refine);
        }

        cpul_task(&cpul);
        usleep(1000 * PERIOD);
    }
}

Through these function interfaces, developers can conveniently implement functions such as initialization, retrieval, setting, and management of CPU load in C language programs.