15191 【设计】【系统监视器】增加处理器/内存/网络详细信息展示
This commit is contained in:
parent
649d360e44
commit
33fd4c8647
|
@ -1,3 +1,12 @@
|
|||
libkysdk-system (2.3.0.0-0k2.0) yangtze; urgency=medium
|
||||
|
||||
* BUG:无
|
||||
* 需求号:15191 【设计】【系统监视器】增加处理器/内存/网络详细信息展示
|
||||
* 其他改动说明:无
|
||||
* 其他改动影响域:无
|
||||
|
||||
-- szm-min <shaozhimin@kylinos.cn> Wed, 11 Oct 2023 16:41:14 +0800
|
||||
|
||||
libkysdk-system (2.3.0.0-0k1.0) yangtze; urgency=medium
|
||||
|
||||
* BUG:无
|
||||
|
|
|
@ -31,7 +31,9 @@ Build-Depends: debhelper-compat (= 12),
|
|||
libhd-dev,
|
||||
libpci-dev,
|
||||
libbluetooth-dev,
|
||||
libsensors4-dev
|
||||
libsensors4-dev,
|
||||
libnl-3-dev,
|
||||
libnl-route-3-dev
|
||||
Standards-Version: 4.4.1
|
||||
Homepage: http://gitlab2.kylin.com/kysdk/kysdk-system
|
||||
|
||||
|
@ -210,7 +212,9 @@ Depends: libkysdk-log(>=1.2.0),
|
|||
libdbus-glib-1-2,
|
||||
libc6,
|
||||
libsystemd0,
|
||||
libx11-6
|
||||
libx11-6,
|
||||
libnl-3-200,
|
||||
libnl-route-3-200
|
||||
Multi-Arch: same
|
||||
Description: 硬件信息获取库
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
Name: libkysdk-realtime
|
||||
Description: kysdk system layer realtime
|
||||
Version: 2.0.0
|
||||
Libs: -L/usr/lib/kysdk/kysdk-system/ -L/usr/lib/kysdk/kysdk-base -lkyrealtimeinfo -Wl,-rpath=/usr/lib/kysdk/kysdk-system/
|
||||
Libs: -L/usr/lib/kysdk/kysdk-system/ -L/usr/lib/kysdk/kysdk-base -lkyrealtime -Wl,-rpath=/usr/lib/kysdk/kysdk-system/
|
||||
Cflags: -I/usr/include/kysdk/kysdk-system/
|
||||
|
|
|
@ -2,6 +2,11 @@ include(FindPkgConfig)
|
|||
pkg_check_modules(GLIB REQUIRED glib-2.0)
|
||||
include_directories(${GLIB_INCLUDE_DIRS})
|
||||
|
||||
pkg_search_module(LIB_NL3 REQUIRED libnl-3.0)
|
||||
pkg_search_module(LIB_NL3_ROUTE REQUIRED libnl-route-3.0)
|
||||
include_directories(${LIB_NL3_INCLUDE_DIRS})
|
||||
include_directories(${LIB_NL3_ROUTE_INCLUDE_DIRS})
|
||||
|
||||
set(HARDWARE_TOP_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
set(hwcode
|
||||
"${HARDWARE_TOP_DIR}/libkybios.h"
|
||||
|
@ -65,7 +70,7 @@ add_executable(kybluetooth-test test/kybluetooth-test.c)
|
|||
add_executable(kyfan-test test/kyfan-test.c)
|
||||
add_executable(kyhw-test test/kyhw-test.c)
|
||||
# 额外链接的库在这一行
|
||||
target_link_libraries(kyhw kylog kyconf pthread systemd cups curl udev ${GLIB_LIBRARIES})
|
||||
target_link_libraries(kyhw kylog kyconf pthread systemd cups curl udev ${GLIB_LIBRARIES} ${LIB_NL3_LIBRARIES} ${LIB_NL3_ROUTE_LIBRARIES})
|
||||
target_link_libraries(kybluetooth bluetooth hd gobject-2.0)
|
||||
target_link_libraries(kyedid kylog m X11 Xrandr hd)
|
||||
target_link_libraries(kyfan sensors)
|
||||
|
|
|
@ -33,7 +33,12 @@ extern const char *kdk_bios_get_vendor();
|
|||
*/
|
||||
extern const char *kdk_bios_get_version();
|
||||
|
||||
extern void kdk_bios_free(char *info);
|
||||
/**
|
||||
* @brief 释放内存
|
||||
*
|
||||
* @param info 本模块接口返回的字符串
|
||||
*/
|
||||
extern void kdk_bios_free(char *info);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -6,13 +6,29 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "sys/sysinfo.h"
|
||||
#include <dirent.h>
|
||||
#include "unistd.h"
|
||||
#ifdef __linux__
|
||||
#include <sys/utsname.h>
|
||||
#endif
|
||||
#define MIDSIZE 128
|
||||
|
||||
#ifndef max
|
||||
# define max(x, y) __extension__ ({ \
|
||||
__typeof__(x) _max1 = (x); \
|
||||
__typeof__(y) _max2 = (y); \
|
||||
(void) (&_max1 == &_max2); \
|
||||
_max1 > _max2 ? _max1 : _max2; })
|
||||
#endif
|
||||
|
||||
#ifndef min
|
||||
# define min(x, y) __extension__ ({ \
|
||||
__typeof__(x) _min1 = (x); \
|
||||
__typeof__(y) _min2 = (y); \
|
||||
(void) (&_min1 == &_min2); \
|
||||
_min1 < _min2 ? _min1 : _min2; })
|
||||
#endif
|
||||
|
||||
struct _cpuInfo{
|
||||
size_t ncpus; // CPU数量
|
||||
size_t corenums; // CPU核心数量
|
||||
|
@ -337,6 +353,7 @@ static void _get_cpu_info()
|
|||
char buffer[CPUINF_BUFSIZE];
|
||||
char *corenums;
|
||||
char *cpu_process;
|
||||
int cur_freq = 0;
|
||||
while (fgets(buffer, CPUINF_BUFSIZE, fp))
|
||||
{
|
||||
// klog_debug("%s",buffer);
|
||||
|
@ -431,6 +448,33 @@ static void _get_cpu_info()
|
|||
{
|
||||
cpuinf->corenums = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
|
||||
if(cpuinf->cur_freq_MHz == NULL)
|
||||
{
|
||||
char ret[MIDSIZE] = {0};
|
||||
int part = 0;
|
||||
char path[128] = {0};
|
||||
int i = 0;
|
||||
char buffer[64] = {0};
|
||||
for(i = 0; i < cpuinf->cpu_process; i++)
|
||||
{
|
||||
memset(path, 0, 128);
|
||||
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", i);
|
||||
fp = fopen(path, "rt");
|
||||
if (!fp)
|
||||
{
|
||||
klog_err("cpuinfo_max_freq 读取失败:%s\n", strerror(errno));
|
||||
return ;
|
||||
}
|
||||
fgets(ret, MIDSIZE, fp);
|
||||
part = atoi(ret);
|
||||
part = part / 1000;
|
||||
cur_freq += part;
|
||||
fclose(fp);
|
||||
}
|
||||
sprintf(buffer, "%ld", cur_freq / cpuinf->cpu_process);
|
||||
cpuinf->cur_freq_MHz = strdup(buffer);
|
||||
}
|
||||
|
||||
#endif
|
||||
return;
|
||||
|
@ -476,3 +520,419 @@ unsigned int kdk_cpu_get_process()
|
|||
{
|
||||
GET_VALUE(cpu_process, 0);
|
||||
}
|
||||
|
||||
float kdk_cpu_get_max_freq_MHz()
|
||||
{
|
||||
char ret[MIDSIZE] = {0};
|
||||
int num = 0;
|
||||
int part = 0;
|
||||
char path[128] = {0};
|
||||
FILE *fp = NULL;
|
||||
#ifdef __linux__
|
||||
int process = kdk_cpu_get_process();
|
||||
int i = 0;
|
||||
for(i = 0; i < process; i++)
|
||||
{
|
||||
memset(path, 0, 128);
|
||||
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", i);
|
||||
fp = fopen(path, "rt");
|
||||
if (!fp)
|
||||
{
|
||||
klog_err("cpuinfo_max_freq 读取失败:%s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
fgets(ret, MIDSIZE, fp);
|
||||
part = atoi(ret);
|
||||
part = part / 1000;
|
||||
num = max(num, part);
|
||||
fclose(fp);
|
||||
}
|
||||
#endif
|
||||
return num;
|
||||
}
|
||||
|
||||
float kdk_cpu_get_min_freq_MHz()
|
||||
{
|
||||
char ret[MIDSIZE] = {0};
|
||||
int num = kdk_cpu_get_max_freq_MHz();
|
||||
int part = 0;
|
||||
char path[128] = {0};
|
||||
FILE *fp = NULL;
|
||||
#ifdef __linux__
|
||||
int process = kdk_cpu_get_process();
|
||||
int i = 0;
|
||||
for(i = 0; i < process; i++)
|
||||
{
|
||||
memset(path, 0, 128);
|
||||
sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", i);
|
||||
fp = fopen(path, "rt");
|
||||
if (!fp)
|
||||
{
|
||||
klog_err("cpuinfo_min_freq 读取失败:%s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
fgets(ret, MIDSIZE, fp);
|
||||
part = atoi(ret);
|
||||
part = part / 1000;
|
||||
num = min(num, part);
|
||||
fclose(fp);
|
||||
}
|
||||
#endif
|
||||
return num;
|
||||
}
|
||||
|
||||
char* kdk_cpu_get_running_time()
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
int time_sec = 0;
|
||||
char *runtime = (char *)malloc(sizeof(char) * 128);
|
||||
if(!runtime)
|
||||
return NULL;
|
||||
memset(runtime, 0, 128);
|
||||
|
||||
if ((fp = fopen("/proc/uptime", "r"))) {
|
||||
|
||||
char buf[128] = {0};
|
||||
long up_sec, up_usec;
|
||||
if (fgets(buf, sizeof(buf), fp)) {
|
||||
int nm = sscanf(buf, "%ld.%ld", &up_sec, &up_usec);
|
||||
if (nm == 2) {
|
||||
time_sec = up_sec;
|
||||
long days = time_sec / 86400;
|
||||
long hours = (time_sec - days * 86400) / 3600;
|
||||
long mins = (time_sec - days * 86400 - hours * 3600) / 60;
|
||||
sprintf(runtime, "%ld天%ld小时%ld分钟", days, hours, mins);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
return runtime;
|
||||
}
|
||||
|
||||
unsigned int kdk_cpu_get_sockets()
|
||||
{
|
||||
int sockets = 0;
|
||||
char buffer[64] = {0};
|
||||
memset(buffer, 0, 64);
|
||||
char buf[2048] = {0};
|
||||
memset(buf, 0, 2048);
|
||||
int i = 0;
|
||||
FILE *pipeLine = popen("lscpu", "r");
|
||||
if (!pipeLine)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (__glibc_likely(pipeLine != NULL))
|
||||
{
|
||||
while (fgets(buf, sizeof(buf), pipeLine))
|
||||
{
|
||||
if(strstr(buf, "座:"))
|
||||
{
|
||||
sscanf(buf, "%*s %s", buffer);
|
||||
strstripspace(buffer);
|
||||
sockets = atoi(buffer);
|
||||
}
|
||||
else if(strstr(buf, "socket(s)"))
|
||||
{
|
||||
sscanf(buf, "%*s %s", buffer);
|
||||
strstripspace(buffer);
|
||||
sockets = atoi(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
pclose(pipeLine);
|
||||
return sockets;
|
||||
}
|
||||
|
||||
unsigned int kdk_cpu_get_L1d_cache()
|
||||
{
|
||||
#ifdef __linux__
|
||||
char res_buffer[64] = {0};
|
||||
char buf[1024] = {0};
|
||||
FILE *fp = NULL;
|
||||
int cache_nums = 0;
|
||||
|
||||
fp = popen("lscpu", "r");
|
||||
if(!fp)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
while (fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
if(strstr(buf, "L1d cache"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
else if(strstr(buf, "L1d 缓存"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
pclose(fp);
|
||||
|
||||
if(cache_nums == 0)
|
||||
{
|
||||
int i = 0;
|
||||
FILE *pipeLine = popen("dmidecode -t cache", "r");
|
||||
if (pipeLine != NULL)
|
||||
{
|
||||
memset(buf, 0, 1024);
|
||||
memset(res_buffer, 0, 64);
|
||||
while (fgets(buf, sizeof(buf), pipeLine))
|
||||
{
|
||||
if(strstr(buf, "L1 Data Cache"))
|
||||
{
|
||||
i = 1;
|
||||
}
|
||||
|
||||
if(i != 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(strstr(buf, "Installed Size"))
|
||||
{
|
||||
strstripspace(buf);
|
||||
sscanf(buf, "Installed Size: %s %*s", res_buffer);
|
||||
cache_nums = atoi(res_buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pclose(pipeLine);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return cache_nums;
|
||||
}
|
||||
|
||||
unsigned int kdk_cpu_get_L1i_cache()
|
||||
{
|
||||
#ifdef __linux__
|
||||
char res_buffer[64] = {0};
|
||||
char buf[1024] = {0};
|
||||
FILE *fp = NULL;
|
||||
int cache_nums = 0;
|
||||
|
||||
fp = popen("lscpu", "r");
|
||||
if(!fp)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
while (fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
if(strstr(buf, "L1i cache"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
else if(strstr(buf, "L1i 缓存"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
pclose(fp);
|
||||
|
||||
if(cache_nums == 0)
|
||||
{
|
||||
int i = 0;
|
||||
FILE *pipeLine = popen("dmidecode -t cache", "r");
|
||||
if (pipeLine != NULL)
|
||||
{
|
||||
memset(buf, 0, 1024);
|
||||
memset(res_buffer, 0, 64);
|
||||
while (fgets(buf, sizeof(buf), pipeLine))
|
||||
{
|
||||
if(strstr(buf, "L1 Instruction Cache"))
|
||||
{
|
||||
i = 1;
|
||||
}
|
||||
|
||||
if(i != 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(strstr(buf, "Installed Size"))
|
||||
{
|
||||
strstripspace(buf);
|
||||
sscanf(buf, "Installed Size: %s %*s", res_buffer);
|
||||
cache_nums = atoi(res_buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pclose(pipeLine);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return cache_nums;
|
||||
}
|
||||
|
||||
unsigned int kdk_cpu_get_L2_cache()
|
||||
{
|
||||
#ifdef __linux__
|
||||
char res_buffer[64] = {0};
|
||||
char buf[1024] = {0};
|
||||
FILE *fp = NULL;
|
||||
int cache_nums = 0;
|
||||
|
||||
fp = popen("lscpu", "r");
|
||||
if(!fp)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
while (fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
if(strstr(buf, "L2 cache"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
else if(strstr(buf, "L2 缓存"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
pclose(fp);
|
||||
|
||||
if(cache_nums == 0)
|
||||
{
|
||||
int i = 0;
|
||||
FILE *pipeLine = popen("dmidecode -t cache", "r");
|
||||
if (pipeLine != NULL)
|
||||
{
|
||||
memset(buf, 0, 1024);
|
||||
memset(res_buffer, 0, 64);
|
||||
while (fgets(buf, sizeof(buf), pipeLine))
|
||||
{
|
||||
if(strstr(buf, "L2 Cache") || strstr(buf, "L2 - Cache"))
|
||||
{
|
||||
i = 1;
|
||||
}
|
||||
|
||||
if(i != 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(strstr(buf, "Installed Size"))
|
||||
{
|
||||
strstripspace(buf);
|
||||
sscanf(buf, "Installed Size: %s %*s", res_buffer);
|
||||
cache_nums = atoi(res_buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pclose(pipeLine);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return cache_nums;
|
||||
}
|
||||
|
||||
unsigned int kdk_cpu_get_L3_cache()
|
||||
{
|
||||
#ifdef __linux__
|
||||
char res_buffer[64] = {0};
|
||||
char buf[1024] = {0};
|
||||
FILE *fp = NULL;
|
||||
int cache_nums = 0;
|
||||
|
||||
fp = popen("lscpu", "r");
|
||||
if(!fp)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
while (fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
if(strstr(buf, "L3 cache"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
else if(strstr(buf, "L3 缓存"))
|
||||
{
|
||||
sscanf(buf, "%*s %*s %s", res_buffer);
|
||||
strstrip(res_buffer, 'K');
|
||||
cache_nums = atoi(res_buffer);
|
||||
if(strstr(buf, "MiB"))
|
||||
{
|
||||
cache_nums = cache_nums * 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
pclose(fp);
|
||||
|
||||
if(cache_nums == 0)
|
||||
{
|
||||
int i = 0;
|
||||
FILE *pipeLine = popen("dmidecode -t cache", "r");
|
||||
if (pipeLine != NULL)
|
||||
{
|
||||
memset(buf, 0, 1024);
|
||||
memset(res_buffer, 0, 64);
|
||||
while (fgets(buf, sizeof(buf), pipeLine))
|
||||
{
|
||||
if(strstr(buf, "L3 Cache") || strstr(buf, "L3 - Cache"))
|
||||
{
|
||||
i = 1;
|
||||
}
|
||||
|
||||
if(i != 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(strstr(buf, "Installed Size"))
|
||||
{
|
||||
strstripspace(buf);
|
||||
sscanf(buf, "Installed Size: %s %*s", res_buffer);
|
||||
cache_nums = atoi(res_buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pclose(pipeLine);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return cache_nums;
|
||||
}
|
||||
|
|
|
@ -60,23 +60,73 @@ extern unsigned int kdk_cpu_get_corenums();
|
|||
extern const char* kdk_cpu_get_virt();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU线程数
|
||||
* @brief 获取CPU线程数/逻辑处理器数
|
||||
*
|
||||
* @return unsigned int 返回cpu支持的线程数
|
||||
* @return unsigned int 返回cpu支持的线程数/逻辑处理器数
|
||||
*/
|
||||
extern unsigned int kdk_cpu_get_process();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU最大频率
|
||||
*
|
||||
* @return float 返回cpu频率最大频率,如“2600.0000”,单位为MHz。
|
||||
*/
|
||||
extern float kdk_cpu_get_max_freq_MHz();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU最小频率
|
||||
*
|
||||
* @return float 返回cpu频率最小频率,如“1900.0000”,单位为MHz。
|
||||
*/
|
||||
extern float kdk_cpu_get_min_freq_MHz();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU运行时间
|
||||
*
|
||||
* @return char* 返回cpu运行时间,如“xx天xx小时xx分钟”;若失败,返回NULL;返回的字符串需要被 free 释放
|
||||
*/
|
||||
extern char* kdk_cpu_get_running_time();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU插槽
|
||||
*
|
||||
* @return unsigned int 返回cpu插槽数量; 失败返回-1。
|
||||
*/
|
||||
extern unsigned int kdk_cpu_get_sockets();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU L1缓存(数据)
|
||||
*
|
||||
* @return unsigned int 返回cpu L1缓存(数据); 失败返回-1。
|
||||
*/
|
||||
extern unsigned int kdk_cpu_get_L1d_cache();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU L1缓存(指令)
|
||||
*
|
||||
* @return unsigned int 返回cpu L1缓存(指令); 失败返回-1。
|
||||
*/
|
||||
extern unsigned int kdk_cpu_get_L1i_cache();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU L2缓存
|
||||
*
|
||||
* @return unsigned int 返回cpu L2缓存; 失败返回-1。
|
||||
*/
|
||||
extern unsigned int kdk_cpu_get_L2_cache();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU L3缓存
|
||||
*
|
||||
* @return unsigned int 返回cpu L3缓存; 失败返回-1。
|
||||
*/
|
||||
extern unsigned int kdk_cpu_get_L3_cache();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \example kysdk-system/src/hardware/test/kycpu-test.c
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -20,6 +20,15 @@
|
|||
#include <glib.h>
|
||||
#include <linux/sockios.h>
|
||||
#include <linux/ethtool.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <linux/wireless.h>
|
||||
#include <libnl3/netlink/route/link.h>
|
||||
#include <libnl3/netlink/route/addr.h>
|
||||
#include <libnl3/netlink/addr.h>
|
||||
#include <libnl3/netlink/types.h>
|
||||
#include <libnl3/netlink/route/route.h>
|
||||
#include <libnl3/netlink/netlink.h>
|
||||
|
||||
|
||||
enum cardspec{
|
||||
NCSPEC_ALL,
|
||||
|
@ -46,6 +55,25 @@ enum cardcfg{
|
|||
#define SIOCGIWNAME 0x8B01
|
||||
#define PCIID_PATH "/usr/share/misc/pci.ids"
|
||||
|
||||
struct get_ipv4_addr_ctx_t {
|
||||
struct nl_sock *sk;
|
||||
int flag; /* 是否找到ip */
|
||||
int index; /* 网卡索引 */
|
||||
unsigned char *ip;
|
||||
unsigned char *netmask;
|
||||
unsigned char *broadaddr;
|
||||
};
|
||||
|
||||
struct get_ipv6_addr_ctx_t {
|
||||
struct nl_sock *sk;
|
||||
int flag; /* 是否找到ip */
|
||||
int index; /* 网卡索引 */
|
||||
unsigned char *ip;
|
||||
int scope;
|
||||
int prefixlen;
|
||||
int type;
|
||||
};
|
||||
|
||||
static GKeyFile *
|
||||
configure_open_file(const gchar *filename)
|
||||
{
|
||||
|
@ -146,24 +174,36 @@ get_ip_list(enum cardcfg type,const char *nc)
|
|||
|
||||
if (0 == strcmp(val->str, nc))
|
||||
{
|
||||
configure_read_string(keyFile, val, value, "address1", "null");
|
||||
if (0 != strcmp(val->str, "null"))
|
||||
//这种实现方式一个网卡只能取到9个ip 因为ascii码只有1-9
|
||||
char num = '1';
|
||||
while (1)
|
||||
{
|
||||
int i = -1;
|
||||
while(val->str[++i] != 0x2f); // 0x2f是'/'的ascii码值
|
||||
val->str[i] = '\0';
|
||||
if (0 != strcmp(curAddr, val->str))
|
||||
char key[16];
|
||||
memset(key,0,16);
|
||||
strncpy(key,"address",7);
|
||||
key[7] = num++;
|
||||
configure_read_string(keyFile, val, value, key, "null");
|
||||
if (0 != strcmp(val->str, "null"))
|
||||
{
|
||||
res = (char **)realloc(result, (++count + 1) * sizeof(char *));
|
||||
if(!res)
|
||||
goto err_out;
|
||||
result = res;
|
||||
result[count - 1] = (char *)calloc(i + 1, sizeof(char));
|
||||
if(!result[count - 1])
|
||||
goto err_out;
|
||||
strncpy(result[count - 1], val->str, i);
|
||||
result[count] = NULL;
|
||||
int i = -1;
|
||||
while (val->str[++i] != 0x2f)
|
||||
; // 0x2f是'/'的ascii码值
|
||||
val->str[i] = '\0';
|
||||
if (0 != strcmp(curAddr, val->str))
|
||||
{
|
||||
res = (char **)realloc(result, (++count + 1) * sizeof(char *));
|
||||
if (!res)
|
||||
goto err_out;
|
||||
result = res;
|
||||
result[count - 1] = (char *)calloc(i + 1, sizeof(char));
|
||||
if (!result[count - 1])
|
||||
goto err_out;
|
||||
strncpy(result[count - 1], val->str, i);
|
||||
result[count] = NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_string_free(val,TRUE);
|
||||
|
@ -676,3 +716,786 @@ inline void kdk_nc_freeall(char **list)
|
|||
}
|
||||
free(list);
|
||||
}
|
||||
|
||||
static void ipv4_prefixlen2netmask(int prefixlen, unsigned char *netmask)
|
||||
{
|
||||
char buffer[32] = {0};
|
||||
|
||||
uint32_t mask = 0XFFFFFFFF;
|
||||
mask = -1 << (32 - prefixlen);
|
||||
sprintf(buffer, "%d.%d.%d.%d", (mask >> 24) & 0xFF, (mask >> 16) & 0xFF, (mask >> 8) & 0xFF, mask & 0xFF);
|
||||
|
||||
strcpy(netmask, buffer);
|
||||
}
|
||||
|
||||
static void get_ipv4_addr_cache_cb(struct nl_object *obj, void *arg)
|
||||
{
|
||||
const size_t bufsiz = 128;
|
||||
char tmpbuf[bufsiz];
|
||||
char broadcast[bufsiz];
|
||||
|
||||
struct get_ipv4_addr_ctx_t *ctx = (struct get_ipv4_addr_ctx_t *)arg;
|
||||
struct rtnl_addr *rtnl_addr = (struct rtnl_addr *)obj;
|
||||
|
||||
if (rtnl_addr_get_ifindex(rtnl_addr) == ctx->index) {
|
||||
struct nl_addr *nl_addr = rtnl_addr_get_local(rtnl_addr);
|
||||
|
||||
if (nl_addr_get_family(nl_addr) == AF_INET && nl_addr_get_len(nl_addr) == 4) {
|
||||
ctx->flag = 0x01;
|
||||
|
||||
nl_addr2str(nl_addr, tmpbuf, bufsiz);
|
||||
tmpbuf[bufsiz - 1] = '\0';
|
||||
char ** res = strsplit(tmpbuf, '/');
|
||||
if(strstr(res[0], ctx->ip))
|
||||
{
|
||||
struct nl_addr *nl_broad = rtnl_addr_get_broadcast(rtnl_addr);
|
||||
nl_addr2str(nl_broad, broadcast, bufsiz);
|
||||
broadcast[bufsiz - 1] = '\0';
|
||||
ctx->broadaddr = (char *)malloc(sizeof(char) * 32);
|
||||
strcpy(ctx->broadaddr, broadcast);
|
||||
|
||||
int netmask_prefixlen = rtnl_addr_get_prefixlen(rtnl_addr);
|
||||
if (netmask_prefixlen <= 32) {
|
||||
ctx->netmask = (char *)malloc(sizeof(char) * 32);
|
||||
memset(ctx->netmask, 0, 32);
|
||||
ipv4_prefixlen2netmask(netmask_prefixlen, ctx->netmask);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void get_ipv6_addr_cache_cb(struct nl_object *obj, void *arg)
|
||||
{
|
||||
const size_t bufsiz = 128;
|
||||
char tmpbuf[bufsiz];
|
||||
char broadcast[bufsiz];
|
||||
|
||||
struct get_ipv6_addr_ctx_t *ctx = (struct get_ipv_addr_ctx_t *)arg;
|
||||
struct rtnl_addr *rtnl_addr = (struct rtnl_addr *)obj;
|
||||
|
||||
if (rtnl_addr_get_ifindex(rtnl_addr) == ctx->index) {
|
||||
struct nl_addr *nl_addr = rtnl_addr_get_local(rtnl_addr);
|
||||
|
||||
if(ctx->type == 1)
|
||||
{
|
||||
if (nl_addr_get_family(nl_addr) == AF_INET && nl_addr_get_len(nl_addr) == 4) {
|
||||
ctx->flag = 0x01;
|
||||
|
||||
nl_addr2str(nl_addr, tmpbuf, bufsiz);
|
||||
tmpbuf[bufsiz - 1] = '\0';
|
||||
char ** res = strsplit(tmpbuf, '/');
|
||||
if(strstr(res[0], ctx->ip))
|
||||
{
|
||||
int netmask_prefixlen = rtnl_addr_get_prefixlen(rtnl_addr);
|
||||
if (netmask_prefixlen <= 32) {
|
||||
ctx->prefixlen = netmask_prefixlen;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
if (nl_addr_get_family(nl_addr) == AF_INET6 && nl_addr_get_len(nl_addr) == 16) {
|
||||
ctx->flag = 0x01;
|
||||
nl_addr2str(nl_addr, tmpbuf, bufsiz);
|
||||
tmpbuf[bufsiz - 1] = '\0';
|
||||
char ** res = strsplit(tmpbuf, '/');
|
||||
if(strstr(res[0], ctx->ip))
|
||||
{
|
||||
int netmask_prefixlen = rtnl_addr_get_prefixlen(rtnl_addr);
|
||||
ctx->prefixlen = netmask_prefixlen;
|
||||
|
||||
int scope = rtnl_addr_get_scope(rtnl_addr);
|
||||
ctx->scope = scope;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *kdk_nc_get_broadAddr(const char *eth_name, const char *address)
|
||||
{
|
||||
if (!address || !eth_name)
|
||||
return NULL;
|
||||
|
||||
int ret = 0;
|
||||
struct nl_cache *addr_cache;
|
||||
struct rtnl_link *link;
|
||||
char *ip = (char *)malloc(sizeof(char) * 32);
|
||||
if(!ip)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memset(ip, 0, 32);
|
||||
strcpy(ip, address);
|
||||
|
||||
char *broadAddr = (char *)malloc(sizeof(char) * 64);
|
||||
if(!broadAddr)
|
||||
{
|
||||
free(ip);
|
||||
return NULL;
|
||||
}
|
||||
memset(broadAddr, 0, 64);
|
||||
|
||||
struct nl_sock *sk = nl_socket_alloc();
|
||||
nl_connect(sk, NETLINK_ROUTE);
|
||||
|
||||
ret = rtnl_link_get_kernel(sk, 0, eth_name, &link);
|
||||
|
||||
if (ret < 0) {
|
||||
ret = -1;
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
struct get_ipv4_addr_ctx_t ctx;
|
||||
memset(&ctx, 0x00, sizeof(struct get_ipv4_addr_ctx_t));
|
||||
|
||||
ctx.index = rtnl_link_get_ifindex(link);
|
||||
ctx.ip = ip;
|
||||
|
||||
rtnl_addr_alloc_cache(sk, &addr_cache);
|
||||
nl_cache_foreach(addr_cache, get_ipv4_addr_cache_cb, &ctx);
|
||||
|
||||
if (ctx.flag == 0) {
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
nl_cache_put(addr_cache);
|
||||
rtnl_link_put(link);
|
||||
free(ip);
|
||||
|
||||
strcpy(broadAddr, ctx.broadaddr);
|
||||
free(ctx.broadaddr);
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
return broadAddr;
|
||||
|
||||
error_out:
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
free(ip);
|
||||
free(broadAddr);
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
char *kdk_nc_get_netmask(const char *eth_name, const char *addr)
|
||||
{
|
||||
if (!addr || !eth_name)
|
||||
return NULL;
|
||||
|
||||
int ret = 0;
|
||||
struct nl_cache *addr_cache;
|
||||
struct rtnl_link *link;
|
||||
char *ip = (char *)malloc(sizeof(char) * 32);
|
||||
if(!ip)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memset(ip, 0, 32);
|
||||
strcpy(ip, addr);
|
||||
|
||||
char *netmask = (char *)malloc(sizeof(char) * 64);
|
||||
if(!netmask)
|
||||
{
|
||||
free(ip);
|
||||
return NULL;
|
||||
}
|
||||
memset(netmask, 0, 64);
|
||||
|
||||
struct nl_sock *sk = nl_socket_alloc();
|
||||
nl_connect(sk, NETLINK_ROUTE);
|
||||
|
||||
ret = rtnl_link_get_kernel(sk, 0, eth_name, &link);
|
||||
|
||||
if (ret < 0) {
|
||||
ret = -1;
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
struct get_ipv4_addr_ctx_t ctx;
|
||||
memset(&ctx, 0x00, sizeof(struct get_ipv4_addr_ctx_t));
|
||||
|
||||
ctx.index = rtnl_link_get_ifindex(link);
|
||||
ctx.ip = ip;
|
||||
|
||||
rtnl_addr_alloc_cache(sk, &addr_cache);
|
||||
nl_cache_foreach(addr_cache, get_ipv4_addr_cache_cb, &ctx);
|
||||
|
||||
if (ctx.flag == 0) {
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
nl_cache_put(addr_cache);
|
||||
rtnl_link_put(link);
|
||||
free(ip);
|
||||
|
||||
strcpy(netmask, ctx.netmask);
|
||||
free(ctx.netmask);
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
return netmask;
|
||||
|
||||
error_out:
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
free(ip);
|
||||
free(netmask);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int kdk_nc_get_mask_length(int type, const char *eth_name, const char *addr)
|
||||
{
|
||||
if (!addr || !eth_name)
|
||||
return -1;
|
||||
|
||||
int ret = 0;
|
||||
struct nl_cache *addr_cache;
|
||||
struct rtnl_link *link;
|
||||
char *ip = (char *)malloc(sizeof(char) * 32);
|
||||
if(!ip)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memset(ip, 0, 32);
|
||||
strcpy(ip, addr);
|
||||
|
||||
struct nl_sock *sk = nl_socket_alloc();
|
||||
nl_connect(sk, NETLINK_ROUTE);
|
||||
|
||||
ret = rtnl_link_get_kernel(sk, 0, eth_name, &link);
|
||||
|
||||
if (ret < 0) {
|
||||
ret = -1;
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
struct get_ipv6_addr_ctx_t ctx;
|
||||
memset(&ctx, 0x00, sizeof(struct get_ipv6_addr_ctx_t));
|
||||
|
||||
ctx.index = rtnl_link_get_ifindex(link);
|
||||
ctx.ip = ip;
|
||||
|
||||
rtnl_addr_alloc_cache(sk, &addr_cache);
|
||||
nl_cache_foreach(addr_cache, get_ipv6_addr_cache_cb, &ctx);
|
||||
|
||||
if (ctx.flag == 0) {
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
nl_cache_put(addr_cache);
|
||||
rtnl_link_put(link);
|
||||
free(ip);
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
return ctx.prefixlen;
|
||||
|
||||
error_out:
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
free(ip);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *kdk_nc_get_conn_type(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return NULL;
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strcpy(ifr.ifr_name, nc);
|
||||
int fd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (!ioctl(fd, SIOCGIFHWADDR, &ifr)) {
|
||||
int m_sa_family = ifr.ifr_hwaddr.sa_family;
|
||||
close(fd);
|
||||
switch (m_sa_family) {
|
||||
case ARPHRD_ETHER:
|
||||
return "Ethernet";
|
||||
case ARPHRD_SLIP:
|
||||
return "SLIP";
|
||||
case ARPHRD_LOOPBACK:
|
||||
return "loopback";
|
||||
case ARPHRD_FDDI:
|
||||
return "FDDI";
|
||||
case ARPHRD_IEEE1394:
|
||||
return "IEEE1394";
|
||||
case ARPHRD_IRDA:
|
||||
return "IRDA";
|
||||
case ARPHRD_PPP:
|
||||
return "PPP";
|
||||
case ARPHRD_X25:
|
||||
return "X25";
|
||||
case ARPHRD_TUNNEL:
|
||||
return "IPtunnel";
|
||||
case ARPHRD_DLCI:
|
||||
return "Framerelay.DLCI";
|
||||
case ARPHRD_FRAD:
|
||||
return "Framerelay.AD";
|
||||
case ARPHRD_TUNNEL6:
|
||||
return "IP6tunnel";
|
||||
case ARPHRD_SIT:
|
||||
return "IP6inIP4";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else{
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
char *kdk_nc_get_wifi_name(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return NULL;
|
||||
struct iwreq wrq;
|
||||
struct iw_statistics stats;
|
||||
char *wifi_name = (char *)malloc(sizeof(char) * 512);
|
||||
if(!wifi_name)
|
||||
return NULL;
|
||||
memset(wifi_name, 0, 512);
|
||||
char buffer[512];
|
||||
memset(&wrq,0,sizeof(wrq));
|
||||
memset(&stats,0,sizeof(stats));
|
||||
memset(buffer, 0, 512);
|
||||
strcpy(wrq.ifr_name, nc);
|
||||
wrq.u.data.pointer = &stats;
|
||||
wrq.u.data.length = sizeof(struct iw_statistics);
|
||||
int sock = 0;
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
close(sock);
|
||||
free(wifi_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ioctl(sock, SIOCGIWSTATS, &wrq) == -1) {
|
||||
close(sock);
|
||||
free(wifi_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wrq.u.essid.pointer = buffer;//如果不写这行可能会错误
|
||||
wrq.u.essid.length = 512;
|
||||
if (ioctl(sock, SIOCGIWESSID, &wrq) == -1) {
|
||||
close(sock);
|
||||
free(wifi_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(wrq.u.essid.flags != 0){
|
||||
strncpy(wifi_name, buffer, 512); //wifi名称
|
||||
}
|
||||
close(sock);
|
||||
return wifi_name;
|
||||
}
|
||||
|
||||
int kdk_nc_get_wifi_signal_qual(const char *nc)
|
||||
{
|
||||
struct iwreq wrq;
|
||||
struct iw_statistics stats;
|
||||
char buffer[512];
|
||||
memset(&wrq,0,sizeof(wrq));
|
||||
memset(&stats,0,sizeof(stats));
|
||||
memset(buffer, 0, 512);
|
||||
strcpy(wrq.ifr_name, nc);
|
||||
wrq.u.data.pointer = &stats;
|
||||
wrq.u.data.length = sizeof(struct iw_statistics);
|
||||
int sock = 0;
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ioctl(sock, SIOCGIWSTATS, &wrq) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wrq.u.essid.pointer = buffer;//如果不写这行可能会错误
|
||||
wrq.u.essid.length = 512;
|
||||
if (ioctl(sock, SIOCGIWESSID, &wrq) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(sock);
|
||||
return stats.qual.qual;
|
||||
}
|
||||
|
||||
int kdk_nc_get_wifi_signal_level(const char *nc)
|
||||
{
|
||||
struct iwreq wrq;
|
||||
struct iw_statistics stats;
|
||||
char buffer[512];
|
||||
memset(&wrq,0,sizeof(wrq));
|
||||
memset(&stats,0,sizeof(stats));
|
||||
memset(buffer, 0, 512);
|
||||
strcpy(wrq.ifr_name, nc);
|
||||
wrq.u.data.pointer = &stats;
|
||||
wrq.u.data.length = sizeof(struct iw_statistics);
|
||||
int sock = 0;
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ioctl(sock, SIOCGIWSTATS, &wrq) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wrq.u.essid.pointer = buffer;//如果不写这行可能会错误
|
||||
wrq.u.essid.length = 512;
|
||||
if (ioctl(sock, SIOCGIWESSID, &wrq) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(sock);
|
||||
return stats.qual.level;
|
||||
}
|
||||
|
||||
int kdk_nc_get_wifi_noise(const char *nc)
|
||||
{
|
||||
struct iwreq wrq;
|
||||
struct iw_statistics stats;
|
||||
char buffer[512];
|
||||
memset(&wrq,0,sizeof(wrq));
|
||||
memset(&stats,0,sizeof(stats));
|
||||
memset(buffer, 0, 512);
|
||||
strcpy(wrq.ifr_name, nc);
|
||||
wrq.u.data.pointer = &stats;
|
||||
wrq.u.data.length = sizeof(struct iw_statistics);
|
||||
int sock = 0;
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ioctl(sock, SIOCGIWSTATS, &wrq) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wrq.u.essid.pointer = buffer;//如果不写这行可能会错误
|
||||
wrq.u.essid.length = 512;
|
||||
if (ioctl(sock, SIOCGIWESSID, &wrq) == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(sock);
|
||||
return stats.qual.noise;
|
||||
}
|
||||
|
||||
char *kdk_nc_get_speed(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return NULL;
|
||||
struct ifreq ifr;
|
||||
struct ethtool_cmd ecmd;
|
||||
char *speed = (char *)malloc(sizeof(char) * 32);
|
||||
if(!speed)
|
||||
return NULL;
|
||||
memset(speed, 0, 32);
|
||||
|
||||
ecmd.cmd = 0x00000001;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strcpy(ifr.ifr_name, nc);
|
||||
|
||||
ifr.ifr_data = &ecmd;
|
||||
int fd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (!ioctl(fd, SIOCETHTOOL, &ifr)) {
|
||||
sprintf(speed, "%d Mb/s", ecmd.speed);
|
||||
close(fd);
|
||||
return speed;
|
||||
}
|
||||
else{
|
||||
close(fd);
|
||||
free(speed);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int kdk_nc_get_rx_packets(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int packets = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/rx_packets", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &packets);
|
||||
fclose(fp);
|
||||
return packets;
|
||||
}
|
||||
|
||||
int kdk_nc_get_rx_bytes(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int bytes = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/rx_bytes", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &bytes);
|
||||
fclose(fp);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int kdk_nc_get_rx_errors(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int errors = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/rx_errors", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &errors);
|
||||
fclose(fp);
|
||||
return errors;
|
||||
}
|
||||
|
||||
int kdk_nc_get_rx_dropped(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int dropped = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/rx_dropped", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &dropped);
|
||||
fclose(fp);
|
||||
return dropped;
|
||||
}
|
||||
|
||||
int kdk_nc_get_rx_fifo_errors(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int fifo_errors = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/rx_fifo_errors", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &fifo_errors);
|
||||
fclose(fp);
|
||||
return fifo_errors;
|
||||
}
|
||||
|
||||
int kdk_nc_get_rx_frame_errors(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int frame_errors = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/rx_frame_errors", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &frame_errors);
|
||||
fclose(fp);
|
||||
return frame_errors;
|
||||
}
|
||||
|
||||
int kdk_nc_get_tx_packets(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int packets = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/tx_packets", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &packets);
|
||||
fclose(fp);
|
||||
return packets;
|
||||
}
|
||||
|
||||
int kdk_nc_get_tx_bytes(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int tx_bytes = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/tx_bytes", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &tx_bytes);
|
||||
fclose(fp);
|
||||
return tx_bytes;
|
||||
}
|
||||
|
||||
int kdk_nc_get_tx_errors(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int tx_errors = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/tx_errors", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &tx_errors);
|
||||
fclose(fp);
|
||||
return tx_errors;
|
||||
}
|
||||
|
||||
int kdk_nc_get_tx_dropped(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int tx_dropped = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/tx_dropped", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &tx_dropped);
|
||||
fclose(fp);
|
||||
return tx_dropped;
|
||||
}
|
||||
|
||||
int kdk_nc_get_tx_fifo_errors(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int fifo_errors = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/tx_fifo_errors", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &fifo_errors);
|
||||
fclose(fp);
|
||||
return fifo_errors;
|
||||
}
|
||||
|
||||
int kdk_nc_get_tx_carrier_errors(const char *nc)
|
||||
{
|
||||
if(!nc)
|
||||
return -1;
|
||||
char filename[256] = {0};
|
||||
char res[64] = {0};
|
||||
int carrier_errors = 0;
|
||||
sprintf(filename, "/sys/class/net/%s/statistics/tx_carrier_errors", nc);
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (! fp)
|
||||
return -1;
|
||||
|
||||
fgets(res, sizeof(res), fp);
|
||||
strstrip(res, '\n');
|
||||
sscanf(res, "%d", &carrier_errors);
|
||||
fclose(fp);
|
||||
return carrier_errors;
|
||||
}
|
||||
|
||||
int kdk_nc_get_scope(const char *eth_name, const char *addr)
|
||||
{
|
||||
if (!addr || !eth_name)
|
||||
return -1;
|
||||
|
||||
int ret = 0;
|
||||
struct nl_cache *addr_cache;
|
||||
struct rtnl_link *link;
|
||||
char *ip = (char *)malloc(sizeof(char) * 32);
|
||||
if(!ip)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memset(ip, 0, 32);
|
||||
strcpy(ip, addr);
|
||||
|
||||
struct nl_sock *sk = nl_socket_alloc();
|
||||
nl_connect(sk, NETLINK_ROUTE);
|
||||
|
||||
ret = rtnl_link_get_kernel(sk, 0, eth_name, &link);
|
||||
|
||||
if (ret < 0) {
|
||||
ret = -1;
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
struct get_ipv6_addr_ctx_t ctx;
|
||||
memset(&ctx, 0x00, sizeof(struct get_ipv6_addr_ctx_t));
|
||||
|
||||
ctx.index = rtnl_link_get_ifindex(link);
|
||||
ctx.ip = ip;
|
||||
|
||||
rtnl_addr_alloc_cache(sk, &addr_cache);
|
||||
nl_cache_foreach(addr_cache, get_ipv6_addr_cache_cb, &ctx);
|
||||
|
||||
if (ctx.flag == 0) {
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
nl_cache_put(addr_cache);
|
||||
rtnl_link_put(link);
|
||||
free(ip);
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
return ctx.scope;
|
||||
|
||||
error_out:
|
||||
|
||||
nl_close(sk);
|
||||
nl_socket_free(sk);
|
||||
free(ip);
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -113,17 +113,192 @@ extern char *kdk_nc_get_driver(const char *nc);
|
|||
*/
|
||||
extern inline void kdk_nc_freeall(char **ptr);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的广播地址
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @param addr ip地址
|
||||
* @return char * 成功返回网卡广播地址, 失败返回NULL。返回的字符串需要被 free 释放
|
||||
*/
|
||||
extern char *kdk_nc_get_broadAddr(const char *nc, const char *addr);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的子网掩码
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @param addr ip地址
|
||||
* @return char * 成功返回子网掩码, 失败返回NULL。返回的字符串需要被 free 释放
|
||||
*/
|
||||
extern char *kdk_nc_get_netmask(const char *nc, const char *addr);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的掩码长度
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @param type 1 ipv4,0 ipv6
|
||||
* @param addr ip地址
|
||||
* @return int 网卡的掩码长度
|
||||
*/
|
||||
extern int kdk_nc_get_mask_length(int type, const char *nc, const char *addr);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的连接类型
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return char * 成功返回网卡的连接类型,如"Ethernet", 失败返回NULL;不需要free。
|
||||
*/
|
||||
extern char *kdk_nc_get_conn_type(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取wifi名称
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return char * 成功返回wifi名称, 返回的字符串需要被 free 释放。
|
||||
*/
|
||||
extern char *kdk_nc_get_wifi_name(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取信号质量(连接 wifi 显示)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回信号质量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_wifi_signal_qual(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取信号强度(连接 wifi 显示)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回信号强度, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_wifi_signal_level(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取底噪(连接 wifi 显示)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回底噪, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_wifi_noise(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取网卡速率
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return char * 成功返回网卡的速率,如"1000Mb/s", 失败返回NULL;返回的字符串需要被 free 释放。
|
||||
*/
|
||||
extern char *kdk_nc_get_speed(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的接收包数量
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的接收包数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_rx_packets(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的总计接收
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的总计接收,单位为字节, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_rx_bytes(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的接收(错误包)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的接收(错误包)数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_rx_errors(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的接收(丢弃包)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的接收(丢弃包)数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_rx_dropped(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的接收FIFO
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的接收FIFO数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_rx_fifo_errors(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的分组帧错误
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的分组帧错误, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_rx_frame_errors(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的发送包数量
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的发送包数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_tx_packets(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的总计发送
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的总计发送, 单位为字节, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_tx_bytes(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的发送(错误包)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的发送(错误包)数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_tx_errors(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的发送(丢弃包)
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的发送(丢弃包)数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_tx_dropped(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的发送FIFO
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的发送FIFO数量, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_tx_fifo_errors(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的载波损耗
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @return int 成功返回网卡的载波损耗, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_tx_carrier_errors(const char *nc);
|
||||
|
||||
/**
|
||||
* @brief 获取指定网卡的域
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @param addr ip地址
|
||||
* @return int 成功返回网卡的域, 失败返回-1。
|
||||
*/
|
||||
extern int kdk_nc_get_scope(const char *nc, const char *addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \example kysdk-system/src/hardware/test/kync-test.c
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "../libkycpu.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -10,5 +11,16 @@ int main()
|
|||
printf("CPU 单核核心数量:%u\n", kdk_cpu_get_corenums());
|
||||
printf("CPU 虚拟化支持:%s\n", kdk_cpu_get_virt());
|
||||
printf("CPU 线程数:%u\n", kdk_cpu_get_process());
|
||||
printf("CPU 最大频率:%0.2f MHz\n", kdk_cpu_get_max_freq_MHz());
|
||||
printf("CPU 最小频率:%0.2f MHz\n", kdk_cpu_get_min_freq_MHz());
|
||||
char *run_time = kdk_cpu_get_running_time();
|
||||
printf("CPU 运行时间:%s\n", run_time);
|
||||
free(run_time);
|
||||
printf("CPU 插槽:%d\n", kdk_cpu_get_sockets());
|
||||
printf("CPU L1缓存(数据):%d\n", kdk_cpu_get_L1d_cache());
|
||||
printf("CPU L1缓存(指令):%d\n", kdk_cpu_get_L1i_cache());
|
||||
printf("CPU L2缓存:%d\n", kdk_cpu_get_L2_cache());
|
||||
printf("CPU L3缓存:%d\n", kdk_cpu_get_L3_cache());
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,17 +15,81 @@ int main()
|
|||
char *driver = kdk_nc_get_driver(cards[index]);
|
||||
char vendor[256] = "\0", product[256] = "\0";
|
||||
kdk_nc_get_vendor_and_product(cards[index], vendor, product);
|
||||
printf("Card %zd: %s\tStatus: %s\tMac: %s\tIPv4: %s\tIPv6: %s\t Vendor: %s\t Product: %s\t Type: %s\t driver: %s\n",
|
||||
|
||||
printf("Card %zd: %s\tStatus: %s\tMac: %s\tIPv4: %s\tIPv6: %s\t Vendor: %s\t Product: %s\t Type: %s\t driver: %s\t \n",
|
||||
index + 1, cards[index], kdk_nc_is_up(cards[index]) == 1 ? "Up" : "Down",
|
||||
mac, ipv4, ipv6, vendor, product,
|
||||
kdk_nc_is_wireless(cards[index]) ? "wireless" : "ethernet", driver);
|
||||
char *conn = kdk_nc_get_conn_type(cards[index]);
|
||||
printf("Conn type = %s\n", conn);
|
||||
|
||||
char *wifi_name = kdk_nc_get_wifi_name(cards[index]);
|
||||
printf("wifi name = %s\n", wifi_name);
|
||||
free(wifi_name);
|
||||
|
||||
char *speed = kdk_nc_get_speed(cards[index]);
|
||||
printf("speed = %s\n", speed);
|
||||
free(speed);
|
||||
|
||||
int rx_packets = kdk_nc_get_rx_packets(cards[index]);
|
||||
printf("rx_packets = %d\n", rx_packets);
|
||||
|
||||
int rx_bytes = kdk_nc_get_rx_bytes(cards[index]);
|
||||
printf("rx_bytes = %d\n", rx_bytes);
|
||||
|
||||
int rx_errors = kdk_nc_get_rx_errors(cards[index]);
|
||||
printf("rx_errors = %d\n", rx_errors);
|
||||
|
||||
int rx_dropped = kdk_nc_get_rx_dropped(cards[index]);
|
||||
printf("rx_dropped = %d\n", rx_dropped);
|
||||
|
||||
int fifo_errors = kdk_nc_get_rx_fifo_errors(cards[index]);
|
||||
printf("fifo_errors = %d\n", fifo_errors);
|
||||
|
||||
int frame_errors = kdk_nc_get_rx_frame_errors(cards[index]);
|
||||
printf("frame_errors = %d\n", frame_errors);
|
||||
|
||||
int tx_packets = kdk_nc_get_tx_packets(cards[index]);
|
||||
printf("tx_packets = %d\n", tx_packets);
|
||||
|
||||
int tx_bytes = kdk_nc_get_tx_bytes(cards[index]);
|
||||
printf("tx_bytes = %d\n", tx_bytes);
|
||||
|
||||
int tx_errors = kdk_nc_get_tx_errors(cards[index]);
|
||||
printf("tx_errors = %d\n", tx_errors);
|
||||
|
||||
int tx_dropped = kdk_nc_get_tx_dropped(cards[index]);
|
||||
printf("tx_dropped = %d\n", tx_dropped);
|
||||
|
||||
int tx_fifo_errors = kdk_nc_get_tx_fifo_errors(cards[index]);
|
||||
printf("tx_fifo_errors = %d\n", tx_fifo_errors);
|
||||
|
||||
int tx_carrier_errors = kdk_nc_get_tx_carrier_errors(cards[index]);
|
||||
printf("tx_carrier_errors = %d\n", tx_carrier_errors);
|
||||
|
||||
int signal_qual = kdk_nc_get_wifi_signal_qual(cards[index]);
|
||||
printf("signal_qual = %d\n", signal_qual);
|
||||
|
||||
int signal_level = kdk_nc_get_wifi_signal_level(cards[index]);
|
||||
printf("signal_level = %d\n", signal_level);
|
||||
|
||||
int noise = kdk_nc_get_wifi_noise(cards[index]);
|
||||
printf("noise = %d\n", noise);
|
||||
|
||||
char **list4 = kdk_nc_get_ipv4(cards[index]);
|
||||
int i = 0;
|
||||
printf("AllIpV4: ");
|
||||
while (list4 && list4[i])
|
||||
{
|
||||
printf("%s\t", list4[i++]);
|
||||
printf("%s\n", list4[i]);
|
||||
char *boardaddr = kdk_nc_get_broadAddr(cards[index], list4[i]);
|
||||
printf("boardaddr = %s\n", boardaddr);
|
||||
free(boardaddr);
|
||||
|
||||
char *netmask = kdk_nc_get_netmask(cards[index], list4[i]);
|
||||
printf("netmask = %s\n", netmask);
|
||||
free(netmask);
|
||||
i++;
|
||||
}
|
||||
printf("\n");
|
||||
char **list6 = kdk_nc_get_ipv6(cards[index]);
|
||||
|
@ -33,8 +97,15 @@ int main()
|
|||
i = 0;
|
||||
while (list6 && list6[i])
|
||||
{
|
||||
printf("%s\t", list6[i++]);
|
||||
printf("%s\t", list6[i]);
|
||||
int len = kdk_nc_get_mask_length(0, cards[index], list6[i]);
|
||||
printf("netmask_length = %d\n", len);
|
||||
|
||||
int scope = kdk_nc_get_scope(cards[index], list6[i]);
|
||||
printf("scope = %d\n", scope);
|
||||
i++;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
free(mac);
|
||||
free(ipv4);
|
||||
|
|
|
@ -48,15 +48,31 @@ typedef struct chain
|
|||
struct chain *next;
|
||||
} Chain, *pChain;
|
||||
|
||||
/// @brief 获取指定网络端口的状态
|
||||
/// @param port : 端口号
|
||||
/// @return 端口状态 TCP_ESTABLISHED:0x1 TCP_SYN_SENT:0x2 TCP_SYN_RECV:0x3 TCP_FIN_WAIT1:0x4 TCP_FIN_WAIT2:0x5 \
|
||||
TCP_TIME_WAIT:0x6 TCP_CLOSE:0x7 TCP_CLOSE_WAIT:0x8 TCP_LAST_ACL:0x9 TCP_LISTEN:0xa TCP_CLOSING:0xb
|
||||
extern int kdk_net_get_port_stat(int port);
|
||||
/**
|
||||
* @brief 获取指定网络端口的状态
|
||||
*
|
||||
* @param port 端口号
|
||||
* @return int 端口状态 TCP_ESTABLISHED:0x1 TCP_SYN_SENT:0x2 TCP_SYN_RECV:0x3 TCP_FIN_WAIT1:0x4 TCP_FIN_WAIT2:0x5
|
||||
TCP_TIME_WAIT:0x6 TCP_CLOSE:0x7 TCP_CLOSE_WAIT:0x8 TCP_LAST_ACL:0x9 TCP_LISTEN:0xa TCP_CLOSING:0xb
|
||||
*/
|
||||
extern int kdk_net_get_port_stat(int port);
|
||||
|
||||
extern int kdk_net_get_multiple_port_stat(int start, int end, int *result);
|
||||
/**
|
||||
* @brief 获取区间端口状态
|
||||
*
|
||||
* @param start 开始端口
|
||||
* @param end 结束端口
|
||||
* @param result 区间端口状态接收缓存区
|
||||
* @return int 成功返回0 失败返回-1
|
||||
*/
|
||||
extern int kdk_net_get_multiple_port_stat(int start, int end, int *result);
|
||||
|
||||
extern prouteMapList kdk_net_get_route();
|
||||
/**
|
||||
* @brief 获取网关信息
|
||||
*
|
||||
* @return prouteMapList 网关信息存储结构体
|
||||
*/
|
||||
extern prouteMapList kdk_net_get_route();
|
||||
|
||||
/**
|
||||
* @brief 释放由kdk_net_get_route返回的网关信息结构体
|
||||
|
@ -79,7 +95,7 @@ extern pChain kdk_net_get_iptable_rules();
|
|||
extern void kdk_net_free_chain(pChain list);
|
||||
|
||||
/**
|
||||
* @brief 获取子网信息
|
||||
* @brief 获取子网掩码信息
|
||||
*
|
||||
* @param nc 网卡名称,如eno1
|
||||
* @param mask 子网掩码
|
||||
|
|
|
@ -327,4 +327,184 @@ int kdk_rti_get_uptime(unsigned int *day, unsigned int *hour, unsigned int *min,
|
|||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_shared_KiB()
|
||||
{
|
||||
unsigned long shmem = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Shmem", &shmem))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return shmem;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_cached_KiB()
|
||||
{
|
||||
unsigned long cached = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Cached", &cached))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return cached;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_buffers_KiB()
|
||||
{
|
||||
unsigned long buffers = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Buffers", &buffers))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return buffers;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_swap_cached_KiB()
|
||||
{
|
||||
unsigned long swapCached = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "SwapCached", &swapCached))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return swapCached;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_active_KiB()
|
||||
{
|
||||
unsigned long active = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Active", &active))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return active;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_inactive_KiB()
|
||||
{
|
||||
unsigned long inactive = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Inactive", &inactive))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return inactive;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_dirty_KiB()
|
||||
{
|
||||
unsigned long dirty = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Dirty", &dirty))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return dirty;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_map_KiB()
|
||||
{
|
||||
unsigned long map = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Mapped", &map))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
unsigned long kdk_rti_get_mem_slab_KiB()
|
||||
{
|
||||
unsigned long slab = 0;
|
||||
#ifdef __linux__
|
||||
FILE *fp = fopen(MEMINFO_FILE, "rt");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
char buffer[1025];
|
||||
while (fgets(buffer, 1024, fp))
|
||||
{
|
||||
if (lookup(buffer, "Slab", &slab))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
#endif
|
||||
|
||||
return slab;
|
||||
}
|
||||
|
|
|
@ -113,17 +113,74 @@ extern float kdk_rti_get_cpu_current_usage();
|
|||
*/
|
||||
extern int kdk_rti_get_uptime(unsigned int *day, unsigned int *hour, unsigned int *min, unsigned int *sec);
|
||||
|
||||
/**
|
||||
* @brief 获取共享内存大小
|
||||
*
|
||||
* @return unsigned long 共享内存大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_shared_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取高速缓存大小
|
||||
*
|
||||
* @return unsigned long 高速缓存大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_cached_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取数据缓存大小
|
||||
*
|
||||
* @return unsigned long 数据缓存大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_buffers_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取交换缓存区大小
|
||||
*
|
||||
* @return unsigned long 交换缓存区大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_swap_cached_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取活跃的缓冲文件大小
|
||||
*
|
||||
* @return unsigned long 活跃的缓冲文件大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_active_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取不活跃的缓冲文件大小
|
||||
*
|
||||
* @return unsigned long 不活跃的缓冲文件大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_inactive_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取脏页大小
|
||||
*
|
||||
* @return unsigned long 脏页大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_dirty_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取映射大小
|
||||
*
|
||||
* @return unsigned long 映射大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_map_KiB();
|
||||
|
||||
/**
|
||||
* @brief 获取内核数据结构缓存大小
|
||||
*
|
||||
* @return unsigned long 内核数据结构缓存大小,KiB为单位
|
||||
*/
|
||||
extern unsigned long kdk_rti_get_mem_slab_KiB();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \example kysdk-system/src/proc/test/kyrtinfo-test.c
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -12,8 +12,17 @@ int main()
|
|||
printf("交换分区已用量:%lu KiB, %f\n", kdk_rti_get_mem_swap_usage_KiB(), kdk_rti_get_mem_swap_usage_percent());
|
||||
printf("交换分区剩余大小:%lu KiB\n", kdk_rti_get_mem_swap_free_KiB());
|
||||
printf("当前CPU使用率:%f\n", kdk_rti_get_cpu_current_usage());
|
||||
printf("共享内存大小:%lu KiB\n", kdk_rti_get_mem_shared_KiB());
|
||||
printf("高速缓存大小:%lu KiB\n", kdk_rti_get_mem_cached_KiB());
|
||||
printf("数据缓存大小:%lu KiB\n", kdk_rti_get_mem_buffers_KiB());
|
||||
printf("交换缓存区大小:%lu KiB\n", kdk_rti_get_mem_swap_cached_KiB());
|
||||
printf("活跃的缓冲文件大小:%lu KiB\n", kdk_rti_get_mem_active_KiB());
|
||||
printf("不活跃的缓冲文件大小:%lu KiB\n", kdk_rti_get_mem_inactive_KiB());
|
||||
printf("脏页大小:%lu KiB\n", kdk_rti_get_mem_dirty_KiB());
|
||||
printf("映射大小:%lu KiB\n", kdk_rti_get_mem_map_KiB());
|
||||
printf("内核数据结构缓存大小:%lu KiB\n", kdk_rti_get_mem_slab_KiB());
|
||||
unsigned int day, hour, min, sec;
|
||||
kdk_rti_get_uptime(&day, &hour, &min, &sec);
|
||||
printf("开机时长:%u天%u小时%u分钟%u秒\n", day, hour, min, sec);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,7 +142,7 @@ float kdk_real_get_net_speed(const char *nc)
|
|||
int ret = 0;
|
||||
memset(&ndev, 0, sizeof(ndev));
|
||||
sprintf(ndev.ifs_name, "%s", nc);
|
||||
ndev.ifs_us = 1000000;
|
||||
ndev.ifs_us = 100000;
|
||||
ret = get_if_speed(&ndev);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include <utime.h>
|
||||
#include "pci/pci.h"
|
||||
#include <locale.h>
|
||||
#include "sys/sysinfo.h"
|
||||
|
||||
#define SAFE_FREE(x) if (x) {free(x); x = NULL;}
|
||||
#define KYLIN_ACTIVATION_DBUS_ADDRESS "org.freedesktop.activation"
|
||||
|
@ -49,6 +50,11 @@ struct device
|
|||
unsigned char *present; /* Maps which configuration bytes are present */
|
||||
};
|
||||
|
||||
uint verify_file(char *pFileName)
|
||||
{
|
||||
return !strncmp(pFileName, "/proc", strlen("/proc"));
|
||||
}
|
||||
|
||||
static char* get_val_from_file(FILE *fp, const char *key)
|
||||
{
|
||||
if (! fp)
|
||||
|
@ -2196,3 +2202,135 @@ char *kdk_system_get_appScene()
|
|||
#endif
|
||||
return scene;
|
||||
}
|
||||
|
||||
unsigned int kdk_system_get_file_descriptor()
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
unsigned int file_nr = 0;
|
||||
|
||||
errno = 0;
|
||||
if ((fp = fopen("/proc/sys/fs/file-nr", "r"))) {
|
||||
char buf[128] = {0};
|
||||
if (fgets(buf, sizeof(buf), fp)) {
|
||||
int nm = sscanf(buf, "%u", &file_nr);
|
||||
if (nm == 1)
|
||||
{
|
||||
return file_nr;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int kdk_system_get_process_nums()
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
int count = 0;
|
||||
|
||||
// 打开/proc目录
|
||||
dir = opendir("/proc");
|
||||
if (dir == NULL) {
|
||||
// perror("无法打开/proc目录");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 遍历目录并计算进程总数
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (entry->d_type == DT_DIR) {
|
||||
// 判断目录名是否为数字(代表进程)
|
||||
char *name = entry->d_name;
|
||||
int is_process = 1;
|
||||
for (int i = 0; name[i] != '\0'; i++) {
|
||||
if (!isdigit(name[i])) {
|
||||
is_process = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_process) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
unsigned int kdk_system_get_thread_nums()
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
char path[128] = {0};
|
||||
struct stat fileStat;
|
||||
int threadCount = 0;
|
||||
char buffer[1024] = {0};
|
||||
|
||||
// 打开/proc目录
|
||||
dir = opendir("/proc");
|
||||
if (dir == NULL) {
|
||||
perror("无法打开/proc目录");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 遍历目录并计算进程总数
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (entry->d_type == DT_DIR) {
|
||||
// 判断目录名是否为数字(代表进程)
|
||||
char *name = entry->d_name;
|
||||
int is_process = 1;
|
||||
for (int i = 0; name[i] != '\0'; i++) {
|
||||
if (!isdigit(name[i])) {
|
||||
is_process = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_process) {
|
||||
// count++;
|
||||
sprintf(path, "/proc/%s/status", entry->d_name);
|
||||
char canonical_filename[4096] = {"\0"};
|
||||
FILE *fp = NULL;
|
||||
if (!realpath(path, canonical_filename) || !verify_file(canonical_filename))
|
||||
{
|
||||
closedir(dir);
|
||||
return -1;
|
||||
}
|
||||
fp = fopen(canonical_filename, "r");
|
||||
if(!fp)
|
||||
{
|
||||
closedir(dir);
|
||||
return -1;
|
||||
}
|
||||
char *threads = get_val_from_file(fp, "Threads");
|
||||
strstripspace(threads);
|
||||
threadCount += atoi(threads);
|
||||
fclose(fp);
|
||||
free(threads);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
return threadCount;
|
||||
}
|
||||
|
||||
kdk_loadavg kdk_system_get_loadavg()
|
||||
{
|
||||
struct sysinfo info;
|
||||
kdk_loadavg loadAvg;
|
||||
int rc = sysinfo(&info);
|
||||
if (!rc) {
|
||||
loadAvg.loadavg_1m = info.loads[0];
|
||||
loadAvg.loadavg_5m = info.loads[1];
|
||||
loadAvg.loadavg_15m = info.loads[2];
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
return loadAvg;
|
||||
}
|
||||
|
|
|
@ -44,6 +44,13 @@ struct KPci
|
|||
struct KPci *next;
|
||||
};
|
||||
|
||||
typedef struct _kdk_loadavg{
|
||||
/* 1, 5, and 15 minute load averages */
|
||||
float loadavg_1m;
|
||||
float loadavg_5m;
|
||||
float loadavg_15m;
|
||||
}kdk_loadavg;
|
||||
|
||||
/**
|
||||
* @brief 获取操作系统架构信息
|
||||
*
|
||||
|
@ -279,17 +286,40 @@ extern void kdk_hw_free_pci_info(struct KPci *info);
|
|||
*/
|
||||
extern char *kdk_system_get_appScene();
|
||||
|
||||
/**
|
||||
* @brief 获取系统中文件描述符数
|
||||
*
|
||||
* @return unsigned int 返回cpu的文件描述符数;
|
||||
*/
|
||||
extern unsigned int kdk_system_get_file_descriptor();
|
||||
|
||||
/**
|
||||
* @brief 获取系统中进程数
|
||||
*
|
||||
* @return unsigned int 返回cpu的进程数;
|
||||
*/
|
||||
extern unsigned int kdk_system_get_process_nums();
|
||||
|
||||
/**
|
||||
* @brief 获取系统中线程数
|
||||
*
|
||||
* @return unsigned int 返回cpu的线程数;
|
||||
*/
|
||||
extern unsigned int kdk_system_get_thread_nums();
|
||||
|
||||
/**
|
||||
* @brief 获取CPU负载均衡
|
||||
*
|
||||
* @return kdk_loadavg 返回cpu的1,5,15分钟负载均衡;
|
||||
*/
|
||||
extern kdk_loadavg kdk_system_get_loadavg();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // KYSDK_SYSTEM_SYSINFO_H__
|
||||
|
||||
/**
|
||||
* \example kysdk-system/src/systeminfo/test/kysysinfo_test.c
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -135,6 +135,12 @@ int main()
|
|||
res = kdk_system_get_appScene();
|
||||
printf("应用场景:%s\n", res);
|
||||
free(res);
|
||||
|
||||
printf("系统中文件描述符数:%d\n", kdk_system_get_file_descriptor());
|
||||
printf("系统中进程数:%d\n", kdk_system_get_process_nums());
|
||||
printf("系统中线程数:%d\n", kdk_system_get_thread_nums());
|
||||
kdk_loadavg loadAvg = kdk_system_get_loadavg();
|
||||
printf("CPU 负载均衡 1m %0.2f, 5m %0.2f, 15m %0.2f\n", loadAvg.loadavg_1m, loadAvg.loadavg_5m, loadAvg.loadavg_15m);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue