Support GC Freezing to improve latency & throughput#9241
Support GC Freezing to improve latency & throughput#9241hnyls2002 merged 7 commits intosgl-project:mainfrom
Conversation
Rebase from upstream
There was a problem hiding this comment.
Summary of Changes
Hello @chanh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a significant enhancement to the server's performance by implementing a garbage collection (GC) freezing mechanism. The primary goal is to eliminate latency spikes and improve throughput by preventing the GC from performing costly full scans on stable, long-lived objects. Users can now warm up the server with typical requests and then activate the GC freeze, ensuring that only newly created, short-lived objects are subject to collection. This change provides a powerful tool for optimizing server responsiveness in production environments.
Highlights
- Core GC Freezing API: A new
freeze_gcmethod has been added to the core engine, allowing the server to explicitly freeze its garbage collection process. This is intended to be called after a warmup period to prevent the GC from collecting long-lived objects, thereby reducing latency spikes. - External API Endpoint: An HTTP endpoint
/freeze_gchas been exposed, providing a convenient way to trigger the GC freezing functionality externally, integrating it into existing server management workflows. - Distributed GC Management: The GC freezing mechanism is now distributed across multiple processes (tokenizer, scheduler, detokenizer managers) using a new communication protocol (
FreezeGCReq,FreezeGCReqOutput) to ensure consistent GC state across the system. - Configurable GC Warnings: A new configuration option,
gc_warning_threshold_secs, has been introduced. This allows users to set a threshold for GC cycle duration, triggering warnings in logs if GC takes too long, helping to identify and debug performance issues. - Enhanced GC Utilities: New utility functions (
gc_object_counts,configure_gc_warning,freeze_gc) have been added to provide granular control and monitoring over Python's garbage collection, including the ability to freeze the collector and log object counts.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces a feature to freeze the garbage collector, aiming to reduce latency and improve throughput. The changes are well-integrated into the existing architecture, with new API endpoints and logic distributed across the relevant manager processes. I've identified a critical issue in the implementation of the GC warning mechanism that prevents it from functioning as intended. My review includes a specific code suggestion to fix this.
|
cc @hnyls2002 for this |
| type=float, | ||
| default=ServerArgs.gc_warning_threshold_secs, | ||
| help="The threshold for long GC warning. If a GC takes longer than this, a warning will be logged. Set to 0 to disable.", | ||
| ) |
There was a problem hiding this comment.
I suggest using a negative number to disable it.
|
@chanh Please fix the lint use |
Co-authored-by: Chanh Nguyen <cnguyen@linkedin.com> Co-authored-by: Liangsheng Yin <hnyls2002@gmail.com>
Motivation
We are seeing the server stall for 100ms - 300ms at a time every 1.5secs or so. This is very very bad for example when we are trying to maintain a 500ms P99 latency.
The culprit was the garbage collector doing a gen2 cycle detection through a large number of objects.
In general, it is usually helpful to start the server and warm it up with real requests to initialize many of the long-lived objects that do not need to be garbage collected. After sufficient warmup, the user can call this freeze_gc API so that all objects created before this point are considered out of scope for garbage collection. This greatly reduces both the need to do gen2 GC and the time it takes when it triggers.
Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist