From 9da2bc06961587ec0d288686099a3e75524f8cc3 Mon Sep 17 00:00:00 2001 From: meijian Date: Wed, 9 Oct 2024 17:34:00 +0800 Subject: [PATCH] apps/net: add get iobinfo api Obtain detailed IOB information by parsing the /proc/iobinfo file. Signed-off-by: meijian --- include/netutils/netlib.h | 5 ++ netutils/netlib/CMakeLists.txt | 4 ++ netutils/netlib/Makefile | 4 ++ netutils/netlib/netlib_getiobinfo.c | 108 ++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 netutils/netlib/netlib_getiobinfo.c diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h index 07a66906c..b5389a55d 100644 --- a/include/netutils/netlib.h +++ b/include/netutils/netlib.h @@ -52,6 +52,7 @@ #include #include +#include #include #include @@ -507,6 +508,10 @@ int netlib_getifstatistics(FAR const char *ifname, int netlib_check_ifconflict(FAR const char *ifname); #endif +#ifdef CONFIG_MM_IOB +int netlib_get_iobinfo(FAR struct iob_stats_s *iob); +#endif + #undef EXTERN #ifdef __cplusplus } diff --git a/netutils/netlib/CMakeLists.txt b/netutils/netlib/CMakeLists.txt index 560a71834..82cc8d610 100644 --- a/netutils/netlib/CMakeLists.txt +++ b/netutils/netlib/CMakeLists.txt @@ -149,6 +149,10 @@ if(CONFIG_NETUTILS_NETLIB) list(APPEND SRCS netlib_checkifconflict.c) endif() + if(CONFIG_MM_IOB) + list(APPEND SRCS netlib_getiobinfo.c) + endif() + target_sources(apps PRIVATE ${SRCS}) endif() diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile index 237cc76de..3d6066e3f 100644 --- a/netutils/netlib/Makefile +++ b/netutils/netlib/Makefile @@ -148,4 +148,8 @@ ifeq ($(CONFIG_NET_ARP_ACD),y) CSRCS += netlib_checkifconflict.c endif +ifeq ($(CONFIG_MM_IOB),y) +CSRCS += netlib_getiobinfo.c +endif + include $(APPDIR)/Application.mk diff --git a/netutils/netlib/netlib_getiobinfo.c b/netutils/netlib/netlib_getiobinfo.c new file mode 100644 index 000000000..fe9a97983 --- /dev/null +++ b/netutils/netlib/netlib_getiobinfo.c @@ -0,0 +1,108 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_getiobinfo.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "netutils/netlib.h" + +#ifdef CONFIG_MM_IOB + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Determines the size of an intermediate buffer that must be large enough + * to handle the longest line generated by this logic. + */ + +#define PROCFS_LINELEN 80 +#define PROCFS_IOB_PATH "/proc/iobinfo" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_get_iobinfo + * + * Description: + * Get the network iob info. + * + * Parameters: + * iob The pointer to return iobinfo. + * + * Return: + * 0 on success; a nagtive on failure. + * + ****************************************************************************/ + +int netlib_get_iobinfo(FAR struct iob_stats_s *iob) +{ + int ret = OK; + FAR FILE *stream; + char line[PROCFS_LINELEN]; + + stream = fopen(PROCFS_IOB_PATH, "r"); + if (stream == NULL) + { + fprintf(stderr, "ERROR: Failed to open path:%s \n", PROCFS_IOB_PATH); + return -errno; + } + + /* Read ioninfo header and next is the data */ + + if (fgets(line, sizeof(line), stream) != NULL) + { + if (fgets(line, sizeof(line), stream) == NULL) + { + ret = -EINVAL; + fclose(stream); + return ret; + } + + if (sscanf(line, "%d %d %d %d", &iob->ntotal, &iob->nfree, + &iob->nwait, &iob->nthrottle) <= 0) + { + ret = -errno; + fclose(stream); + return ret; + } + } + + fclose(stream); + return ret; +} + +#endif /* CONFIG_MM_IOB */ \ No newline at end of file