Chinaunix首页 | 论坛 | 博客
  • 博客访问: 341038
  • 博文数量: 89
  • 博客积分: 5152
  • 博客等级: 大校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-25 15:12
文章分类

全部博文(89)

文章存档

2012年(1)

2011年(5)

2010年(14)

2009年(69)

我的朋友

分类: 嵌入式

2011-04-27 09:53:36

头文件

 

  1. #ifndef __ANDROIDSYSINFO_H__
  2. #define __ANDROIDSYSINFO_H__



  3. #include <stdio.h>

  4. #include <string.h>

  5. #include <stdlib.h>

  6. #include <fcntl.h>

  7. #include <android/log.h>

  8. #include <unistd.h>



  9. typedef unsigned long DWORD;



  10. struct systemCPUdata

  11. {

  12.   unsigned long utime,stime,cutime,cstime;

  13.   unsigned long vmem, rmem;

  14.   char pname[255];

  15. };



  16. class AndroidMemInfo

  17. {

  18. public:

  19.     AndroidMemInfo();

  20.     ~AndroidMemInfo();    

  21.     void GetTotalPhys();

  22.     void GetAvailPhys();

  23.     DWORD ullTotalPhys;

  24.     DWORD ullAvailPhys;

  25.     

  26. private:

  27.     int m_memFile;

  28.     

  29. };



  30. class AndroidCpuUsage

  31. {

  32. public:

  33.     AndroidCpuUsage();
  34.     struct timeval m_lasttime,m_nowtime;

  35.     struct systemCPUdata m_lastproc, m_nowproc;

  36.     void getProcCPUStat(struct systemCPUdata *proc, int pid);

  37.     DWORD GetUsedCpu();

  38. };

  39. #endif //__ANDROIDSYSINFO_H__

 

CPP 文件

 

  1. #include "AndroidSysInfo.h"

  2. #define  LOGW(x...)  __android_log_print(ANDROID_LOG_ERROR,"ANDROIDSYSINFO",x)



  3. AndroidMemInfo::AndroidMemInfo()

  4. {

  5.     m_memFile = -1;

  6.       ullTotalPhys = 0;

  7.     ullAvailPhys = 0;

  8. }



  9. AndroidMemInfo::~AndroidMemInfo()

  10. {

  11. }



  12. void AndroidMemInfo::GetAvailPhys()

  13. {

  14.     m_memFile = open("/proc/meminfo", O_RDONLY);



  15.     if (m_memFile < 0)

  16.     {

  17.         LOGW("Unable to open /proc/meminfo");

  18.     }



  19.     char buffer[256];

  20.     const int len = read(m_memFile, buffer, sizeof(buffer)-1);

  21.     close(m_memFile);



  22.     if (len < 0)

  23.     {

  24.         LOGW("Unable to read /proc/meminfo");

  25.     }

  26.     buffer[len] = 0;



  27.     int numFound = 0;



  28.     static const char* const sums[] = { "MemFree:", "Cached:", NULL };

  29.     static const int sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), NULL };



  30.     char* p = buffer;

  31.     while (*p && numFound < 2)

  32.     {

  33.         int i = 0;

  34.         while (sums[i])

  35.         {

  36.             if (strncmp(p, sums[i], sumsLen[i]) == 0)

  37.             {

  38.                 p += sumsLen[i];

  39.                 while (*p == ' ') p++;

  40.                 char* num = p;

  41.                 while (*p >= '0' && *p <= '9') p++;

  42.                 if (*p != 0)

  43.                 {

  44.                     *p = 0;

  45.                     p++;

  46.                     if (*p == 0) p--;

  47.                 }

  48.                 ullAvailPhys += atoll(num) * 1024;

  49.                 numFound++;

  50.                 break;

  51.             }

  52.             i++;

  53.         }

  54.         p++;

  55.     }

  56. }



  57. void AndroidMemInfo::GetTotalPhys()

  58. {

  59.     m_memFile = open("/proc/meminfo", O_RDONLY);



  60.     if (m_memFile < 0)

  61.     {

  62.         LOGW("Unable to open /proc/meminfo");

  63.     }



  64.     char buffer[256];

  65.     const int len = read(m_memFile, buffer, sizeof(buffer)-1);

  66.     close(m_memFile);



  67.     if (len < 0)

  68.     {

  69.         LOGW("Unable to read /proc/meminfo");

  70.     }

  71.     

  72.     buffer[len] = 0;

  73.     

  74.     static const char* const sums[] = { "MemTotal:", NULL };

  75.     static const int sumsLen[] = { strlen("MemTotal:"), NULL };



  76.     char* p = buffer;

  77.     while (*p )

  78.     {

  79.         int i = 0;

  80.         while (sums[i])

  81.         {

  82.             if (strncmp(p, sums[i], sumsLen[i]) == 0)

  83.             {

  84.                 p += sumsLen[i];

  85.                 while (*p == ' ') p++;

  86.                 char* num = p;

  87.                 while (*p >= '0' && *p <= '9') p++;

  88.                 if (*p != 0)

  89.                 {

  90.                     *p = 0;

  91.                     p++;

  92.                     if (*p == 0) p--;

  93.                 }

  94.                 ullTotalPhys += atoll(num) * 1024;

  95.                 break;

  96.             }

  97.             i++;

  98.         }

  99.         p++;

  100.     }

  101. }



  102. AndroidCpuUsage::AndroidCpuUsage()

  103. {

  104.     timerclear(&m_lasttime);

  105.     timerclear(&m_nowtime);

  106.     

  107.     m_lastproc.utime = 0;

  108.     m_lastproc.stime = 0;

  109.     m_lastproc.cutime = 0;

  110.     m_lastproc.cstime = 0;

  111.     

  112.     m_nowproc.utime = 0;

  113.     m_nowproc.stime = 0;

  114.     m_nowproc.cutime = 0;

  115.     m_nowproc.cstime = 0;

  116. }



  117. void AndroidCpuUsage::getProcCPUStat(struct systemCPUdata *proc, int pid)

  118. {

  119.   FILE *file;

  120.   unsigned long itemp;

  121.   char ctemp;

  122.   char line[255];



  123.   sprintf(line, "/proc/%d/stat", pid);

  124.   file = fopen(line, "r");

  125.   if (file == NULL)

  126.   {

  127.         LOGW("AndroidCpuUsage Error: process not found!\n");

  128.         return;

  129.   }

  130.   fgets(line, 255, file);

  131.   fclose(file);

  132.   sscanf(line, "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu "

  133.            "%ld %ld %ld %ld %ld %ld %lu %lu %ld",

  134.            (int*)&itemp, proc->pname, &ctemp, (int*)&itemp, (int*)&itemp, (int*)&itemp, (int*)&itemp, (int*)&itemp,

  135.            &itemp, &itemp, &itemp, &itemp, &itemp, &proc->utime, &proc->stime,

  136.            &proc->cutime, &proc->cstime, &itemp, &itemp, &itemp, &itemp, &itemp, &proc->vmem, &proc->rmem

  137.            );



  138. }



  139. DWORD AndroidCpuUsage::GetUsedCpu()

  140. {



  141.     gettimeofday(&m_nowtime, NULL);

  142.     

  143.     getProcCPUStat(&m_nowproc,getpid());

  144.     DWORD percent = 0;

  145.     

  146.     // for the first time ,it's always return 0;

  147.     if ( m_lasttime.tv_sec == 0 && m_lasttime.tv_usec == 0)

  148.     {

  149.         percent = 0;

  150.     }

  151.     else

  152.     {

  153.         DWORD usedtime = m_nowproc.utime + m_nowproc.stime - m_lastproc.utime - m_lastproc.stime;

  154.         percent = usedtime/(m_nowtime.tv_sec - m_lasttime.tv_sec);

  155.     }

  156.     m_lastproc.utime = m_nowproc.utime;

  157.     m_lastproc.stime = m_nowproc.stime;

  158.     m_lastproc.cutime = m_nowproc.cutime;

  159.     m_lastproc.cstime = m_nowproc.cstime;

  160.     

  161.     m_lasttime.tv_sec = m_nowtime.tv_sec;

  162.     m_lasttime.tv_usec = m_nowtime.tv_usec;

  163.     

  164.     char tchBuf[128] = "\0";

  165.     memset(tchBuf, 0, sizeof(tchBuf));

  166.     snprintf(tchBuf, sizeof(tchBuf), "AndroidCpuUsage GetUsedCpu:%d",percent);

  167.     

  168.     LOGW(tchBuf);

  169.     return percent;

  170. }

 

 

 

阅读(3783) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~