From patchwork Mon Apr 24 13:02:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 126471 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 425F4429DB; Mon, 24 Apr 2023 15:09:19 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EFBB042B8E; Mon, 24 Apr 2023 15:09:15 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id A7671410D0 for ; Mon, 24 Apr 2023 15:09:13 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Q4ljY29LxzncKN; Mon, 24 Apr 2023 21:05:21 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Mon, 24 Apr 2023 21:09:11 +0800 From: Chengwen Feng To: , CC: Subject: [RFC 2/3] examples/coroutine: support coroutine examples Date: Mon, 24 Apr 2023 13:02:07 +0000 Message-ID: <20230424130208.9517-3-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230424130208.9517-1-fengchengwen@huawei.com> References: <20230424130208.9517-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.50.163.32] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch adds coroutine example, usage: 1. start examples: dpdk-coroutine -a 0000:7d:00.2 -l 10-11 2. will output: Start yield coroutine test! I am in yield coroutine 111! I am in yield coroutine 222! I am in yield coroutine 333! I am in yield coroutine 111! I am in yield coroutine 222! I am in yield coroutine 333! ... Start delay coroutine test! I am in delay coroutine 111! I am in delay coroutine 222! I am in delay coroutine 222! I am in delay coroutine 111! I am in delay coroutine 222! I am in delay coroutine 222! ... 3. use ctrl+c to exit example. Signed-off-by: Chengwen Feng --- examples/coroutine/main.c | 153 +++++++++++++++++++++++++++++++++ examples/coroutine/meson.build | 10 +++ examples/meson.build | 1 + 3 files changed, 164 insertions(+) create mode 100644 examples/coroutine/main.c create mode 100644 examples/coroutine/meson.build diff --git a/examples/coroutine/main.c b/examples/coroutine/main.c new file mode 100644 index 0000000000..2704ad1dc9 --- /dev/null +++ b/examples/coroutine/main.c @@ -0,0 +1,153 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 HiSilicon Limited + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static volatile bool force_quit; + +static struct rte_schedule *target_s; + +static void +yield_coroutine_1(void *arg) +{ + RTE_SET_USED(arg); + int i = 10; + while (i--) { + printf("\tI am in yield coroutine 111!\n"); + rte_co_yield(); + } +} + +static void +yield_coroutine_2(void *arg) +{ + RTE_SET_USED(arg); + int i = 10; + while (i--) { + printf("\tI am in yield coroutine 222!\n"); + rte_co_yield(); + } +} + +static void +yield_coroutine_3(void *arg) +{ + RTE_SET_USED(arg); + int i = 10; + while (i--) { + printf("\tI am in yield coroutine 333!\n"); + rte_co_yield(); + } +} + +static void +yield_coroutine_test(void) +{ + printf("Start yield coroutine test!\n"); + rte_co_create(target_s, yield_coroutine_1, NULL, 0); + rte_co_create(target_s, yield_coroutine_2, NULL, 0); + rte_co_create(target_s, yield_coroutine_3, NULL, 0); + sleep(1); +} + +static void +delay_coroutine_1(void *arg) +{ + RTE_SET_USED(arg); + int i = 10; + while (i--) { + printf("\tI am in delay coroutine 111!\n"); + rte_co_delay(100 * 1000); + } +} + +static void +delay_coroutine_2(void *arg) +{ + RTE_SET_USED(arg); + int i = 20; + while (i--) { + printf("\tI am in delay coroutine 222!\n"); + rte_co_delay(50 * 1000); + } +} + +static void +delay_coroutine_test(void) +{ + printf("Start delay coroutine test!\n"); + rte_co_create(target_s, delay_coroutine_1, NULL, 0); + rte_co_create(target_s, delay_coroutine_2, NULL, 0); + sleep(1); +} + +static int +co_main_loop(void *arg) +{ + RTE_SET_USED(arg); + while (!force_quit) + rte_schedule_run(target_s); + return 0; +} + +static void +signal_handler(int signum) +{ + if (signum == SIGINT || signum == SIGTERM) { + printf("\n\nSignal %d received, preparing to exit...\n", + signum); + force_quit = true; + } +} + +int +main(int argc, char **argv) +{ + uint32_t lcore_id = rte_lcore_id(); + int ret; + + /* Init EAL. 8< */ + ret = rte_eal_init(argc, argv); + if (ret < 0) + rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); + + force_quit = false; + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + + /* Check if there is enough lcores for all ports. */ + if (rte_lcore_count() < 2) + rte_exit(EXIT_FAILURE, + "There should be at least one worker lcore.\n"); + + target_s = rte_schedule_create("co-sched-test", 128); + if (target_s == NULL) + rte_exit(EXIT_FAILURE, + "Create target scheduler failed!\n"); + + lcore_id = rte_get_next_lcore(lcore_id, true, true); + rte_eal_remote_launch(co_main_loop, NULL, lcore_id); + + yield_coroutine_test(); + delay_coroutine_test(); + + /* force_quit is true when we get here */ + rte_eal_mp_wait_lcore(); + + /* clean up the EAL */ + rte_eal_cleanup(); + + printf("Bye...\n"); + return 0; +} diff --git a/examples/coroutine/meson.build b/examples/coroutine/meson.build new file mode 100644 index 0000000000..c3576fe2f3 --- /dev/null +++ b/examples/coroutine/meson.build @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2023 HiSilicon Limited + +allow_experimental_apis = true + +deps += ['coroutine'] + +sources = files( + 'main.c', +) diff --git a/examples/meson.build b/examples/meson.build index 6968c09252..111d628065 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -11,6 +11,7 @@ all_examples = [ 'bbdev_app', 'bond', 'cmdline', + 'coroutine', 'distributor', 'dma', 'ethtool',