
    6h=                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	SSK
Jr  SSKJrJrJrJr  \	R"                  " SS\SS	9  S
 rS rS r\R,                  (       a  \R.                  S:w  d  \R0                  (       a  Sr\R0                  rOSrSrSS jr   SS jrSS jrS rSS jr\R0                  (       a  \r O\r \r!S r"g)    )annotationsN   )_)encodingerrorpycompatscmutilignorez_This process \(pid=\d+\) is multi-threaded, use of fork\(\) may lead to deadlocks in the child.zmercurial.worker)messagecategorymodulec                      [        [        R                  " S5      5      n U S:  a  U $   [        [
        R                  S   5      n U S:  a  U $  g! [        [        4 a     N9f = f! [        [        4 a     gf = f)z-try to count the number of CPUs on the systemSC_NPROCESSORS_ONLNr   s   NUMBER_OF_PROCESSORSr   )intossysconfAttributeError
ValueErrorr   environKeyError)ns    2/usr/lib/python3/dist-packages/mercurial/worker.py	countcpusr   (   s    

012q5H   !89:q5H 
  J'  j! s"   &A #A' A$#A$'A:9A:c                    U R                  SS5      nU(       a   [        U5      nUS:  a  U$  [        [        [        5       S5      S5      $ ! [         a     [        R                  " [        S5      5      ef = f)N   workers   numcpusr   s!   number of cpus must be an integer       )	configr   r   r   Abortr   minmaxr   )uisr   s      r   _numworkersr$   >   su    
		)Z(A	GAAAv  s9;"B''  	G++a DEFF	Gs   A *A7c                 X    [         R                  " 5       [         R                  " 5       :H  $ N)	threadingcurrent_threadmain_thread     r   ismainthreadr,   J   s    ##%)>)>)@@@r+   s   OpenVMSg{Gz?gꌠ9Y>)FFc                r    U(       d  [         (       a  gX-  n[        U 5      nU[        U-  XE-  -   -
  nUS:  $ )zatry to determine whether the benefit of multiple processes can
outweigh the cost of starting themFg333333?)_DISALLOW_THREAD_UNSAFEr$   _STARTUP_COST)r"   	costperopnops
threadsafelinearworkersbenefits          r   
worthwhiler6   [   sB     11F"oG/&2BBCGd?r+   c           	         U R                  SS5      nU(       a  [        [        L a  [        5       (       d  SnU(       a$  [	        X[        U5      US9(       a  [        XX4XWS9$ U" X44-   6 $ )a  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 Callable that is invoked prior to forking the
process.  fork() is only used on non-Windows platforms, but is also not
called on POSIX platforms if the work amount doesn't warrant a worker.
r   s   enabledF)r2   )prefork)
configbool_platformworker_posixworkerr,   r6   len)	r"   
costperargfunc
staticargsargs	hasretvalr2   r8   enableds	            r   workerrC   h   sd    J mmIz2G?l2<>> :bc$iJOj	
 	
 g%&&r+   c           	   #  	  ^^^^^^^^^^^^^ ^!#    [        U 5      n[        R                  " [        R                  5      m[        R                  " [        R                  [        R                  5        [        5       S/smmUU4S jmSUU4S jjm UUU 4S jn[        R                  " [        R                  U5      mU R                  5         [        R                  " 5       n/ m0 n	U(       a  U" 5         [        U[        U[        U5      5      5       GH  m[        R                  " 5       u  mm!TR                  TT!45        Sn
 [        R                  " 5       nUS:X  ak  [        R                  " [        R                  T5        [        R                  " [        R                  T5        UUUUUU!4S jn[         R"                  " X5      n
[        R                  " 5       U:w  a+   U R                  5         [        R.                  " U
S	-  5        O TR1                  W5        GM     [2        R4                  " 5       mT HR  u  mm![        R6                  " T!5        TR9                  [        R:                  " TS
S5      [2        R<                  5        MT     S mU4S jnUUUUU 4S jn [        T5      nUS:  ag  TR?                  5        HK  u  nn U" UR@                  5       H.  nU(       a   US   (       a  U	RC                  US   5        M*  Uv   M0     MM     US:  a  Mg  U" 5       nU(       aG  US:  a+  [        RH                  " [        R                  " 5       U* 5        [J        RL                  " U5      eU(       a  SU	4v   g g !   [        R                  " 5       U:X  a  e [$        R&                  " 5       S   n[)        U[*        5      (       + nU R-                  US9   GN= f!    GN= f! [        R.                  " U
S	-  5        f = f! [        R                  " 5       U:w  aQ   U R                  5         O!    O= f[        R.                  " U
S	-  5        f ! [        R.                  " U
S	-  5        f = ff = f! [D         a?    TRG                  UR@                  5        UR@                  R7                  5         US-  n GM  f = f!   T" 5         U" 5         e = f7f)Nr   c                    > [         R                   " [         R                  T5        T H)  n  [        R                  " U [         R                  5        M+     g ! [
         a     M;  f = fr&   )signalSIGCHLDr   killSIGTERMProcessLookupError)poldchldhandlerpidss    r   killworkers!_posixworker.<locals>.killworkers   sL     	fnnn5A6>>*  & s   %A
A%$A%Tc                Z  > TR                  5        Hu  nS=p# [        R                  " X(       a  SO[        R                  5      u  p#U(       d  M?  TR                  U5        [        U5      nU(       d  Md  TS   (       a  Mp  UTS'   Mw     g ! [         a    TR                  U5         Naf = fNr   )copyr   waitpidWNOHANGChildProcessErrordiscard_exitstatus)blockingpidrK   strM   problems       r   waitforworkers$_posixworker.<locals>.waitforworkers   s    99;CJA"

3hBJJH
  LLORBr'!**
  % " S!"s   /BB*)B*c                6   > T" SS9  TS   (       a  T" 5         g g )NF)rX   r   r*   )signumframerN   r[   r\   s     r   sigchldhandler$_posixworker.<locals>.sigchldhandler   s    &1:M r+   c                 t  > TS S  H1  u  p[         R                  " U 5        [         R                  " U5        M3     [         R                  " T5        [         R                  " T	S5       nT" TT4-   6  H)  n[        R                  " X25        UR                  5         M+     S S S 5        g! , (       d  f       g= f)Nrc   wbr   )r   closefdopenpickledumpflush)
rwwfresultr>   pargspipesrfdr?   wfds
       r   
workerfunc _posixworker.<locals>.workerfunc   s     %cr
 !+ HHSM3-&*Z5(-B&DF"KK3HHJ 'E . 	 .- s   )7B))
B7)force   rbi   c                    [         R                  " U R                  5       S5        U R                  5       n[         R                  " U R                  5       S5        U$ NFT)r   set_blockingfilenopeek)fress     r   peek_nonblock#_posixworker.<locals>.peek_nonblock  s:    

E*ffh

D)
r+   c              3     >#    [         R                  " U 5      v   [        T" U 5      5      S:  a0  [         R                  " U 5      v   [        T" U 5      5      S:  a  M/  g g 7frQ   )rh   loadr<   )r}   r   s    r   load_all_posixworker.<locals>.load_all  sJ      kk!n-"#a'++a.  -"#a's   AA" A"c                    > [         R                   " [         R                  T5        T" 5         [         R                   " [         R                  T 5        TR                  5         TS   $ rQ   )rF   SIGINTrG   rf   )rL   
oldhandlerr[   selectorr\   s   r   cleanup_posixworker.<locals>.cleanup  sA    fmmZ0fnnn5qzr+   r   T)'r$   rF   	getsignalr   SIG_IGNsetrG   rj   r   getpid	partitionr    r<   pipeappendforkr	   	callcatchsysexc_info
issubclassKeyboardInterrupt	traceback_exitadd	selectorsDefaultSelectorrf   registerrg   
EVENT_READselectfileobjupdateEOFError
unregisterrH   r   WorkerError)"r"   r>   r?   r@   rA   r8   r4   ra   	parentpidretvalretrY   rs   exctyperu   r   r   	openpipeskeyeventsr~   statusrN   rL   r   ro   r   rM   rp   r[   rq   r   r\   rr   s"    ``                   @@@@@@@@@@@@r   r;   r;      s    "oG!!&--0J
MM&--0EA3MD'
   $
 ]]6>>>BNHHJ		IEF	4Wc$i!89 779Sc3Z  	('')CaxfmmZ8fnnn=	 	 ''7 yy{i'(HHJ HHS3Y' ( 	a :b ((*HS

 	"))Ct4i6J6JK ! J	!m'0V#'4$Q"MM#a&1"%I	  5  1 !m$ YFA:GGBIIK&)''Fl O	&yy{i'llnQ'G"7,=>>ELLuL%
HHS3Y' yy{i'(HHJHHS3Y'BHHS3Y' (b   #''4KK%%'NI#	s   D5SBNS&O"6B1S('S AQ7
S  A$SAOP"O'$O**PSQ4#P43Q4P8	6Q;Q4Q0	0Q44S7AS ;S ?S  S SSc                    [         R                  " U 5      (       a  [         R                  " U 5      $ [         R                  " U 5      (       a  [         R                  " U 5      * $ g)zconvert a posix exit status into the same form returned by
os.spawnv

returns None if the process was stopped instead of exitingN)r   	WIFEXITEDWEXITSTATUSWIFSIGNALEDWTERMSIG)codes    r   _posixexitstatusr   9  sH    
 
||D~~d##			T"## 
r+   c              #    ^ ^#     " S S[         R                  5      n/ mUU 4S jn[        T 5      n[        R                  R                  5       n	[        R                  R                  5       n
0 n[        X8S-  5       H  nU
R                  U5        M     [        U5       H-  nU" XX5      nTR                  U5        UR                  5         M/      [        T5      S:  a  U	R                  5       (       dQ  U	R                  5       nU(       a  US   (       a  UR                  US   5        OUv   U	R                  5       (       d  MQ  TS   R                  S5        T Vs/ s H  nUR!                  5       (       a  M  UPM     nnU H-  nUR"                  b  UR"                  eTR%                  U5        M/     [        T5      S:  a  M  U	R                  5       (       dQ  U	R                  5       nU(       a  US   (       a  UR                  US   5        OUv   U	R                  5       (       d  MQ  U(       a  SU4v   g g s  snf ! [&        [(        4 a	    U" 5         e f = f7f)	Nc                  &    \ rS rSrS rS rS rSrg)_windowsworker.<locals>.WorkeriE  c                    [         R                  R                  " U /UQ70 UD6  Xl        X l        X0l        X@l        SU l        SU l        S U l	        g ry   )
r'   Thread__init__
_taskqueue_resultqueue_func_staticargs_interrupteddaemon	exception)self	taskqueueresultqueuer>   r?   r@   kwargss          r   r   '_windowsworker.<locals>.Worker.__init__F  sN     %%d<T<V<'O +J) %DDK!DNr+   c                    SU l         g )NT)r   )r   s    r   	interrupt(_windowsworker.<locals>.Worker.interruptR  s
     $Dr+   c                    U R                   R                  5       (       d   U R                   R                  5       nU R                  " U R                  U4-   6  H1  nU R
                  R                  U5        U R                  (       d  M1    g    U R                   R                  5       (       d  M  g g ! [        R                  R                   a     g f = f! [         a  nX0l        e S nAff = fr&   )r   empty
get_nowaitr   r   r   putr   r   queueEmpty	Exceptionr   )r   r@   r~   es       r   run"_windowsworker.<locals>.Worker.runU  s    ////11
#99;#'::t/?/?4'/I#JC --11#6  $000 & $K ////11 $>>//   "#	sG   C A$B1 
B1 B1 C 1CC CC 
C+C&&C+)r   r   r   r   r   r   r   N)__name__
__module____qualname____firstlineno__r   r   r   __static_attributes__r*   r+   r   Workerr   E  s    
	"	%	r+   r   c                 .  > [         R                   " 5       S-   n T H  nUR                  5         M     T H]  nU [         R                   " 5       -
  nUR                  U5        UR                  5       (       d  MC  TR	                  [        S5      5          g    g )Nr   s:   failed to kill worker threads while handling an exception
)timer   joinis_alivewarnr   )
cleanupendtremainingtimethreadsr"   s      r   trykillworkers&_windowsworker.<locals>.trykillworkersk  sv    YY[1_
AKKM A&4MFF=!zz||
 3  r+      r   r   g?T)r'   r   r$   r   r   Queuer   r   ranger   startr<   r   getr   r   r   r   remover   r   )r"   r>   r?   r@   rA   r8   r   r   r4   r   r   r   ro   _ir   r~   _tfinishedthreadsr   s   `                 @r   _windowsworkerr   D  s    "!! "H G* "oG..&&(K$$&IF 42.e /Gn94<q		 'lQ!''))!oo'QMM#a&)I "'')) AJOOD!,3IGb2;;=rGOI$;;*++%q! % 'lQ  !!ooQMM#a&!I !! Fl  J
 () sE   CIA3I I  H>=H>AI 	A$I/I>I IIc              #  B   #    [        U5       H  nXSU2   v   M     g7f)a  partition a list into N slices of roughly equal size

The current strategy takes every Nth element from the input. If
we ever write workers that need to preserve grouping in input
we should consider allowing callers to specify a partition strategy.

olivia is not a fan of this partitioning strategy when files are involved.
In his words:

    Single-threaded Mercurial makes a point of creating and visiting
    files in a fixed order (alphabetical). When creating files in order,
    a typical filesystem is likely to allocate them on nearby regions on
    disk. Thus, when revisiting in the same order, locality is maximized
    and various forms of OS and disk-level caching and read-ahead get a
    chance to work.

    This effect can be quite significant on spinning disks. I discovered it
    circa Mercurial v0.4 when revlogs were named by hashes of filenames.
    Tarring a repo and copying it to another disk effectively randomized
    the revlog ordering on disk by sorting the revlogs by hash and suddenly
    performance of my kernel checkout benchmark dropped by ~10x because the
    "working set" of sectors visited no longer fit in the drive's cache and
    the workload switched from streaming to random I/O.

    What we should really be doing is have workers read filenames from a
    ordered queue. This preserves locality and also keeps any worker from
    getting more than one file out of balance.
N)r   )lstnslicesis      r   r   r     s#     : 7^*W*o s   r   )FTNr&   )#
__future__r   r   rh   r   rF   r   r'   r   warningsi18nr    r   r   r   r	   filterwarningsDeprecationWarningr   r$   r,   isposixsysplatform	iswindowsr/   r.   r6   rC   r;   r   r   r:   rW   r   r*   r+   r   <module>r      s    # 	    
      	  	@  ,	(A
 --;M '00M#
& /'d\~$`F $O"O"Kr+   