aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/AggregatedBlockCacheStats.java
blob: 295b702fa7966f04578246491c2617e3e5a7710e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (c) 2024, Google LLC and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.internal.storage.dfs;

import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheTable.BlockCacheStats;

import java.util.List;

import org.eclipse.jgit.internal.storage.pack.PackExt;

/**
 * Aggregates values for all given {@link BlockCacheStats}.
 */
class AggregatedBlockCacheStats implements BlockCacheStats {
	private final List<BlockCacheStats> blockCacheStats;

	static BlockCacheStats fromStatsList(
			List<BlockCacheStats> blockCacheStats) {
		if (blockCacheStats.size() == 1) {
			return blockCacheStats.get(0);
		}
		return new AggregatedBlockCacheStats(blockCacheStats);
	}

	private AggregatedBlockCacheStats(List<BlockCacheStats> blockCacheStats) {
		this.blockCacheStats = blockCacheStats;
	}

	@Override
	public String getName() {
		return AggregatedBlockCacheStats.class.getName();
	}

	@Override
	public long[] getCurrentSize() {
		long[] sums = emptyPackStats();
		for (BlockCacheStats blockCacheStatsEntry : blockCacheStats) {
			sums = add(sums, blockCacheStatsEntry.getCurrentSize());
		}
		return sums;
	}

	@Override
	public long[] getHitCount() {
		long[] sums = emptyPackStats();
		for (BlockCacheStats blockCacheStatsEntry : blockCacheStats) {
			sums = add(sums, blockCacheStatsEntry.getHitCount());
		}
		return sums;
	}

	@Override
	public long[] getMissCount() {
		long[] sums = emptyPackStats();
		for (BlockCacheStats blockCacheStatsEntry : blockCacheStats) {
			sums = add(sums, blockCacheStatsEntry.getMissCount());
		}
		return sums;
	}

	@Override
	public long[] getTotalRequestCount() {
		long[] sums = emptyPackStats();
		for (BlockCacheStats blockCacheStatsEntry : blockCacheStats) {
			sums = add(sums, blockCacheStatsEntry.getTotalRequestCount());
		}
		return sums;
	}

	@Override
	public long[] getHitRatio() {
		long[] hit = getHitCount();
		long[] miss = getMissCount();
		long[] ratio = new long[Math.max(hit.length, miss.length)];
		for (int i = 0; i < ratio.length; i++) {
			if (i >= hit.length) {
				ratio[i] = 0;
			} else if (i >= miss.length) {
				ratio[i] = 100;
			} else {
				long total = hit[i] + miss[i];
				ratio[i] = total == 0 ? 0 : hit[i] * 100 / total;
			}
		}
		return ratio;
	}

	@Override
	public long[] getEvictions() {
		long[] sums = emptyPackStats();
		for (BlockCacheStats blockCacheStatsEntry : blockCacheStats) {
			sums = add(sums, blockCacheStatsEntry.getEvictions());
		}
		return sums;
	}

	private static long[] emptyPackStats() {
		return new long[PackExt.values().length];
	}

	private static long[] add(long[] first, long[] second) {
		long[] sums = new long[Integer.max(first.length, second.length)];
		int i;
		for (i = 0; i < Integer.min(first.length, second.length); i++) {
			sums[i] = first[i] + second[i];
		}
		for (int j = i; j < first.length; j++) {
			sums[j] = first[i];
		}
		for (int j = i; j < second.length; j++) {
			sums[j] = second[i];
		}
		return sums;
	}
}