]> source.dussan.org Git - jgit.git/commitdiff
DfsBlockCacheConfigs: add debug configuration print 52/1198152/17
authorLaura Hamelin <haowl@google.com>
Mon, 29 Jul 2024 23:12:15 +0000 (16:12 -0700)
committerLaura Hamelin <haowl@google.com>
Tue, 15 Oct 2024 21:56:51 +0000 (14:56 -0700)
This will write out configuration values on a line by line basis to a
given PrintWriter.
Primary usage is as a semi-formatted debug print of the configuration
values used by dfs block cache.

Change-Id: I96724262245e4aa3423734a8b10de83322c4f89f

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfigTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfig.java

index 4b7aae05f0aa8ddac5de4bc60dd4ece9edff909b..65774e6d6ca78afbfd40ee9d9dfcbb25e086f91e 100644 (file)
@@ -54,7 +54,12 @@ import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThrows;
 
+import java.io.ByteArrayOutputStream;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 import org.eclipse.jgit.internal.JGitText;
@@ -250,6 +255,45 @@ public class DfsBlockCacheConfigTest {
                                () -> new DfsBlockCacheConfig().fromConfig(config));
        }
 
+       @Test
+       public void writeConfigurationDebug_writesConfigsToWriter()
+                       throws Exception {
+               Config config = new Config();
+               config.setLong(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
+                               CONFIG_KEY_BLOCK_LIMIT, 50 * 1024);
+               config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
+                               CONFIG_KEY_BLOCK_SIZE, 1024);
+               config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
+                               CONFIG_KEY_CONCURRENCY_LEVEL, 3);
+               config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
+                               CONFIG_KEY_STREAM_RATIO, "0.5");
+               addPackExtConfigEntry(config, "pack", List.of(PackExt.PACK),
+                               /* blockLimit= */ 20 * 512, /* blockSize= */ 512);
+
+               DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig()
+                               .fromConfig(config);
+               Map<PackExt, Integer> hotmap = Map.of(PackExt.PACK, 10);
+               cacheConfig.setCacheHotMap(hotmap);
+
+               ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+               cacheConfig.print(new PrintWriter(byteArrayOutputStream, true,
+                               StandardCharsets.UTF_8));
+
+               String writenConfig = byteArrayOutputStream
+                               .toString(StandardCharsets.UTF_8);
+
+               List<String> writenLines = Arrays.asList(writenConfig.split("\n"));
+               assertThat(writenLines,
+                               equalTo(List.of("Name: dfs", "  BlockLimit: " + (50 * 1024),
+                                               "  BlockSize: 1024", "  StreamRatio: 0.5",
+                                               "  ConcurrencyLevel: 3",
+                                               "  CacheHotMapEntry: " + PackExt.PACK + " : " + 10,
+                                               "  Name: dfs.pack", "    BlockLimit: " + 20 * 512,
+                                               "    BlockSize: 512", "    StreamRatio: 0.3",
+                                               "    ConcurrencyLevel: 32",
+                                               "    PackExts: " + List.of(PackExt.PACK))));
+       }
+
        private static void addPackExtConfigEntry(Config config, String configName,
                        List<PackExt> packExts, long blockLimit, int blockSize) {
                String packExtConfigName = CONFIG_DFS_CACHE_PREFIX + configName;
index 52eb9e0785ce5248cd69ec713ca6a094bc59f763..0d56689a67f93b95b42fa81555248489d041cee4 100644 (file)
@@ -19,6 +19,7 @@ import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CONCURRENCY_LEVEL;
 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PACK_EXTENSIONS;
 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
 
+import java.io.PrintWriter;
 import java.text.MessageFormat;
 import java.time.Duration;
 import java.util.ArrayList;
@@ -82,6 +83,51 @@ public class DfsBlockCacheConfig {
                packExtCacheConfigurations = Collections.emptyList();
        }
 
+       /**
+        * Print the current cache configuration to the given {@link PrintWriter}.
+        *
+        * @param writer
+        *            {@link PrintWriter} to write the cache's configuration to.
+        */
+       public void print(PrintWriter writer) {
+               print(/* linePrefix= */ "", /* pad= */ "  ", writer);
+       }
+
+       /**
+        * Print the current cache configuration to the given {@link PrintWriter}.
+        *
+        * @param linePrefix
+        *            prefix to prepend all writen lines with. Ex a string of 0 or
+        *            more " " entries.
+        * @param pad
+        *            filler used to extend linePrefix. Ex a multiple of " ".
+        * @param writer
+        *            {@link PrintWriter} to write the cache's configuration to.
+        */
+       private void print(String linePrefix, String pad, PrintWriter writer) {
+               String currentPrefixLevel = linePrefix;
+               if (!name.isEmpty() || !packExtCacheConfigurations.isEmpty()) {
+                       String name = this.name;
+                       if (name.isEmpty()) {
+                               name = "<Default>";
+                       }
+                       writer.println(linePrefix + "Name: " + name);
+                       currentPrefixLevel += pad;
+               }
+               writer.println(currentPrefixLevel + "BlockLimit: " + blockLimit);
+               writer.println(currentPrefixLevel + "BlockSize: " + blockSize);
+               writer.println(currentPrefixLevel + "StreamRatio: " + streamRatio);
+               writer.println(
+                               currentPrefixLevel + "ConcurrencyLevel: " + concurrencyLevel);
+               for (Map.Entry<PackExt, Integer> entry : cacheHotMap.entrySet()) {
+                       writer.println(currentPrefixLevel + "CacheHotMapEntry: "
+                                       + entry.getKey() + " : " + entry.getValue());
+               }
+               for (DfsBlockCachePackExtConfig extConfig : packExtCacheConfigurations) {
+                       extConfig.print(currentPrefixLevel, pad, writer);
+               }
+       }
+
        /**
         * Get the name for the block cache configured by this cache config.
         *
@@ -531,5 +577,11 @@ public class DfsBlockCacheConfig {
                        return new DfsBlockCachePackExtConfig(EnumSet.copyOf(packExts),
                                        dfsBlockCacheConfig);
                }
+
+               void print(String linePrefix, String pad, PrintWriter writer) {
+                       packExtCacheConfiguration.print(linePrefix, pad, writer);
+                       writer.println(linePrefix + pad + "PackExts: "
+                                       + packExts.stream().sorted().toList());
+               }
        }
-}
\ No newline at end of file
+}