From 26e5ff81650e3cf8171d8b2bb1a9839e2994015e Mon Sep 17 00:00:00 2001 From: Ekaterina Romanova Date: Fri, 2 Mar 2018 03:51:27 +0000 Subject: [PATCH] [ThinLTO] Added a couple of C LTO API interfaces to control the cache policy. - thinlto_codegen_set_cache_size_bytes to control the absolute size of cache directory. - thinlto_codegen_set_cache_size_files the size and amount of files in cache directory. These functions have been supported in C++ LTO API for a long time, but were absent in C LTO API. Differential Revision: https://reviews.llvm.org/D42446 llvm-svn: 326537 --- include/llvm-c/lto.h | 24 ++++++++++++- .../llvm/LTO/legacy/ThinLTOCodeGenerator.h | 15 ++++++++ test/ThinLTO/X86/cache.ll | 34 +++++++++++++++++++ tools/llvm-lto/llvm-lto.cpp | 10 ++++++ tools/lto/lto.cpp | 10 ++++++ tools/lto/lto.exports | 6 ++-- 6 files changed, 96 insertions(+), 3 deletions(-) diff --git a/include/llvm-c/lto.h b/include/llvm-c/lto.h index 4eee5f1f113..65d172dea18 100644 --- a/include/llvm-c/lto.h +++ b/include/llvm-c/lto.h @@ -44,7 +44,7 @@ typedef bool lto_bool_t; * @{ */ -#define LTO_API_VERSION 21 +#define LTO_API_VERSION 22 /** * \since prior to LTO_API_VERSION=3 @@ -816,6 +816,28 @@ extern void thinlto_codegen_set_final_cache_size_relative_to_available_space( extern void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg, unsigned expiration); +/** + * Sets the maximum size of the cache directory (in bytes). A value over the + * amount of available space on the disk will be reduced to the amount of + * available space. An unspecified default value will be applied. A value of 0 + * will be ignored. + * + * \since LTO_API_VERSION=22 + */ +extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg, + unsigned max_size_bytes); + +/** + * Sets the maximum number of files in the cache directory. An unspecified + * default value will be applied. A value of 0 will be ignored. + * + * \since LTO_API_VERSION=22 + */ +extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg, + unsigned max_size_files); + + + /** * @} // endgroup LLVMCTLTO_CACHING */ diff --git a/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h b/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h index e47456445f0..b32a972542c 100644 --- a/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h +++ b/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h @@ -184,6 +184,21 @@ public: CacheOptions.Policy.MaxSizePercentageOfAvailableSpace = Percentage; } + /// Cache policy: the maximum size for the cache directory in bytes. A value + /// over the amount of available space on the disk will be reduced to the + /// amount of available space. A value of 0 will be ignored. + void setCacheMaxSizeBytes(unsigned MaxSizeBytes) { + if (MaxSizeBytes) + CacheOptions.Policy.MaxSizeBytes = MaxSizeBytes; + } + + /// Cache policy: the maximum number of files in the cache directory. A value + /// of 0 will be ignored. + void setCacheMaxSizeFiles(unsigned MaxSizeFiles) { + if (MaxSizeFiles) + CacheOptions.Policy.MaxSizeFiles = MaxSizeFiles; + } + /**@}*/ /// Set the path to a directory where to save temporaries at various stages of diff --git a/test/ThinLTO/X86/cache.ll b/test/ThinLTO/X86/cache.ll index 985e741099a..f3fcaf30987 100644 --- a/test/ThinLTO/X86/cache.ll +++ b/test/ThinLTO/X86/cache.ll @@ -80,6 +80,40 @@ ; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-pruning-interval 0 ; RUN: not ls %t.cache/llvmcache-foo +; Verify that specifying max size for the cache directory prunes it to this +; size, removing the largest files first. +; RUN: rm -Rf %t.cache && mkdir %t.cache +; Create cache files with different sizes. +; Only 8B, 16B and 76B files should stay after pruning. +; RUN: %python -c "print(' ' * 1023)" > %t.cache/llvmcache-foo-1024 +; RUN: %python -c "print(' ' * 15)" > %t.cache/llvmcache-foo-16 +; RUN: %python -c "print(' ' * 7)" > %t.cache/llvmcache-foo-8 +; RUN: %python -c "print(' ' * 75)" > %t.cache/llvmcache-foo-76 +; RUN: %python -c "print(' ' * 76)" > %t.cache/llvmcache-foo-77 +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-max-size-bytes 100 +; RUN: ls %t.cache/llvmcache-foo-16 +; RUN: ls %t.cache/llvmcache-foo-8 +; RUN: ls %t.cache/llvmcache-foo-76 +; RUN: not ls %t.cache/llvmcache-foo-1024 +; RUN: not ls %t.cache/llvmcache-foo-77 + +; Verify that specifying max number of files in the cache directory prunes +; it to this amount, removing the largest files first. +; RUN: rm -Rf %t.cache && mkdir %t.cache +; Create cache files with different sizes. +; Only 8B and 16B files should stay after pruning. +; RUN: %python -c "print(' ' * 1023)" > %t.cache/llvmcache-foo-1024 +; RUN: %python -c "print(' ' * 15)" > %t.cache/llvmcache-foo-16 +; RUN: %python -c "print(' ' * 7)" > %t.cache/llvmcache-foo-8 +; RUN: %python -c "print(' ' * 75)" > %t.cache/llvmcache-foo-76 +; RUN: %python -c "print(' ' * 76)" > %t.cache/llvmcache-foo-77 +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-max-size-files 2 +; RUN: ls %t.cache/llvmcache-foo-16 +; RUN: ls %t.cache/llvmcache-foo-8 +; RUN: not ls %t.cache/llvmcache-foo-76 +; RUN: not ls %t.cache/llvmcache-foo-1024 +; RUN: not ls %t.cache/llvmcache-foo-77 + target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0" diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp index 9e5458f4ad9..87c998c9032 100644 --- a/tools/llvm-lto/llvm-lto.cpp +++ b/tools/llvm-lto/llvm-lto.cpp @@ -160,6 +160,14 @@ static cl::opt ThinLTOCachePruningInterval("thinlto-cache-pruning-interval", cl::init(1200), cl::desc("Set ThinLTO cache pruning interval.")); +static cl::opt + ThinLTOCacheMaxSizeBytes("thinlto-cache-max-size-bytes", + cl::desc("Set ThinLTO cache pruning directory maximum size in bytes.")); + +static cl::opt + ThinLTOCacheMaxSizeFiles("thinlto-cache-max-size-files", cl::init(1000000), + cl::desc("Set ThinLTO cache pruning directory maximum number of files.")); + static cl::opt ThinLTOSaveTempsPrefix( "thinlto-save-temps", cl::desc("Save ThinLTO temp files using filenames created by adding " @@ -475,6 +483,8 @@ public: ThinGenerator.setTargetOptions(Options); ThinGenerator.setCacheDir(ThinLTOCacheDir); ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval); + ThinGenerator.setCacheMaxSizeFiles(ThinLTOCacheMaxSizeFiles); + ThinGenerator.setCacheMaxSizeBytes(ThinLTOCacheMaxSizeBytes); ThinGenerator.setFreestanding(EnableFreestanding); // Add all the exported symbols to the table of symbols to preserve. diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index e6843986dbe..997c16e0fee 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -586,6 +586,16 @@ void thinlto_codegen_set_final_cache_size_relative_to_available_space( return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage); } +void thinlto_codegen_set_cache_size_bytes( + thinlto_code_gen_t cg, unsigned MaxSizeBytes) { + return unwrap(cg)->setCacheMaxSizeBytes(MaxSizeBytes); +} + +void thinlto_codegen_set_cache_size_files( + thinlto_code_gen_t cg, unsigned MaxSizeFiles) { + return unwrap(cg)->setCacheMaxSizeFiles(MaxSizeFiles); +} + void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg, const char *save_temps_dir) { return unwrap(cg)->setSaveTempsDir(save_temps_dir); diff --git a/tools/lto/lto.exports b/tools/lto/lto.exports index 2e09026ae50..abde3894cb2 100644 --- a/tools/lto/lto.exports +++ b/tools/lto/lto.exports @@ -56,15 +56,17 @@ thinlto_codegen_set_pic_model thinlto_codegen_set_cache_dir thinlto_codegen_set_cache_pruning_interval thinlto_codegen_set_cache_entry_expiration +thinlto_codegen_set_final_cache_size_relative_to_available_space +thinlto_codegen_set_cache_size_bytes +thinlto_codegen_set_cache_size_files thinlto_codegen_set_savetemps_dir thinlto_codegen_set_cpu thinlto_debug_options lto_module_is_thinlto thinlto_codegen_add_must_preserve_symbol thinlto_codegen_add_cross_referenced_symbol -thinlto_codegen_set_final_cache_size_relative_to_available_space thinlto_codegen_set_codegen_only thinlto_codegen_disable_codegen thinlto_module_get_num_object_files thinlto_module_get_object_file -thinlto_set_generated_objects_dir \ No newline at end of file +thinlto_set_generated_objects_dir