OCT 1. 优化vCPE BRAS后线程linux系统文件描述符fd资源占用 1100+->300+

This commit is contained in:
huangxin 2023-03-13 17:03:02 +08:00
parent 701d9ac1f0
commit f262e748ea
12 changed files with 90 additions and 129 deletions

View File

@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR)
INCLUDE(CMakeDependentOption) INCLUDE(CMakeDependentOption)
OPTION(VCPE_AGENT "Enable vCPE agent test application" OFF) OPTION(VCPE_AGENT "Enable vCPE agent test application" OFF)
OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" OFF) OPTION(VCPE_PPPOE "Enable vCPE work befof PPPoE mode" OFF)
OPTION(USED_OPENDHCPD "DHCP server for vCPE" OFF) OPTION(USED_OPENDHCPD "DHCP server for vCPE" OFF)
OPTION(USED_OPENDHCPDDNS "DHCP And DNS server for vCPE" OFF) OPTION(USED_OPENDHCPDDNS "DHCP And DNS server for vCPE" OFF)
OPTION(USED_USER_VNI "Support pass user vni id from console command line" OFF) OPTION(USED_USER_VNI "Support pass user vni id from console command line" OFF)
@ -18,6 +18,8 @@ INCLUDE(system_libs)
SET(VCPE_VERSION "1.0.1") SET(VCPE_VERSION "1.0.1")
PROJECT(vCPE VERSION "${VCPE_VERSION}") PROJECT(vCPE VERSION "${VCPE_VERSION}")
CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF) CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF)
CMAKE_DEPENDENT_OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" ON "VCPE_PPPOE" OFF)
CMAKE_DEPENDENT_OPTION(USED_ZMQ "ZeroMQ support for vCPE" ON "VCPE_PPPOE" OFF)
# #
OPTION(USED_REDIS "Add redis database support for vCPE" OFF) OPTION(USED_REDIS "Add redis database support for vCPE" OFF)
@ -96,6 +98,16 @@ IF (USED_HTTP_SVR)
MESSAGE("Select Option USED_HTTP_SVR") MESSAGE("Select Option USED_HTTP_SVR")
ENDIF () ENDIF ()
IF (USED_ZMQ)
LIST(APPEND COMMON_DEFINE "-DZEROMQ_ON")
MESSAGE("Select Option USED_ZMQ")
ENDIF ()
IF (VCPE_PPPOE)
LIST(APPEND COMMON_DEFINE "-DPPPOE_ON")
MESSAGE("Select Option VCPE_PPPOE")
ENDIF ()
IF (USED_LWIP) IF (USED_LWIP)
LIST(APPEND COMMON_DEFINE "-DLWIP_ON") LIST(APPEND COMMON_DEFINE "-DLWIP_ON")
MESSAGE("Select Option USED_LWIP") MESSAGE("Select Option USED_LWIP")

View File

@ -16,7 +16,7 @@ SET(CMAKE_C_STANDARD 99)
FILE(GLOB VCPE_HEADS include/*.h include/uthash/*.h include/s2j/*.h FILE(GLOB VCPE_HEADS include/*.h include/uthash/*.h include/s2j/*.h
./httpserver/include/*.h ./httpserver/src/haywire/*.h ./httpserver/src/haywire/configuration/*.h) ./httpserver/include/*.h ./httpserver/src/haywire/*.h ./httpserver/src/haywire/configuration/*.h)
IF (USED_LWIP) IF (VCPE_PPPOE)
AUX_SOURCE_DIRECTORY(pppoe VCPE_SRC) AUX_SOURCE_DIRECTORY(pppoe VCPE_SRC)
AUX_SOURCE_DIRECTORY(user VCPE_SRC) AUX_SOURCE_DIRECTORY(user VCPE_SRC)
ENDIF () ENDIF ()

View File

@ -17,7 +17,9 @@ AUX_SOURCE_DIRECTORY(banner C_SRC)
AUX_SOURCE_DIRECTORY(configure C_SRC) AUX_SOURCE_DIRECTORY(configure C_SRC)
AUX_SOURCE_DIRECTORY(network C_SRC) AUX_SOURCE_DIRECTORY(network C_SRC)
AUX_SOURCE_DIRECTORY(task C_SRC) AUX_SOURCE_DIRECTORY(task C_SRC)
IF (USED_ZMQ)
AUX_SOURCE_DIRECTORY(mq C_SRC) AUX_SOURCE_DIRECTORY(mq C_SRC)
ENDIF ()
AUX_SOURCE_DIRECTORY(cmdline C_SRC) AUX_SOURCE_DIRECTORY(cmdline C_SRC)
AUX_SOURCE_DIRECTORY(crypto C_SRC) AUX_SOURCE_DIRECTORY(crypto C_SRC)
AUX_SOURCE_DIRECTORY(hardware C_SRC) AUX_SOURCE_DIRECTORY(hardware C_SRC)

View File

@ -98,10 +98,12 @@ static CFG_ITEM g_cfgItem[] = {
DEF_CFG_ITEM(CFG_DB_SQLITE_DB_NAME, "database.sqlite_dbname", VAL_STR, "", "SQLite3 database file name"), DEF_CFG_ITEM(CFG_DB_SQLITE_DB_NAME, "database.sqlite_dbname", VAL_STR, "", "SQLite3 database file name"),
DEF_CFG_ITEM(CFG_DB_SQLITE_PASSWD, "database.sqlite_passwd", VAL_STR, ".main", "SQLite3 database password"), DEF_CFG_ITEM(CFG_DB_SQLITE_PASSWD, "database.sqlite_passwd", VAL_STR, ".main", "SQLite3 database password"),
#endif #endif
#ifdef ZEROMQ_ON
/* 消息队列相配置 */ /* 消息队列相配置 */
/* ZeroMq配置 */ /* ZeroMq配置 */
DEF_CFG_ITEM(CFG_MQ_SVR_PORT, "zero_mq.svr_port", VAL_INT, "6278", "ZeroMQ server port"), DEF_CFG_ITEM(CFG_MQ_SVR_PORT, "zero_mq.svr_port", VAL_INT, "6278", "ZeroMQ server port"),
DEF_CFG_ITEM(CFG_MQ_DATA_PATH, "zero_mq.agent_addr", VAL_STR, "ipc:///tmp/msg_fifo0", "ZeroMQ Agent data path"), DEF_CFG_ITEM(CFG_MQ_DATA_PATH, "zero_mq.agent_addr", VAL_STR, "ipc:///tmp/msg_fifo0", "ZeroMQ Agent data path"),
#endif
/* vxLan 隧道配置 */ /* vxLan 隧道配置 */
DEF_CFG_ITEM(CFG_VXLAN_NIC_NAME, "vxlan_wan.nic", VAL_STR, "", "Network card name to send data"), DEF_CFG_ITEM(CFG_VXLAN_NIC_NAME, "vxlan_wan.nic", VAL_STR, "", "Network card name to send data"),
DEF_CFG_ITEM(CFG_VXLAN_SUPPORT, "vxlan_wan.enable", VAL_BOOL, "1", "Is support vxLan tune"), DEF_CFG_ITEM(CFG_VXLAN_SUPPORT, "vxlan_wan.enable", VAL_BOOL, "1", "Is support vxLan tune"),

View File

@ -183,6 +183,7 @@ const char *cfg_get_mysql_database() {
} }
#endif #endif
#ifdef ZEROMQ_ON
int cfg_get_zero_mq_port() { int cfg_get_zero_mq_port() {
return (unsigned short)cfg_get_integral_value(CFG_MQ_SVR_PORT); return (unsigned short)cfg_get_integral_value(CFG_MQ_SVR_PORT);
} }
@ -190,3 +191,4 @@ int cfg_get_zero_mq_port() {
const char *cfg_get_zero_mq_data_path() { const char *cfg_get_zero_mq_data_path() {
return cfg_get_string_value(CFG_MQ_DATA_PATH); return cfg_get_string_value(CFG_MQ_DATA_PATH);
} }
#endif

View File

@ -135,25 +135,7 @@ static void cpuUsedRefresh() {
} }
} }
_Noreturn void cpuCalcCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_cpu_refresh_period();
if (cfg_get_watch_cpu()) {
cpuUsedRefresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int cpu_watch_init() { int cpu_watch_init() {
static uv_thread_t uvThread;
memset(&g_cpuInfo, 0, sizeof(CPU_INFO)); memset(&g_cpuInfo, 0, sizeof(CPU_INFO));
g_cpuInfo.nCpus = get_cpu_number(); g_cpuInfo.nCpus = get_cpu_number();
@ -163,9 +145,6 @@ int cpu_watch_init() {
get_cpu_desc(&g_cpuInfo.cpuCoreDesc); get_cpu_desc(&g_cpuInfo.cpuCoreDesc);
g_isInit = TRUE; g_isInit = TRUE;
uv_thread_create(&uvThread, cpuCalcCb, NULL);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
@ -184,6 +163,8 @@ int get_cpu_info(PCPU_INFO pInfo) {
get_cpu_desc(&g_cpuInfo.cpuCoreDesc); get_cpu_desc(&g_cpuInfo.cpuCoreDesc);
g_isInit = TRUE; g_isInit = TRUE;
} else if (cfg_get_watch_cpu()) {
cpuUsedRefresh();
} }
memcpy(pInfo, &g_cpuInfo, sizeof(CPU_INFO)); memcpy(pInfo, &g_cpuInfo, sizeof(CPU_INFO));

View File

@ -105,6 +105,10 @@ int get_disk_info(PDISK_INFO pInfo) {
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
if (cfg_get_watch_disk()) {
disk_info_refresh();
}
uv_rwlock_rdlock(&g_uvLock); uv_rwlock_rdlock(&g_uvLock);
pInfo->nItems = HASH_COUNT(g_diskPartInfo); pInfo->nItems = HASH_COUNT(g_diskPartInfo);
pInfo->timestamp = time(NULL); pInfo->timestamp = time(NULL);
@ -122,32 +126,12 @@ int get_disk_info(PDISK_INFO pInfo) {
return ERR_SUCCESS; return ERR_SUCCESS;
} }
_Noreturn void diskRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_disk_refresh_period();
if (cfg_get_watch_disk()) {
disk_info_refresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int disk_watch_info() { int disk_watch_info() {
static uv_thread_t uvThread;
uv_rwlock_init(&g_uvLock); uv_rwlock_init(&g_uvLock);
if (disk_info_refresh() != ERR_SUCCESS) { if (disk_info_refresh() != ERR_SUCCESS) {
return -ERR_SYS_DISK_GET_INFO; return -ERR_SYS_DISK_GET_INFO;
} }
uv_thread_create(&uvThread, diskRefreshCb, NULL);
return ERR_SUCCESS; return ERR_SUCCESS;
} }

View File

@ -9,51 +9,48 @@
#include "user_errno.h" #include "user_errno.h"
#include "config.h" #include "config.h"
#include "misc.h" #include "misc.h"
#include "task_manager.h"
static HARDWARE_INFO g_hardwareInfo; static HARDWARE_INFO g_hardwareInfo;
_Noreturn void hardwareRefreshCb(void *UNUSED(pArg)) { void hardwareRefreshCb(uv_timer_t *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_hardware_refresh_period();
if (cfg_get_watch_sensor()) { if (cfg_get_watch_sensor()) {
get_sensor_info(&g_hardwareInfo.sensorInfo); get_sensor_info(&g_hardwareInfo.sensorInfo);
} }
if (cfg_get_watch_disk()) { if (cfg_get_watch_disk()) {
get_disk_info(&g_hardwareInfo.diskInfo); get_disk_info(&g_hardwareInfo.diskInfo);
} }
if (cfg_get_watch_cpu()) { if (cfg_get_watch_cpu()) {
get_cpu_info(&g_hardwareInfo.cpuInfo); get_cpu_info(&g_hardwareInfo.cpuInfo);
} }
if (cfg_get_watch_memory()) { if (cfg_get_watch_memory()) {
get_memory_info(&g_hardwareInfo.memInfo); get_memory_info(&g_hardwareInfo.memInfo);
} }
if (period < REFRESH_MAX_PERIOD && period >= 1) { printf("%s\n", get_hardware_json());
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
uv_sleep(1000);
} while (TRUE);
} }
int init_hardware() { int init_hardware() {
static uv_thread_t uvThread; static uv_timer_t uvTm;
unsigned int period = cfg_get_hardware_refresh_period();
if (period >= REFRESH_MAX_PERIOD) {
period = 10000;
} else {
period *= 1000;
}
uv_timer_init(get_task_manager(), &uvTm);
memset(&g_hardwareInfo, 0, sizeof(HARDWARE_INFO)); memset(&g_hardwareInfo, 0, sizeof(HARDWARE_INFO));
uv_thread_create(&uvThread, hardwareRefreshCb, NULL);
cpu_watch_init(); cpu_watch_init();
memory_watch_init(); memory_watch_init();
disk_watch_info(); disk_watch_info();
sensor_watch_init(); sensor_watch_init();
uv_timer_start(&uvTm, hardwareRefreshCb, period, period);
return ERR_SUCCESS; return ERR_SUCCESS;
} }

View File

@ -16,6 +16,7 @@
//#define CMD_SENSOR_INFO ("ipmitool sensor | awk '{gsub(/\|/, \"\"); print $0}'") //#define CMD_SENSOR_INFO ("ipmitool sensor | awk '{gsub(/\|/, \"\"); print $0}'")
#define CMD_SENSOR_INFO ("ipmitool sensor") #define CMD_SENSOR_INFO ("ipmitool sensor")
#define CMD_IPMITOOL_ENABLE ("whereis ipmitool | awk '{print $2}'")
typedef struct { typedef struct {
char name[MAX_SENSOR_STR]; ///< 传感器名称 char name[MAX_SENSOR_STR]; ///< 传感器名称
@ -42,9 +43,16 @@ static int sensor_info_refresh() {
int errCode = ERR_SUCCESS; int errCode = ERR_SUCCESS;
FILE *fp; FILE *fp;
char buf[1024]; char buf[1024];
char *pRet = NULL;
if (shell_with_output(CMD_IPMITOOL_ENABLE, &pRet) != ERR_SUCCESS || pRet == NULL || strlen(pRet) == 0) {
if (pRet) {
free(pRet);
}
return -ERR_ITEM_UNEXISTS;
}
fp = popen(CMD_SENSOR_INFO, "r"); fp = popen(CMD_SENSOR_INFO, "r");
if (fp == NULL) { if (fp == NULL) {
return -ERR_OPEN_FILE; return -ERR_OPEN_FILE;
} }
@ -132,6 +140,10 @@ int get_sensor_info(PSENSOR_INFO pInfo) {
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
if (cfg_get_watch_sensor()) {
sensor_info_refresh();
}
uv_rwlock_rdlock(&g_uvLock); uv_rwlock_rdlock(&g_uvLock);
pInfo->nItems = HASH_COUNT(g_pSensorInfo); pInfo->nItems = HASH_COUNT(g_pSensorInfo);
pInfo->timestamp = time(NULL); pInfo->timestamp = time(NULL);
@ -149,25 +161,7 @@ int get_sensor_info(PSENSOR_INFO pInfo) {
return ERR_SUCCESS; return ERR_SUCCESS;
} }
_Noreturn void sensorRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_sensor_refresh_period();
if (cfg_get_watch_sensor()) {
sensor_info_refresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int sensor_watch_init() { int sensor_watch_init() {
static uv_thread_t uvThread;
int i, devOk = FALSE; int i, devOk = FALSE;
int n = ARRAY_SIZE(g_ipmiDev); int n = ARRAY_SIZE(g_ipmiDev);
@ -188,7 +182,5 @@ int sensor_watch_init() {
return -ERR_SYS_SENSOR_GET_INFO; return -ERR_SYS_SENSOR_GET_INFO;
} }
uv_thread_create(&uvThread, sensorRefreshCb, NULL);
return ERR_SUCCESS; return ERR_SUCCESS;
} }

View File

@ -95,33 +95,13 @@ static int memory_info_refresh() {
return errCode; return errCode;
} }
_Noreturn void memRefreshCb(void *UNUSED(pArg)) {
do {
unsigned int period = cfg_get_mem_refresh_period();
if (cfg_get_watch_memory()) {
memory_info_refresh();
}
if (period < REFRESH_MAX_PERIOD && period >= 1) {
uv_sleep(1000 * period);
} else {
uv_sleep(1000);
}
} while (TRUE);
}
int memory_watch_init() { int memory_watch_init() {
static uv_thread_t uvThread;
uv_rwlock_init(&g_uvLock); uv_rwlock_init(&g_uvLock);
if (memory_info_refresh() != ERR_SUCCESS) { if (memory_info_refresh() != ERR_SUCCESS) {
return -ERR_SYS_DISK_GET_INFO; return -ERR_SYS_DISK_GET_INFO;
} }
uv_thread_create(&uvThread, memRefreshCb, NULL);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
@ -142,6 +122,10 @@ int get_memory_info(PMEMORY_INFO pInfo) {
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
if (cfg_get_watch_memory()) {
memory_info_refresh();
}
uv_rwlock_rdlock(&g_uvLock); uv_rwlock_rdlock(&g_uvLock);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {

View File

@ -61,8 +61,10 @@ typedef enum {
CFG_DB_SQLITE_DB_NAME, CFG_DB_SQLITE_DB_NAME,
CFG_DB_SQLITE_PASSWD, CFG_DB_SQLITE_PASSWD,
#endif #endif
#ifdef ZEROMQ_ON
CFG_MQ_SVR_PORT, CFG_MQ_SVR_PORT,
CFG_MQ_DATA_PATH, CFG_MQ_DATA_PATH,
#endif
CFG_VXLAN_NIC_NAME, CFG_VXLAN_NIC_NAME,
CFG_VXLAN_SUPPORT, CFG_VXLAN_SUPPORT,
CFG_VXLAN_PEER_IP, CFG_VXLAN_PEER_IP,

View File

@ -134,7 +134,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
if (cfg_get_hardware_watch_enable()) { if (cfg_get_hardware_watch_enable()) {
init_hardware(); init_hardware();
} }
#ifdef ZEROMQ_ON
if ((ret = mq_init()) != ERR_SUCCESS) { if ((ret = mq_init()) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret); LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
} }
@ -142,6 +142,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
if ((ret = mq_data_init()) != ERR_SUCCESS) { if ((ret = mq_data_init()) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret); LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
} }
#endif
#ifdef HTTPSERVER_ON #ifdef HTTPSERVER_ON
http_svr_init(); http_svr_init();
#endif #endif
@ -156,7 +157,9 @@ void user_uninit() {
#ifdef HTTPSERVER_ON #ifdef HTTPSERVER_ON
http_svr_uinit(); http_svr_uinit();
#endif #endif
#ifdef ZEROMQ_ON
mq_uninit(); mq_uninit();
#endif
zlog_fini(); zlog_fini();
uninit_config_system(); uninit_config_system();
uv_loop_close(get_task_manager()); uv_loop_close(get_task_manager());