# Nsight Systems
###### tags: `NVIDIA Nsight`
本篇是介紹Nsight Systems如何安裝與運行以及遇到的問題,目的是察看`board-system`的NVIDIA GPU使用情況。本次將在xavierNX ARM64bits與Docker Container Ubuntu x86_64兩個平台上操作。
## 1. Introduction
簡單來說,它是一款系統範圍的***性能分析工具***,目的是可視化要觀察的應用程序執行時的各個function、cuda、CPU、GPU等的使用情況,來幫助開發者更直觀地看到性能的瓶頸點再針對硬地優化。
* 官方介紹: [NVIDIA Nsight Sytems](https://developer.nvidia.com/nsight-systems)
* 官方使用者教學: [NVIDIA Nsight Systems User Guide](https://docs.nvidia.com/nsight-systems/UserGuide/index.html)
## 2. Installation Guide
由於優化後的`board-system`在NVIDIA Jetson系列上的性能並不如預期,因此本次安裝的目的是希望能通過Nsight Systems了解是記憶體搬運問題、GPU效能並未發揮到極致還是其他等等的問題。
**注意:**
**本次是要觀察`board-system`在NVIDIA Jetson XavierNX的瓶頸點,但如果要在上面安裝Nsight Systems需要JetPack 4.5的版本,而本次使用的Jetson XavierNX的JetPack版本為4.4,為了避免提升版本所帶來可能的版本衝突而選擇用一般方式安裝。其中Nsight Systems對ARM平台只提供CLI版本,並未提供GUI([official description](https://docs.nvidia.com/nsight-systems/InstallationGuide/#finding-right-package))。為了能更值觀察結果圖像,需要在另一個有提供GUI版本的平台上安裝,本文是在Ubuntu x86_64上(若將JetPack提升到4.5,可直接在Ubutnu上通過ssh的方式連接Jetson board進行遠端profile)。**
* [NVIDIA Nsight Systems Installation Guide](https://docs.nvidia.com/nsight-systems/InstallationGuide/)
* [NVIDIA JetPack SDK](https://docs.nvidia.com/jetson/jetpack/install-jetpack/index.html)
### 2.1 Download
1. 確認系統版本與相關依賴是否支援([詳細資訊](https://docs.nvidia.com/nsight-systems/InstallationGuide/#system-requirements))
1. CUDA Version
* Nsight Systems supports CUDA 10.0, 10.1, 10.2, and 11.0 for most platforms
* Nsight Systems on ARM SBSA supports 10.2 and 11.0
NOTE THAT: CUDA version and driver version must be compatible. But if you use the Jetson board, the driver is not necessary.

<center>表一: cuda與NVIDIA driver對應表</center>
2. Requirements for x86_64, Power, and ARM SBSA Targets on Linux
* 確認Linux Perf
* `cat /proc/sys/kernel/perf_event_paranoid`
* 若輸出大於2,則可用以下兩種方法進行暫時或永久改動(暫時改動在每次重新啟動機器後都需要在重新操作 )
* Temporarily: `sudo sh -c 'echo 2 >/proc/sys/kernel/perf_event_paranoid'`
* permanent: `sudo sh -c 'echo 2 >/proc/sys/kernel/perf_event_paranoid'`
* 確認kernel版本
* `uname -a`
* 3.10.0-693 or later for CentOS and RedHat Enterprise Linux 7.4+
* 4.3 or greater for all other distros including Ubuntu
* 確認glibc版本(target device)
* `ldd --version`
* 版本要在2.14或更新
**Linux Ubuntu 14.04 and higher are known to work, running on other modern distributions should be possible as well.**
**Only 64-bit versions are supported.**
2. 從[nsight-systems-download](https://developer.nvidia.com/gameworksdownload#?dn=nsight-systems-2021-2-1-58)中下載對應的安裝檔(也可從[介紹](#1. 介紹)中的官方介紹連結下載)

<center>圖一: Nsight Systems 下載頁面</center>

<center>圖二: 依照自身os版本下載Nsight Systems: 紅框是Jetson XavierNX;藍色是Ubuntu</center>
### 2.2 Installation Steps
* 指令:
* Jetson: `sudo dpkg -i nsight-systems-cli-2021.2.1_2021.2.1.58-1_arm64.deb`
* x86_64 Ubuntu: `./NsightSystems-linux-public-2021.2.1.58-642947b.run`
* 確認是否安裝成功:
```shell
~$ nsys status -e
Sampling Environment Check
Linux Kernel Paranoid Level = 1: OK
Linux Distribution = Ubuntu
Linux Kernel Version = 4.15.0-109-generic: OK
Linux perf_event_open syscall available: OK
Sampling trigger event available: OK
Intel(c) Last Branch Record support: Available
Sampling Environment: OK
```
## 3. Using Nsight Systems to analyze board-system on XavierNX
本段是說明如何在XavierNX運行,以及如何察看結果。
**在開始之前,再次強調,由於以下兩個原因,所以本次分析操作會較繁瑣:**
1. **避免可能產生的版本衝突,而不將Jetson4.4升到4.5,導致無法安裝Jetson專門的Nsight Systems**
2. **官方提供的Nsight Systems只提供CLI的版本給ARM平台,導致無法直接查看**
**而以上兩個原因也造成無法將其他平台作為host,XavierNx作為Target的方式直接使用Nsight System中的ssh進行操作。**
**本次操作流程如下:**
1. 在XavierNX端用Nsight Systems CLI分析`board-system`,並將結果儲存
* 指令如下。輸入以下指令後,會產生reportNUMBER.qdrep的結果檔,之後可通過GUI開啟。檔名中的NUMBER是依照當前的report數累計。
* `[sudo] nsys profile python YOUR_PATH/board-system/main.py -infor -config ./run/xavier-trt.ini -source YOUR_PATH_TO_SOURCE -loop 0`
* ```shell
~$sudo nsys profile python3 ~/hamilton/board-system/main.py -infor -config ./run/xavier-trt7.ini -source ~/board-system/40.jpg -loop 0
~$sudo nsys profile python3 ~/hamilton/board-system/main.py -infor -config ./run/xavier-trt7.ini -source ~/hamilton/board-system/inference_dataset/test_dir/ -loop 0
```
2. 通過scp或是usb等方式將結果放至x86_64 Ubuntu中
3. 在x86_64 Ubuntu中開啟Nsight System GUI後,再開啟要觀察的檔案進行分析
* `sudo YOUR_PATH/nsight-systems-xxxx.x.x/bin/nsys-ui`
