下面是头文件内容,代码是mooon的一部分,对应的CPP文件请直接浏览:http://code.google.com/p/mooon/source/browse/trunk/common_library/src/sys/info.cpp
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: JianYi, eyjian@qq.com or eyjian@gmail.com
*/
#include <sys/times.h>
#include <sys/sysinfo.h>
#include <sys/resource.h>
#include "sys/info.h"
#include "sys/close_helper.h"
#include "util/string_util.h"
SYS_NAMESPACE_BEGIN
bool CInfo::get_sys_info(sys_info_t& sys_info)
{
struct sysinfo info;
if (-1 == sysinfo(&info)) return false;
sys_info.uptime_second = info.uptime;
sys_info.average_load[0] = info.loads[0];
sys_info.average_load[1] = info.loads[1];
sys_info.average_load[2] = info.loads[2];
sys_info.ram_total = info.totalram;
sys_info.ram_free = info.freeram;
sys_info.ram_shared = info.sharedram;
sys_info.ram_buffer = info.bufferram;
sys_info.swap_total = info.totalswap;
sys_info.swap_free = info.freeswap;
sys_info.process_number = info.procs;
return true;
}
bool CInfo::get_mem_info(mem_info_t& mem_info)
{
FILE* fp = fopen("/proc/meminfo", "r");
if (NULL == fp) return false;
sys::CloseHelper<FILE*> ch(fp);
int i = 0;
int value;
char name[LINE_MAX];
char line[LINE_MAX];
int filed_number = 2;
int member_number = 7;
while (fgets(line, sizeof(line)-1, fp))
{
if (sscanf(line, "%s%u", name, &value) != filed_number)
continue;
if (0 == strcmp(name