共用方式為


PgoAutoSweep

PgoAutoSweep 會將目前的設定檔計數器資訊儲存至檔案,然後重設計數器。 在設定檔引導優化定型期間使用 函式,將所有設定檔資料從執行中的程式寫入檔案 .pgc ,以供稍後在優化組建中使用。

語法

void PgoAutoSweep(const char* name);    // ANSI/MBCS
void PgoAutoSweep(const wchar_t* name); // UNICODE

參數

name
識別已儲存 .pgc 檔案的字串。

備註

您可以從應用程式呼叫 PgoAutoSweep ,在應用程式執行期間隨時儲存和重設設定檔資料。 在檢測的組建中,擷 PgoAutoSweep 取目前的分析資料、將它儲存在檔案中,並重設設定檔計數器。 相當於在可執行檔的特定點呼叫 pgosweep 命令。 在優化的組建中, PgoAutoSweep 是無作業。

儲存的設定檔計數器資料會放在名為 base_name 名稱 - 檔案中! value.pgc ,其中 base_name 是可執行檔的基底名稱,name 是傳遞至 PgoAutoSweep 的參數, value 是唯一值,通常是單調增加的數位,以防止檔案名衝突。

.pgcPgoAutoSweep 建立的檔案必須合併到 .pgd 檔案中,才能用來建立優化的可執行檔。 您可以使用 pgomgr 命令來執行合併。

您可以在優化建置期間,使用 PGD= filename 引數將合併 .pgd 檔案的名稱傳遞至 /USEPROFILE 連結器選項,或使用已被取代 的 /PGD 連結器選項。 如果您將 .pgc 檔案合併至名為 base_name.pgd 的檔案,則不需要在命令列上指定檔案名,因為連結器預設會挑選此檔案名。

PgoAutoSweep 式會維護建立檢測組建時所指定的執行緒安全性設定。 如果您使用預設設定,或指定 /GENPROFILE 或 /FASTGENPROFILE 連結器選項的 NOEXACT 引數,則 對 PgoAutoSweep 的呼叫不是安全線程。 EXACT 引數會建立安全且更精確的執行緒,但較慢且檢測的可執行檔。

需求

常式 必要的標頭
PgoAutoSweep <pgobootrun.h>

可執行檔必須在連結的程式庫中包含 pgobootrun.lib 檔案。 此檔案包含在 Visual Studio 安裝中,每個支援的架構的 VC 程式庫目錄中。

範例

下列範例會使用 PgoAutoSweep 在執行期間在不同的點建立兩個 .pgc 檔案。 第一個包含描述執行時間行為直到 count 等於 3 的資料,而第二個數據則包含在此點之後收集的資料,直到應用程式終止之前為止。

// pgoautosweep.cpp
// Compile by using: cl /c /GL /W4 /EHsc /O2 pgoautosweep.cpp
// Link to instrument: link /LTCG /genprofile pgobootrun.lib pgoautosweep.obj
// Run to generate data: pgoautosweep
// Merge data by using command line pgomgr tool:
// pgomgr /merge pgoautosweep-func1!1.pgc pgoautosweep-func2!1.pgc pgoautosweep.pgd
// Link to optimize: link /LTCG /useprofile pgobootrun.lib pgoautosweep.obj

#include <iostream>
#include <windows.h>
#include <pgobootrun.h>

void func2(int count)
{
    std::cout << "hello from func2 " << count << std::endl;
    Sleep(2000);
}

void func1(int count)
{
    std::cout << "hello from func1 " << count << std::endl;
    Sleep(2000);
}

int main()
{
    int count = 10;
    while (count--)
    {
        if (count < 3)
            func2(count);
        else
        {
            func1(count);
            if (count == 3)
            {
                PgoAutoSweep("func1");
            }
        }
    }
    PgoAutoSweep("func2");
}

在開發人員命令提示字元中,使用此命令將程式碼編譯為物件檔:

cl /c /GL /W4 /EHsc /O2 pgoautosweep.cpp

然後使用下列命令產生已檢測的組建以進行定型:

link /LTCG /genprofile pgobootrun.lib pgoautosweep.obj

執行已檢測的可執行檔來擷取定型資料。 呼叫 PgoAutoSweep 的資料輸出會儲存在名為 pgoautosweep-func1!1.pgc 和 pgoautosweep-func2!1.pgc 的檔案中。 程式的輸出看起來應該像這樣,因為它執行:

hello from func1 9
hello from func1 8
hello from func1 7
hello from func1 6
hello from func1 5
hello from func1 4
hello from func1 3
hello from func2 2
hello from func2 1
hello from func2 0

執行 pgomgr 命令,將已儲存的資料合併至設定檔定型 資料庫:

pgoautosweep-func1!1.pgc pgoautosweep-func2!1.pgc

此命令的輸出看起來像這樣:

Microsoft (R) Profile Guided Optimization Manager 14.13.26128.0
Copyright (C) Microsoft Corporation. All rights reserved.

Merging pgoautosweep-func1!1.pgc
pgoautosweep-func1!1.pgc: Used  3.8% (22304 / 589824) of total space reserved.  0.0% of the counts were dropped due to overflow.
Merging pgoautosweep-func2!1.pgc
pgoautosweep-func2!1.pgc: Used  3.8% (22424 / 589824) of total space reserved.  0.0% of the counts were dropped due to overflow.

現在您可以使用此定型資料來產生優化的組建。 使用此命令來建置優化的可執行檔:

link /LTCG /useprofile pgobootrun.lib pgoautosweep.obj

Microsoft (R) Incremental Linker Version 14.13.26128.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Merging pgoautosweep!1.pgc
pgoautosweep!1.pgc: Used  3.9% (22904 / 589824) of total space reserved.  0.0% of the counts were dropped due to overflow.
  Reading PGD file 1: pgoautosweep.pgd
Generating code

0 of 0 ( 0.0%) original invalid call sites were matched.
0 new call sites were added.
294 of 294 (100.00%) profiled functions will be compiled for speed
348 of 1239 inline instances were from dead/cold paths
294 of 294 functions (100.0%) were optimized using profile data
16870 of 16870 instructions (100.0%) were optimized using profile data
Finished generating code

另請參閱

特性指引最佳化
pgosweep