#!/usr/bin/env python3 """analyze-kconfig - Generate a model for KConfig selections analyze-kconfig builds a model determining system attributes (e.g. ROM or RAM usage) based on KConfig configuration variables. Only boolean variables are supported at the moment. """ import argparse import json import kconfiglib import logging import os import numpy as np from dfatool.loader import KConfigAttributes from dfatool.model import KConfigModel def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__ ) parser.add_argument( "--failing-symbols", action="store_true", help="Show Kconfig symbols related to build failures. Must be used with an experiment result directory.", ) parser.add_argument( "--nop-symbols", action="store_true", help="Show Kconfig symbols which are only present in a single configuration. Must be used with an experiment result directory.", ) parser.add_argument( "--export-tree", type=str, help="Export decision tree model to file", metavar="FILE", ) parser.add_argument( "--config", type=str, help="Show model results for symbols in .config file", metavar="FILE", ) parser.add_argument( "--attribute", choices=["rom", "ram"], default="rom", help="Model attribute" ) parser.add_argument( "--max-loss", type=float, help="Maximum acceptable model loss for DecisionTree Leaves", default=10, ) parser.add_argument( "--log-level", default=logging.INFO, type=lambda level: getattr(logging, level.upper()), help="Set log level", ) parser.add_argument( "--info", action="store_true", help="Show Kconfig and benchmark information" ) parser.add_argument( "--sample-size", type=int, help="Restrict model generation to N random samples", metavar="N", ) parser.add_argument("kconfig_path", type=str, help="Path to Kconfig file") parser.add_argument( "model", type=str, help="Path to experiment results directory or model.json file", ) args = parser.parse_args() if isinstance(args.log_level, int): logging.basicConfig(level=args.log_level) else: print(f"Invalid log level. Setting log level to INFO.", file=sys.stderr) if os.path.isdir(args.model): data = KConfigAttributes(args.kconfig_path, args.model) if args.failing_symbols: show_failing_symbols(data) if args.nop_symbols: show_nop_symbols(data) if args.sample_size: shuffled_data_indices = np.random.permutation(np.arange(len(data.data))) sample_indices = shuffled_data_indices[: args.sample_size] model = KConfigModel.from_benchmark( data, args.attribute, indices=sample_indices ) else: model = KConfigModel.from_benchmark(data, args.attribute) if args.max_loss: model.max_loss = args.max_loss model.build_tree() else: with open(args.model, "r") as f: model = KConfigModel.from_json(json.load(f)) if args.info: print("TODO") if args.export_tree: with open(args.export_tree, "w") as f: json.dump(model.to_json(), f) if args.config: kconf = kconfiglib.Kconfig(args.kconfig_path) kconf.load_config(args.config) print(f"Model result for .config: {model.value_for_config(kconf)}") for symbol in model.symbols: kconf2 = kconfiglib.Kconfig(args.kconfig_path) kconf2.load_config(args.config) kconf_sym = kconf2.syms[symbol] if kconf_sym.tri_value == 0 and 2 in kconf_sym.assignable: kconf_sym.set_value(2) elif kconf_sym.tri_value == 2 and 0 in kconf_sym.assignable: kconf_sym.set_value(0) else: continue # specific to multipass: # Do not suggest changes which affect the application skip = False num_changes = 0 changed_symbols = list() for i, csymbol in enumerate(model.symbols): if kconf.syms[csymbol].tri_value != kconf2.syms[csymbol].tri_value: num_changes += 1 changed_symbols.append(csymbol) if ( csymbol.startswith("app_") and kconf.syms[csymbol].tri_value != kconf2.syms[csymbol].tri_value ): skip = True break if skip: continue try: model_diff = model.value_for_config(kconf2) - model.value_for_config( kconf ) if kconf_sym.choice: print( f"Setting {kconf_sym.choice.name} to {kconf_sym.name} changes {num_changes:2d} symbols, model change: {model_diff:+5.0f}" ) else: print( f"Setting {symbol} to {kconf_sym.str_value} changes {num_changes:2d} symbols, model change: {model_diff:+5.0f}" ) except TypeError: if kconf_sym.choice: print( f"Setting {kconf_sym.choice.name} to {kconf_sym.name} changes {num_changes:2d} symbols, model is undefined" ) else: print( f"Setting {symbol} to {kconf_sym.str_value} changes {num_changes:2d} symbols, model is undefined" ) for changed_symbol in changed_symbols: print( f" {changed_symbol:30s} -> {kconf2.syms[changed_symbol].str_value}" ) def show_failing_symbols(data): for symbol in data.symbol_names: failed_true = len( list(filter(lambda config: config[symbol] == True, data.failures)) ) failed_false = len( list(filter(lambda config: config[symbol] == False, data.failures)) ) success_true = len( list(filter(lambda config: config[0][symbol] == True, data.data)) ) success_false = len( list(filter(lambda config: config[0][symbol] == False, data.data)) ) if success_false == 0 and failed_false > 0: print(f"Setting {symbol} to n reliably causes the build to fail") if success_true == 0 and failed_true > 0: print(f"Setting {symbol} to y reliably causes the build to fail") def show_nop_symbols(data): for symbol in data.symbol_names: true_count = len( list(filter(lambda config: config[symbol] == True, data.failures)) ) + len(list(filter(lambda config: config[0][symbol] == True, data.data))) false_count = len( list(filter(lambda config: config[symbol] == False, data.failures)) ) + len(list(filter(lambda config: config[0][symbol] == False, data.data))) if false_count == 0: print(f"Symbol {symbol} is never n") if true_count == 0: print(f"Symbol {symbol} is never y") pass if __name__ == "__main__": main()