ESM6802是英創(chuàng)公司推出的基于Freescale i.MX6DL雙核處理器(ARM Cortex-A9,主頻1GHz)的高性能工控主板,預裝正版Windows Embedded Compact 7(WEC7)嵌入式操作系統(tǒng),WEC7一個最重要的特性就是對多核處理器的支持(Symmetric Multi-Processing(SMP)),下面將通過應用程序來測試在單核和多核情況下系統(tǒng)的執(zhí)行情況,為了更直觀的比較,同時參與測試的還有ESM3354,ESM3354是基于TI Coertex-A8處理器的工控主板,CPU主頻1GHz,同樣預裝WEC7操作系統(tǒng)。
所設計的測試程序代碼如下,其中的TestSmp函數(shù)有兩個輸入參數(shù),第一參數(shù)表示要創(chuàng)建測試線程的數(shù)量,第二個參數(shù)為所創(chuàng)建線程的運行時長。cbTestSmp是被創(chuàng)建的測試線程,測試線程主要是在一個while循環(huán)中,反復讀取內(nèi)存變量然后與預設值進行比較,在運行設定的時間后自動退出循環(huán),其中的threadParam->loops變量會記錄下while循環(huán)總共執(zhí)行的次數(shù)。
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循環(huán)的執(zhí)行時間均為10000ms,測試結果如下:
1、創(chuàng)建單個線程
測試主板與線程 | ESM3354(1GHz單核 Cortex-A8) | ESM6802(1GHz雙核Cortex-A9) |
循環(huán)次數(shù) | 6791 | 7493 |
當測試程序只創(chuàng)建一個測試線程時,ESM3354的while循環(huán)執(zhí)行了6791次,ESM6802執(zhí)行7493次,雖然ESM6802為雙核處理器,但由于程序只有一個線程,即同一時刻只有一個線程在運行,所以在相同的時間內(nèi),循環(huán)的次數(shù)僅略多于ESM3354。由于ESM3354和ESM6802的CPU主頻同樣都是1GHz,所以可以認為ESM6802多出的循環(huán)次數(shù)也就是Cortex-A8與Cortex-A9在代碼執(zhí)行效率上的差別。
2、創(chuàng)建兩個線程
測試主板與線程 | ESM3354(1GHz單核 Cortex-A8) | ESM6802(1GHz雙核Cortex-A9) |
線程1循環(huán)次數(shù) | 3390 | 7438 |
線程2循環(huán)次數(shù) | 3442 | 7452 |
總循環(huán)次數(shù) | 6832 | 14890 |
當測試程序創(chuàng)建了兩個線程時,ESM3354會將CPU資源大約平均的分配給兩個線程,如上表中線程1執(zhí)行了3390次,線程2執(zhí)行了3442次,兩個線程總共執(zhí)行的次數(shù)與只創(chuàng)建單個線程測試時的循環(huán)次數(shù)相當。ESM6802為雙核CPU,在測試程序有兩個線程的情況下,在同一時刻兩個線程可以同時運行,所以總的循環(huán)次數(shù)大約是單個線程測試時的兩倍。
通過上面的測試可以看到,在多線程情況下,如果操作系統(tǒng)支持多核處理器,那么雙核CPU的運算能力將是單核CPU的兩倍。
-
WINDOWS
+關注
關注
4文章
3551瀏覽量
88922 -
嵌入式主板
+關注
關注
7文章
6085瀏覽量
35449
發(fā)布評論請先 登錄
相關推薦
評論