From d01d15a19173bf61127c1c0d5a7ae6945a991673 Mon Sep 17 00:00:00 2001 From: LHesperus <2639350497@qq.com> Date: Thu, 20 Jun 2024 10:19:10 +0800 Subject: [PATCH] add netif_rx counting pmu instance --- include/pmu_plugin.h | 1 + pmu/CMakeLists.txt | 1 + pmu/plugin/plugin.c | 16 ++++ pmu/plugin/plugin_comm.h | 1 + pmu/plugin/plugin_netif_rx.c | 170 +++++++++++++++++++++++++++++++++++ pmu/plugin/plugin_netif_rx.h | 35 ++++++++ 6 files changed, 224 insertions(+) create mode 100644 pmu/plugin/plugin_netif_rx.c create mode 100644 pmu/plugin/plugin_netif_rx.h diff --git a/include/pmu_plugin.h b/include/pmu_plugin.h index 20c7ef7..ca0777d 100644 --- a/include/pmu_plugin.h +++ b/include/pmu_plugin.h @@ -20,6 +20,7 @@ extern "C" { #define PMU_CYCLES_SAMPLING "pmu_cycles_sampling" #define PMU_UNCORE "pmu_uncore_counting" #define PMU_SPE "pmu_spe_sampling" +#define PMU_NETIF_RX "pmu_netif_rx_counting" #ifdef __cplusplus } diff --git a/pmu/CMakeLists.txt b/pmu/CMakeLists.txt index 9da7622..50c4cef 100644 --- a/pmu/CMakeLists.txt +++ b/pmu/CMakeLists.txt @@ -22,6 +22,7 @@ set(pmu_src plugin/plugin_spe.c plugin/plugin_uncore.c plugin/pmu_uncore.c + plugin/plugin_netif_rx.c plugin/plugin_comm.c plugin/plugin.c ) diff --git a/pmu/plugin/plugin.c b/pmu/plugin/plugin.c index d231c26..2c2e933 100644 --- a/pmu/plugin/plugin.c +++ b/pmu/plugin/plugin.c @@ -14,6 +14,7 @@ #include "plugin_counting.h" #include "plugin_uncore.h" #include "plugin_spe.h" +#include "plugin_netif_rx.h" #define INS_COLLECTOR_MAX 10 @@ -75,6 +76,20 @@ struct Interface spe_collector = { .run = spe_run, }; +struct Interface netif_rx_collector = { + .get_version = netif_rx_get_version, + .get_description = netif_rx_get_description, + .get_priority = netif_rx_get_priority, + .get_type = netif_rx_get_type, + .get_dep = netif_rx_get_dep, + .get_name = netif_rx_get_name, + .get_period = netif_rx_get_period, + .enable = netif_rx_enable, + .disable = netif_rx_disable, + .get_ring_buf = netif_rx_get_ring_buf, + .run = netif_rx_run, +}; + int get_instance(struct Interface **interface) { int ins_count = 0; @@ -83,6 +98,7 @@ int get_instance(struct Interface **interface) ins_collector[ins_count++] = counting_collector; ins_collector[ins_count++] = uncore_collector; ins_collector[ins_count++] = spe_collector; + ins_collector[ins_count++] = netif_rx_collector; *interface = &ins_collector[0]; return ins_count; diff --git a/pmu/plugin/plugin_comm.h b/pmu/plugin/plugin_comm.h index 20acb12..4ce0ef6 100644 --- a/pmu/plugin/plugin_comm.h +++ b/pmu/plugin/plugin_comm.h @@ -20,6 +20,7 @@ extern "C" { #define CYCLES_SAMPLING_BUF_SIZE 10 #define UNCORE_BUF_SIZE 10 #define SPE_BUF_SIZE 10 +#define NETIF_RX_BUF_SIZE 10 struct DataRingBuf; struct PmuData; diff --git a/pmu/plugin/plugin_netif_rx.c b/pmu/plugin/plugin_netif_rx.c new file mode 100644 index 0000000..717f091 --- /dev/null +++ b/pmu/plugin/plugin_netif_rx.c @@ -0,0 +1,170 @@ +/****************************************************************************** + * Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved. + * oeAware is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + ******************************************************************************/ +#include +#include +#include +#include "pmu.h" +#include "pcerrc.h" +#include "interface.h" +#include "pmu_plugin.h" +#include "plugin_comm.h" + +static bool event_is_open = false; +static int pmu_id = -1; +static struct DataRingBuf *ring_buf = NULL; +struct PmuData *pmu_data = NULL; + +static int init() +{ + ring_buf = init_buf(NETIF_RX_BUF_SIZE, PMU_NETIF_RX); + if (!ring_buf) { + return -1; + } + + return 0; +} + +static void finish() +{ + if (!ring_buf) { + return; + } + + free_buf(ring_buf); + ring_buf = NULL; +} + +static int open() +{ + struct PmuAttr attr; + char *evtList[1]; + int pd; + + evtList[0] = "net:netif_rx"; + + attr.evtList = evtList; + attr.numEvt = 1; + attr.pidList = NULL; + attr.numPid = 0; + attr.cpuList = NULL; + attr.numCpu = 0; + + pd = PmuOpen(COUNTING, &attr); + if (pd == -1) { + printf("%s\n", Perror()); + return pd; + } + + event_is_open = true; + return pd; +} + +static void netif_rx_close() +{ + PmuClose(pmu_id); + pmu_id = -1; + event_is_open = false; +} + +bool netif_rx_enable() +{ + if (!ring_buf) { + int ret = init(); + if (ret != 0) { + goto err; + } + } + + if (!event_is_open) { + pmu_id = open(); + if (pmu_id == -1) { + finish(); + goto err; + } + } + + return PmuEnable(pmu_id) == 0; + +err: + return false; +} + +void netif_rx_disable() +{ + PmuDisable(pmu_id); + netif_rx_close(); + finish(); +} + +const struct DataRingBuf *netif_rx_get_ring_buf() +{ + return (const struct DataRingBuf *)ring_buf; +} + +static void reflash_ring_buf() +{ + struct DataRingBuf *data_ringbuf; + int len; + + data_ringbuf = (struct DataRingBuf *)ring_buf; + if (!data_ringbuf) { + printf("ring_buf has not malloc\n"); + return; + } + + PmuDisable(pmu_id); + len = PmuRead(pmu_id, &pmu_data); + PmuEnable(pmu_id); + + fill_buf(data_ringbuf, pmu_data, len); +} + +void netif_rx_run(const struct Param *param) +{ + (void)param; + reflash_ring_buf(); +} + +const char *netif_rx_get_version() +{ + return NULL; +} + +const char *netif_rx_get_name() +{ + return PMU_NETIF_RX; +} + +const char *netif_rx_get_description() +{ + return NULL; +} + +const char *netif_rx_get_dep() +{ + return NULL; +} + +int netif_rx_get_priority() +{ + return 0; +} + +int netif_rx_get_type() +{ + return -1; +} + +int netif_rx_get_period() +{ + return 100; +} diff --git a/pmu/plugin/plugin_netif_rx.h b/pmu/plugin/plugin_netif_rx.h new file mode 100644 index 0000000..0c82936 --- /dev/null +++ b/pmu/plugin/plugin_netif_rx.h @@ -0,0 +1,35 @@ +/****************************************************************************** + * Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved. + * oeAware is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + ******************************************************************************/ +#ifndef __PLUGIN_NETIF_RX_H__ +#define __PLUGIN_NETIF_RX_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +const char *netif_rx_get_version(); +const char *netif_rx_get_name(); +const char *netif_rx_get_description(); +const char *netif_rx_get_dep(); +int netif_rx_get_priority(); +int netif_rx_get_type(); +int netif_rx_get_period(); +bool netif_rx_enable(); +void netif_rx_disable(); +const struct DataRingBuf *netif_rx_get_ring_buf(); +void netif_rx_run(const struct Param *param); + +#ifdef __cplusplus +} +#endif + +#endif -- 2.45.2.windows.1