本文說明如何變更虛擬機器 (VM) 執行個體中啟用的效能監控單元 (PMU) 類型,或停用 PMU。如要進一步瞭解 PMU,請參閱 PMU 總覽。
在 VM 中啟用 PMU 並在 VM 上執行效能監控軟體後,您可以執行下列操作:
變更 VM 中啟用的 PMU 類型。如要追蹤不同類型的低階 CPU 事件,這項變更就很有用。
在 VM 中停用 PMU。如果您不再需要存取 PMU 中的效能計數器,這項動作就非常實用。
事前準備
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
如要更新 VM:
按一下 VM 上的
compute.instances.update
如要驗證 VM 中啟用的 PMU 類型,請檢查
performanceMonitoringUnit
欄位的值。如果缺少這個欄位,PMU 就會停用。如要啟用「增強型 PMU」類型,請檢查
machineType
欄位的值,確認 VM 使用的 C4 機器類型具有 96 或 192 個 vCPU。如果沒有,請變更 VM 的機器類型。建立空白 YAML 檔案。
如要將 VM 的屬性匯出至您剛建立的 YAML 檔案,請使用
gcloud compute instances export
指令:gcloud compute instances export VM_NAME \ --destination=YAML_FILE \ --zone=ZONE
更改下列內容:
VM_NAME
:VM 名稱。YAML_FILE
:您在上一步建立的 YAML 檔案路徑。ZONE
:VM 所在的區域。
在 YAML 設定檔中找出
performanceMonitoringUnit
欄位,然後變更其值,指定其他 PMU 類型:advancedMachineFeatures: performanceMonitoringUnit: PMU_TYPE
將
PMU_TYPE
替換為下列任一值:架構 PMU 類型:
ARCHITECTURAL
標準 PMU 類型:
STANDARD
加強型 PMU 類型:
ENHANCED
如要更新 VM 並重新啟動,請使用
gcloud compute instances update-from-file
指令,並將--most-disruptive-allowed-action
旗標設為RESTART
:gcloud compute instances update-from-file VM_NAME \ --most-disruptive-allowed-action=RESTART \ --source=YAML_FILE \ --zone=ZONE
更改下列內容:
VM_NAME
:VM 名稱。YAML_FILE
:YAML 檔案的路徑,內含您在上一步修改的設定資料。ZONE
:VM 所在的區域。
建立空白的 JSON 檔案。
如要查看現有 VM 的屬性,請向
instances.get
方法發出GET
要求:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID
:VM 所在的專案 ID。ZONE
:VM 所在的區域。VM_NAME
:現有 VM 的名稱。
在您於上一個步驟中建立的空白 JSON 檔案中,執行下列操作:
輸入
GET
要求輸出內容中的 VM 屬性。找出
performanceMonitoringUnit
欄位,然後變更其值,指定其他 PMU 類型:"advancedMachineFeatures": { "performanceMonitoringUnit": "PMU_TYPE" }
將
PMU_TYPE
替換為下列其中一個值:架構 PMU 類型:
ARCHITECTURAL
標準 PMU 類型:
STANDARD
加強型 PMU 類型:
ENHANCED
如要更新 VM 並重新啟動,請對
instances.update
方法發出PUT
要求。在要求中執行下列操作:在要求網址中,加入設為
RESTART
的mostDisruptiveAllowedAction
查詢參數。在要求主體中,使用您在前幾個步驟中建立及更新的 JSON 檔案中的 VM 屬性。
要求類似於下列內容:
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?mostDisruptiveAllowedAction=RESTART { "advancedMachineFeatures": { "performanceMonitoringUnit": "PMU_TYPE" }, ... }
建立空白 YAML 檔案。
如要將 VM 的屬性匯出至您剛建立的 YAML 檔案,請使用
gcloud compute instances export
指令:gcloud compute instances export VM_NAME \ --destination=YAML_FILE \ --zone=ZONE
更改下列內容:
VM_NAME
:VM 名稱。YAML_FILE
:您在上一步建立的 YAML 檔案路徑。ZONE
:VM 所在的區域。
在 YAML 設定檔中,找出
performanceMonitoringUnit
欄位,並將其值設為NONE
:advancedMachineFeatures: performanceMonitoringUnit: NONE
如要更新 VM 並重新啟動,請使用
gcloud compute instances update-from-file
指令,並將--most-disruptive-allowed-action
旗標設為RESTART
:gcloud compute instances update-from-file VM_NAME \ --most-disruptive-allowed-action=RESTART \ --source=YAML_FILE \ --zone=ZONE
更改下列內容:
VM_NAME
:VM 名稱。YAML_FILE
:YAML 檔案的路徑,內含您在上一步修改的設定資料。ZONE
:VM 所在的區域。
建立空白的 JSON 檔案。
如要查看現有 VM 的屬性,請向
instances.get
方法發出GET
要求:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID
:VM 所在的專案 ID。ZONE
:VM 所在的區域。VM_NAME
:現有 VM 的名稱。
在您於上一個步驟中建立的空白 JSON 檔案中,執行下列操作:
輸入
GET
要求輸出內容中的 VM 屬性。找出
performanceMonitoringUnit
欄位,然後將其值變更為NONE
:"advancedMachineFeatures": { "performanceMonitoringUnit": "NONE" }
如要更新 VM 並重新啟動,請對
instances.update
方法發出PUT
要求。在要求中執行下列操作:在要求網址中,加入設為
RESTART
的mostDisruptiveAllowedAction
查詢參數。在要求主體中,使用您在前幾個步驟中建立及更新的 JSON 檔案中的 VM 屬性。
要求類似於下列內容:
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?mostDisruptiveAllowedAction=RESTART { "advancedMachineFeatures": { "performanceMonitoringUnit": "NONE" }, ... }
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
必要的角色
如要取得變更 VM 中啟用的 PMU 類型或停用 PMU 類型所需的權限,請要求管理員授予您專案的 Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。這個預先定義的角色具備變更 VM 中已啟用 PMU 類型或停用該類型的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要變更 VM 中啟用的 PMU 類型或停用 PMU,您必須具備下列權限:
變更 VM 中的 PMU 類型
變更現有 C4A 或 C4 VM 中啟用的 PMU 類型前,請先使用 Google Cloud CLI 或 REST API 查看 VM 詳細資料。在指令或 API 要求的輸出內容中,確認下列事項:
您不必停止 VM,即可變更 VM 中啟用的 PMU 類型。不過,如要讓變更生效,您必須按照本節所述重新啟動 VM。
如要變更現有 VM 中已啟用的 PMU 類型,請選取下列其中一個選項:
gcloud
REST
如要進一步瞭解如何更新 VM 屬性,請參閱「更新 VM 屬性」。
在 VM 中停用 PMU
您不必停止 VM 即可停用 PMU。不過,如要讓變更生效,您必須按照本節所述重新啟動 VM。
如要在現有 VM 中停用 PMU,請選取下列其中一個選項:
gcloud
REST
如要進一步瞭解如何更新 VM 屬性,請參閱「更新 VM 屬性」。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-11 (世界標準時間)。
-