Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2013-09-01 15:21:36 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2013-09-01 15:21:36 (GMT)
commitb5e5e75337b415218496552834b66f5953d7b3d6 (patch)
tree855b66528c4dc6ce69171a302d8aa10a8349d348
parentf63baafe795c24115fd9ed74e0b48470064e708d (diff)
Limit disk cache with percents and exact size
-rwxr-xr-xsugar-network7
-rw-r--r--sugar_network/client/__init__.py11
-rw-r--r--sugar_network/client/cache.py46
-rwxr-xr-xtests/units/client/cache.py3
4 files changed, 42 insertions, 25 deletions
diff --git a/sugar-network b/sugar-network
index 5276eae..8f07434 100755
--- a/sugar-network
+++ b/sugar-network
@@ -362,10 +362,9 @@ toolkit.cachedir.value = client.profile_path('tmp')
Option.seek('main', [
application.debug, porcelain, post_data, post_file, json, offline,
])
-Option.seek('client', [
- client.api_url, client.layers, client.ipc_port, client.local_root,
- client.no_dbus, client.anonymous, client.accept_language,
- ])
+Option.seek('main', [toolkit.cachedir])
+Option.seek('client', client)
+Option.seek('db', db)
locale.setlocale(locale.LC_ALL, '')
diff --git a/sugar_network/client/__init__.py b/sugar_network/client/__init__.py
index 077027a..d9e68fd 100644
--- a/sugar_network/client/__init__.py
+++ b/sugar_network/client/__init__.py
@@ -120,10 +120,17 @@ accept_language = Option(
name='accept-language', short_option='-l')
cache_limit = Option(
- 'the minimal disk free space, in percents, to preserve '
- 'while recycling disk cache',
+ 'the minimal disk free space, in bytes, to preserve while recycling '
+ 'disk cache; the final limit will be a minimal between --cache-limit '
+ 'and --cache-limit-percent',
default=10, type_cast=int, name='cache-limit')
+cache_limit_percent = Option(
+ 'the minimal disk free space, in percentage terms, to preserve while '
+ 'recycling disk cache; the final limit will be a minimal between '
+ '--cache-limit and --cache-limit-percent',
+ default=1024 * 1024 * 10, type_cast=int, name='cache-limit-percent')
+
cache_lifetime = Option(
'the number of days to keep unused objects on disk cache '
'before recycling',
diff --git a/sugar_network/client/cache.py b/sugar_network/client/cache.py
index 3043d25..685bdf4 100644
--- a/sugar_network/client/cache.py
+++ b/sugar_network/client/cache.py
@@ -41,22 +41,10 @@ class Cache(object):
def ensure(self, requested_size, temp_size=0):
self._ensure_open()
- stat = os.statvfs(client.local_root.value)
- if stat.f_blocks == 0:
- # TODO Sonds like a tmpfs or so
- return
- total = stat.f_blocks * stat.f_frsize
- free = stat.f_bfree * stat.f_frsize
-
- to_free = max(client.cache_limit.value * total / 100, temp_size) - \
- (free - requested_size)
+ to_free = self._to_free(requested_size, temp_size)
if to_free <= 0:
return
-
- _logger.debug('Recycle %s byte free=%d requested_size=%d temp_size=%d',
- to_free, free, requested_size, temp_size)
enforce(self._du >= to_free, 'No free disk space')
-
for guid, size, mtime in self._reversed_iter():
self._checkout(guid, (size, mtime))
to_free -= size
@@ -86,12 +74,8 @@ class Cache(object):
def recycle(self):
self._ensure_open()
- stat = os.statvfs(client.local_root.value)
- total = stat.f_blocks * stat.f_frsize
- free = stat.f_bfree * stat.f_frsize
- to_free = client.cache_limit.value * total / 100 - free
ts = time.time()
-
+ to_free = self._to_free(0, 0)
for guid, size, mtime in self._reversed_iter():
if to_free > 0:
self._checkout(guid, (size, mtime))
@@ -131,6 +115,32 @@ class Cache(object):
self._pool[guid] = (size, mtime)
self._du += size
+ def _to_free(self, requested_size, temp_size):
+ if not client.cache_limit.value and \
+ not client.cache_limit_percent.value:
+ return 0
+
+ stat = os.statvfs(client.local_root.value)
+ if stat.f_blocks == 0:
+ # TODO Sonds like a tmpfs or so
+ return 0
+
+ limit = 0
+ free = stat.f_bfree * stat.f_frsize
+ if client.cache_limit_percent.value:
+ total = stat.f_blocks * stat.f_frsize
+ limit = client.cache_limit_percent.value * total / 100
+ if client.cache_limit.value:
+ limit = min(limit, client.cache_limit.value)
+ to_free = max(limit, temp_size) - (free - requested_size)
+
+ if to_free > 0:
+ _logger.debug(
+ 'Need to recycle %d bytes, '
+ 'free_size=%d requested_size=%d temp_size=%d',
+ to_free, free, requested_size, temp_size)
+ return to_free
+
def _reversed_iter(self):
i = self._pool.head.prev
while True:
diff --git a/tests/units/client/cache.py b/tests/units/client/cache.py
index 871e62c..af05f43 100755
--- a/tests/units/client/cache.py
+++ b/tests/units/client/cache.py
@@ -13,7 +13,7 @@ from __init__ import tests
from sugar_network import db
from sugar_network.model.context import Context
from sugar_network.model.implementation import Implementation
-from sugar_network.client import cache_limit, cache_lifetime, IPCConnection
+from sugar_network.client import cache_limit, cache_limit_percent, cache_lifetime, IPCConnection
from sugar_network.client.cache import Cache
from sugar_network.toolkit import http
@@ -31,6 +31,7 @@ class CacheTest(tests.Test):
self.statvfs = statvfs
self.override(os, 'statvfs', lambda *args: statvfs())
cache_limit.value = 0
+ cache_limit_percent.value = 0
def test_open(self):
volume = db.Volume('db', [Context, Implementation])