# CPE測試說明
CPE密碼:111111
## 資源調用步驟
README裡寫的步驟是基於CentOS,但CPE是灌ubuntu所以指令會有所不同
先pip install protobuf(一定要是**3.20.0** 太新的不行)和grpcio(1.64.0)
```
pip install protobuf grpcio
```
接下來要啟用GRPC server:
跟README寫的不同 我是輸入
```
systemctl start qmonitor-proxy
```
```
systemctl status qmonitor-proxy
```

有顯示active(running)就有成功跑起來了
```
關掉server: systemctl stop qmonitor-proxy
```
接下來要開防火牆
在ubuntu裡面要用ufw開啟防火牆 預設端口62472
```
sudo ufw enable
sudo ufw allow 62472/tcp
sudo ufw status
```

接著在examples/python底下輸入
```
python3 QAicMonitorClient.py --device_info --sync_mode --print_json
```
就可以成功調用各種資源

## CPE測試
我開了兩個視窗,右邊執行政憲之前寫的多線程執行,左邊寫了一個無限迴圈,持續監視系統資源
持續調用資源程式碼參考:
```
import multiprocessing
import subprocess
from concurrent.futures import ThreadPoolExecutor
def monitor_resources():
while True:
result = subprocess.run(
['python3', '/opt/qti-aic/examples/python/QAicMonitorClient.py', '--device_info', '-d', '-1', '--sync_mode', '--print_json'],
capture_output=True,
text=True
)
# 如果返回碼不是0,表示有錯誤
if result.returncode != 0:
print("Error:", result.stderr)
continue
# 解析輸出並提取資源參數
output_lines = result.stdout.strip().split('\n')
for line in output_lines:
if "neural_processors_free" in line:
neural_processors_free_str = line.split(":")[-1].strip().rstrip(',')
neural_processors_free = int(neural_processors_free_str)
print("Neural Processors Free:", neural_processors_free)
elif "neural_processors_total" in line:
neural_processors_total_str = line.split(":")[-1].strip().rstrip(',')
neural_processors_total = int(neural_processors_total_str)
print("Neural Processors total:", neural_processors_total)
elif "dram_bw" in line:
dram_bw_str = line.split(":")[-1].strip().rstrip(',')
dram_bw = float(dram_bw_str)
print("DRAM Bandwidth:", dram_bw)
elif "num_networks_active" in line:
num_networks_active_str = line.split(":")[-1].strip().rstrip(',')
num_networks_active = int(num_networks_active_str)
print("Number of Active Networks:", num_networks_active)
elif "num_networks_loaded" in line:
num_networks_loaded_str = line.split(":")[-1].strip().rstrip(',')
num_networks_loaded = int(num_networks_loaded_str)
print("Number of Loaded Networks:", num_networks_loaded)
print('\n')
# 資源監控進程的啟動和終止
if __name__ == "__main__":
monitor_process = multiprocessing.Process(target=monitor_resources)
monitor_process.start()
# 等待資源監控進程完成
monitor_process.join()
```

結果發現的確有調用到AI-chip,可以看到閒置的Neural Processors從2變為0,而我同時要求三次模型推論,但只有兩個推論成功,所以推測多線程的處理跟Neural Processors的個數有很大的關係,但不知道為甚麼處理器總共有9個,但實際上閒置可調用的只有2個,所以導致多線程最多只能同時跑兩個推論。

