10 ms·
Go, Containers, and the Linux Scheduler
- ntonozzi 3y agoI've been bitten many times by the CFS scheduler while using containers and cgroups. What's the new scheduler? Has anyone here tried it in a production cluster? We're now going on two decades of wasted cores: https://people.ece.ubc.ca/sasha/papers/eurosys16-final29.pdf https://people.ece.ubc.ca/sasha/papers/eurosys16-final29.pdf.
- donaldihunter 3y agohttps://kernelnewbies.org/Linux_6.6#New_task_scheduler:_EEVDF https://kernelnewbies.org/Linux_6.6#New_task_scheduler:_EEVD...
- the8472 3y agoThe problem here isn't the scheduler. It's resource restrictions imposed by the container but the containerized process (Go) not checking the OS features used to do that when calculating the available amount of parallelism.
- dilyevsky 3y agoThis is subtly incorrect - as far as Docker is concerned CFS cgroup extension has several knobs to tune - cfs_quota_us, cfs_period_us (typical default is 100ms not a second) and shares. When you set shares you get weighted proportional scheduling (but only when there's contention). The former two enforce strict quota. Don't use Docker's --cpu flag and instead use --cpu-shares to avoid (mostly useless) quota enforcement. From Linux docs: - cpu.shares: The weight of each group living in the same hierarchy, that translates into the amount of CPU it is expected to get. Upon cgroup creation, each group gets assigned a default of 1024. The percentage of CPU assigned to the cgroup is the value of shares divided by the sum of all shares in all cgroups in the same level. - cpu.cfs_period_us: The duration in microseconds of each scheduler period, for bandwidth decisions. This defaults to 100000us or 100ms. Larger periods will improve throughput at the expense of latency, since the scheduler will be able to sustain a cpu-bound workload for longer. The opposite of true for smaller periods. Note that this only affects non-RT tasks that are scheduled by the CFS scheduler. - cpu.cfs_quota_us: The maximum time in microseconds during each cfs_period_us in for the current group will be allowed to run. For instance, if it is set to half of cpu_period_us, the cgroup will only be able to peak run for 50 % of the time. One should note that this represents aggregate time over all CPUs in the system. Therefore, in order to allow full usage of two CPUs, for instance, one should set this value to twice the value of cfs_period_us.
- Thaxll 3y agoPeople using Kubernetes don't tune or change those settings, it's up to the app to behave properly.
- dilyevsky 3y agoFalse. Kubernetes cpu request sets the shares, cpu limit sets the cfs quota
- Thaxll 3y agoYou said to change docker flags. Anyway your post is irrelevant, the goal is to let know the runtime about how many posix threads should it use. If you set request / limit to 1 core but you run on 64 cores node , then you runtime will see that which will bring performance down.
- dilyevsky 3y agoOriginal article is about docker. That’s the point of my comment - dont set cpu limit
- riv991 3y agoI intended it to be applicable to all containerised environments. Docker is just easiest on my local machine. I still believe it's best to set these variables regardless of cpu limits and/or cpu shares
- dilyevsky 3y agoAll you did is kneecapped your app to have lower performance so it fits under your arbitrary limit. Hardly what most people describe as “best” - only useful in small percentage of usecases (like reselling compute)
- 3y ago
- gregfurman 3y agoDiscovered this sometime last year in my previous role as a platform engineer managing our on-prem kubernetes cluster as well as the CI/CD pipeline infrastructure. Although I saw this dissonance between actual and assigned CPU causing issues, particularly CPU throttling, I struggled to find a scalable solution that would affect all Go deployments on the cluster. Getting all devs to include that autoprocs dependency was not exactly an option for hundreds of projects. Alternatively, setting all CPU request/limit to a whole number and then assigning that to a GOMAXPROCS environment variable in a k8s manifest was also clunky and infeasible. I ended up just using this GOMAXPROCS variable for some of our more highly multithreaded applications which yielded some improvements but I’ve yet to find a solution that is applicable to all deployments in a microservices architecture with a high variability of CPU requirements for each project.
- jeffbee 3y agoThere isn't one answer for this. Capping GOMAXPROCS may cause severe latency problems if your process gets a burst of traffic and has naive queueing. It's best really to set GOMAXPROCS to whatever the hardware offers regardless of your ideas about how much time the process will use on average.
- linuxftw 3y agoYou could define a mutating webhook to inject GOMAXPROCS into all pod containers.
- deleted 3y ago[deleted]
- hiroshi3110 3y agoHow about GKE and containerd?
- rickette 3y agoBesides GOMAXPROCS there's also GOMEMLIMIT in recent Go releases. You can use https://github.com/KimMachineGun/automemlimit https://github.com/KimMachineGun/automemlimit to automatically set this this limit, kinda like https://github.com/uber-go/automaxprocs https://github.com/uber-go/automaxprocs.
- ImJasonH 3y agoThanks for sharing this! And as a maintainer of ko[1], it was a pleasant surprised to see ko mentioned briefly, so that's for that too :) 1: https://ko.build https://ko.build
- dekhn 3y agoThe common problem I see across many languages is: applications detect machine cores by looking at /proc/cpuinfo. However, in a docker container (or other container technology), that file looks the same as the container host (listing all cores, regardless of how few have been assigned to the container). I wondered for a while if docker could make a fake /proc/cpuinfo that apps could parse that just listed "docker cpus" allocated to the job, but upon further reflection, that probably wouldn't work for many reasons.
- dharmab 3y agoPoint of clarification: Containers, when using quota based limits, can use all of the CPU cores on the host. They're limited in how much time they can spend using them. (There are exceptions, such as documented here: https://kubernetes.io/docs/tasks/administer-cluster/cpu-management-policies/ https://kubernetes.io/docs/tasks/administer-cluster/cpu-mana...)
- dekhn 3y agoMaybe I should be clearer: Let's say I have a 16 core host and I start a flask container with cpu=0.5 that forks and has a heavy post-fork initializer. flask/gunicorn will fork 16 processes (by reading /proc/cpuinfo and counting cores) all of which will try to share 0.5 cores worth of CPU power (maybe spread over many physical CPUs; I don't really care about that). I can solve this by passing a flag to my application; my complaint is more that apps shouldn't consult /proc/cpuinfo, but have another standard interface to ask "what should I set my max parallelism (NOT CONCURRENCY, ROB) so my worker threads get adequate CPU time so the framework doesn't time out on startup.
- status_quo69 3y agohttps://stackoverflow.com/questions/65551215/get-docker-cpu-memory-limit-inside-container https://stackoverflow.com/questions/65551215/get-docker-cpu-... Been a bit but I do believe that dotnet does this exact behavior. Sounds like gunicorn needs a pr to mimic, if they want to replicate this. https://github.com/dotnet/runtime/issues/8485 https://github.com/dotnet/runtime/issues/8485
- bruh2 3y agoAs someone not that familiar with Docker or Go, is this behavior intentional? Could the Go team make it aware of the CGroups limit? Do other runtimes behave similarly?
- eloisant 3y agoYes, I've experienced the same problem with the JVM (in Scala).
- yjftsjthsd-h 3y agoI'm fairly certain that that .net had to deal with it and Java had or still has a problem, I forget which. (Or did you mean runtimes like containerd?)
- richdougherty 3y agoSupported in Java 10 (and backported to Java 8) since 2018. Not sure about .NET. - "The JVM has been modified to be aware that it is running in a Docker container and will extract container specific configuration information instead of querying the operating system. The information being extracted is the number of CPUs and total memory that have been allocated to the container." https://www.oracle.com/java/technologies/javase/8u191-relnotes.html https://www.oracle.com/java/technologies/javase/8u191-relnot... - Here's a more detailed explanation and even a shared library that can be used to patch container unaware versions of Java. I wonder if the same could be done for Go? "LD_PRELOAD=/path/to/libproccount.so java <args>" https://stackoverflow.com/a/64271429 https://stackoverflow.com/a/64271429 https://gist.github.com/apangin/78d7e6f7402b1a5da0fa3abd93811f36 https://gist.github.com/apangin/78d7e6f7402b1a5da0fa3abd9381... - There are more recent changes to Java container awareness as well: https://developers.redhat.com/articles/2022/04/19/java-17-whats-new-openjdks-container-awareness# https://developers.redhat.com/articles/2022/04/19/java-17-wh...
- jpolidor 3y agoThen in Java, if you don't set the limits, it gets the CPU from the VM via Runtime.getRuntime().availableProcessors()... this method returns the number of CPUs of the VM or the value set as CPU Quota. Starting from Java 11 the -XX:+PreferContainerQuotaForCPUCount is by default true. For Java <= 10 the CPU count is equal to the CPU shares. That method then is used to calculate the GC threads, fork join pool size, compiler threads etc. The solution would be to set -XX:ActiveProcessorCount=X where X is ideally the CPU shares value but as we know shares can change over time, so you would change this value over time... Edit: or set -XX:-PreferContainerQuotaForCPUCount
- evntdrvn 3y agoI know that the .NET CLR team adjusted its behavior to address this scenario, fwiw!
- the8472 3y agoSo did OpenJDK and the Rust standard library.
- marcus_holmes 3y agoI still don't get the benefit of running Go binaries in containers. Totally get it for Rails, Python, etc, where the program needs a consistent set of supporting libraries. But Go doesn't need that. Especially now we can embed whole file systems into the actual binary. I've been looking at going in the other direction and using a Go binary with a unikernel; the machine just runs the binary and nothing else. I haven't got this working to my satisfaction yet - it works but there's still too much infrastructure, and deployment is still "fun". But I think this is way more interesting for Go deployments than using containers.
- tail_exchange 3y agoContainers can give you better isolation between the application and the host, as well as making horizontal scaling easier. It's also a must if you are using a system like Kubernetes. If you are running multiple applications in the same host, you also have control over resource limits.
- jeffbee 3y agoKubernetes requires a platform runtime that answers its requests, but there's no law enforcement agency that will prevent you from using a custom runtime that ignores the very existence of Linux control groups.
- tail_exchange 3y agoYes, that is true. Though if you are using Google's or Amazon's managed Kubernetes services, I think you need to use Docker.
- jen20 3y agoCertainly EKS is less managed than it appears, I’m quite confident that a node running something implementing the kubelet API convincingly would work. They changed recently (1.23 maybe) from Docker to containerd.
- 3y ago
- alilleybrinker 3y agoI feel like this isn't the first time I've read about issues with schedulers-in-schedulers, but I also can't find any immediate references on hand for other examples. Anyone know of any?
- WesternStar 3y agoI think this is a great article talking about a thorny point in Golang but boy do I wish I never read this article. I wish this article was never useful to anyone.
- otterley 3y agoThis sort of tuning isn't necessary if you use CPU reservations instead of limits, as you should: https://home.robusta.dev/blog/stop-using-cpu-limits https://home.robusta.dev/blog/stop-using-cpu-limits CPU reservations are limits, just implicit ones and declared as guarantees. So let the Go runtime use all the CPUs available, and let the Linux scheduler throttle according to your declared reservations if the CPU is contended for.
- yipbub 3y agoInteresting. This is not true for Memory, correct? The OOMKiller might get you. You also cannot achieve a QoS class of Guaranteed without both CPU and Memory limits, so the pod might be evicted at some point.
- dilyevsky 3y agoCorrect regarding memory - not true for memory because it's non-fungible unlike CPU shares > You also cannot achieve a QoS class of Guaranteed without both CPU and Memory limits, so the pod might be evicted at some point. Evicted due to node pressure - yes (but if all other pods also don't have limits it doesn't matter). For preemption QoS is not factored in the decision [0] [0] - https://kubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption/#interactions-of-pod-priority-and-qos https://kubernetes.io/docs/concepts/scheduling-eviction/pod-...
- iTokio 3y ago> Memory is different because it is non-compressible - once you give memory you can't take it away without killing the process
- irogers 3y agoThere are also GC techniques to make the pause shorter, for example, doing the work for the pause concurrently and then repeating it in the safepoint. The hope is that the concurrent work will turn the safepoint work into a simpler check that no work is necessary. Doubling the work may hurt GC throughput.
- goimonkeen 3y ago[flagged]
- perryizgr8 3y agoIsn't this a bug in the Go runtime and shouldn't they fix it? It looks like they are using the wrong metric to tune the internal scheduler.
- Ayushmishra23 3y ago[flagged]
- kevincox 3y agoThis is talking about containers but it seems that the problem is just whenever Go has access to less CPU time than it expects. Wouldn't the same thing happen when running Go on a system with another process that is using CPU? Or even just two Go programs at the same time?