Low Memory Killer In QNX

QNX系统下的Low Memory Killer,当内存极度低时,采取一定的措施将内存消耗的大户,做重启动作,防止整个系统不能工作;

A

B

1
2
3
4
devctl( fd, DCMD_PROC_SYSINFO, NULL, 0, &totalsize );

buffer = malloc( totalsize );
devctl( fd, DCMD_PROC_SYSINFO, buffer, totalsize, NULL );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include<algorithm>  
#include<vector>
#include<iostream>
using namespace std;

typedef struct _proc_info {
int pid;
string name;
int mem_usage;
}tProcInfo;

vector<tProcInfo> qnx;

bool Comp(const tProcInfo &a,const tProcInfo &b)
{
return a.mem_usage < b.mem_usage;
}

int main(){
tProcInfo qconn={1556,string("qconn"),1024};
tProcInfo audio={1550,string("audio"),4024};
tProcInfo cluster={1559,string("cluster"),2024};

qnx.push_back(qconn);
qnx.push_back(audio);
qnx.push_back(cluster);
vector<tProcInfo>::iterator it;
for(it=qnx.begin();it!=qnx.end();it++)
cout<< it->name <<endl;

sort(qnx.begin(),qnx.end(),Comp);
for(it=qnx.begin();it!=qnx.end();it++)
cout<< it->name <<endl;

cout << "Hello World" << endl;
return 0;
}

// ProcInfo.h: interface for the CProcInfo class.
//
//
#if !defined(AFX_PROCINFO_H__E3782DFC_59DE_45FC_BF1F_D8C8BF0181C1__INCLUDED_)
#define AFX_PROCINFO_H__E3782DFC_59DE_45FC_BF1F_D8C8BF0181C1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CProcInfo
{
public:
CProcInfo();
virtual ~CProcInfo();
public:
static void InitSysInfo();
static void ClearSysInfo();
static int GetFreeMem();
static int GetTotalMem();
static int GetFreeMemPercent();
static void GetSysInfo(int &TotalMem,int &CpuSpeed,int &BootTime,char *pszCpuName);
static void GetDiskInfo(int &Total,int &Free);
static int GetFreeDiskPercent();
static int normalize_data_size(int &size);

static bool GetProcName(const int iPid,char *pszProcName,int &fd);
static bool GetSingleProcInfo(const int fd,long &StartTsp,int &CpuTime,int &MemSize);
private:
static int s_iTotalMem;
static int s_iCpuSpeed;
static int s_iBootTime;
static char s_strCpuName[32];
static int s_hSysProc;
static int s_hRootFile;
};
#endif // !defined(AFX_PROCINFO_H__E3782DFC_59DE_45FC_BF1F_D8C8BF0181C1__INCLUDED_)

// ProcInfo.cpp: implementation of the CProcInfo class.
//
//
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#include <sys/neutrino.h>
#include <sys/procfs.h>
#include <sys/stat.h>
#include "ProcInfo.h"
#include <libgen.h>
//
// Construction/Destruction
//
int CProcInfo::s_iTotalMem;
int CProcInfo::s_iCpuSpeed;
int CProcInfo::s_iBootTime;
char CProcInfo::s_strCpuName[32]={'/0'};

int CProcInfo::s_hSysProc=-1;
int CProcInfo::s_hRootFile=-1;
CProcInfo::CProcInfo()
{
}
CProcInfo::~CProcInfo()
{
}
void CProcInfo::InitSysInfo()
{
char buffer[50];
procfs_sysinfo *sysinfo;
struct cpuinfo_entry *cpu;
int i;
sprintf(buffer, "/proc");

if ((s_hSysProc = open(buffer, O_RDONLY)) == -1)
printf( "couldn't open %s: %s/n", buffer, strerror(errno));
sysinfo = (procfs_sysinfo *)buffer;
if (devctl(s_hSysProc, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
printf( "couldn't get info for %s: %s/n", buffer, strerror(errno));
i = sysinfo->total_size;
if(!(sysinfo = (procfs_sysinfo *)alloca(i)))
printf( "couldn't get memory for %s: %s/n", buffer, strerror(errno));
if (devctl(s_hSysProc, DCMD_PROC_SYSINFO, sysinfo, i, 0) != EOK)
printf( "couldn't get info for %s: %s/n", buffer, strerror(errno));
s_iTotalMem = _SYSPAGE_ENTRY(sysinfo, system_private)->ramsize;
s_iBootTime = _SYSPAGE_ENTRY(sysinfo, qtime)->boot_time;
cpu = _SYSPAGE_ENTRY(sysinfo, cpuinfo);
// printf("%s ", &_SYSPAGE_ENTRY(sysinfo, strings)->data[cpu->name]); //实际name
strcpy(s_strCpuName,"Cyrix X86");
s_iCpuSpeed=cpu->speed;
}
void CProcInfo::ClearSysInfo()
{
close(s_hSysProc);
s_hSysProc=-1;
close(s_hRootFile);
s_hRootFile=-1;
}
void CProcInfo::GetSysInfo(int &TotalMem,int &CpuSpeed,int &BootTime,char *pszCpuName)
{
TotalMem=s_iTotalMem;
CpuSpeed=s_iCpuSpeed;
BootTime=s_iBootTime;
if (pszCpuName)
{
strcpy(pszCpuName,s_strCpuName);
}
}
int CProcInfo::GetFreeMem()
{
if (s_hSysProc==-1)
{
s_hSysProc=open("/proc", O_RDONLY);
}
struct stat st;
if (fstat(s_hSysProc, &st) == -1)
printf( "couldn't get stat info for %s: %s/n", "/proc", strerror(errno));
return st.st_size;
}
int CProcInfo::GetTotalMem()
{
return s_iTotalMem;
}

int CProcInfo::GetFreeMemPercent()
{
int freemem=GetFreeMem();
return ( (int)( freemem*100.0/s_iTotalMem ) );
}
int CProcInfo::GetFreeDiskPercent()
{
int iTotal,iFree;
GetDiskInfo(iTotal,iFree);
return ( (int)( iFree*100.0/iTotal ) );
}
int CProcInfo::normalize_data_size(int &size) {
char sym = 0;
if (size > 8192) {
size /= 1024;
sym = 'K';
if (size > 8192) {
size /= 1024;
sym = 'M';
}
if (size > 8192) {
size /= 1024;
sym = 'G';
}
if (size > 8192) {
size /= 1024;
sym = 'T';
}
}
return sym;
}
void CProcInfo::GetDiskInfo(int &Total,int &Free)
{
if (-1==s_hRootFile)
{
s_hRootFile = open("/",O_RDONLY);
}
if (-1==s_hRootFile)
{
Total=-1;
Free=-1;
return;
}
struct statvfs FileBuff;
int tmpret=0;

if (-1==(tmpret=fstatvfs(s_hRootFile,&FileBuff)))
{
printf("fstatvfs ret=%d/n",tmpret);
printf("fstatvfs is:%s/n",strerror(tmpret));
close(s_hRootFile);
Total=-1;
Free=-1;
return ;
}

// printf("f_blocks=%d,bfree=%d,f_bsize=%ld/n",FileBuff.f_blocks,FileBuff.f_bfree,FileBuff.f_bsize);
// printf("DT=%ld,DF=%ld/n",FileBuff.f_blocks*FileBuff.f_bsize,FileBuff.f_bfree*FileBuff.f_bsize);
Total=FileBuff.f_blocks*FileBuff.f_bsize;
Free=FileBuff.f_bfree*FileBuff.f_bsize;
}

bool CProcInfo::GetProcName(const int iPid,char *pszProcName,int &fd)
{
if (!pszProcName)
{
return false;
}
char buf[PATH_MAX + 1];
struct dinfo_s {
procfs_debuginfo info;
char pathbuffer[PATH_MAX]; /* 1st byte is info.path[0] */
}dinfo;

sprintf(buf, "/proc/%d/as", iPid);

if ((fd = open(buf, O_RDONLY|O_NONBLOCK)) == -1){
return false;
}
if (devctl(fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL) != EOK) {
close(fd);
return false;
}
strcpy(pszProcName,basename(dinfo.info.path));
return true;
}
//StartTsp:单位:秒
//CpuTime:单位:毫秒
//MemSize:单位:字节
bool CProcInfo::GetSingleProcInfo(const int fd,long &StartTsp,int &CpuTime,int &MemSize)
{
procfs_info infos;
if (-1==fd)
{
return false;
}

if (devctl(fd, DCMD_PROC_INFO, &infos, sizeof infos, 0) != EOK) {
return false;
}

StartTsp=infos.start_time/1000000000;
CpuTime=(infos.stime + infos.utime)/1000000;
//MemSize
MemSize=0;//
procfs_mapinfo *mapinfos = NULL;
static int num_mapinfos = 0;
procfs_mapinfo *mapinfo_p;
int flags = ~0, err, num, i;
// Get the number of map entrys
if((err = devctl(fd, DCMD_PROC_MAPINFO, NULL, 0, &num )) != EOK)
{
printf("failed devctl num mapinfos - %d (%s)/n", err, strerror(err));
return false;
}
// malloc enough memory for all of them
if ( (mapinfos = (procfs_mapinfo*)malloc( num*sizeof(procfs_mapinfo) )) == NULL )
{
printf("failed malloc - %d (%s)/n", err, strerror(err));
return false;
}
num_mapinfos = num;
mapinfo_p = mapinfos;
// fill the map entrys
if((err = devctl(fd, DCMD_PROC_MAPINFO, mapinfo_p, num*sizeof(procfs_mapinfo), &num)) != EOK)
{
printf("failed devctl mapinfos - %d (%s)/n", err, strerror(err));
free(mapinfos);
return false;
}
if (num>num_mapinfos)
{
num=num_mapinfos;
}
//
// Run through the list of mapinfo's, and store the data and text info
// so we can print it at the bottom of the loop.
//
for ( mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++ )
{
if ( !(mapinfo_p->flags & flags) )
mapinfo_p->ino = 0;
if ( mapinfo_p->ino == 0 ) /* already visited */
continue;
MemSize+=mapinfo_p->size;
}
free(mapinfos);
return true;
}

// CpuUsed.h: interface for the CCpuUsed class.
//
//
#if !defined(AFX_CPUUSED_H__DA812B5A_099E_43F0_9C27_E84A9F0A8E78__INCLUDED_)
#define AFX_CPUUSED_H__DA812B5A_099E_43F0_9C27_E84A9F0A8E78__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "QnxHardTimer.h"
class CCpuUsed : public CQnxHardTimer
{
public:
CCpuUsed();
virtual ~CCpuUsed();
virtual void RepeatTimerThread();
int GetCpuUsed();
public:
static CCpuUsed s_CpuUsed;
protected:
int m_iCpuUsed;
clockid_t m_iClockID;
uint64_t m_ut_old;
struct timespec m_tt_old;
};
#endif // !defined(AFX_CPUUSED_H__DA812B5A_099E_43F0_9C27_E84A9F0A8E78__INCLUDED_)

// CpuUsed.cpp: implementation of the CCpuUsed class.
//
//
#include <stdint.h>
#include <stdio.h>
#include "CpuUsed.h"
//
// Construction/Destruction
//
CCpuUsed CCpuUsed::s_CpuUsed;
CCpuUsed::CCpuUsed()
{
m_iClockID=ClockId(1, 1); //取空闲线程;
m_ut_old=0;
}
CCpuUsed::~CCpuUsed()
{
}
void CCpuUsed::RepeatTimerThread()
{
uint64_t ut_now=0;
struct timespec tt_now;
uint64_t ut_sub; //时间差
uint64_t ut_tmp;
ClockTime(m_iClockID, NULL, &ut_now);
clock_gettime( CLOCK_REALTIME, &tt_now);
ut_tmp=tt_now.tv_sec-m_tt_old.tv_sec;
ut_sub=ut_tmp*1000000000+tt_now.tv_nsec-m_tt_old.tv_nsec;
m_iCpuUsed=(int)( ((float)(ut_sub-(ut_now-m_ut_old))/(float)ut_sub)*100 );
m_ut_old=ut_now;
m_tt_old=tt_now;
// printf("m_iCpuUsed=%d%%/n",m_iCpuUsed);
}
int CCpuUsed::GetCpuUsed()
{
if (m_iCpuUsed>100)
{
return 100;
}else if (m_iCpuUsed<0)
{
return 0;
}else
return m_iCpuUsed;
}

参考链接

QNX:DCMD_PROC_SYSINFO

QNX下获取系统信息的方法(cpu,内存,进程等等)

C++在线环境

C++ Vector详解