<commit_before>import os
from multiprocessing import cpu_count

_cpu_count = cpu_count()


if hasattr(os, 'getloadavg'):
    def load_fair():
        try:
            load = os.getloadavg()[0] / _cpu_count
        except OSError:  # as of May 2016, Windows' Linux subsystem throws OSError on getloadavg
            load = -1
        return 'load', load
elif os.name != 'nt':
    # There exist some Unix platforms (like Android) which don't
    # have `getloadavg` implemented, but obviously aren't Windows
    # so going down the below branch is worthless. There's not much
    # we can do on these systems than report clearly that we can't
    # determine the load.
    def load_fair():
        return 'load', -1
else:  # pragma: no cover
    from dmoj.utils.winperfmon import PerformanceCounter
    from threading import Thread
    from collections import deque
    from time import sleep

    class SystemLoadThread(Thread):
        def __init__(self):
            super(SystemLoadThread, self).__init__()
            self.daemon = True
            self.samples = deque(maxlen=10)
            self.load = 0.5
            self.counter = PerformanceCounter(r'\System\Processor Queue Length', r'\Processor(_Total)\% Processor Time')

        def run(self):
            while True:
                pql, pt = self.counter.query()
                self.samples.append(pql)
                if pt >= 100:
                    self.load = max(sum(self.samples) / len(self.samples) / _cpu_count, pt / 100.)
                else:
                    self.load = pt / 100.
                sleep(1)

    _load_thread = SystemLoadThread()
    _load_thread.start()

    def load_fair():
        return 'load', _load_thread.load


def cpu_count():
    return 'cpu-count', _cpu_count


report_callbacks = [load_fair, cpu_count]
<commit_msg>Parse `/proc/loadavg` on Android systems; #308<commit_after>import os
from multiprocessing import cpu_count

_cpu_count = cpu_count()


if hasattr(os, 'getloadavg'):
    def load_fair():
        try:
            load = os.getloadavg()[0] / _cpu_count
        except OSError:  # as of May 2016, Windows' Linux subsystem throws OSError on getloadavg
            load = -1
        return 'load', load
elif os.name != 'nt':
    # There exist some Unix platforms (like Android) which don't
    # have `getloadavg` implemented, but obviously aren't Windows
    # so we manually read the `/proc/loadavg` file.
    def load_fair():
        try:
            with open('/proc/loadavg', 'r') as f:
                load = float(f.read().split()[0]) / _cpu_count
        except FileNotFoundError:
            load = -1
        return 'load', load
else:  # pragma: no cover
    from dmoj.utils.winperfmon import PerformanceCounter
    from threading import Thread
    from collections import deque
    from time import sleep

    class SystemLoadThread(Thread):
        def __init__(self):
            super(SystemLoadThread, self).__init__()
            self.daemon = True
            self.samples = deque(maxlen=10)
            self.load = 0.5
            self.counter = PerformanceCounter(r'\System\Processor Queue Length', r'\Processor(_Total)\% Processor Time')

        def run(self):
            while True:
                pql, pt = self.counter.query()
                self.samples.append(pql)
                if pt >= 100:
                    self.load = max(sum(self.samples) / len(self.samples) / _cpu_count, pt / 100.)
                else:
                    self.load = pt / 100.
                sleep(1)

    _load_thread = SystemLoadThread()
    _load_thread.start()

    def load_fair():
        return 'load', _load_thread.load


def cpu_count():
    return 'cpu-count', _cpu_count


report_callbacks = [load_fair, cpu_count]

{'fraction_non_alphanumeric': 0.09691160809371673, 'fraction_numerical': 0.012247071352502662, 'mean_word_length': 2.123025768911056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3548611', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1062, 'n_words': 323}