使用C的系统调用,如何获得CPU的利用率?

前端之家收集整理的这篇文章主要介绍了使用C的系统调用,如何获得CPU的利用率?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在FreeBSD上的C中,如何访问cpu利用率?

我正在编写一些代码来处理HTTP重定向.如果cpu负载超过FReeBSD系统的阈值,我想重定向客户端请求.查看手册页,kvm_getpcpu()似乎是正确的答案,但手册页(我读过)没有记录用法.

任何提示或指示都会受到欢迎 – 谢谢!

在这里阅读答案后,我能够提出以下内容.由于文档很差,我不是100%肯定它是正确的,但顶部似乎同意.感谢所有回答的人.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/sysctl.h>
  5. #include <unistd.h>
  6.  
  7. #define CP_USER 0
  8. #define CP_NICE 1
  9. #define CP_SYS 2
  10. #define CP_INTR 3
  11. #define CP_IDLE 4
  12. #define cpuSTATES 5
  13.  
  14. int main()
  15. {
  16. long cur[cpuSTATES],last[cpuSTATES];
  17. size_t cur_sz = sizeof cur;
  18. int state,i;
  19. long sum;
  20. double util;
  21.  
  22. memset(last,sizeof last);
  23.  
  24. for (i=0; i<6; i++)
  25. {
  26. if (sysctlbyname("kern.cp_time",&cur,&cur_sz,NULL,0) < 0)
  27. {
  28. printf ("Error reading kern.cp_times sysctl\n");
  29. return -1;
  30. }
  31.  
  32. sum = 0;
  33. for (state = 0; state<cpuSTATES; state++)
  34. {
  35. long tmp = cur[state];
  36. cur[state] -= last[state];
  37. last[state] = tmp;
  38. sum += cur[state];
  39. }
  40.  
  41. util = 100.0L - (100.0L * cur[CP_IDLE] / (sum ? (double) sum : 1.0L));
  42. printf("cpu utilization: %7.3f\n",util);
  43. sleep(1);
  44. }
  45.  
  46. return 0;
  47. }

解决方法

从MAN页面

名称

kvm_getmaxcpu,kvm_getpcpu – 访问每cpu数据

图书馆

内核数据访问库(libkvm,-lkvm)

概要

  1. #include <sys/param.h>
  2. #include <sys/pcpu.h>
  3. #include <sys/sysctl.h>
  4. #include <kvm.h>
  5.  
  6. int
  7. kvm_getmaxcpu(kvm_t *kd);
  8.  
  9. void *
  10. kvm_getpcpu(kvm_t *kd,int cpu);

描述

kvm_getmaxcpu()和kvm_getpcpu()函数用于访问
由kd指示的内核中活动处理器的每cpu数据.该
kvm_getmaxcpu()函数返回支持的最大cpu
内核. kvm_getpcpu()函数返回一个缓冲区,其中包含
单个cpucpu数据.这个缓冲区由struct pcpu描述
类型.调用者负责通过调用释放缓冲区
免费(3)不再需要时.如果cpu不活动,则为NULL
而是返回.

CACHING

这些函数缓存各种内核变量的nlist值
在连续的调用中重复使用.您可以使用kd set调用任一函数
为NULL以清除此缓存.

返回值

成功时,kvm_getmaxcpu()函数返回最大数量
内核支持cpu.如果发生错误,则返回-1.

成功时,kvm_getpcpu()函数返回指向已分配的指针
缓冲区或NULL.如果发生错误,则返回-1.

如果任一函数遇到错误,则可能出现错误消息
通过kvm_geterr检索(3.)

编辑

这是kvm_t结构:

  1. struct __kvm {
  2. /*
  3. * a string to be prepended to error messages
  4. * provided for compatibility with sun's interface
  5. * if this value is null,errors are saved in errbuf[]
  6. */
  7. const char *program;
  8. char *errp; /* XXX this can probably go away */
  9. char errbuf[_POSIX2_LINE_MAX];
  10. #define ISALIVE(kd) ((kd)->vmfd >= 0)
  11. int pmfd; /* physical memory file (or crashdump) */
  12. int vmfd; /* virtual memory file (-1 if crashdump) */
  13. int unused; /* was: swap file (e.g.,/dev/drum) */
  14. int nlfd; /* namelist file (e.g.,/kernel) */
  15. struct kinfo_proc *procbase;
  16. char *argspc; /* (dynamic) storage for argv strings */
  17. int arglen; /* length of the above */
  18. char **argv; /* (dynamic) storage for argv pointers */
  19. int argc; /* length of above (not actual # present) */
  20. char *argbuf; /* (dynamic) temporary storage */
  21. /*
  22. * Kernel virtual address translation state. This only gets filled
  23. * in for dead kernels; otherwise,the running kernel (i.e. kmem)
  24. * will do the translations for us. It could be big,so we
  25. * only allocate it if necessary.
  26. */
  27. struct vmstate *vmst;
  28. };

猜你在找的C&C++相关文章