add function get_sys_total_mem_size, ONLY support Linux and FreeBSD

pull/5/head
yuqing 2015-11-02 16:19:49 +08:00
parent 81f75a019c
commit 3e1d1e2ae1
3 changed files with 55 additions and 2 deletions

View File

@ -1,9 +1,10 @@
Version 1.23 2015-10-30 Version 1.23 2015-11-02
* sched_thread.c: task can execute in a new thread * sched_thread.c: task can execute in a new thread
* sched_thread.c: support delay tasks * sched_thread.c: support delay tasks
* add function get_current_time_us and get_current_time_ms * add function get_current_time_us and get_current_time_ms
* mblock add stat function * mblock add stat function
* add function get_sys_total_mem_size, ONLY support Linux and FreeBSD
Version 1.22 2015-10-10 Version 1.22 2015-10-10
* export php function: fastcommon_get_first_local_ip * export php function: fastcommon_get_first_local_ip

View File

@ -24,10 +24,19 @@
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <math.h> #include <math.h>
#include "shared_func.h" #include "shared_func.h"
#include "logger.h" #include "logger.h"
#include "sockopt.h" #include "sockopt.h"
#ifdef OS_LINUX
#include <sys/sysinfo.h>
#else
#ifdef OS_FREEBSD
#include <sys/sysctl.h>
#endif
#endif
char *formatDatetime(const time_t nTime, \ char *formatDatetime(const time_t nTime, \
const char *szDateFormat, \ const char *szDateFormat, \
char *buff, const int buff_size) char *buff, const int buff_size)
@ -2259,7 +2268,7 @@ int64_t get_current_time_us()
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
"call gettimeofday fail, " \ "call gettimeofday fail, " \
"errno=%d, error info: %s", \ "errno: %d, error info: %s", \
__LINE__, errno, STRERROR(errno)); __LINE__, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM; return errno != 0 ? errno : EPERM;
} }
@ -2267,3 +2276,39 @@ int64_t get_current_time_us()
return ((int64_t)tv.tv_sec * 1000 * 1000 + (int64_t)tv.tv_usec); 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
}

View File

@ -528,6 +528,13 @@ int64_t get_current_time_us();
#define get_current_time_ms() (get_current_time_us() / 1000) #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);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif