intel / linux-sgx

Intel SGX for Linux*
https://www.intel.com/content/www/us/en/developer/tools/software-guard-extensions/linux-overview.html
Other
1.33k stars 544 forks source link

Vtune profiling support in SGX enclave #472

Open weicz11 opened 4 years ago

weicz11 commented 4 years ago

Hi, as I know, "SGX Hotspots" is supproted in vtune since 2016. I noticed that there is a piece of code of function __create_enclave in psw (urts/urts_com.h), so what is the propose of this code below? Is it helpful for vtune profiling in sgx enclave? How can I enable the isVTuneProfiling flag?

` if(debug || !(get_enclave_creator()->use_se_hw())) { SE_TRACE(SE_TRACE_DEBUG, "Debug enclave. Checking if VTune is profiling or SGX_DBG_OPTIN is set\n");

    __itt_init_ittlib(NULL, __itt_group_none);
    bool isVTuneProfiling;
    if(__itt_get_ittapi_global()->api_initialized && __itt_get_ittapi_global()->lib)
        isVTuneProfiling = true;
    else
        isVTuneProfiling = false;

    bool is_SGX_DBG_OPTIN_set = false;
    is_SGX_DBG_OPTIN_set = is_SGX_DBG_OPTIN_variable_set();
    if (isVTuneProfiling || is_SGX_DBG_OPTIN_set)
    {
        SE_TRACE(SE_TRACE_DEBUG, "VTune is profiling or SGX_DBG_OPTIN is set\n");

        bool thread_updated;
        thread_updated = enclave->update_debug_flag(1);

        if(thread_updated == false)
        {
            SE_TRACE(SE_TRACE_DEBUG, "Failed to update debug OPTIN bit\n");
        }
        else
        {
            SE_TRACE(SE_TRACE_DEBUG, "Updated debug OPTIN bit\n");
        }

        if (isVTuneProfiling)
        {
            uint64_t enclave_start_addr;
            uint64_t enclave_end_addr;
            const char* enclave_path;
            enclave_start_addr = (uint64_t) loader.get_start_addr();
            enclave_end_addr = enclave_start_addr + (uint64_t) metadata->enclave_size -1;

            SE_TRACE(SE_TRACE_DEBUG, "Invoking VTune's module mapping API __itt_module_load \n");
            SE_TRACE(SE_TRACE_DEBUG, "Enclave_start_addr==0x%llx\n", enclave_start_addr);
            SE_TRACE(SE_TRACE_DEBUG, "Enclave_end_addr==0x%llx\n", enclave_end_addr);

            enclave_path = (const char*)file.name;
            SE_TRACE(SE_TRACE_DEBUG, "Enclave_path==%s\n",  enclave_path);
            __itt_module_load((void*)enclave_start_addr, (void*) enclave_end_addr, enclave_path);
        }
    }`
MWShan commented 4 years ago

This topic was also discussed here: https://software.intel.com/en-us/forums/intel-software-guard-extensions-intel-sgx/topic/840322.

The isVTuneProfiling flag can be set by either running the application in the VTune Profiler or by setting an environment variable defined on page 20 of https://01.org/sites/default/files/documentation/intel_sgx_sdk_developer_reference_for_linux_os_pdf.pdf

If VTune is profiling, then the code will:

dsethi commented 3 years ago

Hello, I am playing around with tune as well. I see that in the code above, the enclave is being loaded by vtune and the line: SE_TRACE(SE_TRACE_DEBUG, "Enclave_path==%s\n", enclave_path); prints the path correctly. However, after the run completes, tune report only shows events from the driver (e.g. page loads) and not application functions/lines which take the most cpu cycles. The application binary does have the symbols as well. Not sure if I am missing something here?