ARM上的 Windows 10 IoT 企业版支持仿真 x86 应用程序,而 ARM上的 Windows 11 IoT 企业版则支持仿真 x86 和 x64 应用程序。英创推出的名片尺寸ARM64工控主板ESM8400,可预装正版Windows 10 IoT企业版操作系统,x86程序可无需修改而直接在ESM8400上运行。
下会将编写一个小程序,分别构建成x86和ARM64格式来测试其运行效率。所设计的测试程序代码如下,其中的TestSmp函数有两个输入参数,第一参数表示要创建测试线程的数量,第二个参数为所创建线程的运行时长。cbTestSmp是被创建的测试线程,测试线程主要是在一个while循环中,反复读取内存变量然后与预设值进行比较,在运行设定的时间后自动退出循环,其中的threadParam->loops变量会记录下while循环总共执行的次数。
typedef struct _SMP_THREAD_PARAM
{UINT32 durationMs;UINT32 cpuId;UINT64 loops;BOOL bSetAffinity;UINT32 sandBoxSize;LPVOID sandBoxStart;
}SMP_THREAD_PARAM, * PSMP_THREAD_PARAM;DWORD WINAPI cbTestSmp(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->cpuId, threadParam->durationMs);// Write to sandboxfor (UINT32 i = 0; i < threadParam->sandBoxSize; i++){buffer[i] = (UINT8)(i);// * (UINT32)threadParam->loops);}while ((GetTickCount() - tStart) < threadParam->durationMs){// Read back from sandboxfor (UINT32 i = 0; i < threadParam->sandBoxSize; i++){//if (buffer[i] != (UINT8)(i * (UINT32)threadParam->loops) )if (buffer[i] != (UINT8)(i))// * (UINT32)threadParam->loops) ){wprintf(L"Thread %d : error at byte %d for loop %I64d !!\r\n",threadParam->cpuId, i, threadParam->loops);}}threadParam->loops++;}wprintf(L"Thread %d : terminating\r\n", threadParam->cpuId);return 0;
}void TestSmp(UINT32 nCpus, UINT32 durationMs)
{UINT32 i;PSMP_THREAD_PARAM threadParams;HANDLE* threadHandles;UINT64 totalLoops = 0;UINT32 sandBoxSize = 1024 * 128; // 128 kBHANDLE h_array[1];threadParams = (PSMP_THREAD_PARAM)malloc(nCpus * sizeof(SMP_THREAD_PARAM));if (threadParams == NULL){wprintf(L"Failed allocating thread params !\r\n");return;}threadHandles = (HANDLE*)malloc(nCpus * sizeof(HANDLE));if (threadHandles == NULL){wprintf(L"Failed allocating thread handles !\r\n");return;}for (i = 0; i < nCpus; i++){threadParams[i].bSetAffinity = TRUE;threadParams[i].cpuId = 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 < nCpus; 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);
}
将上述代码分别编译构建成x86格式和ARM64模式,设置while循环执行10000ms,在ESM8400上的测试结果如下:
ESM8400 Win10 ARM工控主板运行x86和ARM64程序效率对比
可以看到相同的代码,构建成本机ARM64格式的运行效率是x86格式的2.2倍以上。
基于微软系统以及其开发工具良好的兼容性,我很容易做了另一个对比实验,将上述代码不经修改直接在VS2008中编译成WEC7应用程序,在英创的几款WEC7工控主板上做了同样的测试,测试结果如下:
ESM3354是英创10年前推出的第一款预装WEC7的工控主板,主CPU采用了TI的单核Cortex-A8芯片——AM3354,ESM3354目前仍在批量供货。而安装Windows 10 IoT的ESM8400工控主板,主CPU为NXP的i.MX8M Plus四核Cortex-A53,与10年前的ESM3354相比,ESM8400的性能有超过10倍的提升。
ARM上的 Windows IoT 企业版可以让习惯使用 x86/x64 的设备开发人员快速进行软件开发,大多数适用于 Windows IoT 企业版的文档都适用于 ARM64 和 x86/x64。通过仿真技术,ARM上的 Windows IoT可按原样运行x86/x64程序而无需修改,而直接构建本机ARM64应用程序能获得最佳的性能、响应能力和能耗。