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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::{
    chunk_generator::ChunkGenerator,
    metrics::{EcsSystemMetrics, JobMetrics, PhysicsMetrics, QueryServerMetrics, TickMetrics},
    HwStats, Tick, TickStart,
};
use common::{resources::TimeOfDay, slowjob::SlowJobPool, terrain::TerrainGrid};
use common_ecs::{Job, Origin, Phase, SysMetrics, System};
use specs::{Entities, Join, Read, ReadExpect};
use std::{
    sync::{Arc, Mutex},
    time::Instant,
};
use veloren_query_server::server::Metrics as RawQueryServerMetrics;

/// This system exports metrics
#[derive(Default)]
pub struct Sys;
impl<'a> System<'a> for Sys {
    type SystemData = (
        Entities<'a>,
        ReadExpect<'a, HwStats>,
        ReadExpect<'a, Tick>,
        ReadExpect<'a, TimeOfDay>,
        ReadExpect<'a, TickStart>,
        ReadExpect<'a, ChunkGenerator>,
        Option<Read<'a, TerrainGrid>>,
        Read<'a, SysMetrics>,
        Read<'a, common_ecs::PhysicsMetrics>,
        ReadExpect<'a, SlowJobPool>,
        ReadExpect<'a, EcsSystemMetrics>,
        ReadExpect<'a, TickMetrics>,
        ReadExpect<'a, PhysicsMetrics>,
        ReadExpect<'a, JobMetrics>,
        Option<Read<'a, Arc<Mutex<RawQueryServerMetrics>>>>,
        ReadExpect<'a, QueryServerMetrics>,
    );

    const NAME: &'static str = "metrics";
    const ORIGIN: Origin = Origin::Server;
    const PHASE: Phase = Phase::Apply;

    fn run(
        _job: &mut Job<Self>,
        (
            entities,
            hw_stats,
            tick,
            time_of_day,
            tick_start,
            chunk_generator,
            terrain,
            sys_metrics,
            phys_metrics,
            slowjobpool,
            export_ecs,
            export_tick,
            export_physics,
            export_jobs,
            raw_query_server,
            export_query_server,
        ): Self::SystemData,
    ) {
        const NANOSEC_PER_SEC: f64 = std::time::Duration::from_secs(1).as_nanos() as f64;

        let start = Instant::now();

        let mut state = sys_metrics.stats.lock().unwrap();
        //this system hasn't run yet
        state.remove(Self::NAME);

        for (name, stat) in common_ecs::gen_stats(
            &state,
            tick_start.0,
            hw_stats.rayon_threads,
            hw_stats.hardware_threads,
        ) {
            export_ecs
                .system_start_time
                .with_label_values(&[&name])
                .set(stat.start_ns() as i64);
            export_ecs
                .system_thread_avg
                .with_label_values(&[&name])
                .set(stat.avg_threads() as f64);
            let len = stat.length_ns();
            export_ecs
                .system_length_time
                .with_label_values(&[&name])
                .set(len as i64);
            export_ecs
                .system_length_count
                .with_label_values(&[&name])
                .inc_by(len);
            export_ecs
                .system_length_hist
                .with_label_values(&[&name])
                .observe(len as f64 / NANOSEC_PER_SEC);
        }

        // Report other info
        export_tick.time_of_day.set(time_of_day.0);
        if tick.0.rem_euclid(100) == 0 {
            if let Some(terrain) = terrain.as_ref() {
                let mut chonk_cnt = 0;
                let mut group_cnt = 0;
                let chunk_cnt = terrain.iter().fold(0, |a, (_, c)| {
                    chonk_cnt += 1;
                    group_cnt += c.sub_chunk_groups();
                    a + c.sub_chunks_len()
                });
                export_tick.chonks_count.set(chonk_cnt as i64);
                export_tick.chunks_count.set(chunk_cnt as i64);
                export_tick.chunk_groups_count.set(group_cnt as i64);
            }

            let entity_count = entities.join().count();
            export_tick.entity_count.set(entity_count as i64);
        }
        common_base::plot!("entity count", entities.join().count() as f64);
        common_base::plot!(
            "pending chunks",
            chunk_generator.pending_chunks().count() as f64
        );
        if let Some(terrain) = terrain.as_ref() {
            common_base::plot!("chunk count", terrain.iter().count() as f64);
        }

        //detailed physics metrics
        export_physics
            .entity_entity_collision_checks_count
            .inc_by(phys_metrics.entity_entity_collision_checks);
        export_physics
            .entity_entity_collisions_count
            .inc_by(phys_metrics.entity_entity_collisions);

        //detailed job metrics
        for (name, jobs) in slowjobpool.take_metrics() {
            let queried = export_jobs.job_queried_hst.with_label_values(&[&name]);
            let executed = export_jobs.job_execution_hst.with_label_values(&[&name]);
            for job in jobs {
                queried.observe(
                    job.execution_start
                        .duration_since(job.queue_created)
                        .as_secs_f64(),
                );
                executed.observe(
                    job.execution_end
                        .duration_since(job.execution_start)
                        .as_secs_f64(),
                );
            }
        }

        // export self time as best as possible
        export_ecs
            .system_start_time
            .with_label_values(&["metrics"])
            .set(start.duration_since(tick_start.0).as_nanos() as i64);
        export_ecs
            .system_thread_avg
            .with_label_values(&["metrics"])
            .set(1.0);
        let len = start.elapsed().as_nanos() as u64;
        export_ecs
            .system_length_time
            .with_label_values(&["metrics"])
            .set(len as i64);
        export_ecs
            .system_length_count
            .with_label_values(&["metrics"])
            .inc_by(len);
        export_ecs
            .system_length_hist
            .with_label_values(&["metrics"])
            .observe(len as f64 / NANOSEC_PER_SEC);

        if let Some(Ok(metrics)) = raw_query_server
            .as_ref()
            // Hold the lock for the shortest time possible
            .map(|m| m.lock().map(|mut metrics| metrics.reset()))
        {
            export_query_server.apply(metrics);
        }
    }
}