I am writing a custom script to evaluate nsys-rep data. I am using the scripts in User Guide — nsight-systems 2025.1 documentation as templates. The data contains sampled Python backtraces - I can see them in nsys-ui. However, I have no idea on how to retrieve them in the Python script. I can see that the corresponding entries in the strings table exist, but I have no idea where they are references. It appears NVTX_EVENTS is related (event type 75).
How do I decode Python Backtrace Data using Python? A code example would be most welcome
The Python backtrace events are exported as NVTX events (table NVTX_EVENTS in the schema). Those events can be distinguished by their domain name - “Python Periodic Sampling” or “Python Backtrace on CUDA”. The backtrace data (function name, file name and line) is accessible only via the “jsonText” column. This means that it is required to export the report with json text included, i.e., nsys export -t sqlite --include-json true report.nsys-rep.
Here is an example of printing the backtrace of the first Python sampling event:
import json
import sqlite3
# Assuming report1.sqlite exists
con = sqlite3.Connection(f'file:report1.sqlite?mode=ro', uri=True)
# Filter Python sampling events out of NVTX events
# For Python backtrace on CUDA, use "Python Backtrace on CUDA" instead of "Python Periodic Sampling"
query = """
SELECT globalTid, start, jsonText
FROM NVTX_EVENTS
JOIN NVTX_PAYLOAD_SCHEMAS ON NVTX_PAYLOAD_SCHEMAS.domainId
WHERE NVTX_PAYLOAD_SCHEMAS.name = "Python Periodic Sampling"
AND jsonText IS NOT NULL
"""
# Fetch the first Python sampling event
globalTid, start, sample = con.execute(query).fetchone()
# Parse the json string
sample = json.loads(sample)
# Print the backtrace data
for frame in sample['Python Periodic Sampling']['Frames']:
# Note that a lookup in StringIds table is required to get the actual file and function names
func_name = con.execute(f'SELECT value FROM StringIds WHERE id = {frame["Function"]}').fetchone()[0]
file_name = con.execute(f'SELECT value FROM StringIds WHERE id = {frame["File"]}').fetchone()[0]
line = frame['Line']
print(f'{file_name}:{line}:{func_name}()')
jsonText is always NULL in NVTX_EVENTS: “select * from NVTX_EVENTS where jsonText IS NOT NULL” is empty.
I do see Python tracebacks when I look at the nsys-rep with Nsight Systems. Export was done with Nsight Systems 2024.6.1.90. I also do see the "“Python Periodic Sampling” in NVTX_PAYLOAD.
What is wrong here?