博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
内存和cpu
阅读量:5329 次
发布时间:2019-06-14

本文共 5259 字,大约阅读时间需要 17 分钟。

http://www.blogjava.net/fjzag/articles/317773.html ubuntu@ubuntu-vm:/work/sv-g5-application/projects/sysmonitor/src$ cat SVPSysMonitor.cpp /*##################################################################################################    Company       : Huizhou Desay SV Automotive Co., Ltd.    Division      : Automotive Electronics, Desay Corporation    Business Unit : Central Technology    Department    : Advanced Development (Huizhou)    Project       : G5 Platform    Module        : System Monitor    Create Date   : Nov 03, 2016 - Liu Chuqun 
Update Date :##################################################################################################*/#include "SVPType.h"#include "SVPTime.h"#include "SVPLog.h"#ifdef SVP_LOG_TAG #undef SVP_LOG_TAG#endif#define SVP_LOG_TAG "sysmonitor"#define NAME_LEN 64#define BUFFER_LEN 1024static SVPChar g_name[NAME_LEN] = { 0 };static SVPChar g_buffer[BUFFER_LEN] = { 0 };static SVPInt32 g_cpuPercent = 0;static SVPInt32 g_memPercent = 0;static SVPInt32 g_memTotal = 0;static SVPInt32 g_memFree = 0;static SVPInt32 g_memAvailable = 0;static SVPInt32 g_topCPU = 0;static SVPInt32 g_topMemUse = 0;void GetCPUInfo();void GetMemInfo();void PrintInfo();void MonitorStrategy();SVPVoid ResetSystem();SVPInt32 main(SVPInt32 argc, SVPChar* argv[]){ while(1) { GetCPUInfo(); GetMemInfo(); PrintInfo(); MonitorStrategy(); sleep(3); } return 0;}void MonitorStrategy(){ SVPUlong ulCurStrategyTickCount = SVPTime::GetTickCount(); if (ulCurStrategyTickCount <= 30000) return;// 开机30秒内不执行策略。 static SVPBool s_bInLimitMode = SVP_FALSE; static SVPUlong s_ulEnterLimitModeTime = 0; static SVPUlong s_ulLimitModeCheckCount = 0; if (g_cpuPercent >= 100) { ++s_ulLimitModeCheckCount; if (SVP_FALSE == s_bInLimitMode) { s_bInLimitMode = SVP_TRUE; s_ulEnterLimitModeTime = ulCurStrategyTickCount; } if (ulCurStrategyTickCount - s_ulEnterLimitModeTime >= 90000) { SVP_INFO("s_ulLimitModeCheckCount[%lu]", s_ulLimitModeCheckCount); if (s_ulLimitModeCheckCount >= 30) ResetSystem(); } } else { s_bInLimitMode = SVP_FALSE; s_ulLimitModeCheckCount = 0; }}void PrintInfo(){ SVP_INFO("[TopMem:%3d%%, TopCPU:%3d%%] MemTotal:%dKB, MemAvailable:%dKB, MEM:%3d%%, CPU:%3d%%.", g_topMemUse, g_topCPU, g_memTotal, g_memAvailable, g_memPercent, g_cpuPercent); static SVPInt32 printCount = 0; ++printCount; if (printCount >= 60) { printCount = 0; system("ps"); usleep(100000); system("top -n1;ls /media"); }}void GetCPUInfo(){
//获取cpu占用率 FILE *fp = fopen("/proc/stat", "r"); if (fp) { memset(g_name, 0, NAME_LEN); memset(g_buffer, 0, BUFFER_LEN); static SVPInt32 idle0 = 0, total0 = 0; SVPInt32 user1, nice, sys, idle1, iowait, irq, softirq, total1 = 0; fgets(g_buffer, sizeof(g_buffer), fp); sscanf(g_buffer, "%s %d %d %d %d %d %d %d", g_name, &user1, &nice, &sys, &idle1, &iowait, &irq, &softirq); total1 = user1 + nice + sys + iowait + irq + softirq + idle1; if (total1 != total0) g_cpuPercent = 100 - ( ((idle1 - idle0) * 100)/ (double) (total1 - total0) ); else SVP_WARN("cpu total is the same. /proc/stat maybe error!"); idle0 = idle1; total0 = total1; if (g_cpuPercent > g_topCPU) g_topCPU = g_cpuPercent; fclose(fp); } else { SVP_ERROR("fopen /proc/stat failed:%s", strerror(errno)); }}void GetMemInfo(){
//获取内存信息 FILE *fp = fopen("/proc/meminfo", "r"); if (fp) { memset(g_name, 0, NAME_LEN); memset(g_buffer, 0, BUFFER_LEN); SVPChar unit[10] = { 0 }; fgets(g_buffer, sizeof(g_buffer), fp); sscanf(g_buffer, "%s %d %s", g_name, &g_memTotal, unit); fgets(g_buffer, sizeof(g_buffer), fp); sscanf(g_buffer, "%s %d %s", g_name, &g_memFree, unit); fgets(g_buffer, sizeof(g_buffer), fp); sscanf(g_buffer, "%s %d %s", g_name, &g_memAvailable, unit); if (g_memTotal) g_memPercent = 100 - (100 * g_memAvailable) / g_memTotal; else SVP_WARN("mem total is 0!"); if (g_memPercent > g_topMemUse) g_topMemUse = g_memPercent; fclose(fp); } else { SVP_ERROR("fopen /proc/meminfo failed:%s", strerror(errno)); }}SVPVoid ResetSystem(){ system("top -n1;ls /media"); SVP_WARN("restart system now ..."); SVPUint8 tryTimes = 0; while(tryTimes < 3) { ++tryTimes; if (-1 != system("powerctrl 10 &")) break; else SVP_ERROR("reboot system fail!"); sleep(1); } //最后一招。 //需要radio软件修改逻辑为:任何时候,只要收到first connect消息,就走core重新启动的逻辑。不要再过滤first connect消息。 sleep(5); SVP_ERROR("can't restart system through the spi msg!"); tryTimes = 0; while(tryTimes < 3) { ++tryTimes; if (-1 != system("reboot -f &")) break; else SVP_ERROR("reboot system fail!"); sleep(1); }}

 

转载于:https://www.cnblogs.com/yuguangyuan/p/7149243.html

你可能感兴趣的文章
关于python中带下划线的变量和函数 的意义
查看>>
asp.net 写入excel时,不能更新。数据库或对象为只读。
查看>>
题1简化版
查看>>
linux清空日志文件内容 (转)
查看>>
jsp中对jstl一些标签的引用方式
查看>>
100. Same Tree
查看>>
[转]java classLoader 体系结构
查看>>
mkdir命令(转)
查看>>
安卓第十三天笔记-服务(Service)
查看>>
css3学习笔记之背景
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
【bzoj5016】[Snoi2017]一个简单的询问 莫队算法
查看>>
[dpdk] 熟悉SDK与初步使用 (二)(skeleton源码分析)
查看>>
Ajax : load()
查看>>
分布式版本控制系统
查看>>
Java出现OutOfMemoryError
查看>>
可行性报告
查看>>
[预打印]使用vbs给PPT(包括公式)去背景
查看>>
HTML5学习笔记简明版(1):HTML5介绍与语法
查看>>
使用IntelliJ IDEA 配置Maven
查看>>