From e8f545861df4696cb8709531a32b3631fd395d11 Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Fri, 13 Jul 2018 15:28:23 +0100 Subject: [PATCH] fw: cache target info Cache target info after pulling it from the device. Attempt to retrieve from cache before querying target. --- wa/framework/configuration/core.py | 4 ++++ wa/framework/target/info.py | 34 ++++++++++++++++++++++++++++++ wa/framework/target/manager.py | 8 +++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/wa/framework/configuration/core.py b/wa/framework/configuration/core.py index 93d03ffe..a65a8047 100644 --- a/wa/framework/configuration/core.py +++ b/wa/framework/configuration/core.py @@ -498,6 +498,10 @@ class MetaConfiguration(Configuration): def additional_packages_file(self): return os.path.join(self.user_directory, 'packages') + @property + def target_info_cache_file(self): + return os.path.join(self.cache_directory, 'targets.json') + def __init__(self, environ=None): super(MetaConfiguration, self).__init__() if environ is None: diff --git a/wa/framework/target/info.py b/wa/framework/target/info.py index 69d19009..70a01aa0 100644 --- a/wa/framework/target/info.py +++ b/wa/framework/target/info.py @@ -14,12 +14,16 @@ # # pylint: disable=protected-access +import os from copy import copy from devlib import AndroidTarget, TargetError from devlib.target import KernelConfig, KernelVersion, Cpuinfo from devlib.utils.android import AndroidProperties +from wa.framework.configuration.core import settings +from wa.utils.serializer import read_pod, write_pod + def cpuinfo_from_pod(pod): cpuinfo = Cpuinfo('') @@ -226,6 +230,36 @@ def get_target_info(target): return info +def read_target_info_cache(): + if not os.path.exists(settings.cache_directory): + os.makedirs(settings.cache_directory) + if not os.path.isfile(settings.target_info_cache_file): + return {} + return read_pod(settings.target_info_cache_file) + + +def write_target_info_cache(cache): + if not os.path.exists(settings.cache_directory): + os.makedirs(settings.cache_directory) + write_pod(cache, settings.target_info_cache_file) + + +def get_target_info_from_cache(system_id): + cache = read_target_info_cache() + pod = cache.get(system_id, None) + if not pod: + return None + return TargetInfo.from_pod(pod) + + +def cache_target_info(target_info, overwrite=False): + cache = read_target_info_cache() + if target_info.system_id in cache and not overwrite: + raise ValueError('TargetInfo for {} is already in cache.'.format(target_info.system_id)) + cache[target_info.system_id] = target_info.to_pod() + write_target_info_cache(cache) + + class TargetInfo(object): @staticmethod diff --git a/wa/framework/target/manager.py b/wa/framework/target/manager.py index e97ec2c0..b3b1a737 100644 --- a/wa/framework/target/manager.py +++ b/wa/framework/target/manager.py @@ -24,7 +24,7 @@ from wa.framework.plugin import Parameter from wa.framework.target.descriptor import (get_target_description, instantiate_target, instantiate_assistant) -from wa.framework.target.info import get_target_info +from wa.framework.target.info import get_target_info, get_target_info_from_cache, cache_target_info from wa.framework.target.runtime_parameter_manager import RuntimeParameterManager @@ -89,7 +89,11 @@ class TargetManager(object): @memoized def get_target_info(self): - return get_target_info(self.target) + info = get_target_info_from_cache(self.target.system_id) + if info is None: + info = get_target_info(self.target) + cache_target_info(info) + return info def reboot(self, context, hard=False): with signal.wrap('REBOOT', self, context):