-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathCommandStats.cs
More file actions
127 lines (112 loc) · 3.94 KB
/
CommandStats.cs
File metadata and controls
127 lines (112 loc) · 3.94 KB
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
124
125
126
127
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Runtime.CompilerServices;
namespace Garnet.server
{
/// <summary>
/// Per-command statistics entry tracking calls, failures, and rejections.
/// Follows the Redis COMMANDSTATS convention.
/// </summary>
public struct CommandStatsEntry
{
/// <summary>
/// Total number of times this command was called.
/// </summary>
public ulong Calls;
/// <summary>
/// Total number of times this command failed (returned an error response).
/// </summary>
public ulong FailedCalls;
/// <summary>
/// Total number of times this command was rejected before execution (e.g., ACL denied, OOM).
/// </summary>
public ulong RejectedCalls;
}
/// <summary>
/// Tracks per-command usage statistics for built-in commands.
/// Array-indexed by RespCommand enum value for O(1) access.
/// Each session owns its own instance (single-writer, no locking needed).
/// </summary>
public class CommandStats
{
/// <summary>
/// Number of entries in the stats array, sized to hold all valid RespCommand values.
/// </summary>
internal static readonly int EntryCount = (int)RespCommandExtensions.LastValidCommand + 1;
/// <summary>
/// Per-command statistics entries indexed by (int)RespCommand.
/// </summary>
internal CommandStatsEntry[] entries;
/// <summary>
/// Creates a new CommandStats instance with zeroed entries.
/// </summary>
public CommandStats()
{
entries = new CommandStatsEntry[EntryCount];
}
/// <summary>
/// Increment the calls counter for the given command.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IncrementCalls(RespCommand cmd)
{
ushort idx = (ushort)cmd;
if (idx < entries.Length)
entries[idx].Calls++;
}
/// <summary>
/// Increment the failed calls counter for the given command.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IncrementFailed(RespCommand cmd)
{
ushort idx = (ushort)cmd;
if (idx < entries.Length)
entries[idx].FailedCalls++;
}
/// <summary>
/// Increment the rejected calls counter for the given command.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IncrementRejected(RespCommand cmd)
{
ushort idx = (ushort)cmd;
if (idx < entries.Length)
entries[idx].RejectedCalls++;
}
/// <summary>
/// Get the stats entry for the given command.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CommandStatsEntry GetEntry(RespCommand cmd)
{
ushort idx = (ushort)cmd;
if (idx < entries.Length)
return entries[idx];
return default;
}
/// <summary>
/// Add another CommandStats instance into this one (for aggregation).
/// </summary>
internal void Add(CommandStats other)
{
if (other?.entries == null)
return;
int len = Math.Min(entries.Length, other.entries.Length);
for (int i = 0; i < len; i++)
{
entries[i].Calls += other.entries[i].Calls;
entries[i].FailedCalls += other.entries[i].FailedCalls;
entries[i].RejectedCalls += other.entries[i].RejectedCalls;
}
}
/// <summary>
/// Reset all entries to zero.
/// </summary>
internal void Reset()
{
Array.Clear(entries, 0, entries.Length);
}
}
}