[Notes] [Git][BuildStream/buildstream][mac_fixes] 4 commits: Moved `resource.getrlimit()`



Title: GitLab

Phillip Smyth pushed to branch mac_fixes at BuildStream / buildstream

Commits:

6 changed files:

Changes:

  • buildstream/_frontend/app.py
    ... ... @@ -116,14 +116,6 @@ class App():
    116 116
             else:
    
    117 117
                 self.colors = False
    
    118 118
     
    
    119
    -        # Increase the soft limit for open file descriptors to the maximum.
    
    120
    -        # SafeHardlinks FUSE needs to hold file descriptors for all processes in the sandbox.
    
    121
    -        # Avoid hitting the limit too quickly.
    
    122
    -        limits = resource.getrlimit(resource.RLIMIT_NOFILE)
    
    123
    -        if limits[0] != limits[1]:
    
    124
    -            # Set soft limit to hard limit
    
    125
    -            resource.setrlimit(resource.RLIMIT_NOFILE, (limits[1], limits[1]))
    
    126
    -
    
    127 119
         # create()
    
    128 120
         #
    
    129 121
         # Should be used instead of the regular constructor.
    

  • buildstream/_platform/darwin.py
    1
    +#
    
    2
    +#  Copyright (C) 2017 Codethink Limited
    
    3
    +#  Copyright (C) 2018 Bloomberg Finance LP
    
    4
    +#
    
    5
    +#  This program is free software; you can redistribute it and/or
    
    6
    +#  modify it under the terms of the GNU Lesser General Public
    
    7
    +#  License as published by the Free Software Foundation; either
    
    8
    +#  version 2 of the License, or (at your option) any later version.
    
    9
    +#
    
    10
    +#  This library is distributed in the hope that it will be useful,
    
    11
    +#  but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    12
    +#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
    
    13
    +#  Lesser General Public License for more details.
    
    14
    +#
    
    15
    +#  You should have received a copy of the GNU Lesser General Public
    
    16
    +#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
    
    17
    +
    
    18
    +import os
    
    19
    +import resource
    
    20
    +
    
    21
    +from .._artifactcache.cascache import CASCache
    
    22
    +from .._exceptions import PlatformError
    
    23
    +from ..sandbox import SandboxChroot
    
    24
    +
    
    25
    +from . import Platform
    
    26
    +
    
    27
    +
    
    28
    +class Darwin(Platform):
    
    29
    +
    
    30
    +    def __init__(self, context):
    
    31
    +
    
    32
    +        super().__init__(context)
    
    33
    +        self._artifact_cache = CASCache(context)
    
    34
    +
    
    35
    +        # Need to set resources for _frontend/app.py as this is dependent on the platform
    
    36
    +        # SafeHardlinks FUSE needs to hold file descriptors for all processes in the sandbox.
    
    37
    +        # Avoid hitting the limit too quickly.
    
    38
    +        limits = resource.getrlimit(resource.RLIMIT_NOFILE)
    
    39
    +        if limits[0] != limits[1]:
    
    40
    +            resource.setrlimit(resource.RLIMIT_NOFILE, (49152, limits[1]))
    
    41
    +
    
    42
    +        # Not necessarily 100% reliable, but we want to fail early.
    
    43
    +        if os.geteuid() != 0:
    
    44
    +            raise PlatformError("Root privileges are required to run without bubblewrap.")
    
    45
    +
    
    46
    +    @property
    
    47
    +    def artifactcache(self):
    
    48
    +        return self._artifact_cache
    
    49
    +
    
    50
    +    def create_sandbox(self, *args, **kwargs):
    
    51
    +        return SandboxChroot(*args, **kwargs)
    
    52
    +
    
    53
    +     def get_cpu_count(self):
    
    54
    +        return str(min(len(os.cpu_count())))

  • buildstream/_platform/linux.py
    ... ... @@ -17,10 +17,12 @@
    17 17
     #  Authors:
    
    18 18
     #        Tristan Maat <tristan maat codethink co uk>
    
    19 19
     
    
    20
    +import os
    
    20 21
     import subprocess
    
    21 22
     
    
    22 23
     from .. import _site
    
    23 24
     from .. import utils
    
    25
    +import resource
    
    24 26
     from .._artifactcache.cascache import CASCache
    
    25 27
     from .._message import Message, MessageType
    
    26 28
     from ..sandbox import SandboxBwrap
    
    ... ... @@ -35,9 +37,19 @@ class Linux(Platform):
    35 37
             super().__init__(context)
    
    36 38
     
    
    37 39
             self._die_with_parent_available = _site.check_bwrap_version(0, 1, 8)
    
    38
    -        self._user_ns_available = self._check_user_ns_available(context)
    
    40
    +
    
    41
    +        # Because of the way we use user namespaces, we must have FUSE in order for it to function
    
    42
    +        self._user_ns_available = self._check_fuse_available() and self._check_user_ns_available(context)
    
    43
    +
    
    39 44
             self._artifact_cache = CASCache(context, enable_push=self._user_ns_available)
    
    40 45
     
    
    46
    +        # Need to set resources for _frontend/app.py as this is dependent on the platform
    
    47
    +        # SafeHardlinks FUSE needs to hold file descriptors for all processes in the sandbox.
    
    48
    +        # Avoid hitting the limit too quickly.
    
    49
    +        limits = resource.getrlimit(resource.RLIMIT_NOFILE)
    
    50
    +        if limits[0] != limits[1]:
    
    51
    +            resource.setrlimit(resource.RLIMIT_NOFILE, (limits[1], limits[1]))
    
    52
    +
    
    41 53
         @property
    
    42 54
         def artifactcache(self):
    
    43 55
             return self._artifact_cache
    
    ... ... @@ -51,8 +63,10 @@ class Linux(Platform):
    51 63
         ################################################
    
    52 64
         #              Private Methods                 #
    
    53 65
         ################################################
    
    54
    -    def _check_user_ns_available(self, context):
    
    66
    +    def _check_fuse_available(self):
    
    67
    +        return os.path.exists('/dev/fuse'):
    
    55 68
     
    
    69
    +    def _check_user_ns_available(self, context):
    
    56 70
             # Here, lets check if bwrap is able to create user namespaces,
    
    57 71
             # issue a warning if it's not available, and save the state
    
    58 72
             # locally so that we can inform the sandbox to not try it
    

  • buildstream/_platform/platform.py
    ... ... @@ -40,19 +40,22 @@ class Platform():
    40 40
     
    
    41 41
         @classmethod
    
    42 42
         def create_instance(cls, *args, **kwargs):
    
    43
    -        if sys.platform.startswith('linux'):
    
    44
    -            backend = 'linux'
    
    45
    -        else:
    
    46
    -            backend = 'unix'
    
    47
    -
    
    48 43
             # Meant for testing purposes and therefore hidden in the
    
    49 44
             # deepest corners of the source code. Try not to abuse this,
    
    50 45
             # please?
    
    51 46
             if os.getenv('BST_FORCE_BACKEND'):
    
    52 47
                 backend = os.getenv('BST_FORCE_BACKEND')
    
    48
    +        elif sys.platform.startswith('linux'):
    
    49
    +            backend = 'linux'
    
    50
    +        elif sys.platform.startswith('darwin'):
    
    51
    +            backend = 'darwin'
    
    52
    +        else:
    
    53
    +            backend = 'unix'
    
    53 54
     
    
    54 55
             if backend == 'linux':
    
    55 56
                 from .linux import Linux as PlatformImpl
    
    57
    +        elif backend == 'darwin':
    
    58
    +            from .darwin import Darwin as PlatformImpl
    
    56 59
             elif backend == 'unix':
    
    57 60
                 from .unix import Unix as PlatformImpl
    
    58 61
             else:
    
    ... ... @@ -92,3 +95,6 @@ class Platform():
    92 95
         def create_sandbox(self, *args, **kwargs):
    
    93 96
             raise ImplError("Platform {platform} does not implement create_sandbox()"
    
    94 97
                             .format(platform=type(self).__name__))
    
    98
    +
    
    99
    +    def get_cpu_count(self):
    
    100
    +        return str(min(len(os.sched_getaffinity(0)), 8))

  • buildstream/_platform/unix.py
    ... ... @@ -18,6 +18,7 @@
    18 18
     #        Tristan Maat <tristan maat codethink co uk>
    
    19 19
     
    
    20 20
     import os
    
    21
    +import resource
    
    21 22
     
    
    22 23
     from .._artifactcache.cascache import CASCache
    
    23 24
     from .._exceptions import PlatformError
    
    ... ... @@ -33,6 +34,13 @@ class Unix(Platform):
    33 34
             super().__init__(context)
    
    34 35
             self._artifact_cache = CASCache(context)
    
    35 36
     
    
    37
    +        # Need to set resources for _frontend/app.py as this is dependent on the platform
    
    38
    +        # SafeHardlinks FUSE needs to hold file descriptors for all processes in the sandbox.
    
    39
    +        # Avoid hitting the limit too quickly.
    
    40
    +        limits = resource.getrlimit(resource.RLIMIT_NOFILE)
    
    41
    +        if limits[0] != limits[1]:
    
    42
    +            resource.setrlimit(resource.RLIMIT_NOFILE, (limits[1], limits[1]))
    
    43
    +
    
    36 44
             # Not necessarily 100% reliable, but we want to fail early.
    
    37 45
             if os.geteuid() != 0:
    
    38 46
                 raise PlatformError("Root privileges are required to run without bubblewrap.")
    

  • buildstream/_project.py
    ... ... @@ -19,6 +19,7 @@
    19 19
     #        Tiago Gomes <tiago gomes codethink co uk>
    
    20 20
     
    
    21 21
     import os
    
    22
    +import sys
    
    22 23
     from collections import Mapping, OrderedDict
    
    23 24
     from pluginbase import PluginBase
    
    24 25
     from . import utils
    
    ... ... @@ -38,6 +39,7 @@ from ._loader import Loader
    38 39
     from .element import Element
    
    39 40
     from ._message import Message, MessageType
    
    40 41
     from ._includes import Includes
    
    42
    +from .._platform import Platform
    
    41 43
     
    
    42 44
     
    
    43 45
     # Project Configuration file
    
    ... ... @@ -140,6 +142,7 @@ class Project():
    140 142
             profile_start(Topics.LOAD_PROJECT, self.directory.replace(os.sep, '-'))
    
    141 143
             self._load(parent_loader=parent_loader, tempdir=tempdir)
    
    142 144
             profile_end(Topics.LOAD_PROJECT, self.directory.replace(os.sep, '-'))
    
    145
    +        self.platform = Platform.get_platform()
    
    143 146
     
    
    144 147
             self._partially_loaded = True
    
    145 148
     
    
    ... ... @@ -594,7 +597,7 @@ class Project():
    594 597
             # Based on some testing (mainly on AWS), maximum effective
    
    595 598
             # max-jobs value seems to be around 8-10 if we have enough cores
    
    596 599
             # users should set values based on workload and build infrastructure
    
    597
    -        output.base_variables['max-jobs'] = str(min(len(os.sched_getaffinity(0)), 8))
    
    600
    +        output.base_variables['max-jobs'] = self.platform.get_cpu_count()
    
    598 601
     
    
    599 602
             # Export options into variables, if that was requested
    
    600 603
             output.options.export_variables(output.base_variables)
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]