本文說明如何變更在虛擬機器 (VM) 例項中啟用的效能監控單元 (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.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
必要的角色
如要取得變更或停用 VM 中啟用的 PMU 類型所需的權限,請要求管理員為您授予專案的 Compute 執行個體管理員 (v1) (roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這個預先定義的角色包含變更 VM 中啟用或停用的 PMU 類型所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要變更或停用 VM 中啟用的 PMU 類型,您必須具備下列權限:
-
如要更新 VM:
compute.instances.update
在 VM 上
變更 VM 中的 PMU 類型
變更現有 C4A 或 C4 VM 中啟用的 PMU 類型前,請使用 Google Cloud CLI 或 REST API 查看 VM 詳細資料。在指令或 API 要求的輸出內容中,確認以下項目:
如要驗證 VM 中啟用的 PMU 類型,請檢查
performanceMonitoringUnit
欄位的值。如果缺少該欄位,則 PMU 會停用。如要啟用 Enhanced PMU 類型,請檢查
machineType
欄位的值,確認 VM 是否使用具有 96 或 192 個 vCPU 的 C4 機器類型。如果不是,請變更 VM 的機器類型。
您不必停止 VM,即可變更其中啟用的 PMU 類型。不過,您必須按照本節所述重新啟動 VM,變更才會生效。
如要變更現有 VM 中啟用的 PMU 類型,請選取下列任一選項:
gcloud
建立空的 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 所在區域。
REST
建立空白 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
要求。在要求中執行以下操作:在要求網址中,將
mostDisruptiveAllowedAction
查詢參數設為RESTART
。針對要求主體,請使用您在先前步驟中建立及更新的 JSON 檔案中的 VM 屬性。
要求會與下列內容相似:
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?mostDisruptiveAllowedAction=RESTART { "advancedMachineFeatures": { "performanceMonitoringUnit": "PMU_TYPE" }, ... }
如要進一步瞭解如何更新 VM 的屬性,請參閱「更新 VM 屬性」。
在 VM 中停用 PMU
您不必停止 VM 即可停用 PMU。不過,您必須按照本節所述重新啟動 VM,變更才能生效。
如要在現有 VM 中停用 PMU,請選取下列任一選項:
gcloud
建立空的 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 所在區域。
REST
建立空白 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
要求。在要求中執行以下操作:在要求網址中,將
mostDisruptiveAllowedAction
查詢參數設為RESTART
。針對要求主體,請使用您在先前步驟中建立及更新的 JSON 檔案中的 VM 屬性。
要求會與下列內容相似:
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?mostDisruptiveAllowedAction=RESTART { "advancedMachineFeatures": { "performanceMonitoringUnit": "NONE" }, ... }
如要進一步瞭解如何更新 VM 的屬性,請參閱「更新 VM 屬性」。