Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obmalloc: stop simple arena thrashing #81438

Closed
tim-one opened this issue Jun 12, 2019 · 2 comments
Closed

obmalloc: stop simple arena thrashing #81438

tim-one opened this issue Jun 12, 2019 · 2 comments
Assignees
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@tim-one
Copy link
Member

tim-one commented Jun 12, 2019

BPO 37257
Nosy @tim-one, @nascheme, @methane
PRs
  • bpo-37257: obmalloc: stop simple arena thrashing #14039
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/tim-one'
    closed_at = <Date 2019-06-13.03:46:07.787>
    created_at = <Date 2019-06-12.20:55:31.210>
    labels = ['interpreter-core', '3.9', 'performance']
    title = 'obmalloc:  stop simple arena thrashing'
    updated_at = <Date 2019-06-13.03:46:07.786>
    user = 'https://github.com/tim-one'

    bugs.python.org fields:

    activity = <Date 2019-06-13.03:46:07.786>
    actor = 'tim.peters'
    assignee = 'tim.peters'
    closed = True
    closed_date = <Date 2019-06-13.03:46:07.787>
    closer = 'tim.peters'
    components = ['Interpreter Core']
    creation = <Date 2019-06-12.20:55:31.210>
    creator = 'tim.peters'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37257
    keywords = ['patch']
    message_count = 2.0
    messages = ['345407', '345453']
    nosy_count = 3.0
    nosy_names = ['tim.peters', 'nascheme', 'methane']
    pr_nums = ['14039']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue37257'
    versions = ['Python 3.9']

    @tim-one
    Copy link
    Member Author

    tim-one commented Jun 12, 2019

    Scenario: all arenas are fully used. A program then runs a loop like:

    while whatever:
        p = malloc(n)
        ...
        free(p)
        
    At the top, a new arena has to be created, and a single object is taken out of a single pool.  At the bottom, that object is freed, so the arena is empty again, and so is returned to the system.  Which cycle continues so long as the loop runs.  Very expensive.

    This is "something like" what happens in practice, and has been reported anecdotally for years, but I've never been clear on _exactly_ what programs were doing in such cases. Neil S pointed out this recent report here:

    https://mail.python.org/pipermail/python-dev/2019-February/156529.html

    Which may or may not be relevant. Inada?

    The "fix" proposed there:

    • if (nf == ao->ntotalpools) {
      + if (nf == ao->ntotalpools && ao != usable_arenas) {

    doesn't appeal to me, because it can lead to states where obmalloc never returns empty arenas, no matter how many pile up. For example, picture a thousand arenas each with just one used pool. The head of the arena list becomes free, so is left alone, but moves to the end of the list (which is sorted by number of free pools). Then the new head of the list becomes free, and ditto. On & on. We're left with a list containing a thousand wholly unused arenas.

    So I suggest instead:

    + if (nf == ao->ntotalpools && ao->nextarena != NULL) {

    That is, instead of exempting the head of the list, exempt the tail of the list. In the example above, the first 999 arenas are returned to the system, but the last one remains in the list for reuse. In general, the change would allow for at most one empty arena in the list.

    We can't in general predict the future, but this would be enough to stop the thrashing in the specific scenario given at the top, with no apparent serious failure modes (potentially "wasting" one arena is minor).

    @tim-one tim-one added the 3.9 only security fixes label Jun 12, 2019
    @tim-one tim-one self-assigned this Jun 12, 2019
    @tim-one tim-one added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Jun 12, 2019
    @tim-one
    Copy link
    Member Author

    tim-one commented Jun 13, 2019

    New changeset d1c85a2 by Tim Peters in branch 'master':
    bpo-37257: obmalloc: stop simple arena thrashing (bpo-14039)
    d1c85a2

    @tim-one tim-one closed this as completed Jun 13, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant