uptime
这里,第一项是当前时间,up 表示系统正在运行,5:53 是系统启动的总时间,最后是系统的负载load信息。若你想深入了解,这里是 uptime man 页中关于最后一项信息的说明:
- 系统负载是处于可运行runnable或不可中断uninterruptable状态的进程的平均数。可运行状态的进程要么正在使用 CPU 要么在等待使用 CPU。 不可中断状态的进程则正在等待某些 I/O 访问,例如等待磁盘 IO。
- 有三个时间间隔的平均值。负载均值的意义根据系统中 CPU 的数量不同而不同,负载为 1 对于一个只有单 CPU 的系统来说意味着负载满了,而对于一个拥有 4 CPU 的系统来说则意味着 75% 的时间里都是空闲的。
若你只想知道系统运行了多长时间,而且希望以更人性化的格式来显示,那么可以使用 -p 项:
你也可以指定 uptme 显示系统开始运行的时间和日期。方法是使用 -s 命令项:
-V 获取版本信息,-h 获取帮助信息:
/proc/uptime
有关/proc/uptime这个文件里两个参数所代表的意义
[root@app ~]#cat /proc/uptime
3387048.81 3310821.00
第一个参数是代表从系统启动到现在的时间(以秒为单位):
3387048.81秒 = 39.20195381944444天,说明这台服务器已连续开机39.20195381944444天
第二个参数是代表系统空闲的时间(以秒为单位):
3310821.00秒 = 38.3196875天,说明这台机器从开机到现在一共只有38天左右没事干。
计算一下空闲率:
3310821.00 / 3387048.81 = 0.9774943278718207
也就是说:它的空闲率是97%
-
空闲率高低并不意味着,它做的工作很多,还有跟服务器的配置和性能有很大的关系,这台服务器有这么低的空闲率,或者说这么高的利用率,是因为它的配置比较低。
-
空闲率跟服务器的配置有很大的关系,服务器的性能越好,配置越高,它处理的速度越快,配置高的服务器处理的时间要小于配置低的服务器。
-
从空闲时间,你就可以判断你的服务器负载是否过大,看一下是不是有导常情况发生,如果空闲时间很小,说明你的服务器已经在满负荷运行,然后决定是不是需要升级你的服务器。
程序
获取从系统启动到现在的时间
#include <stdio.h>
#include <string.h>
#include "procfile.h"
#include <stdlib.h>
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
typedef long long collected_number;
typedef uint64_t kernel_uint_t;
/*
* 获取从系统启动到现在的时间 * 1000
* */
static procfile *read_proc_uptime_ff = NULL;
static inline collected_number read_proc_uptime(char *filename) {
if(unlikely(!read_proc_uptime_ff)) {
read_proc_uptime_ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
if(unlikely(!read_proc_uptime_ff)){
printf("(%s, %s(%d)) can't open %s, maybe path isn't exist\n ", __FILE__, __FUNCTION__ , __LINE__, filename);
return 0;
}
}
read_proc_uptime_ff = procfile_readall(read_proc_uptime_ff);
if(unlikely(!read_proc_uptime_ff)) {
printf("(%s, %s(%d)) %s is empty\n ", __FILE__, __FUNCTION__ , __LINE__, filename);
return 0;
}
if(unlikely(procfile_lines(read_proc_uptime_ff) < 1)) {
printf("(%s, %s(%d)) system error, /proc/uptime has no lines.", __FILE__, __FUNCTION__ , __LINE__);
exit(0);
}
if(unlikely(procfile_linewords(read_proc_uptime_ff, 0) < 1)) {
printf("(%s, %s(%d)) system error, /proc/uptime has less than 1 word in it.", __FILE__, __FUNCTION__ , __LINE__);
exit(0);
}
//strtod(将字符串转换成浮点数)
return (collected_number)(strtold(procfile_lineword(read_proc_uptime_ff, 0, 0), NULL) * 1000.0);
}
int main(int argc, char **argv)
{
printf("%lld\n",read_proc_uptime("/proc/uptime"));
}