在 Solaris 10 上从 C 获取 CPU 使用统计信息

发布于 2024-08-02 07:31:40 字数 317 浏览 4 评论 0原文

在 Solaris 10 和 C 语言中,我想定期查询数字,例如

  • 该操作系统进程中特定 LWP 的 CPU 使用情况。
  • 该操作系统进程的内存使用情况。

我已经知道如何通过打开 /proc/pid/psinfo 并从中读取 (pr_rssize) 来实现后者,但是有没有办法实现前者?

过去,我分叉了 prstat 的副本,解析其输出并将其读回到我的 C 代码中。当我想要查询越来越多的此类信息时,这变得越来越乏味,而且感觉完全错误。当然有一种方法可以通过一些简单的 C 代码来做到这一点。

谢谢你的帮助。 尼克B

On Solaris 10, and in C, I'd like to regularly query numbers like

  • The CPU usage for specific LWPs within that OS process.
  • The memory usage for that OS process.

I already know how to do the latter by opening /proc/pid/psinfo and reading from that (pr_rssize), but is there a way to implement the former?

In the past I have forked off a copy of prstat, parsed the output from that and read it back into my C code. As I'm looking to query more and more of this type of information, this is getting increasingly tedious, and just feels plain wrong. Surely there is a way to do this with some simple C code.

Thanks for any help.
NickB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-08-09 07:31:40

在 Solaris 上,可以通过读取 /proc/pid/lwp/ 获取特定于 lwp 的 ps 信息lwpid/lwpsinfo。该文件包含一个 lwpsinfo 结构,其中包括:

timestruc_t pr_time;      /* cpu time for this lwp */

请参阅 proc(4) 了解更多详细信息。

On Solaris, the lwp-specific ps information can be be obtained by reading /proc/pid/lwp/lwpid/lwpsinfo. This file contains a lwpsinfo structure which includes:

timestruc_t pr_time;      /* cpu time for this lwp */

See proc(4) for additional details.

浮萍、无处依 2024-08-09 07:31:40

当您寻求 C 解决方案时,也许您可​​以查看 perl 模块 Solaris::Procfs 看看它如何提取信息?

While you ask for a C solution, maybe you could look into the perl module Solaris::Procfs to see what it does to extract information?

不奢求什么 2024-08-09 07:31:40

<代码> <代码>

#include <stdio.h>
#include <dirent.h>
#include <procfs.h>

int psinfo_thread_info(pid_t pid)
{
    int i, nent, nread = 0;
    size_t entsz;

    FILE *fp = NULL;
    char file_name[128] = {0,};
    char *ptr, *buf;
    prheader_t  header;
    lwpsinfo_t *lwpsinfo;

    snprintf(file_name, sizeof(file_name), "/proc/%ld/lpsinfo", pid);
    if ((fp = fopen(file_name, "r")) == NULL) {
        return -1;
    }

    nread = fread(&header, sizeof(prheader_t), 1, fp);
    if (nread < 0) {
        fclose(fp);
        return -1;
    }

    nent = header.pr_nent;
    printf("  Thread_num: %d\n", nent);

    entsz = header.pr_entsize * nent;
    ptr = buf = malloc(entsz);
    if (pread(fileno(fp), buf, entsz, sizeof (struct prheader)) != entsz) {
        free(ptr);
        free(buf);
        fclose(fp);
        return -1;
    }
    fclose(fp);

    for (i = 0; i < nent; i++, ptr += header.pr_entsize)
    {
        lwpsinfo = (lwpsinfo_t *)ptr;
        if (lwpsinfo == NULL) {
            continue;
        }
        printf("[%2d thread] cpu_usage = %.2lf%%\n",
                lwpsinfo->pr_lwpid,
                ((double)(lwpsinfo->pr_pctcpu * 100.0) / 0x8000));
    }
    free(ptr);
    free(buf);
    return 0;
}

int main(void)
{
    FILE *fp = NULL;
    DIR *proc_dir = NULL;
    struct dirent *dir_entry = NULL;
    int nread = 0;
    char file_name[128] = {0,};
    psinfo_t pinfo;

    if ((proc_dir = opendir("/proc")) == NULL) {
        printf("opendir failed\n");
        return -1;
    }

    while ((dir_entry = readdir(proc_dir)) != NULL)
    {
        if (atoi(dir_entry->d_name) == 0) {
            continue;
        }
        snprintf(file_name, sizeof(file_name), "/proc/%s/psinfo",
                dir_entry->d_name);

        if ((fp = fopen(file_name, "r")) == NULL) {
            continue;
        }

        nread = fread(&pinfo, sizeof(pinfo), 1, fp);
        if (nread < 0) {
            fclose(fp);
            continue;
        }
        fclose(fp);

        printf("---------------------------\n");
        printf("\nPROC:%s PID:%ld, CPU_USAGE:%.2lf%% ",
                pinfo.pr_fname, pinfo.pr_pid,
                ((double)(pinfo.pr_pctcpu * 100.0) / 0x8000));

        psinfo_thread_info(pinfo.pr_pid);
    }
    closedir(proc_dir);

    return 0;
}

>

#include <stdio.h>
#include <dirent.h>
#include <procfs.h>

int psinfo_thread_info(pid_t pid)
{
    int i, nent, nread = 0;
    size_t entsz;

    FILE *fp = NULL;
    char file_name[128] = {0,};
    char *ptr, *buf;
    prheader_t  header;
    lwpsinfo_t *lwpsinfo;

    snprintf(file_name, sizeof(file_name), "/proc/%ld/lpsinfo", pid);
    if ((fp = fopen(file_name, "r")) == NULL) {
        return -1;
    }

    nread = fread(&header, sizeof(prheader_t), 1, fp);
    if (nread < 0) {
        fclose(fp);
        return -1;
    }

    nent = header.pr_nent;
    printf("  Thread_num: %d\n", nent);

    entsz = header.pr_entsize * nent;
    ptr = buf = malloc(entsz);
    if (pread(fileno(fp), buf, entsz, sizeof (struct prheader)) != entsz) {
        free(ptr);
        free(buf);
        fclose(fp);
        return -1;
    }
    fclose(fp);

    for (i = 0; i < nent; i++, ptr += header.pr_entsize)
    {
        lwpsinfo = (lwpsinfo_t *)ptr;
        if (lwpsinfo == NULL) {
            continue;
        }
        printf("[%2d thread] cpu_usage = %.2lf%%\n",
                lwpsinfo->pr_lwpid,
                ((double)(lwpsinfo->pr_pctcpu * 100.0) / 0x8000));
    }
    free(ptr);
    free(buf);
    return 0;
}

int main(void)
{
    FILE *fp = NULL;
    DIR *proc_dir = NULL;
    struct dirent *dir_entry = NULL;
    int nread = 0;
    char file_name[128] = {0,};
    psinfo_t pinfo;

    if ((proc_dir = opendir("/proc")) == NULL) {
        printf("opendir failed\n");
        return -1;
    }

    while ((dir_entry = readdir(proc_dir)) != NULL)
    {
        if (atoi(dir_entry->d_name) == 0) {
            continue;
        }
        snprintf(file_name, sizeof(file_name), "/proc/%s/psinfo",
                dir_entry->d_name);

        if ((fp = fopen(file_name, "r")) == NULL) {
            continue;
        }

        nread = fread(&pinfo, sizeof(pinfo), 1, fp);
        if (nread < 0) {
            fclose(fp);
            continue;
        }
        fclose(fp);

        printf("---------------------------\n");
        printf("\nPROC:%s PID:%ld, CPU_USAGE:%.2lf%% ",
                pinfo.pr_fname, pinfo.pr_pid,
                ((double)(pinfo.pr_pctcpu * 100.0) / 0x8000));

        psinfo_thread_info(pinfo.pr_pid);
    }
    closedir(proc_dir);

    return 0;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文