mac.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "plt.h"
  2. static struct cpu_info_t cpu_info[2];
  3. void mac_get_cpu_info(struct cpu_info_t* cpu_info)
  4. {
  5. FILE *fd;
  6. char buff[256];
  7. memset(buff, '\0', 256);
  8. fd = fopen("/proc/stat", "r");
  9. fgets(buff, sizeof(buff), fd);
  10. sscanf(buff, "%s %u %u %u %u", cpu_info->name, &cpu_info->user, &cpu_info->nic,
  11. &cpu_info->system, &cpu_info->idle);
  12. fclose(fd);
  13. }
  14. float mac_cal_cpu_occupy(struct cpu_info_t *first, struct cpu_info_t*second)
  15. {
  16. unsigned long old_CPU_Time, new_CPU_Time;
  17. unsigned long usr_Time_Diff, sys_Time_Diff, nic_Time_Diff;
  18. float cpu_use = 0.0;
  19. old_CPU_Time = (unsigned long)(first->user + first->nic + first->system + first->idle);
  20. new_CPU_Time = (unsigned long)(second->user + second->nic + second->system + second->idle);
  21. usr_Time_Diff = (unsigned long)(second->user - first->user);
  22. sys_Time_Diff = (unsigned long)(second->system - first->system);
  23. nic_Time_Diff = (unsigned long)(second->nic -first->nic);
  24. if ((new_CPU_Time - old_CPU_Time) != 0)
  25. cpu_use = (float)100*(usr_Time_Diff + sys_Time_Diff + nic_Time_Diff)/(new_CPU_Time - old_CPU_Time);
  26. else
  27. cpu_use = 0.0;
  28. return cpu_use;
  29. }
  30. /* unit:% */
  31. float mac_get_cpu_occupy()
  32. {
  33. float cpu_rate;
  34. struct cpu_info_t first, second;
  35. mac_get_cpu_info(&first);
  36. sleep(5);
  37. mac_get_cpu_info(&second);
  38. cpu_rate = mac_cal_cpu_occupy(&first, &second);
  39. return cpu_rate;
  40. }
  41. struct mem_info_t{
  42. char name[20];
  43. unsigned long total;
  44. char name2[20];
  45. };
  46. /* unit:% */
  47. float mac_get_mem_occupy()
  48. {
  49. struct sysinfo tmp;
  50. int ret = 0;
  51. float free, total;
  52. ret = sysinfo(&tmp);
  53. if (ret == 0) {
  54. free = (unsigned long)tmp.freeram/(1024*1024);
  55. total = (unsigned long)tmp.totalram/(1024*1024);
  56. return (total - free)/total*100.0;
  57. } else {
  58. return -1.0;
  59. }
  60. }
  61. float mac_get_disk_occupy()
  62. {
  63. FILE * fp;
  64. int h=0;
  65. char buffer[80],a[80],d[80],e[80],f[80],buf[256];
  66. double c,b;
  67. double dev_total=0,dev_used=0;
  68. float occupy;
  69. fp = popen("df","r");
  70. fgets(buf,256,fp);
  71. while(6==fscanf(fp,"%s %lf %lf %s %s %s",a,&b,&c,d,e,f)){
  72. dev_total+=b;
  73. dev_used+=c;
  74. }
  75. occupy = dev_used/dev_total*100;
  76. pclose(fp);
  77. return occupy;
  78. }
  79. int mac_init( void )
  80. {
  81. int ret = 0;
  82. MDL.mac.cal_timer = 0;
  83. MDL.mac.cal_intv = 60;
  84. MDL.mac.cpu_occupy = 0.0;
  85. MDL.mac.mem_occupy = 0.0;
  86. MDL.mac.disk_occupy = 0.0;
  87. mac_get_cpu_info(&cpu_info[0]);
  88. log_dbg("%s, ret:%d", __func__, ret);
  89. return ret;
  90. }
  91. void mac_exe( void )
  92. {
  93. if( MDL.mac.cal_timer++ >= MDL.mac.cal_intv ){
  94. MDL.mac.cal_timer = 0;
  95. mac_get_cpu_info(&cpu_info[1]);
  96. MDL.mac.cpu_occupy = mac_cal_cpu_occupy(&cpu_info[0], &cpu_info[1]);
  97. cpu_info[0] = cpu_info[1];
  98. MDL.mac.mem_occupy = mac_get_mem_occupy();
  99. //mdl.mac.disk_occupy = mac_get_disk_occupy();
  100. }
  101. }