Files
clang-p2996/lldb/test/API/functionalities/module_cache/simple_exe/TestModuleCacheSimple.py
Greg Clayton da816ca0cb Added the ability to cache the finalized symbol tables subsequent debug sessions to start faster.
This is an updated version of the https://reviews.llvm.org/D113789 patch with the following changes:
- We no longer modify modification times of the cache files
- Use LLVM caching and cache pruning instead of making a new cache mechanism (See DataFileCache.h/.cpp)
- Add signature to start of each file since we are not using modification times so we can tell when caches are stale and remove and re-create the cache file as files are changed
- Add settings to control the cache size, disk percentage and expiration in days to keep cache size under control

This patch enables symbol tables to be cached in the LLDB index cache directory. All cache files are in a single directory and the files use unique names to ensure that files from the same path will re-use the same file as files get modified. This means as files change, their cache files will be deleted and updated. The modification time of each of the cache files is not modified so that access based pruning of the cache can be implemented.

The symbol table cache files start with a signature that uniquely identifies a file on disk and contains one or more of the following items:
- object file UUID if available
- object file mod time if available
- object name for BSD archive .o files that are in .a files if available

If none of these signature items are available, then the file will not be cached. This keeps temporary object files from expressions from being cached.

When the cache files are loaded on subsequent debug sessions, the signature is compare and if the file has been modified (uuid changes, mod time changes, or object file mod time changes) then the cache file is deleted and re-created.

Module caching must be enabled by the user before this can be used:

symbols.enable-lldb-index-cache (boolean) = false

(lldb) settings set symbols.enable-lldb-index-cache true

There is also a setting that allows the user to specify a module cache directory that defaults to a directory that defaults to being next to the symbols.clang-modules-cache-path directory in a temp directory:

(lldb) settings show symbols.lldb-index-cache-path
/var/folders/9p/472sr0c55l9b20x2zg36b91h0000gn/C/lldb/IndexCache

If this setting is enabled, the finalized symbol tables will be serialized and saved to disc so they can be quickly loaded next time you debug.

Each module can cache one or more files in the index cache directory. The cache file names must be unique to a file on disk and its architecture and object name for .o files in BSD archives. This allows universal mach-o files to support caching multuple architectures in the same module cache directory. Making the file based on the this info allows this cache file to be deleted and replaced when the file gets updated on disk. This keeps the cache from growing over time during the compile/edit/debug cycle and prevents out of space issues.

If the cache is enabled, the symbol table will be loaded from the cache the next time you debug if the module has not changed.

The cache also has settings to control the size of the cache on disk. Each time LLDB starts up with the index cache enable, the cache will be pruned to ensure it stays within the user defined settings:

(lldb) settings set symbols.lldb-index-cache-expiration-days <days>

A value of zero will disable cache files from expiring when the cache is pruned. The default value is 7 currently.

(lldb) settings set symbols.lldb-index-cache-max-byte-size <size>

A value of zero will disable pruning based on a total byte size. The default value is zero currently.
(lldb) settings set symbols.lldb-index-cache-max-percent <percentage-of-disk-space>

A value of 100 will allow the disc to be filled to the max, a value of zero will disable percentage pruning. The default value is zero.

Reviewed By: labath, wallace

Differential Revision: https://reviews.llvm.org/D115324
2021-12-16 09:59:55 -08:00

100 lines
4.6 KiB
Python

"""Test the LLDB module cache funcionality."""
import glob
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import os
import time
class ModuleCacheTestcaseSimple(TestBase):
mydir = TestBase.compute_mydir(__file__)
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number in a(int) to break at.
self.cache_dir = os.path.join(self.getBuildDir(), 'lldb-module-cache')
# Set the lldb module cache directory to a directory inside the build
# artifacts directory so no other tests are interfered with.
self.runCmd('settings set symbols.lldb-index-cache-path "%s"' % (self.cache_dir))
self.runCmd('settings set symbols.enable-lldb-index-cache true')
self.build()
def get_module_cache_files(self, basename):
module_file_glob = os.path.join(self.cache_dir, "llvmcache-*%s*" % (basename))
return glob.glob(module_file_glob)
# Doesn't depend on any specific debug information.
@no_debug_info_test
def test(self):
"""
Test module cache functionality for a simple object file.
This will test that if we enable the module cache, we have a
corresponding index cache entry for the symbol table for the
executable. It also removes the executable, rebuilds so that the
modification time of the binary gets updated, and then creates a new
target and should cause the cache to get updated so the cache file
should get an updated modification time.
"""
exe = self.getBuildArtifact("a.out")
# Create a module with no depedencies.
target = self.createTestTarget(load_dependent_modules=False)
# Get the executable module and get the number of symbols to make
# sure the symbol table gets parsed and cached. The module cache is
# enabled in the setUp() function.
main_module = target.GetModuleAtIndex(0)
self.assertTrue(main_module.IsValid())
# Make sure the symbol table gets loaded and cached
main_module.GetNumSymbols()
cache_files = self.get_module_cache_files("a.out")
self.assertEqual(len(cache_files), 1,
"make sure there is only one cache file for 'a.out'")
symtab_cache_path = cache_files[0]
exe_mtime_1 = os.path.getmtime(exe)
symtab_mtime_1 = os.path.getmtime(symtab_cache_path)
# Now remove the executable and sleep for a few seconds to make sure we
# get a different creation and modification time for the file since some
# OSs store the modification time in seconds since Jan 1, 1970.
os.remove(exe)
self.assertEqual(os.path.exists(exe), False,
'make sure we were able to remove the executable')
time.sleep(2)
# Now rebuild the binary so it has a different content which should
# update the UUID to make the cache miss when it tries to load the
# symbol table from the binary at the same path.
self.build(dictionary={'CFLAGS_EXTRAS': '-DEXTRA_FUNCTION'})
self.assertEqual(os.path.exists(exe), True,
'make sure executable exists after rebuild')
# Make sure the modification time has changed or this test will fail.
exe_mtime_2 = os.path.getmtime(exe)
self.assertNotEqual(
exe_mtime_1,
exe_mtime_2,
"make sure the modification time of the executable has changed")
# Make sure the module cache still has an out of date cache with the
# same old modification time.
self.assertEqual(symtab_mtime_1,
os.path.getmtime(symtab_cache_path),
"check that the 'symtab' cache file modification time doesn't match the executable modification time after rebuild")
# Create a new target and get the symbols again, and make sure the cache
# gets updated for the symbol table cache
target = self.createTestTarget(load_dependent_modules=False)
main_module = target.GetModuleAtIndex(0)
self.assertTrue(main_module.IsValid())
main_module.GetNumSymbols()
self.assertEqual(os.path.exists(symtab_cache_path), True,
'make sure "symtab" cache files exists after cache is updated')
symtab_mtime_2 = os.path.getmtime(symtab_cache_path)
self.assertNotEqual(
symtab_mtime_1,
symtab_mtime_2,
'make sure modification time of "symtab-..." changed')