c_cpp [System Vitals]系统负载平均值,使用情况,进程#tags:procs,进程,系统负载,系统负载,负载平均值,负载平均值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp [System Vitals]系统负载平均值,使用情况,进程#tags:procs,进程,系统负载,系统负载,负载平均值,负载平均值相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <dirent.h>
#include <procfs.h>
#include <stdlib.h>
#include <unistd.h>
/*
 * Assuming name is psinfo, compile with:
 * CC='/opt/gcc-5.1.0/bin/gcc -m64' gcc -Wall -g ./psinfo.c -o psinfo
 */
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("[%2ld 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;
}
/*
 * name: loadav.c
 * description: Basic 3 tupe output with 1m, 5m, 15m averages.
 * 				# kstat unix:0:system_misc:avenrun*
 * CC=/opt/gcc-5.1.0/bin gcc -m64 -o loadav -Wall loadav.c
 */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/loadavg.h>

double loadav[3] ;
const int ONEM  = 0 ;
const int FIVEM = 1 ;
const int FTNM  = 2 ;

/*
 *** REMINDER ***
 * To compute load average from raw data, same data we get from kstat,
 * `kstat unix:0:system_misc:avenrun*` we divide raw value by 1<<8.
 * For example, if value is 256, we devide it by 1<<8, result being 1.0.
 */
int main(void) {
    if (getloadavg(loadav, 3) == -1) {
        printf("Error: Something bad happened.\n");
        return 1;
    }
    printf("1m=%.4f 5m=%.4f 15m=%.4f\n", 
            loadav[ONEM], loadav[FIVEM], loadav[FTNM]);
    return 0;
}

以上是关于c_cpp [System Vitals]系统负载平均值,使用情况,进程#tags:procs,进程,系统负载,系统负载,负载平均值,负载平均值的主要内容,如果未能解决你的问题,请参考以下文章

Android Vitals 和卡住唤醒锁?

一起看 I/O | Core Web Vitals 与业务影响

如何在谷歌开发者控制台中启用 Android Vitals?

Failed to compile./src/reportWebVitals.jsModule not found: Can‘t resolve ‘web-vitals‘ in ..

Failed to compile./src/reportWebVitals.jsModule not found: Can‘t resolve ‘web-vitals‘ in ..

应用崩溃了?Android vitals 帮您精确诊断