美文网首页
uptime命令

uptime命令

作者: 小怪兽狂殴奥特曼 | 来源:发表于2020-11-28 11:27 被阅读0次

网上很多博文解释uptime命令都是错误的。uptime是用来查看cpu负载的情况
uptime的输出主要看load average,有三个值,分别表示cpu在1分钟,5分钟,15分钟内的系统负载。
那么怎么看呢?
首先必须要看系统的cpu核心数,可以用lscpu看 CPU(s)一栏得到。
假设为4核心,uptime输出load average: 5.12,2.00,1.00
那么应该解释为:
1分钟负载(5.12-4)/4=28%,单核平均过载28%
5分钟负载(2-4)/4=-50%,单核平均空闲50%
5分钟负载(1-4)/4=-75%,单核平均空闲75%

可以用如下代码解释

#include <iostream>
#include<stdio.h>
#include<unistd.h>
#include <pthread.h>

const int TIME_UINT_US=1;
const int TIME_UINT_MS=1000*TIME_UINT_US;
const int TIME_UNIT_SEC=1000000UL;
const int SAMPLE_INTVL=100*TIME_UINT_MS;
const int SAMPLE_CNT=1000;

int get_cpu_num()
{
    int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
    printf("_SC_NPROCESSORS_CONF=%d\n",cpu_num);
    return cpu_num;
}

u_long GetTickCount()
{
    timespec now;
    clock_gettime(CLOCK_MONOTONIC, &now);
    u_long sec = now.tv_sec;
    u_long nsec = now.tv_nsec;
    return sec*TIME_UNIT_SEC + nsec/1000;
}
void cpu_cost(int rate)
{
    int busy_time=SAMPLE_INTVL*rate/100;
    int idle_time=SAMPLE_INTVL*(100-rate)/100;
    while(true)
    {
        u_long start_time=GetTickCount();
        while(GetTickCount()-start_time < busy_time);
        usleep(idle_time);
    }
}

// 系统负载
int g_rate=50;


void *task(void * arg)
{
    cpu_cost(g_rate);
}
int main() {


    for(int i=0;i<get_cpu_num()-1;i++)
    {
        pthread_t pid;
        pthread_create(&pid,NULL,task,NULL);
    }
    cpu_cost(g_rate);
    return 0;
}

双核机器上,设定负载g_rate=50,最终update出来的值在1.00左右。

相关文章

  • 04 uptime 命令

    uptime命令 来自:http://man.linuxde.net/uptime uptime命令能够打印系统总...

  • 到底怎么理解“平均负载” 笔记

    uptime 介绍 平均负载可以通过linux的 uptime命令查看首先,我们介绍一下uptime命令的输出内容...

  • uptime命令

    网上很多博文解释uptime命令都是错误的。uptime是用来查看cpu负载的情况uptime的输出主要看load...

  • top/htop内容的含义

    uptime uptime命令显示了load avg,它其实是读取的/proc/uptime文件: /proc/u...

  • Linux性能调优 | 01 平均负载的理解和分析

    01 uptime命令 通常我们发现系统变慢时,我们都会执行top或者uptime命令,来查看当前系统的负载情况,...

  • 系统负载Load的理解和分析

    01 uptime命令 通常我们发现系统变慢时,我们都会执行top或者uptime命令,来查看当前系统的负载情况,...

  • 如何查看linux系统的附载

    uptime命令 uptime18:01:09 up 2 days,xxx,x user,load average...

  • 负载(Load)

    在Linux机器上,有多个命令都可以查看机器的负载信息。其中包括uptime、top、w等。 1、uptime命令...

  • uptime

    1.命令简介 uptime 用于显示系统总共运行了多长时间和系统的平均负载。 无选项 uptime 命令会显示一行...

  • 查看linux服务器负载

    查看服务器负载有多种命令,w,vmstat,uptime或者top都可以直接展示负载。 1,uptime [roo...

网友评论

      本文标题:uptime命令

      本文链接:https://www.haomeiwen.com/subject/viuqwktx.html