c - 使用来自 C 的系统调用,我如何获得 CPU 的利用率?

标签 c freebsd

在 FreeBSD 上的 C 中,如何访问 CPU 利用率?

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

欢迎任何提示或指示 - 谢谢!


阅读此处的答案后,我得出了以下结论。由于文档不完善,我不能 100% 确定它是正确的,但 top 似乎同意。感谢所有回答的人。

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h>

#define CP_USER   0
#define CP_NICE   1
#define CP_SYS    2
#define CP_INTR   3
#define CP_IDLE   4
#define CPUSTATES 5

int main()
{
        long cur[CPUSTATES], last[CPUSTATES];
        size_t cur_sz = sizeof cur;
        int state, i;
        long sum;
        double util;

        memset(last, 0, sizeof last);

        for (i=0; i<6; i++)
        {
                if (sysctlbyname("kern.cp_time", &cur, &cur_sz, NULL, 0) < 0)
                {
                        printf ("Error reading kern.cp_times sysctl\n");
                        return -1;
                }

                sum = 0;
                for (state = 0; state<CPUSTATES; state++)
                {
                        long tmp = cur[state];
                        cur[state] -= last[state];
                        last[state] = tmp;
                        sum += cur[state];
                }

                util = 100.0L - (100.0L * cur[CP_IDLE] / (sum ? (double) sum : 1.0L));
                printf("cpu utilization: %7.3f\n", util);
                sleep(1);
        }

        return 0;
}

最佳答案

来自手册页

姓名

kvm_getmaxcpu、kvm_getpcpu -- 访问每个 CPU 的数据

图书馆

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

概要

 #include <sys/param.h>
 #include <sys/pcpu.h>
 #include <sys/sysctl.h>
 #include <kvm.h>

 int
 kvm_getmaxcpu(kvm_t *kd);

 void *
 kvm_getpcpu(kvm_t *kd, int cpu);

描述

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

缓存

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

返回值

成功时,kvm_getmaxcpu() 函数返回最大数量的 内核支持的 CPU。如果发生错误,它会返回 -1。

成功时,kvm_getpcpu() 函数返回一个指向已分配的 缓冲区或 NULL。如果发生错误,它会返回 -1。

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


编辑

这是 kvm_t 结构:

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

关于c - 使用来自 C 的系统调用,我如何获得 CPU 的利用率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5329149/

相关文章:

c - 读取内存时避免编译器优化

c - 如何减少C中文件指针的值?

kernel - 向 FreeBSD 内核添加带参数的系统调用

c - 使用读/写锁时操作系统崩溃

bash - shell 脚本中 test/[ 中的 -d、-e 和 -f 标志有什么区别?

c++ - 将 C/C++ 源文件作为插件包含在 IL2CPP 中

c - BSS 是程序文件的一部分吗?

c - 微型 C 编译器 (TCC) 和 winsock?

ssh - 在远程服务器上发出命令时如何允许输入密码?

linux - FreeBSD 指定部分目录名称以使用 find 进行搜索