Soft Patch Panel
 help / color / Atom feed
* [spp] [PATCH] spp-ctl: add API for getting CPU usage
@ 2019-06-24 10:45 yasufum.o
  0 siblings, 0 replies; only message in thread
From: yasufum.o @ 2019-06-24 10:45 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

From: Yasufumi Ogawa <yasufum.o@gmail.com>

API for getting CPU usage is intended to be used for suggesting unused
lcores for `pri; launch` command.

This update is also revise method of primary for getting status to be
the same as other secondary processes.

Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
 src/spp-ctl/spp_ctl.py    | 54 ++++++++++++++++++++++++++++++++++++++-
 src/spp-ctl/spp_proc.py   |  6 +----
 src/spp-ctl/spp_webapi.py | 18 ++++++++-----
 3 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/src/spp-ctl/spp_ctl.py b/src/spp-ctl/spp_ctl.py
index 93b87dd..ea19aa6 100644
--- a/src/spp-ctl/spp_ctl.py
+++ b/src/spp-ctl/spp_ctl.py
@@ -18,12 +18,12 @@ import spp_webapi
 
 LOG = logging.getLogger(__name__)
 
-
 MSG_SIZE = 4096
 
 # relative path of `cpu_layout.py`
 CPU_LAYOUT_TOOL = 'tools/helpers/cpu_layout.py'
 
+
 class Controller(object):
 
     def __init__(self, host, pri_port, sec_port, api_port):
@@ -143,6 +143,7 @@ class Controller(object):
 
     def _update_procs(self):
         """Remove no existing processes from `self.procs`."""
+
         removed_ids = []
         for idx, proc in self.procs.items():
             if proc.id != spp_proc.ID_PRIMARY:
@@ -168,6 +169,57 @@ class Controller(object):
             procs.append(p)
         return procs
 
+    def get_cpu_usage(self):
+        """Get cpu usage from each of status of SPP processes.
+
+        If process returns invalid message or cannot connect, remove
+        it from `self.procs` as in _update_procs().
+        """
+
+        removed_ids = []
+        cpus = []
+        for idx, proc in self.procs.items():
+            try:
+                # Check the process can be accessed. If not, go
+                # to except block.
+                stat = proc.get_status()
+                if proc.id == spp_proc.ID_PRIMARY:
+                    cpus.append(
+                            {'proc-type': proc.type,
+                                'master-lcore': stat['lcores'][0],
+                                'lcores': stat['lcores']})
+                elif proc.type == 'nfv':
+                    cpus.append(
+                            {'proc-type': proc.type,
+                                'client-id': proc.id,
+                                'master-lcore': stat['master-lcore'],
+                                'lcores': stat['lcores']})
+                elif proc.type in ['vf', 'mirror', 'pcap']:
+                    master_lcore = stat['info']['master-lcore']
+                    lcores = [stat['info']['master-lcore']]
+                    # TODO(yasufum) revise tag name 'core'.
+                    for val in stat['info']['core']:
+                        lcores.append(val['core'])
+                    cpus.append(
+                            {'proc-type': proc.type,
+                                'client-id': proc.id,
+                                'master-lcore': master_lcore,
+                                'lcores': lcores})
+                else:
+                    LOG.debug('No supported proc type: {}'.format(
+                        roc.type))
+
+            except Exception as e:
+                LOG.error("get_cpu_usage: {}".format(e))
+                removed_ids.append(idx)
+
+        for idx in removed_ids:
+            LOG.info("Remove no existing {}:{}.".format(
+                self.procs[idx].type, self.procs[idx].id))
+            del self.procs[idx]
+
+        return cpus
+
     def get_cpu_layout(self):
         """Get cpu layout with helper tool 'cpu_layout.py'."""
 
diff --git a/src/spp-ctl/spp_proc.py b/src/spp-ctl/spp_proc.py
index cdd7b49..fe7ecea 100644
--- a/src/spp-ctl/spp_proc.py
+++ b/src/spp-ctl/spp_proc.py
@@ -284,12 +284,8 @@ class PrimaryProc(SppProc):
     def __init__(self, conn):
         super(PrimaryProc, self).__init__(TYPE_PRIMARY, ID_PRIMARY, conn)
 
-    @staticmethod
-    def _decode_reply(data):
-        return data.strip('\0')
-
     @exec_command
-    def status(self):
+    def get_status(self):
         return "status"
 
     @exec_command
diff --git a/src/spp-ctl/spp_webapi.py b/src/spp-ctl/spp_webapi.py
index 3ad34d0..7f2baad 100644
--- a/src/spp-ctl/spp_webapi.py
+++ b/src/spp-ctl/spp_webapi.py
@@ -53,7 +53,7 @@ class BaseHandler(bottle.Bottle):
             if if_type not in ["phy", "vhost", "ring", "pcap", "nullpmd"]:
                 raise
             int(if_num)
-        except:
+        except Exception:
             raise KeyInvalid('port', port)
 
     def log_url(self):
@@ -152,16 +152,22 @@ class V1Handler(BaseHandler):
 
     def set_route(self):
         self.route('/processes', 'GET', callback=self.get_processes)
-        self.route('/cpus', 'GET', callback=self.get_cpu_layout)
+        self.route('/cpu_usage', 'GET', callback=self.get_cpu_usage)
+        self.route('/cpu_layout', 'GET', callback=self.get_cpu_layout)
 
     def get_processes(self):
         LOG.info("get processes called.")
         return self.ctrl.get_processes()
 
+    def get_cpu_usage(self):
+        LOG.info("get cpu usage called.")
+        return self.ctrl.get_cpu_usage()
+
     def get_cpu_layout(self):
         LOG.info("get cpu layout called.")
         return self.ctrl.get_cpu_layout()
 
+
 class V1VFCommon(object):
     """Define common methods for vf and mirror handler."""
 
@@ -251,7 +257,7 @@ class V1VFHandler(BaseHandler, V1VFCommon):
                     if vlan['operation'] == "add":
                         int(vlan['id'])
                         int(vlan['pcp'])
-                except:
+                except Exception:
                     raise KeyInvalid('vlan', vlan)
 
     def vf_comp_port(self, proc, name, body):
@@ -277,7 +283,7 @@ class V1VFHandler(BaseHandler, V1VFCommon):
     def _validate_mac(self, mac_address):
         try:
             netaddr.EUI(mac_address)
-        except:
+        except Exception:
             raise KeyInvalid('mac_address', mac_address)
 
     def _validate_vf_classifier(self, body):
@@ -296,7 +302,7 @@ class V1VFHandler(BaseHandler, V1VFCommon):
         if body['type'] == "vlan":
             try:
                 int(body['vlan'])
-            except:
+            except Exception:
                 raise KeyInvalid('vlan', body.get('vlan'))
 
     def vf_classifier(self, proc, body):
@@ -466,7 +472,7 @@ class V1PrimaryHandler(BaseHandler):
 
     def get_status(self):
         proc = self._get_proc()
-        return self.convert_status(proc.status())
+        return proc.get_status()
 
     def clear_status(self):
         proc = self._get_proc()
-- 
2.17.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-24 10:45 [spp] [PATCH] spp-ctl: add API for getting CPU usage yasufum.o

Soft Patch Panel

Archives are clonable:
	git clone --mirror http://inbox.dpdk.org/spp/0 spp/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 spp spp/ http://inbox.dpdk.org/spp \
		spp@dpdk.org
	public-inbox-index spp


Newsgroup available over NNTP:
	nntp://inbox.dpdk.org/inbox.dpdk.spp


AGPL code for this site: git clone https://public-inbox.org/ public-inbox