
                            S r SSKrSSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr  S	 r " S
 S\R                  5      r\R                  R                   S\R                  R"                  S\R                  R$                  S\R                  R&                  S\R                  R(                  S\R                  R*                  S0rSrSS jrS rS r\\SSSS4S jrS rS r " S S5      rg)z3Used to collect anonymous transfer-related metrics.    N)storage_url)	task_util)metrics)
properties)files)	platformsc                  ~   [         R                  R                  R                  R	                  5       (       a  [         R                  R
                  R                  R                  5       R                  SS5      n [         R                  R                  [         R                  R
                  R                  U S5        gg)a;  Transform the user agent when the gsutil shim is used to run gcloud.

This transforms `gcloud.storage.command` to `gcloud.gsutil.command`.

This needs to be called by every command, so the best place to put this is
likely surface/storage/__init__.py, but if there's a better place it could be
called there instead.
zgcloud.storage.zgcloud.gslibshim.N)
r   VALUESstoragerun_by_gsutil_shimGetBoolr   command_nameGetreplaceSetInvocationValue)command_path_strings    6lib/googlecloudsdk/command_lib/storage/metrics_util.pyfix_user_agent_for_gsutil_shimr      s     1199;;$++33@@DDFNN.0((!!..0CTK <    c                       \ rS rSrSrSrSrg)ParallelismStrategy,          N)__name__
__module____qualname____firstlineno__
SEQUENTIALPARALLEL__static_attributes__r   r   r   r   r   ,   s    *(r   r   r   r               c                     [         R                  R                  R                  R	                  5       n[        R
                  " USU -   U5        g)zCommon code for processing an event.

Args:
  metric (str): The metric being recorded.
  value (mixed): The value being recorded.
zStorage-N)r   r
   r   r   r   CustomKeyValue)metricvaluer   s      r   _record_storage_eventr+   >   s;     ""**77;;=,	zF':EBr   c                      [         R                  " 5       (       a  [        R                  R                  $ [        R
                  R                  $ N)r   should_use_parallelismr   r!   r*   r    r   r   r   _get_parallelism_strategyr/   I   s5    %%''''---		'	'	-	--r   c                  x    [         R                  R                  R                  R	                  5       (       a  S$ S$ )Nr   r   )r   r
   r   r   r   r   r   r   _get_run_by_gsutil_shimr1   O   s-    ''::BBDDK!Kr   c                     [        S[        5       5        [        S[        5       5        [        SU 5        [        SU5        [        SU5        [        SU5        [        SU5        [        SU5        g	)
a  Reports metrics for a transfer.

Args:
  source_scheme (int): The source scheme, i.e. 'gs' or 's3'.
  destination_scheme (int): The destination scheme i.e. 'gs' or 's3'.
  num_files (int): The number of files transferred.
  size (int): The size of the files transferred, in bytes.
  avg_speed (int): The average throughput of a transfer in bytes/sec.
  disk_io_time (int): The time spent on disk of a transfer in ms.
r   RunByGsutilShimSourceSchemeDestinationSchemeNumberOfFilesSizeOfFilesBytesAverageSpeedBytesPerSecDiskIoTimeMsN)r+   r/   r1   source_schemedestination_scheme	num_filessize	avg_speeddisk_io_times         r   reportrA   S   sb     -/H/JK)+B+DE6+-?@3*D119=5r   c                  Z   / n  [         R                  " S5       nUR                  5       SS nU HB  nUR                  5       u      pEUS   R	                  5       (       d  M1  U R                  U5        MD     SSS5        U $ ! , (       d  f       U $ = f! [         R                   a     U $ f = f)zYRetrieves a list of disk partitions.

Returns:
  An array of partition names as strings.
z/proc/partitionsr   N)r   
FileReader	readlinessplitisdigitappendError)
partitionsflinesline_names         r   _get_partitionsrP   i   s     *
				,	-kkmABe$

1a8


D
!  
. 
 
.	- 
 
 			s5   B AB !B 6B  
B
B B B*)B*c                     Sn [        5       n0 n [        R                  " S5       nUR                  5       nU Hu  nUR	                  5       SS nUu    pxppppX;   d  M&  [        U
5      U -  n
[        U5      U -  n[        U	5      n	[        U5      n[        U5      n[        U5      nXXX4X('   Mw     SSS5        U$ ! , (       d  f       U$ = f! [        R                   a     U$ f = f)a  Retrieves disk I/O statistics for all disks.

Adapted from the psutil module's psutil._pslinux.disk_io_counters:
  http://code.google.com/p/psutil/source/browse/trunk/psutil/_pslinux.py

Originally distributed under under a BSD license.
Original Copyright (c) 2009, Jay Loden, Dave Daeschler, Giampaolo Rodola.

Returns:
  A dictionary containing disk names mapped to the disk counters from
  /disk/diskstats.
i   z/proc/diskstatsN   )rP   r   rD   rE   rF   intrI   )sector_sizerJ   retdictrK   rL   rM   valuesrN   rO   readsrbytesrtimewriteswbyteswtimes                  r   get_disk_countersr]      s      + *'				+	,kkme$cr"HNE1EfVv;,&v;,&e*%v;&e*%e*% &%G'-  
-$ 
.% 
-	,$ 
. 
 		.	s5   C 5B<AB<2C <
CC C C&%C&c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
MetricsReporter   z9Mix-in for tracking metrics during task status reporting.c                 N    [         U l        [         U l        [        5       U l        g r-   )UNSET_source_scheme_destination_schemer]   _disk_counters_start)selfs    r   __init__MetricsReporter.__init__   s    D$D 1 3Dr   c                 B    U(       a  [         UR                     $ [        $ )z;Extracts the scheme as an integer value from a storage_url.)PROVIDER_PREFIX_TO_METRICS_KEYschemerb   )rf   urls     r   _get_scheme_value!MetricsReporter._get_scheme_value   s    
+CJJ77Lr   c                     U R                   [        :X  a   U R                  UR                  5      U l         U R                  [        :X  a!  U R                  UR
                  5      U l        gg)ztSets source and destination schemes, if available.

Args:
  status_message (thread_messages.*): Message to process.
N)rc   rb   rm   
source_urlrd   destination_url)rf   status_messages     r   #_set_source_and_destination_schemes3MetricsReporter._set_source_and_destination_schemes   sZ     e# 22>3L3LMd5(!%!7!7

(
("*d )r   c           	         [         R                  R                  5       nU[         R                  R                  :X  a{  U R                  n[        5       n[        UR                  5        Vs/ s H  oDS   US   -   PM     sn5      [        UR                  5        Vs/ s H  oDS   US   -   PM     sn5      -
  $ [        $ s  snf s  snf )z&Calculate deltas of time spent on I/O.r$   r%   )	r   OperatingSystemCurrentLINUXre   r]   sumrV   rb   )rf   
current_os
disk_startdisk_endstats        r   _calculate_disk_io"MetricsReporter._calculate_disk_io   s    **224JY..444,,j"$h1BC1B7T!W$1BCD1B1B1DE1D7T!W$1DEFG HL DEs   *B>C
c           	          [        [        U5      [        U5      -  5      n[        U R                  U R                  UUUU R                  5       S9  g)zReports back all tracked events via report method.

Args:
  total_bytes (int): Amount of data transferred in bytes.
  time_delta (int): Time elapsed during the transfer in seconds.
  num_files (int): Number of files processed
r:   N)roundfloatrA   rc   rd   r~   )rf   total_bytes
time_deltar=   r?   s        r   _report_metricsMetricsReporter._report_metrics   sL     eK(5+<<=I
))33,,.0r   )rd   re   rc   N)r   r   r   r   __doc__rg   rm   rs   r~   r   r"   r   r   r   r_   r_      s    A4
*	0r   r_   )r   )r   enum"googlecloudsdk.command_lib.storager   (googlecloudsdk.command_lib.storage.tasksr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r   r   Enumr   ProviderPrefixFILEGCSHTTPHTTPSPOSIXS3rj   rb   r+   r/   r1   rA   rP   r]   r_   r   r   r   <module>r      s    :  : > ' * * .K $))  ##Q""A##Q$$a$$a!!1"  	
C.L 5AQQ6,.(V70 70r   