#!/usr/bin/python
"""
Copyright 2014 Domas Mituzas, Facebook

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

# This script deals with buddy allocation starving
# on larger page allocations.
# It detects if 16k+ pages are less than 10% of
# available memory and recompacts via procfs
#

import os


def buddy_zones():
    """Returns buddy zone information"""

    for line in open("/proc/buddyinfo"):
        zone = {}
        line = line.strip().split()
        zone["type"] = line[3]

        k = os.sysconf("SC_PAGESIZE") / 1024
        for count in line[4:]:
            zone[k] = int(count)
            k *= 2
        yield zone


def total_size(zone, min=0, max=1048576):
    """Calculates total size in kilobytes per zone
    of specied slab sizes (min and max are in k)
    """
    total = 0
    for k, v in zone.items():
        if k == "type":
            continue
        if k < min or k > max:
            continue
        total += (k * v)

    return total / 4


def maybe_compact():
    need_compaction = False
    for z in buddy_zones():
        if z["type"] != "Normal":
            continue
        if total_size(z, 4, 8) / total_size(z, 16) >= 10:
            need_compaction = True

    if need_compaction:
        try:
            open("/proc/sys/vm/compact_memory", "w").write("1")
        except IOError:
            pass


if __name__ == "__main__":
    maybe_compact()