From patchwork Fri Aug 4 08:07:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Bodireddy, Bhanuprakash" X-Patchwork-Id: 797677 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) 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 3xP0G83z1Jz9s72 for ; Fri, 4 Aug 2017 18:19:44 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 79227B62; Fri, 4 Aug 2017 08:17:40 +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 25CF6B4A for ; Fri, 4 Aug 2017 08:17:38 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id A27E314D for ; Fri, 4 Aug 2017 08:17:37 +0000 (UTC) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Aug 2017 01:17:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,320,1498546800"; d="scan'208";a="295690561" Received: from silpixa00393942.ir.intel.com (HELO silpixa00393942.ger.corp.intel.com) ([10.237.223.42]) by fmsmga004.fm.intel.com with ESMTP; 04 Aug 2017 01:17:36 -0700 From: Bhanuprakash Bodireddy To: dev@openvswitch.org Date: Fri, 4 Aug 2017 09:07:50 +0100 Message-Id: <1501834086-31829-4-git-send-email-bhanuprakash.bodireddy@intel.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <1501834086-31829-1-git-send-email-bhanuprakash.bodireddy@intel.com> References: <1501834086-31829-1-git-send-email-bhanuprakash.bodireddy@intel.com> X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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] [PATCH v3 03/19] process: Add helper function to 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 commit also enables the fields relating to process name, state and core the process was last scheduled. The APIs will be used by keepalive monitoring framework in future commits. Signed-off-by: Bhanuprakash Bodireddy --- lib/process.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------ lib/process.h | 13 +++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/lib/process.c b/lib/process.c index 0b8d195..cbf83db 100644 --- a/lib/process.c +++ b/lib/process.c @@ -64,7 +64,23 @@ struct raw_process_info { long long int uptime; /* ms since started. */ long long int cputime; /* ms of CPU used during 'uptime'. */ pid_t ppid; /* Parent. */ - char name[18]; /* Name (surrounded by parentheses). */ + char state; /* Process state. */ + int core_id; /* Core id last executed on. */ + char name[18]; /* Name. */ +}; + +struct pstate2Num { + char pidstate; + int num; +}; + +const struct pstate2Num pstate_map[] = { + { 'S', STOPPED_STATE }, + { 'R', ACTIVE_STATE }, + { 't', TRACED_STATE }, + { 'Z', DEFUNC_STATE }, + { 'D', UNINTERRUPTIBLE_SLEEP_STATE }, + { '\0', UNUSED_STATE }, }; /* Pipe used to signal child termination. */ @@ -421,8 +437,8 @@ get_raw_process_info(pid_t pid, struct raw_process_info *raw) n = fscanf(stream, "%*d " /* (1. pid) */ - "%17s " /* 2. process name */ - "%*c " /* (3. state) */ + "(%17[^)]) " /* 2. process name */ + "%c " /* 3. state */ "%lu " /* 4. ppid */ "%*d " /* (5. pgid) */ "%*d " /* (6. sid) */ @@ -458,7 +474,7 @@ get_raw_process_info(pid_t pid, struct raw_process_info *raw) "%*u " /* (36. always 0) */ "%*u " /* (37. always 0) */ "%*d " /* (38. exit_signal) */ - "%*d " /* (39. task_cpu) */ + "%d " /* 39. task_cpu */ #if 0 /* These are here for documentation but #if'd out to save * actually parsing them from the stream for no benefit. */ @@ -468,9 +484,10 @@ get_raw_process_info(pid_t pid, struct raw_process_info *raw) "%*lu " /* (43. gtime) */ "%*ld" /* (44. cgtime) */ #endif - , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss); + , raw->name, &raw->state, &ppid, &utime, &stime, &start_time, + &vsize, &rss, &raw->core_id); fclose(stream); - if (n != 7) { + if (n != 9) { VLOG_ERR_ONCE("%s: fscanf failed", file_name); return false; } @@ -496,12 +513,15 @@ get_process_info(pid_t pid, struct process_info *pinfo) return false; } + ovs_strlcpy(pinfo->name, child.name, sizeof pinfo->name); + pinfo->state = child.state; pinfo->vsz = child.vsz; pinfo->rss = child.rss; pinfo->booted = child.uptime; pinfo->crashes = 0; pinfo->uptime = child.uptime; pinfo->cputime = child.cputime; + pinfo->core_id = child.core_id; if (child.ppid) { struct raw_process_info parent; @@ -579,6 +599,30 @@ process_run(void) #endif } +bool +process_is_active(int pid) +{ + struct process_info pinfo; + int pstate = UNUSED_STATE; + + ovs_assert(LINUX); + + int success = get_process_info(pid, &pinfo); + if (success) { + for (int i = 0; pstate_map[i].pidstate != '\0'; i++) { + if (pstate_map[i].pidstate == pinfo.state) { + VLOG_DBG_ONCE("The state is %c", pstate_map[i].pidstate); + pstate = pstate_map[i].num; + break; + } + } + + if (pstate == ACTIVE_STATE) { + return true; + } + } + return false; +} /* 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 999ac68..69ab77f 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; struct process_info { @@ -29,6 +38,9 @@ struct process_info { int crashes; /* # of crashes (usually 0). */ long long int uptime; /* ms since last (re)started by monitor. */ long long int cputime; /* ms of CPU used during 'uptime'. */ + char state; + int core_id; + char name[18]; }; /* Starting and monitoring subprocesses. @@ -47,6 +59,7 @@ bool process_exited(struct process *); int process_status(const struct process *); void process_run(void); void process_wait(struct process *); +bool process_is_active(int); int count_crashes(pid_t); bool get_process_info(pid_t, struct process_info *);