diff mbox series

[5/5] trace-cruncher: Add kernel histogram example

Message ID 20211207142811.398929-6-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series trace-cruncher: Kernel histograms | expand

Commit Message

Yordan Karadzhov Dec. 7, 2021, 2:28 p.m. UTC
This is a very basic possible example, demonstration the usage of
the new APIs for kernel histograms.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 examples/hist.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
 create mode 100755 examples/hist.py
diff mbox series

Patch

diff --git a/examples/hist.py b/examples/hist.py
new file mode 100755
index 0000000..d668039
--- /dev/null
+++ b/examples/hist.py
@@ -0,0 +1,66 @@ 
+#!/usr/bin/env python3
+
+"""
+SPDX-License-Identifier: CC-BY-4.0
+
+Copyright 2021 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+"""
+
+import sys
+import time
+
+import tracecruncher.ftracepy as ft
+
+inst_name = 'khist_example'
+
+cmds = ['start', 'stop', 'show', 'continue', 'clear', 'close']
+
+def get_hist():
+    hist = ft.hist(name='h1',
+                   system='kmem',
+                   event='kmalloc',
+                   axes={'call_site': 'sym',
+                         'bytes_req': 'n'})
+
+    hist.add_value(value='bytes_alloc')
+    hist.sort_keys(keys=['bytes_req', 'bytes_alloc'])
+    hist.sort_key_direction(sort_key='bytes_req', direction='desc')
+
+    return hist
+
+if __name__ == "__main__":
+    if len(sys.argv) != 2:
+        sys.exit(1)
+
+    if not sys.argv[1].isdigit() and not sys.argv[1] in cmds:
+        sys.exit(1)
+
+    arg1 = sys.argv[1]
+    if  arg1.isdigit() or arg1 == 'start':
+        inst = ft.create_instance(name=inst_name)
+        hist = get_hist()
+        hist.start(inst)
+
+        if arg1.isdigit():
+            time.sleep(int(arg1))
+            hist.stop(inst)
+            print(hist.read(inst))
+            hist.close(inst)
+        else:
+            ft.detach(inst)
+    else:
+        inst = ft.find_instance(name=inst_name)
+        hist = get_hist()
+
+        if arg1 == 'stop':
+            hist.stop(inst)
+        elif arg1 == 'show':
+            print(hist.read(inst))
+        elif arg1 == 'continue':
+            hist.resume(inst)
+        elif arg1 == 'clear':
+            hist.clear(inst)
+
+        if arg1 == 'close':
+            ft.attach(inst)
+            hist.close(inst)