【发布时间】: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 调用/秒,这非常令人惊讶,因为 GCCrand实现 should takes more than 3 cycles and is intrinsically sequential。因此,要么你有一个运行频率超过 16.7 GHz 的不切实际的处理器,要么编译器删除了 rand 调用,因为它们实际上并不需要,而你测量的可能是噪声!请参阅here 了解更多信息。
标签: performance docker