/***************************************************************************** * @copyright Copyright (c) 2025-2055 Gary. All rights reserved. * @file bsp_data_mode.c * @brief xx功能 * @author Gary * @date 2024-09-04 * @remark *****************************************************************************/ #include #include "bsp_data_mode.h" #include "bsp_rtdb.h" #include "../driver/drv_gpio.h" #include "../kernel/kit_core.h" // 计算系统 CPU 利用率 double calculateSysCpuUsge(cpu_time_t prev, cpu_time_t curr) { uint64_t prev_total = prev.user + prev.nice + prev.system + prev.idle + prev.iowait + prev.irq + prev.softirq + prev.steal; uint64_t curr_total = curr.user + curr.nice + curr.system + curr.idle + curr.iowait + curr.irq + curr.softirq + curr.steal; uint64_t prev_idle = prev.idle; uint64_t curr_idle = curr.idle; uint64_t total_diff = curr_total - prev_total; uint64_t idle_diff = curr_idle - prev_idle; return (double)(total_diff - idle_diff) / total_diff * 100.0; } // 计算当前进程 CPU 占用率 double calculateProCpuUsage(cpu_time_t prev_system_time, cpu_time_t curr_system_time, cpu_time_t prev_process_time, cpu_time_t curr_process_time) { uint64_t system_total_time_diff = (curr_system_time.user + curr_system_time.nice + curr_system_time.system + curr_system_time.idle + curr_system_time.iowait + curr_system_time.irq + curr_system_time.softirq + curr_system_time.steal) - (prev_system_time.user + prev_system_time.nice + prev_system_time.system + prev_system_time.idle + prev_system_time.iowait + prev_system_time.irq + prev_system_time.softirq + prev_system_time.steal); uint64_t process_total_time_diff = (curr_process_time.user + curr_process_time.system) - (prev_process_time.user + prev_process_time.system); if (system_total_time_diff == 0) { return 0.0; } return (double)process_total_time_diff / system_total_time_diff * 100.0; } // 计算内存使用率的函数 double calculateMemUsage(memory_info_t info) { // 计算已使用内存大小,总内存减去空闲内存、缓冲区内存和缓存内存 uint64_t used = info.total - info.free - info.buffers - info.cached; // 计算内存使用率,已使用内存除以总内存再乘以 100 return (double)used / info.total * 100.0; } // 本机信息采集接口 直接写入共享内存 void getCpuAndMemUsages(uint8_t onoff) { pid_t pid = getpid(); static cpu_time_t prev_system_time, curr_system_time; static cpu_time_t prev_process_time, curr_process_time; static memory_info_t info; static double memUsage; static double ProcMemUsage; static double system_utilization; static double process_usage; if (onoff) { getSysCpuTime(&prev_system_time); getProCpuTime(&prev_process_time, pid); return; } getMemInfo(&info); // 获取内存信息 memUsage = calculateMemUsage(info); // 计算内存使用率 ProcMemUsage = calculateProcMemUsage(info, pid); // 计算当前进程内存占用率 getSysCpuTime(&curr_system_time); getProCpuTime(&curr_process_time, pid); system_utilization = calculateSysCpuUsge(prev_system_time, curr_system_time); process_usage = calculateProCpuUsage(prev_system_time, curr_system_time, prev_process_time, curr_process_time); setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_CpuTotalUsage, system_utilization); setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_ProcessCpuUsage, process_usage); setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_MemTotalSize, info.total); setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_MemUsageRate, memUsage); setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_ProcessMemUsage, ProcMemUsage); #if 0 printf("\r\npid:%d \r\nCPU_Usage TOTAL:%.2f%% CRP:%.2f%%\r\n", (uint32_t)pid, getRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_CpuTotalUsage), getRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_ProcessCpuUsage)); printf("MEM_Usage TOTAL:%ld ", (uint64_t)getRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_MemTotalSize)); printf("TOTALUSG:%.2f%% ", getRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_MemUsageRate)); printf("CRPUSG:%.2f%%\r\n", getRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, kEms_ProcessMemUsage)); #endif return; } // 本机信息GPIO采集接口 直接写入共享内存 void getGpioMsge() { int loop = 0; int tmp = 0; // DI写入共享内存 for (loop = 0; loop < DI_End; loop++) { if (kEms_Di_Start + loop < kEms_Di_End) { tmp = drvGpioRead(loop + DI_Start); if (tmp < 0 || tmp > 1) { tmp = 0; } // printf("DI%d vlue:%d \r\n",loop,tmp); setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, (kEms_Di_Start + loop), (double_t)tmp); } } } // 本机设备数量获取 直接写入共享内存 void getDevNumToRtdb() { int validLoop = 0; while (validLoop < kDev_Type_End) { setRtdbPointValue(kSign_ShMem, kDev_Type_EMS, 0, (kEms_DevNumStart + validLoop), (gStDevTypeNum[validLoop] == 0? 1 : gStDevTypeNum[validLoop])); validLoop++; } return; } // 本机信息采集接口 获取EMS cpu和内存利用率及GPIO等信息 直接写入共享内存 void* getLocalParams(void* itime) { uint16_t* time = (uint16_t*)itime; uint16_t cnt = 0; while (1) { // 内存计算 if (cnt == 0) { getCpuAndMemUsages(1); } else if (cnt == *time) { getCpuAndMemUsages(0); cnt = 0; } // GPIO读取存入 if (0 == cnt % 10) { getGpioMsge(); } cnt++; usleep(50000); } } // 初始化 本机信息采集 任务入口 void creatGetLocalParamTaskEntry(uint16_t time) { static uint16_t time1 = 0; time1 = time; // EMS下设备初始数量写入db getDevNumToRtdb(); pthread_t pfd; if (pthread_create(&pfd, NULL, getLocalParams, (void *) &time1) == 0) { KITPTF(LOG_MODBUS_EN, INFO_EN, "EMS本地信息采集 线程 创建成功"); KITLOG(LOG_MODBUS_EN, INFO_EN, "EMS本地信息采集 线程 创建成功"); } }