Skip to main content

sui_core/execution_cache/
metrics.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use tracing::trace;
5
6use mysten_metrics::LATENCY_SEC_BUCKETS;
7use prometheus::{
8    HistogramVec, IntCounter, IntCounterVec, IntGauge, Registry,
9    register_histogram_vec_with_registry, register_int_counter_vec_with_registry,
10    register_int_counter_with_registry, register_int_gauge_with_registry,
11};
12
13pub struct ExecutionCacheMetrics {
14    pub(crate) pending_notify_read: IntGauge,
15    pub(crate) cache_requests: IntCounterVec,
16    pub(crate) cache_hits: IntCounterVec,
17    pub(crate) cache_negative_hits: IntCounterVec,
18    pub(crate) cache_misses: IntCounterVec,
19    pub(crate) cache_writes: IntCounterVec,
20    pub(crate) expired_tickets: IntCounter,
21    pub(crate) backpressure_status: IntGauge,
22    pub(crate) backpressure_toggles: IntCounter,
23    pub(crate) implicit_system_object_read_waits: IntCounterVec,
24    pub(crate) implicit_system_object_read_wait_latency: HistogramVec,
25}
26
27impl ExecutionCacheMetrics {
28    pub fn new(registry: &Registry) -> Self {
29        Self {
30            pending_notify_read: register_int_gauge_with_registry!(
31                "pending_notify_read",
32                "Pending notify read requests",
33                registry,
34            )
35            .unwrap(),
36            // `request_type` is "object_by_version", "object_latest", "transaction", etc
37            // level in these metrics may be "uncommitted", "committed", "package_cache" or "db"
38            cache_requests: register_int_counter_vec_with_registry!(
39                "execution_cache_requests",
40                "Execution cache requests",
41                &["request_type", "level"],
42                registry,
43            )
44            .unwrap(),
45            cache_hits: register_int_counter_vec_with_registry!(
46                "execution_cache_hits",
47                "Execution cache hits",
48                &["request_type", "level"],
49                registry,
50            )
51            .unwrap(),
52            cache_negative_hits: register_int_counter_vec_with_registry!(
53                "execution_cache_negative_hits",
54                "Execution cache negative hits",
55                &["request_type", "level"],
56                registry,
57            )
58            .unwrap(),
59            cache_misses: register_int_counter_vec_with_registry!(
60                "execution_cache_misses",
61                "Execution cache misses",
62                &["request_type", "level"],
63                registry,
64            )
65            .unwrap(),
66
67            // `collection` should be "object", "marker", "transaction_effects", etc
68            cache_writes: register_int_counter_vec_with_registry!(
69                "execution_cache_writes",
70                "Execution cache writes",
71                &["collection"],
72                registry,
73            )
74            .unwrap(),
75
76            expired_tickets: register_int_counter_with_registry!(
77                "execution_cache_expired_tickets",
78                "Failed inserts to monotonic caches because of expired tickets",
79                registry,
80            )
81            .unwrap(),
82            backpressure_status: register_int_gauge_with_registry!(
83                "execution_cache_backpressure_status",
84                "Backpressure status (1 = on, 0 = off)",
85                registry,
86            )
87            .unwrap(),
88            backpressure_toggles: register_int_counter_with_registry!(
89                "execution_cache_backpressure_toggles",
90                "Number of times backpressure was turned on or off",
91                registry,
92            )
93            .unwrap(),
94            implicit_system_object_read_waits: register_int_counter_vec_with_registry!(
95                "implicit_system_object_read_waits",
96                "Number of times execution blocked because a system object read during \
97                 execution had not yet caught up locally to the required version",
98                &["object_id"],
99                registry,
100            )
101            .unwrap(),
102            implicit_system_object_read_wait_latency: register_histogram_vec_with_registry!(
103                "implicit_system_object_read_wait_latency",
104                "Seconds execution blocked waiting for a system object to catch up locally \
105                 to the required version",
106                &["object_id"],
107                LATENCY_SEC_BUCKETS.to_vec(),
108                registry,
109            )
110            .unwrap(),
111        }
112    }
113
114    pub(crate) fn record_cache_request(&self, request_type: &'static str, level: &'static str) {
115        trace!(target: "cache_metrics", "Cache request: {} {}", request_type, level);
116        self.cache_requests
117            .with_label_values(&[request_type, level])
118            .inc();
119    }
120
121    pub(crate) fn record_cache_multi_request(
122        &self,
123        request_type: &'static str,
124        level: &'static str,
125        count: usize,
126    ) {
127        trace!(
128            target: "cache_metrics",
129            "Cache multi request: {} {} count: {}",
130            request_type,
131            level,
132            count
133        );
134        self.cache_requests
135            .with_label_values(&[request_type, level])
136            .inc_by(count as u64);
137    }
138
139    pub(crate) fn record_cache_hit(&self, request_type: &'static str, level: &'static str) {
140        trace!(target: "cache_metrics", "Cache hit: {} {}", request_type, level);
141        self.cache_hits
142            .with_label_values(&[request_type, level])
143            .inc();
144    }
145
146    pub(crate) fn record_cache_miss(&self, request_type: &'static str, level: &'static str) {
147        trace!(target: "cache_metrics", "Cache miss: {} {}", request_type, level);
148        self.cache_misses
149            .with_label_values(&[request_type, level])
150            .inc();
151    }
152
153    pub(crate) fn record_cache_negative_hit(
154        &self,
155        request_type: &'static str,
156        level: &'static str,
157    ) {
158        trace!(target: "cache_metrics", "Cache negative hit: {} {}", request_type, level);
159        self.cache_negative_hits
160            .with_label_values(&[request_type, level])
161            .inc();
162    }
163
164    pub(crate) fn record_cache_write(&self, collection: &'static str) {
165        self.cache_writes.with_label_values(&[collection]).inc();
166    }
167
168    pub(crate) fn record_ticket_expiry(&self) {
169        self.expired_tickets.inc();
170    }
171}