unsupported
  !   ˆÀ�þY•—–nP¸VDHÓ@þÉ„�y¯È¿ÇÆ ?8    # worker.py - master-slave parallelism support
#
# Copyright 2013 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import os
import pickle
import selectors
import signal
import sys
import threading
import time

from .i18n import _
from . import (
    encoding,
    error,
    pycompat,
    scmutil,
)


def countcpus():
    '''try to count the number of CPUs on the system'''

    # posix
    try:
        n = int(os.sysconf('SC_NPROCESSORS_ONLN'))
        if n > 0:
            return n
    except (AttributeError, ValueError):
        pass

    # windows
    try:
        n = int(encoding.environ[b'NUMBER_OF_PROCESSORS'])
        if n > 0:
            return n
    except (KeyError, ValueError):
        pass

    return 1


def _numworkers(ui):
    s = ui.config(b'worker', b'numcpus')
    if s:
        try:
            n = int(s)
            if n >= 1:
                return n
        except ValueError:
            raise error.Abort(_(b'number of cpus must be an integer'))
    return min(max(countcpus(), 4), 32)


def ismainthread():
    return threading.current_thread() == threading.main_thread()


class _blockingreader:
    """Wrap unbuffered stream such that pickle.load() works with it.

    pickle.load() expects that calls to read() and readinto() read as many
    bytes as requested. On EOF, it is fine to read fewer bytes. In this case,
    pickle.load() raises an EOFError.
    """

    def __init__(self, wrapped):
        self._wrapped = wrapped

    def readline(self):
        return self._wrapped.readline()

    def readinto(self, buf):
        pos = 0
        size = len(buf)

        with memoryview(buf) as view:
            while pos < size:
                with view[pos:] as subview:
                    ret = self._wrapped.readinto(subview)
                if not ret:
                    break
                pos += ret

        return pos

    # issue multiple reads until size is fulfilled (or EOF is encountered)
    def read(self, size=-1):
        if size < 0:
            return self._wrapped.readall()

        buf = bytearray(size)
        n_read = self.readinto(buf)
        del buf[n_read:]
        return bytes(buf)


if pycompat.isposix or pycompat.iswindows:
    _STARTUP_COST = 0.01
    # The Windows worker is thread based. If tasks are CPU bound, threads
    # in the presence of the GIL result in excessive context switching and
    # this overhead can slow down execution.
    _DISALLOW_THREAD_UNSAFE = pycompat.iswindows
else:
    _STARTUP_COST = 1e30
    _DISALLOW_THREAD_UNSAFE = False


def worthwhile(ui, costperop, nops, threadsafe=True):
    """try to determine whether the benefit of multiple processes can
    outweigh the cost of starting them"""

    if not threadsafe and _DISALLOW_THREAD_UNSAFE:
        return False

    linear = costperop * nops
    workers = _numworkers(ui)
    benefit = linear - (_STARTUP_COST * workers + linear / workers)
    return benefit >= 0.15


def worker(
    ui,
    costperarg,
    func,
    staticargs,
    args,
    hasretval=False,
    threadsafe=True,
    prefork=None,
):
    """run a function, possibly in parallel in multiple worker
    processes.

    returns a progress iterator

    costperarg - cost of a single task

    func - function to run. It is expected to return a progress iterator.

    staticargs - arguments to pass to every invocation of the function

    args - arguments to split into chunks, to pass to individual
    workers

    hasretval - when True, func and the current function return an progress
    iterator then a dict (encoded as an iterator that yield many (False, ..)
    then a (True, dict)). The dicts are joined in some arbitrary order, so
    overlapping keys are a bad idea.

    threadsafe - whether work items are thread safe and can be executed using
    a thread-based worker. Should be disabled for CPU heavy tasks that don't
    release the GIL.

    prefork - a parameterless C