ESM6802是英創公司推出的基于Freescale i.MX6DL雙核處理器(ARM Cortex-A9,主頻1GHz)的高性能工控主板,預裝正版Windows Embedded Compact 7(WEC7)嵌入式操作系統,WEC7一個最重要的特性就是對多核處理器的支持(Symmetric Multi-Processing(SMP)),下面將通過應用程序來測試在單核和多核情況下系統的執行情況,為了更直觀的比較,同時參與測試的還有ESM3354,ESM3354是基于TI Coertex-A8處理器的工控主板,CPU主頻1GHz,同樣預裝WEC7操作系統。
所設計的測試程序代碼如下,其中的TestSmp函數有兩個輸入參數,第一參數表示要創建測試線程的數量,第二個參數為所創建線程的運行時長。cbTestSmp是被創建的測試線程,測試線程主要是在一個while循環中,反復讀取內存變量然后與預設值進行比較,在運行設定的時間后自動退出循環,其中的threadParam->loops變量會記錄下while循環總共執行的次數。
typedefstruct_SMP_THREAD_PARAM
{
UINT32 durationMs;
UINT32 threadId;
UINT64 loops;
BOOL bSetAffinity;
UINT32 sandBoxSize;
LPVOID sandBoxStart;
}SMP_THREAD_PARAM, *PSMP_THREAD_PARAM;
ULONGcbTestSmp(LPVOID param)
{
PSMP_THREAD_PARAM threadParam = (PSMP_THREAD_PARAM)param;
DWORD tStart = GetTickCount();
UINT8 *buffer = (UINT8 *)threadParam->sandBoxStart;
wprintf(L"Ahou, Thread %d, running for %d ms\r\n", threadParam->threadId,
threadParam->durationMs);
// Write to sandbox
for(UINT32 i = 0; i < threadParam->sandBoxSize; i++)
{
buffer[i] = (UINT8)(i);
}
while( (GetTickCount() - tStart) < threadParam->durationMs)
{
// Read back from sandbox
for(UINT32 i = 0; i < threadParam->sandBoxSize; i++)
{
if(buffer[i] != (UINT8)(i))
{
wprintf(L"Thread %d : error at byte %d for loop %I64d !!\r\n",
threadParam->threadId, i, threadParam->loops);
}
}
threadParam->loops++;
}
wprintf(L"Thread %d : terminating\r\n", threadParam->threadId);
return0;
}
voidTestSmp(UINT32 nNumOfThread, UINT32 durationMs)
{
UINT32 i;
PSMP_THREAD_PARAM threadParams;
HANDLE *threadHandles;
UINT64 totalLoops = 0;
UINT32 sandBoxSize = 1024 * 128; // 128 kB
HANDLE h_array[1];
threadParams = (PSMP_THREAD_PARAM)malloc(nNumOfThread *sizeof(SMP_THREAD_PARAM));
if(threadParams == NULL)
{
wprintf(L"Failed allocating thread params !\r\n");
return;
}
threadHandles = (HANDLE *)malloc(nNumOfThread *sizeof(HANDLE));
if(threadHandles == NULL)
{
wprintf(L"Failed allocating thread handles !\r\n");
return;
}
for(i = 0; i < nNumOfThread; i++)
{
threadParams[i].bSetAffinity = TRUE;
threadParams[i].threadId = i;
threadParams[i].durationMs = durationMs;
threadParams[i].loops = 0;
threadParams[i].sandBoxSize = sandBoxSize;
threadParams[i].sandBoxStart = malloc(sandBoxSize);
threadHandles[i] = CreateThread(NULL, 0, cbTestSmp, &threadParams[i], 0, NULL);
wprintf(L"Thread handle %d : 0x%x\r\n", i, threadHandles[i]);
}
h_array[0] = threadHandles[0];
DWORD res = WaitForSingleObject(h_array[0], INFINITE);
Sleep(500);
if(res == WAIT_TIMEOUT)
{
wprintf(L"Timeout waiting for threads !\r\n");
}
else
{
wprintf(L"All threads exited\r\n");
}
for(i = 0; i < nNumOfThread; i++)
{
wprintf(L"Thread %d did run %I64d loops\r\n", i, threadParams[i].loops);
totalLoops += threadParams[i].loops;
free(threadParams[i].sandBoxStart);
CloseHandle(threadHandles[i]);
}
wprintf(L"Total number of loops %I64d (%I64d millions)\r\n", totalLoops,
totalLoops / 1000000);
free(threadHandles);
free(threadParams);
}
將上述測試代碼編譯生成為exe文件,分別在ESM3354和ESM6802上運行,設置while循環的執行時間均為10000ms,測試結果如下:
1、創建單個線程
測試主板與線程 | ESM3354(1GHz單核 Cortex-A8) | ESM6802(1GHz雙核Cortex-A9) |
循環次數 | 6791 | 7493 |
當測試程序只創建一個測試線程時,ESM3354的while循環執行了6791次,ESM6802執行7493次,雖然ESM6802為雙核處理器,但由于程序只有一個線程,即同一時刻只有一個線程在運行,所以在相同的時間內,循環的次數僅略多于ESM3354。由于ESM3354和ESM6802的CPU主頻同樣都是1GHz,所以可以認為ESM6802多出的循環次數也就是Cortex-A8與Cortex-A9在代碼執行效率上的差別。
2、創建兩個線程
測試主板與線程 | ESM3354(1GHz單核 Cortex-A8) | ESM6802(1GHz雙核Cortex-A9) |
線程1循環次數 | 3390 | 7438 |
線程2循環次數 | 3442 | 7452 |
總循環次數 | 6832 | 14890 |
當測試程序創建了兩個線程時,ESM3354會將CPU資源大約平均的分配給兩個線程,如上表中線程1執行了3390次,線程2執行了3442次,兩個線程總共執行的次數與只創建單個線程測試時的循環次數相當。ESM6802為雙核CPU,在測試程序有兩個線程的情況下,在同一時刻兩個線程可以同時運行,所以總的循環次數大約是單個線程測試時的兩倍。
通過上面的測試可以看到,在多線程情況下,如果操作系統支持多核處理器,那么雙核CPU的運算能力將是單核CPU的兩倍。
-
WINDOWS
+關注
關注
4文章
3555瀏覽量
89049 -
嵌入式主板
+關注
關注
7文章
6086瀏覽量
35505
發布評論請先 登錄
相關推薦
評論