Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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?

#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;
}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top