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的两倍。
全部0条评论
快来发表一下你的评论吧 !