aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/telemetry
diff options
context:
space:
mode:
Diffstat (limited to 'resources/tools/telemetry')
-rwxr-xr-xresources/tools/telemetry/__main__.py4
-rw-r--r--resources/tools/telemetry/bundle_bpf.py19
-rw-r--r--resources/tools/telemetry/bundle_perf_stat.py112
-rw-r--r--resources/tools/telemetry/bundle_vppctl.py6
-rw-r--r--resources/tools/telemetry/constants.py4
-rw-r--r--resources/tools/telemetry/metrics.py8
-rw-r--r--resources/tools/telemetry/serializer.py2
7 files changed, 140 insertions, 15 deletions
diff --git a/resources/tools/telemetry/__main__.py b/resources/tools/telemetry/__main__.py
index 2ab87b661a..7a612b8eea 100755
--- a/resources/tools/telemetry/__main__.py
+++ b/resources/tools/telemetry/__main__.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
-# Copyright (c) 2021 Cisco and/or its affiliates.
+# Copyright (c) 2022 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
@@ -19,6 +19,7 @@ from argparse import ArgumentParser, RawDescriptionHelpFormatter
from .executor import Executor
+
def main():
"""
Main entry function when called from cli
@@ -45,5 +46,6 @@ def main():
else:
Executor(args.config).execute(args.hook)
+
if __name__ == u"__main__":
main()
diff --git a/resources/tools/telemetry/bundle_bpf.py b/resources/tools/telemetry/bundle_bpf.py
index c376da9d63..58cfd5d0b6 100644
--- a/resources/tools/telemetry/bundle_bpf.py
+++ b/resources/tools/telemetry/bundle_bpf.py
@@ -52,12 +52,15 @@ class BundleBpf:
self.obj = BPF(text=self.code)
- def attach(self, duration):
+
+ def attach(self, sample_period):
"""
Attach events to BPF.
- :param duration: Trial duration.
- :type duration: int
+ :param sample_period: A "sampling" event is one that generates
+ an overflow notification every N events, where N is given by
+ sample_period.
+ :type sample_period: int
"""
try:
for event in self.events:
@@ -65,15 +68,16 @@ class BundleBpf:
ev_type=event[u"type"],
ev_config=event[u"name"],
fn_name=event[u"target"],
- sample_period=duration
+ sample_period=sample_period
)
except AttributeError:
- getLogger("console_stderr").error(u"Could not attach BPF events!")
+ getLogger("console_stderr").error(f"Could not attach BPF event: "
+ f"{event[u'name']}")
sys.exit(Constants.err_linux_attach)
def detach(self):
"""
- Dettach events from BPF.
+ Detach events from BPF.
"""
try:
for event in self.events:
@@ -98,6 +102,9 @@ class BundleBpf:
for _, metric_list in self.metrics.items():
for metric in metric_list:
+ if table_name != metric[u"name"]:
+ table_name = metric[u"name"]
+ text += f"{table_name}\n"
for (key, val) in self.obj.get_table(metric[u"name"]).items():
item = dict()
labels = dict()
diff --git a/resources/tools/telemetry/bundle_perf_stat.py b/resources/tools/telemetry/bundle_perf_stat.py
new file mode 100644
index 0000000000..471dd07b18
--- /dev/null
+++ b/resources/tools/telemetry/bundle_perf_stat.py
@@ -0,0 +1,112 @@
+# Copyright (c) 2022 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Perf Stat performance bundle."""
+
+from logging import getLogger
+import sys
+import subprocess
+
+from .constants import Constants
+
+
+class BundlePerfStat:
+ """
+ Creates a Perf stat object. This is the main object for defining a Perf Stat
+ program and interacting with its output.
+
+ Syntax: perf stat [-e <EVENT> | --event=EVENT] [-a] — <command> [<options>]
+ """
+ def __init__(self, program, serializer, hook):
+ """Initialize Bundle Perf Stat event class.
+
+ :param program: events
+ :param serializer: Metric serializer.
+ :param hook: Process ID.
+ :type program: dict
+ :type serializer: Serializer
+ :type hook: int
+ """
+ self.metrics = program[u"metrics"]
+ self.events = program[u"events"]
+ self.api_replies_list = list()
+ self.serializer = serializer
+ self.hook = hook
+
+ def attach(self, duration=1):
+ """
+ Performs perf stat.
+
+ :param duration: Time how long perf stat is collecting data (in
+ seconds). Default value is 1 second.
+ :type duration: int
+ """
+ try:
+ self.serializer.create(metrics=self.metrics)
+ event = self.events[0]
+ text = subprocess.getoutput(
+ f"""sudo perf stat -x\; -e\
+ '{{cpu/event={hex(event[u"eventcode"])},\
+ umask={hex(event[u"umask"])}/u}}'\
+ -a --per-thread\
+ sleep {duration}"""
+ )
+ except subprocess.CalledProcessError:
+ getLogger("console_stderr").error(f"Could not successfully run "
+ f"perf stat command.")
+ sys.exit(Constants.err_linux_perf_stat)
+
+ if text == u"":
+ getLogger("console_stdout").info(event[u"eventcode"])
+ else:
+ for line in text.splitlines():
+ if line.count(u";") < 6:
+ getLogger("console_stdout").info(
+ f"Could not get counters for current thread."
+ f"{line}"
+ )
+ continue
+ item = dict()
+ labels = dict()
+ item[u"name"] = self.metrics['counter'][0]['name']
+ item[u"value"] = line.split(";")[1]
+ labels["thread"] = u"-".join(
+ line.split(";")[0].split("-")[0:-1]
+ )
+ labels["pid"] = line.split(";")[0].split("-")[-1]
+ item[u"labels"] = labels
+
+ getLogger("console_stdout").info(item)
+ self.api_replies_list.append(item)
+
+
+ def detach(self):
+ """
+ Detach function.
+ """
+ pass
+
+ def fetch_data(self):
+ """
+ Fetch data function.
+ """
+ pass
+
+ def process_data(self):
+ """
+ Post process API replies.
+ """
+ for item in self.api_replies_list:
+ self.serializer.serialize(
+ metric=item[u"name"], labels=item[u"labels"], item=item
+ )
diff --git a/resources/tools/telemetry/bundle_vppctl.py b/resources/tools/telemetry/bundle_vppctl.py
index 5334237496..d0ee3754c7 100644
--- a/resources/tools/telemetry/bundle_vppctl.py
+++ b/resources/tools/telemetry/bundle_vppctl.py
@@ -30,7 +30,7 @@ M_RUN_SEPARATOR = (
r"(-)+"
)
M_RUN_NODES = (
- r"(?P<name>\S+)\s+"
+ r"(?P<node_name>\S+)\s+"
r"(?P<state>\S+\s\S+|\S+)\s+"
r"(?P<calls>\d+)\s+"
r"(?P<vectors>\d+)\s+"
@@ -284,7 +284,7 @@ class BundleVppctl:
item = dict()
labels = dict()
item["name"] = metric
- labels["name"] = nodes["name"]
+ labels["node_name"] = nodes["node_name"]
labels["state"] = nodes["state"]
try:
labels["thread_name"] = thread["thread_name"]
@@ -364,7 +364,7 @@ class BundleVppctl:
item = dict()
labels = dict()
item["name"] = metric
- labels["name"] = node["node_name"]
+ labels["node_name"] = node["node_name"]
labels["thread_name"] = thread["thread_name"]
labels["thread_id"] = thread["thread_id"]
item["labels"] = labels
diff --git a/resources/tools/telemetry/constants.py b/resources/tools/telemetry/constants.py
index 9961a07b8b..d5641ede2d 100644
--- a/resources/tools/telemetry/constants.py
+++ b/resources/tools/telemetry/constants.py
@@ -17,6 +17,7 @@
does not need to be hard coded here, but can be read from environment variables.
"""
+
class Constants:
"""Constants used in telemetry.
1-10: Telemetry errors
@@ -46,3 +47,6 @@ class Constants:
# Could not detach BPF events
err_linux_detach = 52
+
+ # Could not successfuly run perf stat command
+ err_linux_perf_stat = 53
diff --git a/resources/tools/telemetry/metrics.py b/resources/tools/telemetry/metrics.py
index 7a22acfd1b..27fad89a5c 100644
--- a/resources/tools/telemetry/metrics.py
+++ b/resources/tools/telemetry/metrics.py
@@ -104,7 +104,7 @@ class Metric:
u"Sample", [u"name", u"labels", u"value", u"timestamp"]
)
- if not re.compile(r"^[a-zA-Z_:][a-zA-Z0-9_:]*$").match(name):
+ if not re.compile(r"^[a-zA-Z._:][a-zA-Z0-9._:]*$").match(name):
raise ValueError(f"Invalid metric name: {name}!")
if typ not in self.metric_types:
raise ValueError(f"Invalid metric type: {typ}!")
@@ -210,11 +210,11 @@ class MetricBase:
:rasies ValueError: If name does not conform with naming conventions.
"""
full_name = u""
- full_name += f"{namespace}_" if namespace else u""
- full_name += f"{subsystem}_" if subsystem else u""
+ full_name += f"{namespace}." if namespace else u""
+ full_name += f"{subsystem}." if subsystem else u""
full_name += name
- if not re.compile(r"^[a-zA-Z_:][a-zA-Z0-9_:]*$").match(full_name):
+ if not re.compile(r"^[a-zA-Z._:][a-zA-Z0-9._:]*$").match(full_name):
raise ValueError(
f"Invalid metric name: {full_name}!"
)
diff --git a/resources/tools/telemetry/serializer.py b/resources/tools/telemetry/serializer.py
index 3da857c0ab..e28454fc8b 100644
--- a/resources/tools/telemetry/serializer.py
+++ b/resources/tools/telemetry/serializer.py
@@ -19,7 +19,7 @@ from logging import getLogger
class Serializer:
"""
- Executor class reponsible for executing configuration.
+ Executor class responsible for executing configuration.
"""
def __init__(self):
"""