在unix下的muticore环境中运行gzip on sigle core

时间:2016-04-17 16:55:32

标签: gzip cpu core

我要求在多核cpu环境中只使用单核来测试gzip性能(在这种情况下不确定gzip的默认设置是什么)。需要帮助找出在单核中执行gzip压缩的命令。

由于

1 个答案:

答案 0 :(得分:0)

gzip默认是单线程的,所以实际上它看起来像是在一个核心上运行,即它可能在几个物理核心上运行,但它不会并行。

如果你绝对必须在一个核心上运行而你在linux上运行,你就会设置与特定核心的亲和力。

http://man7.org/linux/man-pages/man2/sched_setaffinity.2.html

这是我从手册页获得的代码。

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                           } while (0)

int
main(int argc, char *argv[])
{
    cpu_set_t set;
    int parentCPU, childCPU;
    int nloops, j;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s parent-cpu child-cpu num-loops\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    parentCPU = atoi(argv[1]);
    childCPU = atoi(argv[2]);
    nloops = atoi(argv[3]);

    CPU_ZERO(&set);

    switch (fork()) {
    case -1:            /* Error */
        errExit("fork");

    case 0:             /* Child */
        CPU_SET(childCPU, &set);

        if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
            errExit("sched_setaffinity");

        for (j = 0; j < nloops; j++)
            getppid();

        exit(EXIT_SUCCESS);

    default:            /* Parent */
        CPU_SET(parentCPU, &set);

        if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
            errExit("sched_setaffinity");

        for (j = 0; j < nloops; j++)
            getppid();

        wait(NULL);     /* Wait for child to terminate */
        exit(EXIT_SUCCESS);
    }
}

如果您需要在没有内核中断的情况下进行测试,则需要为此编写内核模块。