NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * Per-model / per-sensor performance metrics (fps + inference latency).
20  *
21  * Self-contained module: depends only on glib + the small types it owns. Callers pass
22  * source_id / latency / now-timestamp as parameters; it never reaches into plugin globals.
23  * Surfaced (additively) through the model/status REST API and gated by the
24  * `attach-perf-metric` config flag (the caller owns the gate; this module is pure mechanism).
25  *
26  * THREADING CONTRACT (important):
27  * - modelmux_perf_mark_bin_in() : HOT PATH (bin-head probe), lock-free. Single writer per ModelMuxPerf.
28  * - modelmux_perf_mark_bin_out() : HOT PATH (bin-tail/provenance probe), lock-free. Single writer
29  * per ModelMuxPerf and per source_id (one bin/one shard serves a source).
30  * Only INCREMENTS monotonic 64-bit counters (atomic load/store on the
31  * target arch) -- never mutates container structure.
32  * - modelmux_perf_tick() : caller holds the bin lock (e.g. ib->lock); reads the monotonic
33  * counters and recomputes the published fps/latency snapshot.
34  * - modelmux_perf_get_model() /
35  * modelmux_perf_get_source() : caller holds the bin lock; read the snapshot only.
36  * No lock is taken inside this module; the single-writer-per-counter + atomic-64 invariant plus
37  * the caller's lock around tick/get is what makes it safe (mirrors the plugin's existing lock-free
38  * `acct` frame-accounting). Per-source storage is a FIXED array (no hot-path allocation), so there
39  * is no structural race between the lock-free writer and the locked reader.
40  */
41 
42 #ifndef GST_NVMODELMUX_PERF_H
43 #define GST_NVMODELMUX_PERF_H
44 
45 #include <glib.h>
46 
47 /* default + clamp for the measurement window (seconds). */
48 #define MM_PERF_DEFAULT_INTERVAL_SEC 5
49 #define MM_PERF_MIN_INTERVAL_SEC 1
50 /* max source_id tracked per model bin (flat, lock-free; matches the plugin's MM_ACCT_MAX). */
51 #define MM_PERF_MAX_SOURCES 256
52 
53 /*
54  * ModelMuxPerfCounter -- one sliding-window rate+latency counter. THE single source of truth for the
55  * fps/latency math: the delta/elapsed/divide-by-zero logic lives ONLY in modelmux_perf_counter_tick().
56  * Reused identically for the bin-aggregate counter AND every per-source counter.
57  */
58 typedef struct
59 {
60  /* monotonic totals -- bumped lock-free by modelmux_perf_counter_record() (hot path). */
61  guint64 frames;
62  guint64 lat_sum_us;
63  guint64 lat_n;
64  /* window bookkeeping + published snapshot -- written ONLY by tick() (under caller lock). */
65  guint64 last_frames;
66  guint64 last_lat_sum;
67  guint64 last_lat_n;
68  gint64 last_ts_us;
69  gdouble fps;
70  gdouble lat_ms;
72 
73 /*
74  * ModelMuxPerf -- a model bin's performance state: one aggregate counter + a fixed per-source array,
75  * plus the batch-arrival timestamp. Opaque to callers other than via the modelmux_perf_* API.
76  */
77 typedef struct _MmPerf
78 {
79  gint64 t_enter_us;
82 } ModelMuxPerf;
83 
84 /* ---- counter primitive (the only place the rate/latency formula lives) ---- */
85 
87 static inline void
89 {
90  c->frames++;
91  if (lat_us) {
92  c->lat_sum_us += lat_us;
93  c->lat_n++;
94  }
95 }
96 
99 void modelmux_perf_counter_tick (ModelMuxPerfCounter * c, gint64 now_us);
100 
103 
104 /* ---- ModelMuxPerf object (per model bin) ---- */
105 
108 
110 static inline void
112 {
113  if (p)
114  p->t_enter_us = now_us;
115 }
116 
121 void modelmux_perf_mark_bin_out (ModelMuxPerf * p, gint64 now_us, gint source_id);
122 
124 void modelmux_perf_tick (ModelMuxPerf * p, gint64 now_us);
125 
127 void modelmux_perf_get_model (const ModelMuxPerf * p, gdouble * fps, gdouble * lat_ms);
128 
130 void modelmux_perf_get_source (const ModelMuxPerf * p, gint source_id, gdouble * fps, gdouble * lat_ms);
131 
134 gboolean modelmux_gpu_stats (gint gpu, guint * util_pct, guint * mem_used_mb,
135  guint * mem_total_mb);
136 
137 #endif /* GST_NVMODELMUX_PERF_H */
modelmux_gpu_stats
gboolean modelmux_gpu_stats(gint gpu, guint *util_pct, guint *mem_used_mb, guint *mem_total_mb)
WHOLE-device stats via NVML (dlopen'd lazily; FALSE = NVML/device unavailable).
_MmPerf::agg
ModelMuxPerfCounter agg
bin aggregate (model throughput/latency)
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:80
modelmux_perf_get_model
void modelmux_perf_get_model(const ModelMuxPerf *p, gdouble *fps, gdouble *lat_ms)
Caller-locked: read the aggregate snapshot.
modelmux_perf_mark_bin_out
void modelmux_perf_mark_bin_out(ModelMuxPerf *p, gint64 now_us, gint source_id)
HOT PATH: called once per output frame at the BIN's inference tail.
modelmux_perf_tick
void modelmux_perf_tick(ModelMuxPerf *p, gint64 now_us)
Caller-locked: tick the aggregate + every active per-source counter.
modelmux_perf_counter_clear
void modelmux_perf_counter_clear(ModelMuxPerfCounter *c)
Zero a counter (totals + snapshot).
_MmPerf::by_source
ModelMuxPerfCounter by_source[MM_PERF_MAX_SOURCES]
per source_id
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:81
_MmPerf
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:77
MM_PERF_MAX_SOURCES
#define MM_PERF_MAX_SOURCES
Definition: 9.2/sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:51
modelmux_perf_new
ModelMuxPerf * modelmux_perf_new(void)
_MmPerf::t_enter_us
gint64 t_enter_us
bin-head batch arrival (single writer)
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:79
modelmux_perf_free
void modelmux_perf_free(ModelMuxPerf *p)
modelmux_perf_counter_record
static void modelmux_perf_counter_record(ModelMuxPerfCounter *c, guint64 lat_us)
Hot-path record: count one frame; if lat_us>0 also add a latency sample.
Definition: 9.2/sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:88
ModelMuxPerfCounter::frames
guint64 frames
frames counted
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:61
ModelMuxPerfCounter::lat_n
guint64 lat_n
number of latency samples
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:63
modelmux_perf_counter_tick
void modelmux_perf_counter_tick(ModelMuxPerfCounter *c, gint64 now_us)
Recompute fps/lat_ms from the delta since the previous tick; advance the window.
ModelMuxPerfCounter::lat_sum_us
guint64 lat_sum_us
sum of per-record latencies (us)
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:62
ModelMuxPerf
struct _MmPerf ModelMuxPerf
modelmux_perf_get_source
void modelmux_perf_get_source(const ModelMuxPerf *p, gint source_id, gdouble *fps, gdouble *lat_ms)
Caller-locked: read a per-source snapshot (0/0 if unknown source or NULL).
ModelMuxPerfCounter
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:58
modelmux_perf_mark_bin_in
static void modelmux_perf_mark_bin_in(ModelMuxPerf *p, gint64 now_us)
HOT PATH: stamp the batch arrival time at the BIN's inference head.
Definition: 9.2/sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_perf.h:111