Josh pushed to branch Qinusty/491 at BuildStream / buildstream
Commits:
-
aa6ff04d
by Josh Smith at 2018-07-25T14:47:53Z
2 changed files:
Changes:
... | ... | @@ -197,30 +197,46 @@ class Context(): |
197 | 197 |
"\nValid values are, for example: 800M 10G 1T 50%\n"
|
198 | 198 |
.format(str(e))) from e
|
199 | 199 |
|
200 |
- # If we are asked not to set a quota, we set it to the maximum
|
|
201 |
- # disk space available minus a headroom of 2GB, such that we
|
|
202 |
- # at least try to avoid raising Exceptions.
|
|
200 |
+ if 'BST_TEST_SUITE' in os.environ:
|
|
201 |
+ headroom = 0
|
|
202 |
+ else:
|
|
203 |
+ headroom = 2e9
|
|
204 |
+ |
|
205 |
+ stat = os.statvfs(artifactdir_volume)
|
|
206 |
+ # Place a slight headroom (2e9 (2GB) on the cache_quota) into
|
|
207 |
+ # available_space to try and avoid exceptions.
|
|
203 | 208 |
#
|
204 | 209 |
# Of course, we might still end up running out during a build
|
205 | 210 |
# if we end up writing more than 2G, but hey, this stuff is
|
206 | 211 |
# already really fuzzy.
|
207 | 212 |
#
|
208 |
- if cache_quota is None:
|
|
209 |
- stat = os.statvfs(artifactdir_volume)
|
|
210 |
- # Again, the artifact directory may not yet have been
|
|
211 |
- # created
|
|
212 |
- if not os.path.exists(self.artifactdir):
|
|
213 |
- cache_size = 0
|
|
214 |
- else:
|
|
215 |
- cache_size = utils._get_dir_size(self.artifactdir)
|
|
216 |
- cache_quota = cache_size + stat.f_bsize * stat.f_bavail
|
|
213 |
+ available_space = (stat.f_bsize * stat.f_bavail) - headroom
|
|
217 | 214 |
|
218 |
- if 'BST_TEST_SUITE' in os.environ:
|
|
219 |
- headroom = 0
|
|
215 |
+ # Again, the artifact directory may not yet have been created yet
|
|
216 |
+ #
|
|
217 |
+ if not os.path.exists(self.artifactdir):
|
|
218 |
+ cache_size = 0
|
|
220 | 219 |
else:
|
221 |
- headroom = 2e9
|
|
220 |
+ cache_size = utils._get_dir_size(self.artifactdir)
|
|
222 | 221 |
|
223 |
- self.cache_quota = cache_quota - headroom
|
|
222 |
+ # Ensure system has enough storage for the cache_quota
|
|
223 |
+ #
|
|
224 |
+ # If cache_quota is none, set it to the maximum it could possibly be.
|
|
225 |
+ #
|
|
226 |
+ if cache_quota is None: # Infinity, set to max system storage
|
|
227 |
+ cache_quota = cache_size + available_space
|
|
228 |
+ elif cache_size + cache_quota > available_space:
|
|
229 |
+ raise LoadError(LoadErrorReason.INVALID_DATA,
|
|
230 |
+ ("Your system does not have enough available " +
|
|
231 |
+ "space to support the cache quota specified.\n" +
|
|
232 |
+ "You currently have:\n" +
|
|
233 |
+ "- {used} of cache in use at {local_cache_path}\n" +
|
|
234 |
+ "- {available} of available system storage.").format(
|
|
235 |
+ used=utils._pretty_size(cache_size),
|
|
236 |
+ local_cache_path=self.artifactdir,
|
|
237 |
+ available=utils._pretty_size(available_space)))
|
|
238 |
+ |
|
239 |
+ self.cache_quota = cache_quota
|
|
224 | 240 |
self.cache_lower_threshold = self.cache_quota / 2
|
225 | 241 |
|
226 | 242 |
# Load artifact share configuration
|
... | ... | @@ -608,6 +608,26 @@ def _parse_size(size, volume): |
608 | 608 |
return int(num) * 1024**units.index(unit)
|
609 | 609 |
|
610 | 610 |
|
611 |
+# _pretty_size()
|
|
612 |
+#
|
|
613 |
+# Converts a number of bytes into a string representation in K, M, G, T.
|
|
614 |
+#
|
|
615 |
+# Args:
|
|
616 |
+# size (int): The size to convert in bytes.
|
|
617 |
+# dec_places (int): The number of decimal places to output to.
|
|
618 |
+#
|
|
619 |
+# Returns:
|
|
620 |
+# (str): The string representation of the number of bytes in the largest
|
|
621 |
+def _pretty_size(size, dec_places=0):
|
|
622 |
+ psize = size
|
|
623 |
+ unit = 'B'
|
|
624 |
+ for unit in ('B', 'K', 'M', 'G', 'T'):
|
|
625 |
+ if psize < 1024:
|
|
626 |
+ break
|
|
627 |
+ else:
|
|
628 |
+ psize /= 1024
|
|
629 |
+ return "{size:g}{unit}".format(size=round(psize, dec_places), unit=unit)
|
|
630 |
+ |
|
611 | 631 |
# A sentinel to be used as a default argument for functions that need
|
612 | 632 |
# to distinguish between a kwarg set to None and an unset kwarg.
|
613 | 633 |
_sentinel = object()
|