From patchwork Wed Jun 7 16:15:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Bodireddy, Bhanuprakash" X-Patchwork-Id: 772503 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wjYpP2Ztsz9s78 for ; Thu, 8 Jun 2017 02:26:21 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 4B9C0BF4; Wed, 7 Jun 2017 16:24:05 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id EA2D8B93 for ; Wed, 7 Jun 2017 16:24:02 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 7448E18F for ; Wed, 7 Jun 2017 16:24:02 +0000 (UTC) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2017 09:24:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,311,1493708400"; d="scan'208";a="271374160" Received: from silpixa00393942.ir.intel.com (HELO silpixa00393942.ger.corp.intel.com) ([10.237.223.42]) by fmsmga004.fm.intel.com with ESMTP; 07 Jun 2017 09:24:01 -0700 From: Bhanuprakash Bodireddy To: dev@openvswitch.org Date: Wed, 7 Jun 2017 17:15:00 +0100 Message-Id: <1496852117-71097-5-git-send-email-bhanuprakash.bodireddy@intel.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <1496852117-71097-1-git-send-email-bhanuprakash.bodireddy@intel.com> References: <1496852117-71097-1-git-send-email-bhanuprakash.bodireddy@intel.com> X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [RFC PATCH 04/21] process: Retrieve process status. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Implement helper function to retrieve the process status. This will be used by keepalive monitoring thread to detect false alarms and to show PMD thread state in future commits. Signed-off-by: Bhanuprakash Bodireddy --- lib/process.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/process.h | 10 ++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/process.c b/lib/process.c index e9d0ba9..3735cf5 100644 --- a/lib/process.c +++ b/lib/process.c @@ -50,6 +50,20 @@ struct process { int status; }; +struct pstate2Num { + char *tidState; + int num; +}; + +const struct pstate2Num pstate_map[] = { + { "S", STOPPED_STATE }, + { "R", ACTIVE_STATE }, + { "t", TRACED_STATE }, + { "Z", DEFUNC_STATE }, + { "D", UNINTERRUPTIBLE_SLEEP_STATE }, + { "NULL", UNUSED_STATE }, +}; + /* Pipe used to signal child termination. */ static int fds[2]; @@ -390,6 +404,52 @@ process_run(void) #endif } +int +get_process_status(int tid, int *pstate) +{ +#ifdef __linux__ + static char process_name[20]; + FILE *stream; + char line[75]; + char Name[15], value[5], status[20]; + int i, ln; + + snprintf(process_name, sizeof(process_name), + "/proc/%d/status", tid); + stream = fopen(process_name, "r"); + if (stream == NULL) { + VLOG_WARN_ONCE("%s: open failed: %s", process_name, + ovs_strerror(errno)); + return errno; + } + + ln=0; + while (fgets(line, sizeof line, stream)) { + if (!ovs_scan(line, + "%6s %2s %14s\n", + Name, value, status)) { + VLOG_WARN_ONCE("%s: could not parse line %d: %s", + process_name, ln, line); + continue; + } + if (!strcmp(Name, "State:")) { + for (i=0; pstate_map[i].tidState != NULL; i++) { + if (strcmp(pstate_map[i].tidState, value) == 0) { + VLOG_WARN_ONCE("The state is %s, status is %d\n", + pstate_map[i].tidState, pstate_map[i].num); + *pstate = pstate_map[i].num; + break; + } + } + break; + } + ln++; + } + return 0; +#else + return ENOSYS; +#endif +} /* Causes the next call to poll_block() to wake up when process 'p' has * exited. */ diff --git a/lib/process.h b/lib/process.h index 3feac7e..8a5513e 100644 --- a/lib/process.h +++ b/lib/process.h @@ -20,6 +20,15 @@ #include #include +enum process_states { + UNUSED_STATE, + STOPPED_STATE, + ACTIVE_STATE, + TRACED_STATE, + DEFUNC_STATE, + UNINTERRUPTIBLE_SLEEP_STATE +}; + struct process; /* Starting and monitoring subprocesses. @@ -38,6 +47,7 @@ bool process_exited(struct process *); int process_status(const struct process *); void process_run(void); void process_wait(struct process *); +int get_process_status(int, int *); /* These functions are thread-safe. */ char *process_status_msg(int);