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 <Chuqun.Liu@desay-svautomotive.com>
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 1024
static 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);
}
}