Common SQLite examples
########################

.. _examples:

This section lists recepies to frequently asked questions on "how-to's" with
NVIDIA Nsight Systems data exported in SQLite format.

General notes
**************

The data exported into SQLite database don't contain any indexes by default and
are as close to the contents of the report as possible. Therefore, for better
readability and speed of execution, queries below will alter tables.

Common helper commands
***********************

When utilizing `sqlite3` commandline tool, it's helpful to have data printed
as named columns, this can be done with:

::

    .mode column
    .headers on

Default column width is determined by the data in the first row of results. If
this doesn't work out well, you can specify widths manually:

::

    .width 10 20 50

Obtaining sample report
************************

CLI interface of Nsight Systems was used to profile ``radixSortThrust``
CUDA sample, then the resulting ``.qdrep`` file was exported using the
``nsys export``.

::

    nsys profile --trace=cuda,osrt radixSortThrust
    nsys export --type sqlite report1.qdrep

Serialized process and thread identifiers
******************************************

.. _globalid:

NVIDIA Nsight Systems stores identifiers where events originated in serialized
form. For events that have ``globalTid`` or ``globalPid`` fields exported,
use the following code to extract numeric TID and PID:

.. code-block:: sql

    SELECT globalTid / 0x1000000 % 0x1000000 AS PID, globalTid % 0x1000000 AS TID FROM TABLE_NAME;


NOTE: ``globalTid`` field includes both TID and PID values, while ``globalPid``
only the PID value.

Correlate CUDA kernel launches with CUDA API kernel launches
*************************************************************

.. _transfer_cuda_kernel_on_api:

.. literalinclude:: cuda_kernel_api.report32.sql
   :language: sql

Results:

.. literalinclude:: cuda_kernel_api.report32.results.txt


Remove ranges overlapping with overhead
****************************************

.. _overhead_overlapping:

.. literalinclude:: overhead.report32.sql
   :language: sql

Results:

.. literalinclude:: overhead.report32.results.txt


Find CUDA API calls that resulted in original graph node creation
******************************************************************

.. _select_origin_cuda_api:

.. literalinclude:: origin.simpleCudaGraphs.sql
   :language: sql

Results:

.. literalinclude:: origin.simpleCudaGraphs.results.txt


Backtraces for OSRT ranges
***************************

.. _osrt_callchains:

.. literalinclude:: osrt.report32.sql
   :language: sql

Results:

.. literalinclude:: osrt.report32.results.txt


Profiled processes output streams
**********************************

.. _processes_streams:

.. literalinclude:: streams.report32.sql
   :language: sql

Results:

.. literalinclude:: streams.report32.results.txt

Thread summary
**********************************

.. _thread_summary_composite:

Please note, that Nsight Systems applies additional logic during sampling events
processing to work around lost events. This means that the results of the below
query might differ slightly from the ones shown in "Analysis summary" tab.

.. literalinclude:: composite.report32.sql
   :language: sql

Results:

.. literalinclude:: composite.report32.results.txt

.. _thread_summary_sched:

Thread running time may be calculated using scheduling data, when PMU counter
data was not collected.

.. literalinclude:: sched.report32.sql
   :language: sql

Results:

.. literalinclude:: sched.report32.results.txt

Function table
**********************************

.. _function_table:

These examples demonstrate how to calculate Flat and BottomUp (for top level
only) views statistics.

.. literalinclude:: func_table.report32.sql
   :language: sql

Results:

.. literalinclude:: func_table.report32.results.txt

DX12 API frame duration histogram
**********************************

.. _dx12_fps_histgram:

The example demonstrates how to calculate DX12 CPU frames durartion and
construct a histogram out of it.

.. literalinclude:: fps_histogram.dx12.sql
   :language: sql

Results:

.. literalinclude:: fps_histogram.dx12.results.txt

GPU context switch events enumeration
**************************************

.. _fecs_enumerate:

GPU context duration is between first BEGIN and a matching END event.

.. literalinclude:: fecs.gpuctxsw.sql
   :language: sql

Results:

.. literalinclude:: fecs.gpuctxsw.results.txt

Rename CUDA kernels with NVTX
******************************

.. _apply_nvtx_2_cuda:

The example demonstrates how to map innermost NVTX push-pop range to a matching
CUDA kernel run.

.. literalinclude:: map.cunvtx.sql
   :language: sql

Results:

.. literalinclude:: map.cunvtx.results.txt

Select CUDA calls with backtraces
**********************************

.. _select_cuda_backtraces:


.. literalinclude:: cuda_bt.report705.sql
   :language: sql

Results:

.. literalinclude:: cuda_bt.report705.results.txt

SLI Peer-to-Peer query
**********************************

.. _sli:

The example demonstrates how to query SLI Peer-to-Peer events with resource size greater than
value and within a time range sorted by resource size descending.

.. literalinclude:: sli.report109.sql
   :language: sql

Results:

.. literalinclude:: sli.report109.results.txt

Generic events
******************************

.. _generic_syscall_histogram:

Syscall usage histogram by PID:

.. literalinclude:: syscall_histogram.ftrace.sql
   :language: sql

Results:

.. literalinclude:: syscall_histogram.ftrace.results.txt

Fetching generic events in JSON format
=======================================

Text and JSON export modes don't include generic events. Use the below queries
(without LIMIT clause) to extract JSON lines representation of generic events,
types and sources.

.. literalinclude:: extract_jsonl.ftrace.sql
   :language: sql

Results:

.. literalinclude:: extract_jsonl.ftrace.results.txt
