Skip to content

Instantly share code, notes, and snippets.

@mkolod
Created April 7, 2020 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkolod/45bba10d192fe1d8e8de8f88ffed1f06 to your computer and use it in GitHub Desktop.
Save mkolod/45bba10d192fe1d8e8de8f88ffed1f06 to your computer and use it in GitHub Desktop.
#!/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))
@mkolod
Copy link
Author

mkolod commented Apr 7, 2020

Example (the sqlite file is a SQLite DB produced by nvprof or Nsight Systems)

python gpu_utilization.py profile.nvvp
GPU utilization (kernel execution only) for profile unannotated_profile.sqlite: 53.55%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment