From patchwork Mon Dec 12 14:52:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Holmes X-Patchwork-Id: 87699 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1682065qgi; Mon, 12 Dec 2016 06:55:30 -0800 (PST) X-Received: by 10.176.67.163 with SMTP id l32mr59124536ual.89.1481554530709; Mon, 12 Dec 2016 06:55:30 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id o4si10993426vkc.67.2016.12.12.06.55.30; Mon, 12 Dec 2016 06:55:30 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 0F9FC60CC3; Mon, 12 Dec 2016 14:55:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2 autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 3CEE460D7D; Mon, 12 Dec 2016 14:53:15 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 11E8060C7E; Mon, 12 Dec 2016 14:52:57 +0000 (UTC) Received: from mail-qk0-f171.google.com (mail-qk0-f171.google.com [209.85.220.171]) by lists.linaro.org (Postfix) with ESMTPS id 037B960C34 for ; Mon, 12 Dec 2016 14:52:38 +0000 (UTC) Received: by mail-qk0-f171.google.com with SMTP id q130so84908523qke.1 for ; Mon, 12 Dec 2016 06:52:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=LjgG+YOE+8TnrJ+bdeqiRGf5Vqaz8jcG6XfmYLPXWXc=; b=gcO+GnnXnYq3Bso4pCRP+GG5e+gHJvwr4AnyNxRAQVDEgk7ZI9TxoMh4CbkwoELo3q yf6QXRkhRjgqYRgFa6rMeJmWy/jjE250xj/AGCUSWxmJH/NmvHTGJl308nJN0dJUwTgq BGCK7fOaCAPLyrJ9TSXFGSk0jYFjzQxnoSgVrYkaydra05T/N+DzMLeXolw9MFBdwMen 9zqWMSJegww+m56xnld7z+7dDRr6fQb1RowbDPDKrGRL2re1XymYQm9gYxyjNBDZpt0f LtiwvbU7Yy5O73n4N6adRTtg8H4w0gAbAkykYLXEo5c8MceOzQUn7dsqykcr9Xs9X+AA JJNQ== X-Gm-Message-State: AKaTC00PKB4/6FZC8oZgl0Tr4q8V/Ry4KPhGUwMMjPK83Ialy1Npd4XJUr8czZYqyVP1RZbQckM= X-Received: by 10.55.175.198 with SMTP id y189mr77477247qke.293.1481554357243; Mon, 12 Dec 2016 06:52:37 -0800 (PST) Received: from localhost (c-98-221-136-245.hsd1.nj.comcast.net. [98.221.136.245]) by smtp.gmail.com with ESMTPSA id r15sm26571918qte.9.2016.12.12.06.52.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 12 Dec 2016 06:52:36 -0800 (PST) From: Mike Holmes To: lng-odp@lists.linaro.org Date: Mon, 12 Dec 2016 09:52:26 -0500 Message-Id: <20161212145230.11412-3-mike.holmes@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161212145230.11412-1-mike.holmes@linaro.org> References: <20161212145230.11412-1-mike.holmes@linaro.org> Subject: [lng-odp] [PATCH 2/6] helper: remove unused linux specific apis X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Signed-off-by: Mike Holmes --- helper/Makefile.am | 2 +- helper/include/odp/helper/threads.h | 83 +------------- helper/test/Makefile.am | 6 - helper/test/process.c | 92 --------------- helper/test/thread.c | 87 --------------- helper/{linux.c => thread.c} | 216 ------------------------------------ 6 files changed, 5 insertions(+), 481 deletions(-) delete mode 100644 helper/test/process.c delete mode 100644 helper/test/thread.c rename helper/{linux.c => thread.c} (70%) -- 2.9.3 diff --git a/helper/Makefile.am b/helper/Makefile.am index 2153d70..942dd60 100644 --- a/helper/Makefile.am +++ b/helper/Makefile.am @@ -35,7 +35,7 @@ __LIB__libodphelper_linux_la_SOURCES = \ eth.c \ ip.c \ chksum.c \ - linux.c \ + thread.c \ hashtable.c \ lineartable.c diff --git a/helper/include/odp/helper/threads.h b/helper/include/odp/helper/threads.h index 238bcf8..7c4b68a 100644 --- a/helper/include/odp/helper/threads.h +++ b/helper/include/odp/helper/threads.h @@ -8,15 +8,15 @@ /** * @file * - * ODP Linux helper API + * ODP thread helper API * * This file is an optional helper to odp.h APIs. These functions are provided * to ease common setups in a Linux system. User is free to implement the same * setups in otherways (not via this API). */ -#ifndef ODPH_LINUX_H_ -#define ODPH_LINUX_H_ +#ifndef ODPH_THREAD_H_ +#define ODPH_THREAD_H_ #ifdef __cplusplus extern "C" { @@ -28,7 +28,7 @@ extern "C" { #include #include -/** @addtogroup odph_linux ODPH LINUX +/** @addtogroup odph_linux ODPH THREAD * @{ */ @@ -94,81 +94,6 @@ typedef struct { }; } odph_odpthread_t; -/** - * Creates and launches pthreads - * - * Creates, pins and launches threads to separate CPU's based on the cpumask. - * - * @param[out] pthread_tbl Table of pthread state information records. Table - * must have at least as many entries as there are - * CPUs in the CPU mask. - * @param mask CPU mask - * @param thr_params Linux helper thread parameters - * - * @return Number of threads created - */ -int odph_linux_pthread_create(odph_linux_pthread_t *pthread_tbl, - const odp_cpumask_t *mask, - const odph_linux_thr_params_t *thr_params); - -/** - * Waits pthreads to exit - * - * Returns when all threads have been exit. - * - * @param thread_tbl Thread table - * @param num Number of threads to create - * - */ -void odph_linux_pthread_join(odph_linux_pthread_t *thread_tbl, int num); - -/** - * Fork a process - * - * Forks and sets CPU affinity for the child process. Ignores 'start' and 'arg' - * thread parameters. - * - * @param[out] proc Pointer to process state info (for output) - * @param cpu Destination CPU for the child process - * @param thr_params Linux helper thread parameters - * - * @return On success: 1 for the parent, 0 for the child - * On failure: -1 for the parent, -2 for the child - */ -int odph_linux_process_fork(odph_linux_process_t *proc, int cpu, - const odph_linux_thr_params_t *thr_params); - - -/** - * Fork a number of processes - * - * Forks and sets CPU affinity for child processes. Ignores 'start' and 'arg' - * thread parameters. - * - * @param[out] proc_tbl Process state info table (for output) - * @param mask CPU mask of processes to create - * @param thr_params Linux helper thread parameters - * - * @return On success: 1 for the parent, 0 for the child - * On failure: -1 for the parent, -2 for the child - */ -int odph_linux_process_fork_n(odph_linux_process_t *proc_tbl, - const odp_cpumask_t *mask, - const odph_linux_thr_params_t *thr_params); - - -/** - * Wait for a number of processes - * - * Waits for a number of child processes to terminate. Records process state - * change status into the process state info structure. - * - * @param proc_tbl Process state info table (previously filled by fork) - * @param num Number of processes to wait - * - * @return 0 on success, -1 on failure - */ -int odph_linux_process_wait_n(odph_linux_process_t *proc_tbl, int num); /** * Creates and launches odpthreads (as linux threads or processes) diff --git a/helper/test/Makefile.am b/helper/test/Makefile.am index 545db73..361e45d 100644 --- a/helper/test/Makefile.am +++ b/helper/test/Makefile.am @@ -6,9 +6,7 @@ AM_LDFLAGS += -static TESTS_ENVIRONMENT += TEST_DIR=${builddir} EXECUTABLES = chksum$(EXEEXT) \ - thread$(EXEEXT) \ parse$(EXEEXT)\ - process$(EXEEXT)\ table$(EXEEXT) COMPILE_ONLY = odpthreads @@ -29,9 +27,5 @@ EXTRA_DIST = odpthreads_as_processes odpthreads_as_pthreads dist_chksum_SOURCES = chksum.c dist_odpthreads_SOURCES = odpthreads.c odpthreads_LDADD = $(LIB)/libodphelper-linux.la $(LIB)/libodp-linux.la -dist_thread_SOURCES = thread.c -thread_LDADD = $(LIB)/libodphelper-linux.la $(LIB)/libodp-linux.la -dist_process_SOURCES = process.c dist_parse_SOURCES = parse.c -process_LDADD = $(LIB)/libodphelper-linux.la $(LIB)/libodp-linux.la dist_table_SOURCES = table.c diff --git a/helper/test/process.c b/helper/test/process.c deleted file mode 100644 index 0e04224..0000000 --- a/helper/test/process.c +++ /dev/null @@ -1,92 +0,0 @@ -/* Copyright (c) 2015, Linaro Limited - * All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include - -#define NUMBER_WORKERS 16 /* 0 = max */ - -static void *worker_fn(void *arg TEST_UNUSED) -{ - /* depend on the odp helper to call odp_init_local */ - printf("Worker thread on CPU %d\n", odp_cpu_id()); - - return 0; -} - -/* Create additional dataplane processes */ -int main(int argc TEST_UNUSED, char *argv[] TEST_UNUSED) -{ - odp_cpumask_t cpu_mask; - int num_workers; - int cpu; - char cpumaskstr[ODP_CPUMASK_STR_SIZE]; - int ret; - odph_linux_process_t proc[NUMBER_WORKERS]; - odp_instance_t instance; - odph_linux_thr_params_t thr_params; - - if (odp_init_global(&instance, NULL, NULL)) { - LOG_ERR("Error: ODP global init failed.\n"); - exit(EXIT_FAILURE); - } - - if (odp_init_local(instance, ODP_THREAD_CONTROL)) { - LOG_ERR("Error: ODP local init failed.\n"); - exit(EXIT_FAILURE); - } - - /* discover how many processes this system can support */ - num_workers = odp_cpumask_default_worker(&cpu_mask, NUMBER_WORKERS); - if (num_workers < NUMBER_WORKERS) { - printf("System can only support %d processes and not the %d requested\n", - num_workers, NUMBER_WORKERS); - } - - /* generate a summary for the user */ - (void)odp_cpumask_to_str(&cpu_mask, cpumaskstr, sizeof(cpumaskstr)); - printf("default cpu mask: %s\n", cpumaskstr); - printf("default num worker processes: %i\n", num_workers); - - cpu = odp_cpumask_first(&cpu_mask); - printf("the first CPU: %i\n", cpu); - - /* reserve cpu 0 for the control plane so remove it from - * the default mask */ - odp_cpumask_clr(&cpu_mask, 0); - num_workers = odp_cpumask_count(&cpu_mask); - (void)odp_cpumask_to_str(&cpu_mask, cpumaskstr, sizeof(cpumaskstr)); - printf("new cpu mask: %s\n", cpumaskstr); - printf("new num worker processes: %i\n\n", num_workers); - - memset(&thr_params, 0, sizeof(thr_params)); - thr_params.thr_type = ODP_THREAD_WORKER; - thr_params.instance = instance; - - /* Fork worker processes */ - ret = odph_linux_process_fork_n(proc, &cpu_mask, &thr_params); - - if (ret < 0) { - LOG_ERR("Fork workers failed %i\n", ret); - return -1; - } - - if (ret == 0) { - /* Child process */ - worker_fn(NULL); - } else { - /* Parent process */ - odph_linux_process_wait_n(proc, num_workers); - - if (odp_term_global(instance)) { - LOG_ERR("Error: ODP global term failed.\n"); - exit(EXIT_FAILURE); - } - } - - return 0; -} diff --git a/helper/test/thread.c b/helper/test/thread.c deleted file mode 100644 index 8268d9f..0000000 --- a/helper/test/thread.c +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (c) 2015, Linaro Limited - * All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include - -#define NUMBER_WORKERS 16 -static void *worker_fn(void *arg TEST_UNUSED) -{ - /* depend on the odp helper to call odp_init_local */ - - printf("Worker thread on CPU %d\n", odp_cpu_id()); - - /* depend on the odp helper to call odp_term_local */ - - return NULL; -} - -/* Create additional dataplane threads */ -int main(int argc TEST_UNUSED, char *argv[] TEST_UNUSED) -{ - odph_linux_pthread_t thread_tbl[NUMBER_WORKERS]; - odp_cpumask_t cpu_mask; - int num_workers; - int cpu; - char cpumaskstr[ODP_CPUMASK_STR_SIZE]; - odp_instance_t instance; - odph_linux_thr_params_t thr_params; - - if (odp_init_global(&instance, NULL, NULL)) { - LOG_ERR("Error: ODP global init failed.\n"); - exit(EXIT_FAILURE); - } - - if (odp_init_local(instance, ODP_THREAD_CONTROL)) { - LOG_ERR("Error: ODP local init failed.\n"); - exit(EXIT_FAILURE); - } - - /* discover how many threads this system can support */ - num_workers = odp_cpumask_default_worker(&cpu_mask, NUMBER_WORKERS); - if (num_workers < NUMBER_WORKERS) { - printf("System can only support %d threads and not the %d requested\n", - num_workers, NUMBER_WORKERS); - } - - /* generate a summary for the user */ - (void)odp_cpumask_to_str(&cpu_mask, cpumaskstr, sizeof(cpumaskstr)); - printf("default cpu mask: %s\n", cpumaskstr); - printf("default num worker threads: %i\n", num_workers); - - cpu = odp_cpumask_first(&cpu_mask); - printf("the first CPU: %i\n", cpu); - - /* reserve cpu 0 for the control plane so remove it from - * the default mask */ - odp_cpumask_clr(&cpu_mask, 0); - num_workers = odp_cpumask_count(&cpu_mask); - (void)odp_cpumask_to_str(&cpu_mask, cpumaskstr, sizeof(cpumaskstr)); - printf("new cpu mask: %s\n", cpumaskstr); - printf("new num worker threads: %i\n\n", num_workers); - - memset(&thr_params, 0, sizeof(thr_params)); - thr_params.start = worker_fn; - thr_params.arg = NULL; - thr_params.thr_type = ODP_THREAD_WORKER; - thr_params.instance = instance; - - odph_linux_pthread_create(&thread_tbl[0], &cpu_mask, &thr_params); - odph_linux_pthread_join(thread_tbl, num_workers); - - if (odp_term_local()) { - LOG_ERR("Error: ODP local term failed.\n"); - exit(EXIT_FAILURE); - } - - if (odp_term_global(instance)) { - LOG_ERR("Error: ODP global term failed.\n"); - exit(EXIT_FAILURE); - } - - return 0; -} diff --git a/helper/linux.c b/helper/thread.c similarity index 70% rename from helper/linux.c rename to helper/thread.c index d5215c2..f946f5a 100644 --- a/helper/linux.c +++ b/helper/thread.c @@ -27,222 +27,6 @@ static struct { int proc; /* true when process mode is required, false otherwise */ } helper_options; -static void *odp_run_start_routine(void *arg) -{ - odph_linux_thr_params_t *thr_params = arg; - - /* ODP thread local init */ - if (odp_init_local(thr_params->instance, thr_params->thr_type)) { - ODPH_ERR("Local init failed\n"); - return NULL; - } - - void *ret_ptr = thr_params->start(thr_params->arg); - int ret = odp_term_local(); - - if (ret < 0) - ODPH_ERR("Local term failed\n"); - else if (ret == 0 && odp_term_global(thr_params->instance)) - ODPH_ERR("Global term failed\n"); - - return ret_ptr; -} - -int odph_linux_pthread_create(odph_linux_pthread_t *pthread_tbl, - const odp_cpumask_t *mask, - const odph_linux_thr_params_t *thr_params) -{ - int i; - int num; - int cpu_count; - int cpu; - int ret; - - num = odp_cpumask_count(mask); - - memset(pthread_tbl, 0, num * sizeof(odph_linux_pthread_t)); - - cpu_count = odp_cpu_count(); - - if (num < 1 || num > cpu_count) { - ODPH_ERR("Invalid number of threads:%d (%d cores available)\n", - num, cpu_count); - return 0; - } - - cpu = odp_cpumask_first(mask); - for (i = 0; i < num; i++) { - cpu_set_t cpu_set; - - CPU_ZERO(&cpu_set); - CPU_SET(cpu, &cpu_set); - - pthread_attr_init(&pthread_tbl[i].attr); - - pthread_tbl[i].cpu = cpu; - - pthread_attr_setaffinity_np(&pthread_tbl[i].attr, - sizeof(cpu_set_t), &cpu_set); - - pthread_tbl[i].thr_params.start = thr_params->start; - pthread_tbl[i].thr_params.arg = thr_params->arg; - pthread_tbl[i].thr_params.thr_type = thr_params->thr_type; - pthread_tbl[i].thr_params.instance = thr_params->instance; - - ret = pthread_create(&pthread_tbl[i].thread, - &pthread_tbl[i].attr, - odp_run_start_routine, - &pthread_tbl[i].thr_params); - if (ret != 0) { - ODPH_ERR("Failed to start thread on cpu #%d\n", cpu); - break; - } - - cpu = odp_cpumask_next(mask, cpu); - } - - return i; -} - -void odph_linux_pthread_join(odph_linux_pthread_t *thread_tbl, int num) -{ - int i; - int ret; - - for (i = 0; i < num; i++) { - /* Wait thread to exit */ - ret = pthread_join(thread_tbl[i].thread, NULL); - if (ret != 0) { - ODPH_ERR("Failed to join thread from cpu #%d\n", - thread_tbl[i].cpu); - } - pthread_attr_destroy(&thread_tbl[i].attr); - } -} - -int odph_linux_process_fork_n(odph_linux_process_t *proc_tbl, - const odp_cpumask_t *mask, - const odph_linux_thr_params_t *thr_params) -{ - pid_t pid; - int num; - int cpu_count; - int cpu; - int i; - - num = odp_cpumask_count(mask); - - memset(proc_tbl, 0, num * sizeof(odph_linux_process_t)); - - cpu_count = odp_cpu_count(); - - if (num < 1 || num > cpu_count) { - ODPH_ERR("Bad num\n"); - return -1; - } - - cpu = odp_cpumask_first(mask); - for (i = 0; i < num; i++) { - cpu_set_t cpu_set; - - CPU_ZERO(&cpu_set); - CPU_SET(cpu, &cpu_set); - - pid = fork(); - - if (pid < 0) { - ODPH_ERR("fork() failed\n"); - return -1; - } - - /* Parent continues to fork */ - if (pid > 0) { - proc_tbl[i].pid = pid; - proc_tbl[i].cpu = cpu; - - cpu = odp_cpumask_next(mask, cpu); - continue; - } - - /* Child process */ - - /* Request SIGTERM if parent dies */ - prctl(PR_SET_PDEATHSIG, SIGTERM); - /* Parent died already? */ - if (getppid() == 1) - kill(getpid(), SIGTERM); - - if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set)) { - ODPH_ERR("sched_setaffinity() failed\n"); - return -2; - } - - if (odp_init_local(thr_params->instance, - thr_params->thr_type)) { - ODPH_ERR("Local init failed\n"); - return -2; - } - - return 0; - } - - return 1; -} - -int odph_linux_process_fork(odph_linux_process_t *proc, int cpu, - const odph_linux_thr_params_t *thr_params) -{ - odp_cpumask_t mask; - - odp_cpumask_zero(&mask); - odp_cpumask_set(&mask, cpu); - return odph_linux_process_fork_n(proc, &mask, thr_params); -} - -int odph_linux_process_wait_n(odph_linux_process_t *proc_tbl, int num) -{ - pid_t pid; - int i, j; - int status = 0; - - for (i = 0; i < num; i++) { - pid = wait(&status); - - if (pid < 0) { - ODPH_ERR("wait() failed\n"); - return -1; - } - - for (j = 0; j < num; j++) { - if (proc_tbl[j].pid == pid) { - proc_tbl[j].status = status; - break; - } - } - - if (j == num) { - ODPH_ERR("Bad pid:%d\n", (int)pid); - return -1; - } - - /* Examine the child process' termination status */ - if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { - ODPH_ERR("Child exit status:%d (pid:%d)\n", - WEXITSTATUS(status), (int)pid); - return -1; - } - if (WIFSIGNALED(status)) { - int signo = WTERMSIG(status); - - ODPH_ERR("Child term signo:%d - %s (pid:%d)\n", - signo, strsignal(signo), (int)pid); - return -1; - } - } - - return 0; -} - /* * wrapper for odpthreads, either implemented as linux threads or processes. * (in process mode, if start_routine returns NULL, the process return FAILURE).