Support - investigate CPU needed by Swarm health checks
I have created a couple of c# functions, very simple ones (get input, process, write output and exit). They work fine. However, today I notice that using docker stats there's some minor CPU usage. Compared to 0% from sample functions. I'm sure no one is using the functions, because this is my development environment. The functions in question are the ones with prefix bot-
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
7e1ff9b96774 hello.1.r433y39yjxgvn66whlbsgcxlh 1.86% 5.215MiB / 7.746GiB 0.07% 8.96kB / 1.49kB 50.4MB / 0B 9
c8d9b3b66cec func_queue-worker.1.m1sql214cek57z2vfnp04vuqh 0.00% 2.484MiB / 50MiB 4.97% 143kB / 38.5kB 262kB / 0B 8
eaa415da59a9 figlet.1.pk6p8vr8pbme5sn8c2vtz8kax 0.00% 2.219MiB / 7.746GiB 0.03% 86.3kB / 0B 6.96MB / 0B 4
947a4aaaeb7d bot-greeter.1.p1d9wqs7xi05klgn1v8qtrqmt 0.80% 4.797MiB / 7.746GiB 0.06% 86.3kB / 0B 262kB / 0B 12
555fb6f76047 bot-jira-comment.1.p51rsxj8bapx4c6p7e4r4d9cn 1.86% 4.137MiB / 7.746GiB 0.05% 86.3kB / 0B 5.3MB / 0B 6
9c9302b8173b bot-revision-process.1.ps6zntdcccw8xlnb0t8avmhrm 1.80% 3.539MiB / 7.746GiB 0.04% 86.3kB / 0B 11.5MB / 0B 6
9878bca7a9bd bot-revision-notice.1.nvj90odq0nzvdkj45rbww1gjm 1.95% 3.453MiB / 7.746GiB 0.04% 86.3kB / 0B 418kB / 0B 7
8c1a62968b50 func_prometheus.1.twvzuwuufti29e027jnfi4zgp 0.14% 48.54MiB / 500MiB 9.71% 1.96MB / 369kB 54.6MB / 22.5MB 25
e95e8e9bfb3b func_alertmanager.1.udi75oc4gcm0d1wg43iqhm61q 0.09% 9.703MiB / 50MiB 19.41% 86.9kB / 680B 23.4MB / 0B 20
4db655296659 func_gateway.1.lzy8gscku66477r4chnm2zri1 0.40% 9.594MiB / 7.746GiB 0.12% 2.31MB / 2.58MB 13.1MB / 0B 19
01a13493f5b9 func_faas-swarm.1.jkmogd5j22rteylqiemq8p9p9 0.34% 8.418MiB / 7.746GiB 0.11% 641kB / 1.75MB 10.9MB / 0B 15
b3b852e95dda func_nats.1.g716jefdoefdvqlz2djovuhh9 0.03% 4.879MiB / 125MiB 3.90% 163kB / 115kB 16.3MB / 0B 18
The very first, "hello", is the sample code as is from the template. Still uses some CPU while not in use.
Expected Behaviour
Functions that are not in use, should report 0% CPU usage.
Current Behaviour
Functions created using the c# template seem to have some minor CPU usage even when functions are not being consumed.
Steps to Reproduce (for bugs)
- Create new function
faas new hello --lang=csharp - Build and deploy
faas -f hello.yml buildfaas -f hello.yml deploy - Execute the function at least once via web UI or otherwise
- Run
docker statsand see function has constant CPU usage
Notice that this seems to be happening only with c# functions. So maybe is something related to the underlying handler of the functions.
Context
This could be problematic when deploying large number of functions. If functions that are not being executed constantly consume CPU, this will eventually consume the CPU resources of the host.
Your Environment
- Docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:17:20 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:15:30 2018
OS/Arch: linux/amd64
Experimental: false
-
Using Docker swarm
-
Operating System: Linux Ubuntu 16.04
Feel free to look at the templates repo to see if you can find more info https://github.com/openfaas/templates
The CPU stats appear to be minimal, perhaps node exporter or cadvisor would give better measurements?
I've also pinged @burtonr to review this issue
Derek add label: support
Derek set title: Support C# Functions using less than 2% CPU constantly
Derek set title: Support - C# Functions using less than 2% CPU constantly
That's interesting... I wouldn't expect a different language template to behave any differently. Especially that the C# function in the templates in merely a console application....
I'll take a look into this later this afternoon and get back to you with what I find
@alexellis I reviewed the code of the template Program.cs and it's pretty straight forward. Read input, instantiate function, execute. So maybe the issue is somewhere else? the fwatchdog perhaps? (not sure what it does but I see that's the entry point command of all images)
The fwatchdog is the piece that listens to the gateway and passes the request to the function via stdin. This is part of all OpenFaaS functions, so I'd expect if there were a problem there, it would be affecting all functions...
One way to diagnose this would be to exec into a container and run top
I have tested the behavior - the cause is the healthcheck. After commenting out the line from the dockerfile the function using 0%.
That is odd...because it seems to be the same healthcheck as other images, but the others are 0% usage (at least figlet)
OK I rebuild the images without the healthcheck and now in my server the CPU usage has been reduced by about 30% (I had several functions deployed)... I'll keep it like that until there's a solution why health check for c# will take CPU but not for others.
The healthcheck should not be disabled, it serves a purpose. I wouldn't be surprised to see that the health check consumes some CPU, but not constantly. You could try fine-tuning the interval at which it runs.
The CSharp / .NET image uses a different base image (Debian-based vs Alpine for the others) - this difference may affect the characteristics of the health check in the CSharp example.
We're still on .NET Core 2.0 and are planning to move up to 2.1 (@burtonr I think may have a PR in testing for this) - it may make a difference.
Try adjusting the 1s interval to something like 5s?
https://github.com/openfaas/templates/blob/master/template/csharp/Dockerfile#L45
The plot thickens... so I changed it to 5s, and sure enough, it's 0% until it's time to do the health check. So every 5 seconds CPU usage goes to ~2%.
But if I enter the container and execute the check manually, there's barely CPU usage. It's somehow the way Docker calls the function on that image.
CPU usage goes to 2%:
docker exec <container> [ -e /tmp/.lock ] && echo $?
CPU usage goes to 0.01% :
docker exec -ti <container> bash
[ -e /tmp/.lock ] && echo $?
Weird to say the least. I don't know enough about the internals of how Docker does things to have an idea.
What do you know... the function I was comparing to, figlet, doesn't have a health check. No wonder.
Now that I have installed sentimentanalysis and nodeinfo (from the store), they too show a ~2% constant CPU usage. So it's not particular to c#.
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
4dc602436215 sentimentanalysis.1.nr6m8qr5byre0d0ckom6n9khq 1.85% 7.578MiB / 7.746GiB 0.10% 14.7kB / 2.16kB 44.3MB / 0B 7
9ab13e06abd3 nodeinfo.1.q745urhi0ijpg49387m86onra 1.36% 5.047MiB / 7.746GiB 0.06% 14.3kB / 523B 40.8MB / 0B 12
5c1025087199 bot-magic-ball.1.gkb2qx00s0yvo6ti84ksdus8n 0.00% 3.242MiB / 7.746GiB 0.04% 174kB / 0B 26.1MB / 0B 6
4f0f04283f88 func_queue-worker.1.x49ta2xy47cgoxaknj1io4i8a 0.00% 1.562MiB / 50MiB 3.12% 907kB / 242kB 21.8MB / 0B 8
50b66f2cb143 func_gateway.1.5k6nuplwiu0lgrwz35dibaqf9 0.02% 8.848MiB / 7.746GiB 0.11% 15.6MB / 20.1MB 103MB / 0B 20
e603e580fd87 func_prometheus.1.wt2kfvbapmmzl223ktzo4bm33 0.71% 43.7MiB / 500MiB 8.74% 13.6MB / 2.78MB 364MB / 167MB 28
5991838aba41 func_nats.1.hzwgf3ztish9o6xz7g0hq2442 0.03% 4.902MiB / 125MiB 3.92% 1.04MB / 727kB 103MB / 0B 19
ab807e77a282 func_faas-swarm.1.23yexwd0gjrx8ggekgyf77tee 0.02% 7.824MiB / 7.746GiB 0.10% 5.58MB / 10.8MB 88.7MB / 0B 20
5ba11eecd713 func_alertmanager.1.9n42nmnyzux7btmuod7wssuym 0.07% 4.84MiB / 50MiB 9.68% 551kB / 680B 131MB / 0B 23
eaa415da59a9 figlet.1.pk6p8vr8pbme5sn8c2vtz8kax 0.00% 3.051MiB / 7.746GiB 0.04% 671kB / 3.25kB 24.3MB / 0B 7
So maybe every 1s is a very aggressive health check?
This seems to have a minimal impact on the environment overall, but I would be OK with upping the interval to something higher. Would you like to raise a PR?
Yes. I'll do some preliminary experimentation to see what's the actual impact once deploying a lot of functions.
This seems to be more significant on an incorrectly configured Swarm cluster with a single node with a single vCPU and very little RAM. I saw one report where someone was doing this on a single SoC ARM board (again - not a production cluster)
For Docker Swarm health checks are a necessary part of the infrastructure. You can try tuning your checks to run less frequently, but you will also incur a slower readiness when functions scale up.
In Kubernetes this is configurable - see the helm chart faas-netes for an example.
The second report of this I heard was from a VM with a single vCPU and 2GB RAM with +/ 15 functions. This is not a production-grade deployment target. Nobody would dream of running a whole Kubernetes cluster in a VM with a single vCPU and 2GB RAM.
I'm going to close the issue for now. If someone else lands here with a proper cluster configuration for production and is noticing a negative effect of Swarm health checks, then please raise an issue on the Moby repo.
On Swarm health checks run at regular intervals and these are tasks which run on a CPU thus increasing the average CPU consumption. The same is with Kubernetes - although configured differently.
Health checks are required for safe scaling up and down of functions. If 0 % CPU is desired across all functions then scaling them to zero when not in use or when idle may be the best path for users.
See also: Zero scale docs