본문 바로가기

work/c / c++

cpu성능정보

 
File Information
Path: libstatgrab-0.11.1/src/libstatgrab/
Name: cpu_stats.c
Lines of Code: 199
Total Lines: 255
File Size: 6.41 kB
Package: libstatgrab-0.11.1
  1. /*
  2. * i-scream libstatgrab
  3. * http://www.i-scream.org
  4. * Copyright (C) 2000-2004 i-scream
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * $Id: cpu_stats.c,v 1.25 2004/11/01 18:30:17 tdb Exp $
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26.  
  27. #include <time.h>
  28. #include "statgrab.h"
  29. #include "tools.h"
  30. #ifdef SOLARIS
  31. #include <kstat.h>
  32. #include <sys/sysinfo.h>
  33. #include <string.h>
  34. #endif
  35. #if defined(LINUX) || defined(CYGWIN)
  36. #include <stdio.h>
  37. #endif
  38. #if defined(FREEBSD) || defined(DFBSD)
  39. #include <sys/sysctl.h>
  40. #include <sys/dkstat.h>
  41. #endif
  42. #ifdef NETBSD
  43. #include <sys/types.h>
  44. #include <sys/param.h>
  45. #include <sys/sysctl.h>
  46. #include <sys/sched.h>
  47. #endif
  48. #ifdef OPENBSD
  49. #include <sys/param.h>
  50. #include <sys/sysctl.h>
  51. #include <sys/dkstat.h>
  52. #endif
  53. #ifdef HPUX
  54. #include <sys/param.h>
  55. #include <sys/pstat.h>
  56. #include <sys/dk.h>
  57. #endif
  58.  
  59. static sg_cpu_stats cpu_now;
  60. static int cpu_now_uninit=1;
  61.  
  62. sg_cpu_stats *sg_get_cpu_stats(){
  63.  
  64. #ifdef HPUX
  65. struct pst_dynamic pstat_dynamic;
  66. int i;
  67. #endif
  68. #ifdef SOLARIS
  69. kstat_ctl_t *kc;
  70. kstat_t *ksp;
  71. cpu_stat_t cs;
  72. #endif
  73. #if defined(LINUX) || defined(CYGWIN)
  74. FILE *f;
  75. #endif
  76. #ifdef ALLBSD
  77. #if defined(NETBSD) || defined(OPENBSD)
  78. int mib[2];
  79. #endif
  80. #ifdef NETBSD
  81. u_int64_t cp_time[CPUSTATES];
  82. #else
  83. long cp_time[CPUSTATES];
  84. #endif
  85. size_t size;
  86. #endif
  87.  
  88. cpu_now.user=0;
  89. /* Not stored in linux or freebsd */
  90. cpu_now.iowait=0;
  91. cpu_now.kernel=0;
  92. cpu_now.idle=0;
  93. /* Not stored in linux, freebsd, or hpux */
  94. cpu_now.swap=0;
  95. cpu_now.total=0;
  96. /* Not stored in solaris */
  97. cpu_now.nice=0;
  98.  
  99. #ifdef HPUX
  100. if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1) {
  101. sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic");
  102. return NULL;
  103. }
  104. cpu_now.user = pstat_dynamic.psd_cpu_time[CP_USER];
  105. cpu_now.iowait = pstat_dynamic.psd_cpu_time[CP_WAIT];
  106. cpu_now.kernel = pstat_dynamic.psd_cpu_time[CP_SSYS] + pstat_dynamic.psd_cpu_time[CP_SYS];
  107. cpu_now.idle = pstat_dynamic.psd_cpu_time[CP_IDLE];
  108. cpu_now.nice = pstat_dynamic.psd_cpu_time[CP_NICE];
  109. for (i = 0; i < PST_MAX_CPUSTATES; i++) {
  110. cpu_now.total += pstat_dynamic.psd_cpu_time[i];
  111. }
  112. #endif
  113. #ifdef SOLARIS
  114. if ((kc = kstat_open()) == NULL) {
  115. sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
  116. return NULL;
  117. }
  118. for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
  119. if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
  120. if (kstat_read(kc, ksp, &cs) == -1) {
  121. continue;
  122. }
  123. cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER];
  124. cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT];
  125. cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL];
  126. cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE];
  127. cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES];
  128. }
  129.  
  130. cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap;
  131. #endif
  132. #if defined(LINUX) || defined(CYGWIN)
  133. if ((f=fopen("/proc/stat", "r" ))==NULL) {
  134. sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/stat");
  135. return NULL;
  136. }
  137. /* The very first line should be cpu */
  138. if((fscanf(f, "cpu %lld %lld %lld %lld", \
  139. &cpu_now.user, \
  140. &cpu_now.nice, \
  141. &cpu_now.kernel, \
  142. &cpu_now.idle)) != 4){
  143. sg_set_error(SG_ERROR_PARSE, "cpu");
  144. fclose(f);
  145. return NULL;
  146. }
  147.  
  148. fclose(f);
  149.  
  150. cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
  151. #endif
  152. #ifdef ALLBSD
  153. #if defined(FREEBSD) || defined(DFBSD)
  154. size = sizeof cp_time;
  155. if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){
  156. sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "kern.cp_time");
  157. return NULL;
  158. }
  159. #else
  160. mib[0] = CTL_KERN;
  161. #ifdef NETBSD
  162. mib[1] = KERN_CP_TIME;
  163. #else
  164. mib[1] = KERN_CPTIME;
  165. #endif
  166. size = sizeof cp_time;
  167. if (sysctl(mib, 2, &cp_time, &size, NULL, 0) < 0) {
  168. #ifdef NETBSD
  169. sg_set_error_with_errno(SG_ERROR_SYSCTL,
  170. "CTL_KERN.KERN_CP_TIME");
  171. #else
  172. sg_set_error_with_errno(SG_ERROR_SYSCTL,
  173. "CTL_KERN.KERN_CPTIME");
  174. #endif
  175. return NULL;
  176. }
  177. #endif
  178.  
  179. cpu_now.user=cp_time[CP_USER];
  180. cpu_now.nice=cp_time[CP_NICE];
  181. cpu_now.kernel=cp_time[CP_SYS];
  182. cpu_now.idle=cp_time[CP_IDLE];
  183. cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle;
  184.  
  185. #endif
  186.  
  187. cpu_now.systime=time(NULL);
  188. cpu_now_uninit=0;
  189.  
  190.  
  191. return &cpu_now;
  192. }
  193.  
  194. sg_cpu_stats *sg_get_cpu_stats_diff(){
  195. static sg_cpu_stats cpu_diff;
  196. sg_cpu_stats cpu_then, *cpu_tmp;
  197.  
  198. if (cpu_now_uninit){
  199. if((cpu_tmp=sg_get_cpu_stats())==NULL){
  200. /* Should sg_get_cpu_stats fail */
  201. return NULL;
  202. }
  203. return cpu_tmp;
  204. }
  205.  
  206.  
  207. cpu_then.user=cpu_now.user;
  208. cpu_then.kernel=cpu_now.kernel;
  209. cpu_then.idle=cpu_now.idle;
  210. cpu_then.iowait=cpu_now.iowait;
  211. cpu_then.swap=cpu_now.swap;
  212. cpu_then.nice=cpu_now.nice;
  213. cpu_then.total=cpu_now.total;
  214. cpu_then.systime=cpu_now.systime;
  215.  
  216. if((cpu_tmp=sg_get_cpu_stats())==NULL){
  217. return NULL;
  218. }
  219.  
  220. cpu_diff.user = cpu_now.user - cpu_then.user;
  221. cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel;
  222. cpu_diff.idle = cpu_now.idle - cpu_then.idle;
  223. cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait;
  224. cpu_diff.swap = cpu_now.swap - cpu_then.swap;
  225. cpu_diff.nice = cpu_now.nice - cpu_then.nice;
  226. cpu_diff.total = cpu_now.total - cpu_then.total;
  227. cpu_diff.systime = cpu_now.systime - cpu_then.systime;
  228.  
  229. return &cpu_diff;
  230. }
  231.  
  232. sg_cpu_percents *sg_get_cpu_percents(){
  233. static sg_cpu_percents cpu_usage;
  234. sg_cpu_stats *cs_ptr;
  235.  
  236. if(cs_ptr==NULL){
  237. return NULL;
  238. }
  239.  
  240. cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100;
  241. cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100;
  242. cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100;
  243. cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100;
  244. cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100;
  245. cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100;
  246. cpu_usage.time_taken = cs_ptr->systime;
  247.  
  248. return &cpu_usage;
  249.  
  250. }
  251.