WindowCacheStats.java

  1. /*
  2.  * Copyright (C) 2018, David Pursehouse <david.pursehouse@gmail.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.storage.file;

  11. import java.util.Map;

  12. import javax.management.MXBean;

  13. import org.eclipse.jgit.internal.storage.file.WindowCache;

  14. /**
  15.  * Cache statistics for {@link WindowCache}.
  16.  *
  17.  * @since 4.11
  18.  */
  19. @MXBean
  20. public interface WindowCacheStats {
  21.     /**
  22.      * @return the number of open files.
  23.      * @deprecated use {@link #getOpenFileCount()} instead
  24.      */
  25.     @Deprecated
  26.     public static int getOpenFiles() {
  27.         return (int) WindowCache.getInstance().getStats().getOpenFileCount();
  28.     }

  29.     /**
  30.      * @return the number of open bytes.
  31.      * @deprecated use {@link #getOpenByteCount()} instead
  32.      */
  33.     @Deprecated
  34.     public static long getOpenBytes() {
  35.         return WindowCache.getInstance().getStats().getOpenByteCount();
  36.     }

  37.     /**
  38.      * @return cache statistics for the WindowCache
  39.      * @since 5.1.13
  40.      */
  41.     public static WindowCacheStats getStats() {
  42.         return WindowCache.getInstance().getStats();
  43.     }

  44.     /**
  45.      * Number of cache hits
  46.      *
  47.      * @return number of cache hits
  48.      */
  49.     long getHitCount();

  50.     /**
  51.      * Ratio of cache requests which were hits defined as
  52.      * {@code hitCount / requestCount}, or {@code 1.0} when
  53.      * {@code requestCount == 0}. Note that {@code hitRate + missRate =~ 1.0}.
  54.      *
  55.      * @return the ratio of cache requests which were hits
  56.      */
  57.     default double getHitRatio() {
  58.         long requestCount = getRequestCount();
  59.         return (requestCount == 0) ? 1.0
  60.                 : (double) getHitCount() / requestCount;
  61.     }

  62.     /**
  63.      * Number of cache misses.
  64.      *
  65.      * @return number of cash misses
  66.      */
  67.     long getMissCount();

  68.     /**
  69.      * Ratio of cache requests which were misses defined as
  70.      * {@code missCount / requestCount}, or {@code 0.0} when
  71.      * {@code requestCount == 0}. Note that {@code hitRate + missRate =~ 1.0}.
  72.      * Cache misses include all requests which weren't cache hits, including
  73.      * requests which resulted in either successful or failed loading attempts.
  74.      *
  75.      * @return the ratio of cache requests which were misses
  76.      */
  77.     default double getMissRatio() {
  78.         long requestCount = getRequestCount();
  79.         return (requestCount == 0) ? 0.0
  80.                 : (double) getMissCount() / requestCount;
  81.     }

  82.     /**
  83.      * Number of successful loads
  84.      *
  85.      * @return number of successful loads
  86.      */
  87.     long getLoadSuccessCount();

  88.     /**
  89.      * Number of failed loads
  90.      *
  91.      * @return number of failed loads
  92.      */
  93.     long getLoadFailureCount();

  94.     /**
  95.      * Ratio of cache load attempts which threw exceptions. This is defined as
  96.      * {@code loadFailureCount / (loadSuccessCount + loadFailureCount)}, or
  97.      * {@code 0.0} when {@code loadSuccessCount + loadFailureCount == 0}.
  98.      *
  99.      * @return the ratio of cache loading attempts which threw exceptions
  100.      */
  101.     default double getLoadFailureRatio() {
  102.         long loadFailureCount = getLoadFailureCount();
  103.         long totalLoadCount = getLoadSuccessCount() + loadFailureCount;
  104.         return (totalLoadCount == 0) ? 0.0
  105.                 : (double) loadFailureCount / totalLoadCount;
  106.     }

  107.     /**
  108.      * Total number of times that the cache attempted to load new values. This
  109.      * includes both successful load operations, as well as failed loads. This
  110.      * is defined as {@code loadSuccessCount + loadFailureCount}.
  111.      *
  112.      * @return the {@code loadSuccessCount + loadFailureCount}
  113.      */
  114.     default long getLoadCount() {
  115.         return getLoadSuccessCount() + getLoadFailureCount();
  116.     }

  117.     /**
  118.      * Number of cache evictions
  119.      *
  120.      * @return number of evictions
  121.      */
  122.     long getEvictionCount();

  123.     /**
  124.      * Ratio of cache evictions. This is defined as
  125.      * {@code evictionCount / requestCount}, or {@code 0.0} when
  126.      * {@code requestCount == 0}.
  127.      *
  128.      * @return the ratio of cache loading attempts which threw exceptions
  129.      */
  130.     default double getEvictionRatio() {
  131.         long evictionCount = getEvictionCount();
  132.         long requestCount = getRequestCount();
  133.         return (requestCount == 0) ? 0.0
  134.                 : (double) evictionCount / requestCount;
  135.     }

  136.     /**
  137.      * Number of times the cache returned either a cached or uncached value.
  138.      * This is defined as {@code hitCount + missCount}.
  139.      *
  140.      * @return the {@code hitCount + missCount}
  141.      */
  142.     default long getRequestCount() {
  143.         return getHitCount() + getMissCount();
  144.     }

  145.     /**
  146.      * Average time in nanoseconds for loading new values. This is
  147.      * {@code totalLoadTime / (loadSuccessCount + loadFailureCount)}.
  148.      *
  149.      * @return the average time spent loading new values
  150.      */
  151.     default double getAverageLoadTime() {
  152.         long totalLoadCount = getLoadSuccessCount() + getLoadFailureCount();
  153.         return (totalLoadCount == 0) ? 0.0
  154.                 : (double) getTotalLoadTime() / totalLoadCount;
  155.     }

  156.     /**
  157.      * Total time in nanoseconds the cache spent loading new values.
  158.      *
  159.      * @return the total number of nanoseconds the cache has spent loading new
  160.      *         values
  161.      */
  162.     long getTotalLoadTime();

  163.     /**
  164.      * Number of pack files kept open by the cache
  165.      *
  166.      * @return number of files kept open by cache
  167.      */
  168.     long getOpenFileCount();

  169.     /**
  170.      * Number of bytes cached
  171.      *
  172.      * @return number of bytes cached
  173.      */
  174.     long getOpenByteCount();

  175.     /**
  176.      * Number of bytes cached per repository
  177.      *
  178.      * @return number of bytes cached per repository
  179.      */
  180.     Map<String, Long> getOpenByteCountPerRepository();

  181.     /**
  182.      * Reset counters. Does not reset open bytes and open files counters.
  183.      */
  184.     void resetCounters();
  185. }