【问题标题】:My C++ application runs slower in a docker container我的 C++ 应用程序在 docker 容器中运行速度较慢
【发布时间】:2020-06-06 00:00:10
【问题描述】:

我正在测试和分析我的一个应用程序,我将其作为 docker 容器运行。在我的测试过程中,我注意到应用程序在我的主机中直接执行时运行得更快,相对于 docker 容器。因此,为了找出这个问题的根本原因(例如使用卷导致性能下降),我编写了这个简单的示例应用程序:

test.cpp

#include <iostream>

void __attribute__ ((noinline)) task()
{
        for (std::size_t i {0}; i < 1000000000; ++i)
        {
                double a { static_cast<double>(rand())/RAND_MAX };
                double b { static_cast<double>(rand())/RAND_MAX };

                double c { a * b };

                c += 1;
        }

}

int main(int argc, char** argv)
{
        task();
}

我使用的编译器:

$ g++ -pg -O3 -o test test.cpp

运行生成的应用程序后,我从gprof获得了这个结果:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
100.70      0.36     0.36                             task()
  0.00      0.36     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z4taskv

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 2.76% of 0.36 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.36    0.00                 task() [1]
-----------------------------------------------
                0.00    0.00       1/1           __libc_csu_init [15]
[9]      0.0    0.00    0.00       1         _GLOBAL__sub_I__Z4taskv [9]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


Index by function name

   [9] _GLOBAL__sub_I__Z4taskv [1] task()

所以,task() 函数大约需要 0.36 秒。

现在,我想尝试在 docker 容器中运行相同的应用程序。由于我的主机操作系统是 ubuntu 18.04,我使用 ubuntu:18.04 作为基础镜像。我使用了这个 Dockerfile:

Dockerfile

FROM ubuntu:18.04

# Install g++ and gcc
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    build-essential \
    g++ \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Copy the test source file to /test/
RUN mkdir /test
COPY ./test.cpp /test/

# Compile the test application
WORKDIR /test
RUN g++ -pg -O3 -o test test.cpp

我使用以下方法构建图像:

$ docker image build -t docker-test .

然后运行它:

$ docker container run -ti --rm docker-test

在那里我运行了测试应用程序并从gprof获得了这个结果:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
100.70      0.58     0.58                             task()
  0.00      0.58     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z4taskv

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 1.71% of 0.58 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.58    0.00                 task() [1]
-----------------------------------------------
                0.00    0.00       1/1           __libc_csu_init [15]
[9]      0.0    0.00    0.00       1         _GLOBAL__sub_I__Z4taskv [9]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


Index by function name

   [9] _GLOBAL__sub_I__Z4taskv [1] task()

因此,同样的 task() 函数这次花费了大约 0.58 秒,几乎比在主机中运行慢 2 倍。

为什么应用程序在 docker 容器中运行较慢?难道我做错了什么?或者有没有办法提高性能?

【问题讨论】:

  • 您是否在两台计算机上使用相同的g++ 版本?我想知道这是否与rand() 有关(如果它使用dev/random 而不是dev/urandom - 或者平台的通过Docker 暴露的RNG 是否不是最佳的)。如果您删除 rand() 并改为执行一些昂贵的 IEEE-754 操作会发生什么?
  • 如果将二进制文件复制到容器中,而不是复制源代码并重新编译,会发生什么?
  • 2000000000 对rand 的调用仅在 0.36 秒内完成?好像很可疑啊!这意味着执行 5.6 G 调用/秒,这非常令人惊讶,因为 GCC rand 实现 should takes more than 3 cycles and is intrinsically sequential。因此,要么你有一个运行频率超过 16.7 GHz 的不切实际的处理器,要么编译器删除了 rand 调用,因为它们实际上并不需要,而你测量的可能是噪声!请参阅here 了解更多信息。

标签: performance docker


【解决方案1】:

谢谢大家的cmets。正如Jérôme Richard 提到的,我的测试结果确实很可疑。此外,我在接下来的几天多次运行相同的测试,得到了不同的结果,其中大多数的执行时间与在主机中获得的时间相同;所以我可能正在与同时运行的其他 docker 容器并行运行我的测试,这会消耗大量 CPU(当时我在同一主机上进行了其他繁重的测试)。

所以,我决定重新开始,这次我采用了一段我试图优化的应用程序的实际代码并重复我的测试。这是我这次使用的代码:

app.h

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <assert.h>
#include <numeric>
#include <mutex>

template<typename T>
class Filter {
public:
    Filter(std::size_t numberChannels);

    void process(const std::vector<T>& input, std::vector<T>& outData);
private:
    std::size_t          numCh;
        std::size_t          order;
        double               gain;
        std::vector<double>  a;
        std::vector<double>  b;
        std::size_t          currentBlockIndex;
        std::size_t          currentIndex;
        std::vector<double>  x;
        std::vector<double>  y;
};

template<typename T>
void writeVectToFile(const std::vector<T>& v, const std::string& fileName);

app.cpp

#include "app.h"

const std::size_t numSamples { 1000000 };

template<typename T>
Filter<T>::Filter(std::size_t numberChannels)
:
    numCh             { numberChannels      },
    order             { 4                   },
    gain              { 1                   },
    a                 { 1.0, -3.74145562, 5.25726624, -3.28776591,  0.77203984 },
    b                 { 5.28396689e-06, 2.11358676e-05, 3.17038014e-05, 2.11358676e-05, 5.28396689e-06 },
    currentBlockIndex { order               },
    currentIndex      { 0                   },
    x                 ( (order +1) * numCh  ),
    y                 ( (order +1) * numCh  )
{
}

template<typename T>
void __attribute__ ((noinline)) Filter<T>::process(const std::vector<T>& inputData, std::vector<T>& outputData)
{
    if (currentIndex == order)
        currentIndex = 0;
    else
        ++currentIndex;

    std::vector<double>::iterator           xCh    { x.begin() + currentIndex };
    std::vector<double>::iterator           yCh    { y.begin() + currentIndex };
    std::vector<double>::const_iterator     aIt    { a.begin()                };
    std::vector<double>::const_iterator     bIt    { b.begin()                };
    typename std::vector<T>::const_iterator dataIt { inputData.begin()        };

    for (std::size_t ch{0}; ch < numCh; ++ch, xCh += order + 1, yCh += order + 1)
    {
        *xCh = static_cast<double>(*dataIt++);
        *yCh= *bIt * *xCh;
    }

    std::size_t previousIndex { currentIndex };
    std::vector<double>::const_iterator xChPrev;
    std::vector<double>::const_iterator yChPrev;

    for (std::size_t t{1}; t < order + 1; ++t)
    {
        if (previousIndex == 0)
            previousIndex = order;
        else
            --previousIndex;

        yCh     = y.begin() + currentIndex;
        xChPrev = x.begin() + previousIndex;
        yChPrev = y.begin() + previousIndex;
        ++aIt;
        ++bIt;

        for (std::size_t ch{0}; ch < numCh; ++ch, yCh += order + 1, xChPrev += order + 1, yChPrev += order + 1)
        {
            *yCh += *bIt * *xChPrev - *aIt * *yChPrev;
        }
    }

    yCh = y.begin() + currentIndex;
    typename std::vector<T>::iterator outIt(outputData.begin());
    for (std::size_t ch{0}; ch < numCh; ++ch, yCh += order + 1)
    {
        *yCh /= a[0];
        *outIt++ = static_cast<T>(*yCh * gain);
    }
}

template<typename T>
void writeVectToFile(const std::vector<T>& v, const std::string& fileName)
{
    std::ofstream outFile(fileName);
    for (const auto &e : v) outFile << e << "\n";
}

int main(int argc, char* argv[])
{
    const std::size_t numberChannels { 2000 };

    std::vector<int32_t> inputData  ( numberChannels );
    std::vector<int32_t> outputData ( numberChannels );
    std::vector<int32_t> sampleIn   ( numSamples     );
    std::vector<int32_t> sampleOut  ( numSamples     );

    // Create filter object
    Filter<int32_t> f(numberChannels);

    // Send 'numSamples' random values through the filter
    for(std::size_t i {0}; i < numSamples; ++i)
    {
        // Generate random samples
            for (auto &d: inputData)
            d = static_cast<int32_t>( (static_cast<double>(rand())/RAND_MAX) * (2147483647.0) );

        f.process(inputData, outputData);

        // Get the first input and output value
        // They will be use to validate the result
        sampleIn[i]  = inputData[0];
        sampleOut[i] = outputData[0];
    }

    // Write the result to disk
    writeVectToFile( sampleIn,  "x.dat" );
    writeVectToFile( sampleOut, "y.dat" );
}

我用这个 Makefile 编译:

生成文件

CXX = g++
CXXFLAGS = -Wall -g -O3 -pg

%.o: %.cpp
        $(CXX) -c -o $@ $< $(CXXFLAGS)

app: app.o
        $(CXX) -o $@ $^ $(CXXFLAGS)

clean:
        rm -rf app app.o *.dat gmon.out

得到了这个结果:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
100.11     19.81    19.81                             Filter<int>::process(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&)
  0.05     19.82     0.01                             void writeVectToFile<int>(std::vector<int, std::allocator<int> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
  0.00     19.82     0.00        1     0.00     0.00  _GLOBAL__sub_I_main

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.05% of 19.82 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     99.9   19.81    0.00                 Filter<int>::process(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&) [1]
-----------------------------------------------
                                                 <spontaneous>
[2]      0.1    0.01    0.00                 void writeVectToFile<int>(std::vector<int, std::allocator<int> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) [2]
-----------------------------------------------
                0.00    0.00       1/1           __libc_csu_init [19]
[10]     0.0    0.00    0.00       1         _GLOBAL__sub_I_main [10]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


Index by function name

  [10] _GLOBAL__sub_I_main (app.cpp) [2] void writeVectToFile<int>(std::vector<int, std::allocator<int> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) [1] Filter<int>::process(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&)

大约 20 秒调用方法 Filter&lt;T&gt;::process 1e6 次。

然后我用(这次我还复制了主机中生成的二进制文件)创建了一个docker镜像:

Dockerfile

FROM ubuntu:18.04

# Install g++ and gcc
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    build-essential \
    gcc \
    g++ \
    && rm -rf /var/lib/apt/lists/*

# Copy the test source file to /test/
RUN mkdir /test
COPY * /test/

# Copy the binary used for testing in the host
RUN mkdir /test-host
COPY ./app /test-host/app

# Compile the test application
WORKDIR /test
RUN make clean && make

我使用以下方法构建图像:

$ docker image build -t docker-test --no-cache .

然后运行:

docker container run -ti --rm docker-test

我得到了:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
100.11     21.71    21.71                             Filter<int>::process(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&)
  0.05     21.72     0.01                             void writeVectToFile<int>(std::vector<int, std::allocator<int> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
  0.00     21.72     0.00        1     0.00     0.00  _GLOBAL__sub_I_main

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.05% of 21.72 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0   21.71    0.00                 Filter<int>::process(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&) [1]
-----------------------------------------------
                                                 <spontaneous>
[2]      0.0    0.01    0.00                 void writeVectToFile<int>(std::vector<int, std::allocator<int> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) [2]
-----------------------------------------------
                0.00    0.00       1/1           __libc_csu_init [19]
[10]     0.0    0.00    0.00       1         _GLOBAL__sub_I_main [10]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Copyright (C) 2012-2018 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


Index by function name

  [10] _GLOBAL__sub_I_main (app.cpp) [2] void writeVectToFile<int>(std::vector<int, std::allocator<int> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) [1] Filter<int>::process(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&)

我使用在主机中编译的二进制文件和在 docker 映像中编译的二进制文件都获得了几乎相同的结果。

这次我获得了在主机中运行与在容器中运行几乎相同的结果(两种情况下大约 20 秒)。因此,在容器中运行时,我看不到任何性能影响。

我主机中的 g++ 编译器是:

$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我容器中的 g++ 编译器是:

# g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我正在使用这个 docker 版本:

$ docker version
Client: Docker Engine - Community
 Version:           19.03.7
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        7141c199a2
 Built:             Wed Mar  4 01:22:36 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.7
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       7141c199a2
  Built:            Wed Mar  4 01:21:08 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 2020-01-02
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    相关资源
    最近更新 更多