Created
April 7, 2020 21:59
-
-
Save mkolod/45bba10d192fe1d8e8de8f88ffed1f06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy as np | |
import sqlite3 as lite | |
import sys | |
from contextlib import ContextDecorator | |
class DbContext(ContextDecorator): | |
def __init__(self, file_path): | |
self.file_path = file_path | |
self.con = None | |
self.cur = None | |
def __enter__(self): | |
self.con = lite.connect(self.file_path) | |
self.cur = self.con.cursor() | |
return self | |
def execute(self, query): | |
self.cur.execute(query) | |
return self.cur.fetchall() | |
def __exit__(self, *exc): | |
if self.con is not None: | |
self.con.close() | |
if __name__ == "__main__": | |
if len(sys.argv) == 1: | |
print("Please provide sqlite database name as an argument to the script.") | |
print("Example: {} foo.nvprof/sqlite/nvvp".format(sys.argv[0])) | |
sys.exit(1) | |
with DbContext(sys.argv[1]) as ctx: | |
query = """ | |
SELECT start, end, (end-start) AS duration | |
FROM CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL; | |
""" | |
qres = ctx.execute(query) | |
durations = np.array(list(map(lambda x: x[2], qres))) | |
total_kernel_dur = durations.sum() | |
total_dur = qres[-1][1] - qres[0][0] | |
gpu_util_pct = total_kernel_dur / total_dur * 100 | |
print("GPU utilization (kernel execution only) for profile {}: {:.2f}%".format(sys.argv[1], gpu_util_pct)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example (the sqlite file is a SQLite DB produced by nvprof or Nsight Systems)