add system_info.h and system_info.c

pull/5/head
yuqing 2016-01-12 15:14:47 +08:00
parent c2b66b3ced
commit 700f8d2c2b
7 changed files with 180 additions and 63 deletions

View File

@ -4,6 +4,7 @@ Version 1.24 2016-01-12
* add skiplist which support stable sort
* make.sh: use sed to replace perl
* support get local mac addresses
* add system_info.h and system_info.c
Version 1.23 2015-11-16
* sched_thread.c: task can execute in a new thread

View File

@ -10,7 +10,8 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.lo \
avl_tree.lo ioevent.lo ioevent_loop.lo fast_task_queue.lo \
fast_timer.lo process_ctrl.lo fast_mblock.lo \
connection_pool.lo fast_mpool.lo fast_allocator.lo \
fast_buffer.lo multi_skiplist.lo flat_skiplist.lo
fast_buffer.lo multi_skiplist.lo flat_skiplist.lo \
system_info.lo
FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
logger.o sockopt.o base64.o sched_thread.o \
@ -18,7 +19,8 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
avl_tree.o ioevent.o ioevent_loop.o fast_task_queue.o \
fast_timer.o process_ctrl.o fast_mblock.o \
connection_pool.o fast_mpool.o fast_allocator.o \
fast_buffer.o multi_skiplist.o flat_skiplist.o
fast_buffer.o multi_skiplist.o flat_skiplist.o \
system_info.o
HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
shared_func.h pthread_func.h ini_file_reader.h _os_define.h \
@ -27,7 +29,7 @@ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
fast_timer.h process_ctrl.h fast_mblock.h \
connection_pool.h fast_mpool.h fast_allocator.h \
fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h \
skiplist_common.h
skiplist_common.h system_info.h
ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS)

View File

@ -2276,51 +2276,6 @@ int64_t get_current_time_us()
return ((int64_t)tv.tv_sec * 1000 * 1000 + (int64_t)tv.tv_usec);
}
int get_sys_total_mem_size(int64_t *mem_size)
{
#ifdef OS_LINUX
struct sysinfo si;
if (sysinfo(&si) != 0)
{
logError("file: "__FILE__", line: %d, " \
"call sysinfo fail, " \
"errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
*mem_size = si.totalram;
return 0;
#else
#ifdef OS_FREEBSD
int mib[2];
size_t len;
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
len = sizeof(*mem_size);
if (sysctl(mib, 2, mem_size, &len, NULL, 0) != 0) {
logError("file: "__FILE__", line: %d, " \
"call sysctl fail, " \
"errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
return 0;
#else
#error port me!
#endif
#endif
}
int get_sys_cpu_count()
{
#if defined(OS_LINUX) || defined(OS_FREEBSD)
return sysconf(_SC_NPROCESSORS_ONLN);
#else
#error port me!
#endif
}
bool is_power2(const int64_t n)
{
int64_t i;

View File

@ -528,20 +528,6 @@ int64_t get_current_time_us();
#define get_current_time_ms() (get_current_time_us() / 1000)
/** get system total memory size
* parameters:
* mem_size: return the total memory size
* return: error no , 0 success, != 0 fail
*/
int get_sys_total_mem_size(int64_t *mem_size);
/** get system CPU count
* parameters:
* return: error no , 0 success, != 0 fail
*/
int get_sys_cpu_count();
/** is the number power 2
* parameters:
* n: the number to test

View File

@ -1857,7 +1857,8 @@ static int getifmac(FastIFConfig *config)
}
close(sockfd);
formatifmac(config->mac, sizeof(config->mac), req->ifr_hwaddr.sa_data);
formatifmac(config->mac, sizeof(config->mac),
(unsigned char *)req->ifr_hwaddr.sa_data);
return 0;
}
#else //FreeBSD

122
src/system_info.c Normal file
View File

@ -0,0 +1,122 @@
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#include "logger.h"
#include "system_info.h"
#ifdef OS_LINUX
#include <sys/sysinfo.h>
#else
#ifdef OS_FREEBSD
#include <sys/sysctl.h>
#endif
#endif
int get_sys_total_mem_size(int64_t *mem_size)
{
#ifdef OS_LINUX
struct sysinfo si;
if (sysinfo(&si) != 0)
{
logError("file: "__FILE__", line: %d, " \
"call sysinfo fail, " \
"errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
*mem_size = si.totalram;
return 0;
#else
#ifdef OS_FREEBSD
int mib[2];
size_t len;
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
len = sizeof(*mem_size);
if (sysctl(mib, 2, mem_size, &len, NULL, 0) != 0)
{
logError("file: "__FILE__", line: %d, " \
"call sysctl fail, " \
"errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
return 0;
#else
#error port me!
#endif
#endif
}
int get_sys_cpu_count()
{
#if defined(OS_LINUX) || defined(OS_FREEBSD)
return sysconf(_SC_NPROCESSORS_ONLN);
#else
#error port me!
#endif
}
int get_uptime(time_t *uptime)
{
#ifdef OS_LINUX
struct sysinfo si;
if (sysinfo(&si) != 0)
{
logError("file: "__FILE__", line: %d, " \
"call sysinfo fail, " \
"errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
*uptime = si.uptime;
return 0;
#else
#ifdef OS_FREEBSD
struct timeval boottime;
size_t size;
int mib[2];
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) == 0 &&
boottime.tv_sec != 0)
{
*uptime = time(NULL) - boottime.tv_sec;
return 0;
}
else
{
*uptime = 0;
logError("file: "__FILE__", line: %d, " \
"call sysctl fail, " \
"errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
#else
#error port me!
#endif
#endif
}

50
src/system_info.h Normal file
View File

@ -0,0 +1,50 @@
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
#ifndef SYSTEM_INFO_H
#define SYSTEM_INFO_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "common_define.h"
#ifdef __cplusplus
extern "C" {
#endif
/** get system total memory size
* parameters:
* mem_size: return the total memory size
* return: error no , 0 success, != 0 fail
*/
int get_sys_total_mem_size(int64_t *mem_size);
/** get system CPU count
* parameters:
* return: error no , 0 success, != 0 fail
*/
int get_sys_cpu_count();
/** get system up time
* parameters:
* uptime: store the up time
* return: error no , 0 success, != 0 fail
*/
int get_uptime(time_t *uptime);
#ifdef __cplusplus
}
#endif
#endif