嵌入式开发中CPU GPU满载压测程序实现

  • CPU满载压测程序实现

CPU的满载可以通过Linux的top命令来查看,现在大部分的CPU都是多核的,例如:我们公司的就是使用四核的CPU

实现CPU的满载首先当然想到的是浮点运算了,于是就写了个计算P=3.1415926的程序,但CPU的满载度并未达到客户的要求,因为程序运行时会有偶尔的低于90%的情况,不符合要求

终于找到一种方法可以让四个核的CPU达到并保持在99%以上,SHA1与md5常用的校验和算法,而且非常简单,直接写个脚本就可以了

#!/bin/sh
sha1sum /dev/zero &
sha1sum /dev/zero &
sha1sum /dev/zero &
sha1sum /dev/zero &

这样就可以让CPU满载 

  • GPU满载压测程序实现

GPU的满载的最开始实现是通过安兔兔测试达到的,因为安兔兔测试3D时可以达到GPU满载,那我们就只执行安兔兔的3D测试循环来实现 

while [ 1 ];
do
  t=`expr $i % 5`
  if [ t -eq 0 ]; 
  then
    echo "tag start"
    input tap 1200 540
  fi
  score=`cat /sys/devices/platform/soc/mali.0/dvfs_usage`
  echo "GPU:"$score
  sleep 1
  i=$((i + 1))
done

后来想着直接通过OpenGLES语言编写程序来实现

在Android的OpenGL的测试例子中找到一个画3D的程序,稍微修改了下,的确可以达到GPU满载,而且比使用安兔兔更方便了,只需执行编译出的可执行程序就可以达到,不用测试前再安装安兔兔了,贴出其主要代码

int main(int argc, char *argv[])
{
    unsigned samples = 0;
    printf("usage: %s [samples]\n", argv[0]);
    if (argc == 2) {
        samples = atoi( argv[1] );
        printf("Multisample enabled: GL_SAMPLES = %u\n", samples);
    }

    WindowSurface windowSurface;
    if (!initGraphics(samples, windowSurface))
    {
        fprintf(stderr, "Graphics initialization failed.\n");
        return EXIT_FAILURE;
    }

    appInit();

    struct timeval timeTemp;
    //int frameCount = 0;
    gettimeofday(&timeTemp, NULL);
    double totalTime = timeTemp.tv_usec/1000000.0 + timeTemp.tv_sec;

    while (gAppAlive)
    {
        struct timeval timeNow;

        gettimeofday(&timeNow, NULL);
        appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000,
                sWindowWidth, sWindowHeight);//通过OpenGL调用GPU来画
        checkGLErrors();
        eglSwapBuffers(sEglDisplay, sEglSurface);//swap back/front framebuffer
        checkEGLErrors();
        //frameCount++;
    }

    gettimeofday(&timeTemp, NULL);

    appDeinit();
    deinitGraphics();

    totalTime = (timeTemp.tv_usec/1000000.0 + timeTemp.tv_sec) - totalTime;
    //printf("totalTime=%f s, frameCount=%d, %.2f fps\n",
            //totalTime, frameCount, frameCount/totalTime);

    return EXIT_SUCCESS;
}
// Called from the app framework.
/* The tick is current time in milliseconds, width and height
 * are the image dimensions to be rendered.
 */
void appRender(long tick, int width, int height)
{
    if (sStartTick == 0)
        sStartTick = tick;
    if (!gAppAlive)
        return;

    // Actual tick value is "blurred" a little bit.
    sTick = (sTick + tick - sStartTick) >> 1;

     //Terminate application after running through the demonstration once.
    if (sTick >= RUN_LENGTH)//本次循环时间达到后,重置全局变量接着去画
    {
        sStartTick = 0;
        sTick = 0;
        sCurrentCamTrack = 0;
        sCurrentCamTrackStartTick = 0;
        sNextCamTrackStartTick = 0x7fffffff;
    }

    // Prepare OpenGL ES for rendering of the frame.
    prepareFrame(width, height);

    // Update the camera position and set the lookat.
    camTrack();

    // Configure environment.
    configureLightAndMaterial();
....................
}

编译出的可执行文件为angeles,通过编写一个脚本就可以实现CPU/GPU的满载

#!/bin/sh

echo "start to run test"
sha1sum /dev/zero &
sha1sum /dev/zero &
sha1sum /dev/zero &
sha1sum /dev/zero &

for i in $(seq 1 4)
  do
    ./angeles &
  done
while [ 1 ];
do
  score=`cat /sys/devices/platform/soc/mali.0/dvfs_usage`
  echo "GPU:"$score
  sleep 1
done

 

你可能感兴趣的:(Android)