mercurial/worker.py
changeset 18636 dcb27c153a40
parent 18635 fed06dd07665
child 18637 ac4dbceeb14a
equal deleted inserted replaced
18635:fed06dd07665 18636:dcb27c153a40
     3 # Copyright 2013 Facebook, Inc.
     3 # Copyright 2013 Facebook, Inc.
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 import os
     8 from i18n import _
       
     9 import os, util
     9 
    10 
    10 def countcpus():
    11 def countcpus():
    11     '''try to count the number of CPUs on the system'''
    12     '''try to count the number of CPUs on the system'''
    12 
    13 
    13     # posix
    14     # posix
    25             return n
    26             return n
    26     except (KeyError, ValueError):
    27     except (KeyError, ValueError):
    27         pass
    28         pass
    28 
    29 
    29     return 1
    30     return 1
       
    31 
       
    32 def _numworkers(ui):
       
    33     s = ui.config('worker', 'numcpus')
       
    34     if s:
       
    35         try:
       
    36             n = int(s)
       
    37             if n >= 1:
       
    38                 return n
       
    39         except ValueError:
       
    40             raise util.Abort(_('number of cpus must be an integer'))
       
    41     return min(max(countcpus(), 4), 32)
       
    42 
       
    43 if os.name == 'posix':
       
    44     _startupcost = 0.01
       
    45 else:
       
    46     _startupcost = 1e30
       
    47 
       
    48 def worthwhile(ui, costperop, nops):
       
    49     '''try to determine whether the benefit of multiple processes can
       
    50     outweigh the cost of starting them'''
       
    51     linear = costperop * nops
       
    52     workers = _numworkers(ui)
       
    53     benefit = linear - (_startupcost * workers + linear / workers)
       
    54     return benefit >= 0.15