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

2023-11-14

有不少朋友问在qnx下如何获取内存及cpu占有率等等,想到两年前自己做过一个类似windows里的任务管理器的东东,里面有一部分就是获取内存,cpu,磁盘及进程信息的GUI程序,记得也美其名曰xxxTaskMan。把里面的关键代码写下来供兄弟们参考。界面部分就不公布了,无非是一些窗口,按钮,list等等。

 

一、ProcInfo.h

 

  1. // ProcInfo.h: interface for the CProcInfo class.
  2. //
  3. //
  4. #if !defined(AFX_PROCINFO_H__E3782DFC_59DE_45FC_BF1F_D8C8BF0181C1__INCLUDED_)
  5. #define AFX_PROCINFO_H__E3782DFC_59DE_45FC_BF1F_D8C8BF0181C1__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. class CProcInfo  
  10. {
  11. public:
  12.     CProcInfo();
  13.     virtual ~CProcInfo();
  14. public:
  15.     static void InitSysInfo();
  16.     static void ClearSysInfo();
  17.     static int GetFreeMem();
  18.     static int GetTotalMem();
  19.     static int GetFreeMemPercent();
  20.     static void GetSysInfo(int &TotalMem,int &CpuSpeed,int &BootTime,char *pszCpuName);
  21.     static void GetDiskInfo(int &Total,int &Free);
  22.     static int  GetFreeDiskPercent();
  23.     static int normalize_data_size(int &size);
  24.     
  25.     static bool GetProcName(const int iPid,char *pszProcName,int &fd);
  26.     static bool GetSingleProcInfo(const int fd,long &StartTsp,int &CpuTime,int &MemSize);
  27. private:
  28.     static int s_iTotalMem;
  29.     static int s_iCpuSpeed;
  30.     static int s_iBootTime;
  31.     static char s_strCpuName[32];
  32.     static int s_hSysProc;
  33.     static int s_hRootFile;
  34. };
  35. #endif // !defined(AFX_PROCINFO_H__E3782DFC_59DE_45FC_BF1F_D8C8BF0181C1__INCLUDED_)

使用方法,启动后先调用一次InitSysInfo(),结束后调用一次ClearSysInfo()进行清理。

 

2、ProcInfo.cpp代码

 

 

 

  1. // ProcInfo.cpp: implementation of the CProcInfo class.
  2. //
  3. //
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/iofunc.h>
  11. #include <sys/dispatch.h>
  12. #include <sys/neutrino.h>
  13. #include <sys/procfs.h>
  14. #include <sys/stat.h>
  15. #include "ProcInfo.h"
  16. #include <libgen.h>
  17. //
  18. // Construction/Destruction
  19. //
  20. int CProcInfo::s_iTotalMem;
  21. int CProcInfo::s_iCpuSpeed;
  22. int CProcInfo::s_iBootTime;
  23. char CProcInfo::s_strCpuName[32]={'/0'};
  24.     
  25. int CProcInfo::s_hSysProc=-1;
  26. int CProcInfo::s_hRootFile=-1;
  27. CProcInfo::CProcInfo()
  28. {
  29. }
  30. CProcInfo::~CProcInfo()
  31. {
  32. }
  33. void CProcInfo::InitSysInfo()
  34. {
  35.     char                    buffer[50];
  36.     procfs_sysinfo          *sysinfo;
  37.     struct cpuinfo_entry    *cpu;
  38.     int                     i;
  39.     sprintf(buffer, "/proc");
  40.     
  41.     if ((s_hSysProc = open(buffer, O_RDONLY)) == -1)
  42.         printf( "couldn't open %s: %s/n", buffer, strerror(errno));
  43.     sysinfo = (procfs_sysinfo *)buffer;
  44.     if (devctl(s_hSysProc, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
  45.         printf( "couldn't get info for %s: %s/n", buffer, strerror(errno));
  46.     i = sysinfo->total_size;
  47.     if(!(sysinfo = (procfs_sysinfo *)alloca(i)))
  48.         printf( "couldn't get memory for %s: %s/n", buffer, strerror(errno));
  49.     if (devctl(s_hSysProc, DCMD_PROC_SYSINFO, sysinfo, i, 0) != EOK)
  50.         printf( "couldn't get info for %s: %s/n", buffer, strerror(errno));
  51.     s_iTotalMem = _SYSPAGE_ENTRY(sysinfo, system_private)->ramsize;
  52.     s_iBootTime = _SYSPAGE_ENTRY(sysinfo, qtime)->boot_time;
  53.     cpu = _SYSPAGE_ENTRY(sysinfo, cpuinfo);
  54. //  printf("%s ", &_SYSPAGE_ENTRY(sysinfo, strings)->data[cpu->name]); //实际name
  55.     strcpy(s_strCpuName,"Cyrix X86");
  56.     s_iCpuSpeed=cpu->speed;
  57. }
  58. void CProcInfo::ClearSysInfo()
  59. {
  60.     close(s_hSysProc);
  61.     s_hSysProc=-1;
  62.     close(s_hRootFile);
  63.     s_hRootFile=-1;
  64. }
  65. void CProcInfo::GetSysInfo(int &TotalMem,int &CpuSpeed,int &BootTime,char *pszCpuName)
  66. {
  67.     TotalMem=s_iTotalMem;
  68.     CpuSpeed=s_iCpuSpeed;
  69.     BootTime=s_iBootTime;
  70.     if (pszCpuName)
  71.     {
  72.         strcpy(pszCpuName,s_strCpuName);
  73.     }
  74. }
  75. int CProcInfo::GetFreeMem()
  76. {
  77.     if (s_hSysProc==-1)
  78.     {
  79.         s_hSysProc=open("/proc", O_RDONLY);
  80.     }
  81.     struct stat             st;
  82.     if (fstat(s_hSysProc, &st) == -1)
  83.         printf( "couldn't get stat info for %s: %s/n""/proc", strerror(errno));
  84.     return st.st_size;
  85. }
  86. int CProcInfo::GetTotalMem()
  87. {
  88.     return s_iTotalMem;
  89. }
  90.     
  91. int CProcInfo::GetFreeMemPercent()
  92. {
  93.     int freemem=GetFreeMem();
  94.     return ( (int)( freemem*100.0/s_iTotalMem ) );
  95. }
  96. int CProcInfo::GetFreeDiskPercent()
  97. {
  98.     int iTotal,iFree;
  99.     GetDiskInfo(iTotal,iFree);
  100.     return ( (int)( iFree*100.0/iTotal ) );
  101. }
  102. int CProcInfo::normalize_data_size(int &size) {
  103.     char sym = 0;
  104.     if (size > 8192) {
  105.         size /= 1024;
  106.         sym = 'K';
  107.         if (size > 8192) {
  108.             size /= 1024;
  109.             sym = 'M';
  110.         } 
  111.         if (size > 8192) {
  112.             size /= 1024;
  113.             sym = 'G';
  114.         }
  115.         if (size > 8192) {
  116.             size /= 1024;
  117.             sym = 'T';
  118.         }
  119.     }
  120.     return sym;
  121. }
  122. void CProcInfo::GetDiskInfo(int &Total,int &Free)
  123. {
  124.     if (-1==s_hRootFile)
  125.     {
  126.         s_hRootFile = open("/",O_RDONLY);
  127.     }
  128.     if (-1==s_hRootFile)
  129.     {
  130.         Total=-1;
  131.         Free=-1;
  132.         return;
  133.     }
  134.     struct statvfs FileBuff;
  135.     int tmpret=0;
  136.     
  137.     if (-1==(tmpret=fstatvfs(s_hRootFile,&FileBuff)))
  138.     {
  139.         printf("fstatvfs ret=%d/n",tmpret);
  140.         printf("fstatvfs is:%s/n",strerror(tmpret)); 
  141.         close(s_hRootFile);
  142.         Total=-1;
  143.         Free=-1;
  144.         return ;
  145.     }
  146.     
  147. //  printf("f_blocks=%d,bfree=%d,f_bsize=%ld/n",FileBuff.f_blocks,FileBuff.f_bfree,FileBuff.f_bsize);
  148. //  printf("DT=%ld,DF=%ld/n",FileBuff.f_blocks*FileBuff.f_bsize,FileBuff.f_bfree*FileBuff.f_bsize);
  149.     Total=FileBuff.f_blocks*FileBuff.f_bsize;
  150.     Free=FileBuff.f_bfree*FileBuff.f_bsize; 
  151. }
  152.     
  153. bool CProcInfo::GetProcName(const int iPid,char *pszProcName,int &fd)
  154. {
  155.     if (!pszProcName)
  156.     {
  157.         return false;
  158.     }
  159.     char            buf[PATH_MAX + 1];
  160.     struct dinfo_s {
  161.         procfs_debuginfo    info;
  162.         char                pathbuffer[PATH_MAX]; /* 1st byte is info.path[0] */
  163.     }dinfo;
  164.     
  165.     sprintf(buf, "/proc/%d/as", iPid);
  166.     
  167.     if ((fd = open(buf, O_RDONLY|O_NONBLOCK)) == -1){
  168.         return false;
  169.     }
  170.     if (devctl(fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL) != EOK) {
  171.         close(fd);
  172.         return false;
  173.     }
  174.     strcpy(pszProcName,basename(dinfo.info.path));
  175.     return true;
  176. }
  177. //StartTsp:单位:秒
  178. //CpuTime:单位:毫秒
  179. //MemSize:单位:字节
  180. bool CProcInfo::GetSingleProcInfo(const int fd,long &StartTsp,int &CpuTime,int &MemSize)
  181. {
  182.     procfs_info     infos;
  183.     if (-1==fd)
  184.     {
  185.         return false;
  186.     }
  187.        
  188.     if (devctl(fd, DCMD_PROC_INFO, &infos, sizeof infos, 0) != EOK) {
  189.         return false;
  190.     }
  191.     
  192.     StartTsp=infos.start_time/1000000000;       
  193.     CpuTime=(infos.stime + infos.utime)/1000000;
  194.     //MemSize
  195.     MemSize=0;//
  196.     procfs_mapinfo      *mapinfos = NULL;
  197.     static int      num_mapinfos = 0;
  198.     procfs_mapinfo      *mapinfo_p;
  199.     int             flags = ~0, err, num, i;
  200.     // Get the number of map entrys
  201.     if((err = devctl(fd, DCMD_PROC_MAPINFO, NULL, 0, &num )) != EOK)
  202.     {
  203.         printf("failed devctl num mapinfos - %d (%s)/n", err, strerror(err));
  204.         return false;
  205.     }
  206.     // malloc enough memory for all of them
  207.     if ( (mapinfos = (procfs_mapinfo*)malloc( num*sizeof(procfs_mapinfo) )) == NULL )
  208.     {
  209.         printf("failed malloc - %d (%s)/n", err, strerror(err));
  210.         return false;
  211.     }
  212.     num_mapinfos = num;
  213.     mapinfo_p = mapinfos;
  214.     // fill the map entrys  
  215.     if((err = devctl(fd, DCMD_PROC_MAPINFO, mapinfo_p, num*sizeof(procfs_mapinfo), &num)) != EOK)
  216.     {
  217.         printf("failed devctl mapinfos - %d (%s)/n", err, strerror(err));
  218.         free(mapinfos);
  219.         return false;
  220.     }
  221.     if (num>num_mapinfos)
  222.     {
  223.         num=num_mapinfos;
  224.     }
  225.     //
  226.     // Run through the list of mapinfo's, and store the data and text info
  227.     // so we can print it at the bottom of the loop.
  228.     //
  229.     for ( mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++ )
  230.     {
  231.         if ( !(mapinfo_p->flags & flags) )
  232.             mapinfo_p->ino = 0;
  233.         if ( mapinfo_p->ino == 0 ) /* already visited */
  234.             continue;
  235.         MemSize+=mapinfo_p->size;
  236.     }
  237.     free(mapinfos);
  238.     return true;
  239. }

3、CpuUsed.h

 

 

 

  1. // CpuUsed.h: interface for the CCpuUsed class.
  2. //
  3. //
  4. #if !defined(AFX_CPUUSED_H__DA812B5A_099E_43F0_9C27_E84A9F0A8E78__INCLUDED_)
  5. #define AFX_CPUUSED_H__DA812B5A_099E_43F0_9C27_E84A9F0A8E78__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include "QnxHardTimer.h"
  10. class CCpuUsed : public CQnxHardTimer  
  11. {
  12. public:
  13.     CCpuUsed();
  14.     virtual ~CCpuUsed();
  15.     virtual void  RepeatTimerThread();
  16.     int GetCpuUsed();
  17. public:
  18.     static CCpuUsed s_CpuUsed;
  19. protected:
  20.     int m_iCpuUsed;
  21.     clockid_t m_iClockID;
  22.     uint64_t  m_ut_old;
  23.     struct timespec m_tt_old;
  24. };
  25. #endif // !defined(AFX_CPUUSED_H__DA812B5A_099E_43F0_9C27_E84A9F0A8E78__INCLUDED_)

4、CpuUsed.cpp

 

 

 

 

  1. // CpuUsed.cpp: implementation of the CCpuUsed class.
  2. //
  3. //
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include "CpuUsed.h"
  7. //
  8. // Construction/Destruction
  9. //
  10. CCpuUsed CCpuUsed::s_CpuUsed;
  11. CCpuUsed::CCpuUsed()
  12. {
  13.     m_iClockID=ClockId(1, 1); //取空闲线程;
  14.     m_ut_old=0;
  15. }
  16. CCpuUsed::~CCpuUsed()
  17. {
  18. }
  19. void  CCpuUsed::RepeatTimerThread()
  20. {
  21.     uint64_t  ut_now=0;
  22.     struct timespec tt_now;
  23.     uint64_t  ut_sub; //时间差
  24.     uint64_t  ut_tmp; 
  25.     ClockTime(m_iClockID, NULL, &ut_now);
  26.     clock_gettime( CLOCK_REALTIME, &tt_now);
  27.     ut_tmp=tt_now.tv_sec-m_tt_old.tv_sec;
  28.     ut_sub=ut_tmp*1000000000+tt_now.tv_nsec-m_tt_old.tv_nsec;
  29.     m_iCpuUsed=(int)( ((float)(ut_sub-(ut_now-m_ut_old))/(float)ut_sub)*100 );
  30.     m_ut_old=ut_now;
  31.     m_tt_old=tt_now;
  32. //  printf("m_iCpuUsed=%d%%/n",m_iCpuUsed);
  33. }
  34. int CCpuUsed::GetCpuUsed()
  35. {
  36.     if (m_iCpuUsed>100)
  37.     {
  38.         return 100;
  39.     }else if (m_iCpuUsed<0)
  40.     {
  41.         return 0;
  42.     }else
  43.         return m_iCpuUsed;
  44. }

注意,CCpuUsed类继承了我自己封装的一个CQnxHardTimer,其实也就是一个定时器,改定时器会重复调用RepeatTimerThread(),在这里面就可以进行系统cpu占用率的计算。用户只需要调用GetCpuUsed()就可以了。

 

5、参考资料

 

pidin的源码。在qnx已公开的源码里就有。

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • Redis过期策略与淘汰策略

    redis为什么这么快 原因之一就是Redis操作都是基于内存的 既然是基于内存的 而内存的大小是有限的 当内存不足或占用过高时 Redis会采用内存淘汰机制进行数据淘汰 一 Redis的过期策略 Redis在设置缓存数据时指定了过期时间
  • 前端常用第三方库

    常用功能的第三方插件集合 一 下载 1 原生 1 a 标签 只能同源下载 2 iframe 下载 支持跨域get请求 3 Form 表单下载 支持跨域get和post请求 2 第三方插件 1 FileSaver js 2 StreamSav
  • [leetcode] 358. Rearrange String k Distance Apart 解题报告

    题目链接 https leetcode com problems rearrange string k distance apart Given a non empty string str and an integer k rearran
  • lock与unlock用法(简单易懂)

    lock最简单的用法就是 在多线程中 我们往往要操作同一块内存 但是同一时间只允许一个线程操作 其他想要操作 只能等到被释放 这个时候就可以用到lock 文件共享锁 用法 mutex 名称 例如 mutex testlock 举个例子 例如
  • 笔记--Ubuntu18.04安装Deepstream 6.0

    目录 1 安装依赖 2 安装Cuda Toolkit 11 4 1 3 安装cudnn 8 2 4 安装TensorRT 8 0 1 5 安装librdkafka 6 安装 deepstream sdk 7 验证与测试 8 问题记录 参考
  • ChatGPT是如何辅助高效撰写论文及使用ChatGPT注意事项

    ChatGPT发布近1年 各大高校对它的态度也发生了极大转变 今年3月发布ChatGPT禁令的牛剑等世界顶级名校也在近期解除了ChatGPT禁令 发布了生成式人工智能使用指南 ChatGPT一定程度上可以解放科研人员的劳动力 与其直接禁止不
  • IDEA 导入依赖,还是报java.lang.NoClassDefFoundError

    java lang NoClassDefFoundError net coobird thumbnailator Thumbnails IDEA 导入依赖 还是报java lang NoClassDefFoundError maven 里面
  • Powershell简介

    Powershell简介 PowerShell是一种功能强大的脚本语言和shell程序框架 主要用于Windows计算机方便管理员进行系统管理并有可能在未来取代Windows上的默认命令提示符 PowerShell脚本因其良好的功能特性常用
  • IPsec over GRE 和GRE over IPsec比较和区别与配置

    GRE over IPsec IPsec over GRE I PSec Over GRE是先ipsec后gre 这种我没用过 GRE Over IPSec 是先gre后ipsec 也就是说ipsec是最后的承载方式 一般常用的就是这种 解
  • webpack4+动态导入(按需加载方式)

    此文为复习或升级webpack过程中的总结 webpack版本为4 29 5 webpack4 动态导入 目的 减少包体积 按需加载 常见使用情况有以下3中 1 使用import 的情况 output filename bundle js
  • $‘\r‘: 未找到命令的解决办法

    前言 有时我们在执行shell脚本的时候 会出现 r 未找到命令 错误 绝大多数情况下是因为在window系统中写的shell脚本 然后上传到服务器执行造成的 解决办法如下 1 安装dos2unix yum install dos2unix
  • 用汇编语言实现从键盘输入一个字符,在下一行上以十六进制形式输出它的ASCII码。

    在我之前的一篇博客里已经写过如何输出一个字符的ASCII码的十进制形式 以十六进制形式输出的方法与其相差不多 详情可查看我的专栏 汇编语言 里的第一篇博客 要求如下 代码如下 data segment string1 db 5 dup st
  • SpringBoot中的bean注入方式和原理介绍

    Spring Boot是一个非常流行的Java框架 它可以帮助开发者快速地构建高效 健壮的应用程序 其中一个重要的功能就是依赖注入 也就是将一个对象注入到另一个对象中 以便它们可以相互协作 在Spring Boot中 依赖注入是通过bean
  • 事务的相关内容

    主要用于处理操作量大 复杂度高的数据 将一组SQL放在一个批次中去执行 事务原则 ACID原则 原子性 一致性 隔离性 持久性 脏读 幻读 原子性 要么都成功 要么都失败 一致性 事务前后的数据完整性要保证一致 持久性 事务一旦提交则不可逆
  • DevExpress ASP.NET Wizard使用

    1 新建项目 2 选择主题后按 Run Wizard 3 选择布局custom 按 4 选择主题 按 5 设置网站设置 6 设置组件设置 设置本地化 修改标题 修改左侧树
  • 带头双向循环链表基础知识归纳

    本节博主将归纳带头双向循环链表的初始化 头尾插和头尾删 首先让我们一起瞅瞅这链表长啥样 图像来源 图解几种常见的线性表 命中水 cxiansheng cn 在这张图中我们能够得出一个双向循环链表所需要的结构体基本框架 struct node
  • 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;

    将输入的字符串 字符串仅包含小写字母 a 到 z 按照如下规则 循环转换后输出 a gt b b gt c y gt z z gt a 若输入的字符串连续出现两个字母相同时 后一个字母需要连续转换2次 例如 aa 转换为 bc zz 转换为
  • 解决maven出现“连接到http://repo.maven.apache.org被拒绝”的问题

    1 原因 这个问题的原因就是无法连接到http repo maven apache org maven2镜像 2 解决办法 修改maven默认镜像仓库地址为国内可用的镜像地址 比如这里推荐阿里的镜像仓库地址 http maven aliyu
  • Android 权限大全

    Key android permission ACCESS CHECKIN PROPERTIES Title 访问检入属性 Memo 允许对检入服务上传的属性进行读 写访问 普通应用程序不能使用此权限 Level 0 Key android
  • QNX下获取系统信息的方法(cpu,内存,进程等等)

    有不少朋友问在qnx下如何获取内存及cpu占有率等等 想到两年前自己做过一个类似windows里的任务管理器的东东 里面有一部分就是获取内存 cpu 磁盘及进程信息的GUI程序 记得也美其名曰xxxTaskMan 把里面的关键代码写下来供兄