| OLD | NEW |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | 1 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """OS specific utility functions. | 5 """OS specific utility functions. |
| 6 | 6 |
| 7 Includes code: | 7 Includes code: |
| 8 - to declare the current system this code is running under. | 8 - to declare the current system this code is running under. |
| 9 - to run a command on user login. | 9 - to run a command on user login. |
| 10 - to restart the host. | 10 - to restart the host. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 from third_party.oauth2client import client | 40 from third_party.oauth2client import client |
| 41 | 41 |
| 42 | 42 |
| 43 # For compatibility with older bot_config.py files. | 43 # For compatibility with older bot_config.py files. |
| 44 cached = tools.cached | 44 cached = tools.cached |
| 45 | 45 |
| 46 | 46 |
| 47 # Global configurable values. These are meant to be the default values under | 47 # Global configurable values. These are meant to be the default values under |
| 48 # which the bot will self-quarantine when the disk size is too small. Setting | 48 # which the bot will self-quarantine when the disk size is too small. Setting |
| 49 # either of the following value to 0 or None disables self-quarantine. | 49 # either of the following value to 0 or None disables self-quarantine. |
| 50 # Self-quarantine only happens when *both* thresholds are under. | 50 # Self-quarantine only happens when the free disk space is below the lower of |
| 51 # | 51 # the two thresholds. It means that for sufficiently large disks THRESHOLD_MB is |
| 52 # the actual threshold (for them disk_size * THRESHOLD_RELATIVE > THRESHOLD_MB). |
| 53 |
| 52 # THRESHOLD_MB is the number of MiB below which the bot will quarantine itself | 54 # THRESHOLD_MB is the number of MiB below which the bot will quarantine itself |
| 53 # automatically. | 55 # automatically. |
| 54 THRESHOLD_MB = 4*1024 | 56 THRESHOLD_MB = 4*1024 |
| 57 |
| 55 # THRESHOLD_RELATIVE is the minimum free space percentage under which the bot | 58 # THRESHOLD_RELATIVE is the minimum free space percentage under which the bot |
| 56 # will quarantine itself automatically. | 59 # will quarantine itself automatically. |
| 60 # |
| 61 # This threshold is effectively disabled for large disks (ones with |
| 62 # disk_size * THRESHOLD_RELATIVE > THRESHOLD_MB), since THRESHOLD_MB takes |
| 63 # precedence there (being smaller). |
| 57 THRESHOLD_RELATIVE = 0.15 | 64 THRESHOLD_RELATIVE = 0.15 |
| 58 | 65 |
| 66 # DESIRED_FREE_DISK_SPACE is percentage of a disk to strive to keep available |
| 67 # (e.g. by trimming the isolate cache appropriately). |
| 68 # |
| 69 # If disk_size * DESIRED_FREE_DISK_SPACE is lower than self-quarantine threshold |
| 70 # (defined above), the self-quarantine threshold will be used instead. Will also |
| 71 # always add 250 MiB as an additional slack space for logs, temporary files etc. |
| 72 # |
| 73 # The goal is to always have some percentage of the disk available above |
| 74 # the self-quarantine threshold. |
| 75 # |
| 76 # The default values in practice mean: |
| 77 # * For large disks (>27GB): try to keep 5%+250MB of disk free, |
| 78 # self-quarantine if the free disk space is below 4GB. |
| 79 # * For small disks (<27GB): try to keep 15%+250MB of disk free, |
| 80 # self-quarantine if the free disk space is below 15%. |
| 81 DESIRED_FREE_DISK_SPACE = 0.05 |
| 82 |
| 59 | 83 |
| 60 # https://cloud.google.com/compute/pricing#machinetype | 84 # https://cloud.google.com/compute/pricing#machinetype |
| 61 GCE_MACHINE_COST_HOUR_US = { | 85 GCE_MACHINE_COST_HOUR_US = { |
| 62 u'n1-standard-1': 0.050, | 86 u'n1-standard-1': 0.050, |
| 63 u'n1-standard-2': 0.100, | 87 u'n1-standard-2': 0.100, |
| 64 u'n1-standard-4': 0.200, | 88 u'n1-standard-4': 0.200, |
| 65 u'n1-standard-8': 0.400, | 89 u'n1-standard-8': 0.400, |
| 66 u'n1-standard-16': 0.800, | 90 u'n1-standard-16': 0.800, |
| 67 u'n1-standard-32': 1.600, | 91 u'n1-standard-32': 1.600, |
| 68 u'f1-micro': 0.008, | 92 u'f1-micro': 0.008, |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 @tools.cached | 398 @tools.cached |
| 375 def get_min_free_space(path): | 399 def get_min_free_space(path): |
| 376 """Returns the minimum free space (in MiB) required to not self-quarantine. | 400 """Returns the minimum free space (in MiB) required to not self-quarantine. |
| 377 | 401 |
| 378 Returns 0 when disabled. | 402 Returns 0 when disabled. |
| 379 """ | 403 """ |
| 380 return min(THRESHOLD_MB or 0, get_disk_size(path) * THRESHOLD_RELATIVE or 0) | 404 return min(THRESHOLD_MB or 0, get_disk_size(path) * THRESHOLD_RELATIVE or 0) |
| 381 | 405 |
| 382 | 406 |
| 383 @tools.cached | 407 @tools.cached |
| 408 def get_desired_free_space(path): |
| 409 """Returns the disk space (in MiB) to strive to keep available.""" |
| 410 mb = get_disk_size(path) * DESIRED_FREE_DISK_SPACE # this can be 0 |
| 411 threshold = get_min_free_space(path) |
| 412 if mb < threshold: |
| 413 mb = threshold |
| 414 return mb + 250 |
| 415 |
| 416 |
| 417 @tools.cached |
| 384 def get_audio(): | 418 def get_audio(): |
| 385 """Returns the active audio card(s).""" | 419 """Returns the active audio card(s).""" |
| 386 # There's a risk that an audio card may "appear", which may be especially true | 420 # There's a risk that an audio card may "appear", which may be especially true |
| 387 # on OSX when an audio cable is plugged in. | 421 # on OSX when an audio cable is plugged in. |
| 388 if sys.platform == 'darwin': | 422 if sys.platform == 'darwin': |
| 389 return platforms.osx.get_audio() | 423 return platforms.osx.get_audio() |
| 390 elif sys.platform == 'linux2': | 424 elif sys.platform == 'linux2': |
| 391 return platforms.linux.get_audio() | 425 return platforms.linux.get_audio() |
| 392 elif sys.platform == 'win32': | 426 elif sys.platform == 'win32': |
| 393 return platforms.win.get_audio() | 427 return platforms.win.get_audio() |
| (...skipping 856 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1250 | 1284 |
| 1251 | 1285 |
| 1252 def trim_rolled_log(name): | 1286 def trim_rolled_log(name): |
| 1253 try: | 1287 try: |
| 1254 for item in glob.iglob('%s.??' % name): | 1288 for item in glob.iglob('%s.??' % name): |
| 1255 os.remove(item) | 1289 os.remove(item) |
| 1256 for item in glob.iglob('%s.???' % name): | 1290 for item in glob.iglob('%s.???' % name): |
| 1257 os.remove(item) | 1291 os.remove(item) |
| 1258 except Exception as e: | 1292 except Exception as e: |
| 1259 logging.exception('trim_rolled_log(%s) failed: %s', name, e) | 1293 logging.exception('trim_rolled_log(%s) failed: %s', name, e) |
| OLD | NEW |