aboutsummaryrefslogtreecommitdiff
path: root/rust/daemon/src/session.rs
blob: fbb87b783f8d25b5117810ced5d13932eb3632c4 (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A module to collect and write session stats

use crate::devices::devices_handler::get_radio_stats;
use crate::events::{ChipRemoved, Event, ShutDown};
use crate::version::get_version;
use anyhow::Context;
use log::error;
use log::info;
use netsim_common::system::netsimd_temp_dir;
use netsim_proto::stats::NetsimStats;
use protobuf_json_mapping::print_to_string;
use std::fs::File;
use std::io::Write;
use std::sync::mpsc::Receiver;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::RwLockWriteGuard;
use std::thread::{Builder, JoinHandle};
use std::time::{Duration, Instant};

const WRITE_INTERVAL: Duration = Duration::from_secs(10);

pub struct Session {
    // Handle for the session monitor thread
    handle: Option<JoinHandle<String>>,
    // Session info is accessed by multiple threads
    info: Arc<RwLock<SessionInfo>>,
}

// Fields accessed by threads
struct SessionInfo {
    stats_proto: NetsimStats,
    current_device_count: i32,
    session_start: Instant,
    write_json: bool,
}

impl Session {
    pub fn new() -> Self {
        Self::new_internal(true)
    }
    fn new_internal(write_json: bool) -> Self {
        Session {
            handle: None,
            info: Arc::new(RwLock::new(SessionInfo {
                stats_proto: NetsimStats { version: Some(get_version()), ..Default::default() },
                current_device_count: 0,
                session_start: Instant::now(),
                write_json,
            })),
        }
    }

    // Start the netsim states session.
    //
    // Starts the session monitor thread to handle events and
    // write session stats to json file on event and periodically.
    pub fn start(&mut self, events_rx: Receiver<Event>) -> &mut Self {
        let info = Arc::clone(&self.info);

        // Start up session monitor thread
        self.handle = Some(
            Builder::new()
                .name("session_monitor".to_string())
                .spawn(move || {
                    let mut next_instant = Instant::now() + WRITE_INTERVAL;
                    loop {
                        let mut write_stats = true;
                        let this_instant = Instant::now();
                        let timeout = if next_instant > this_instant {
                            next_instant - this_instant
                        } else {
                            Duration::ZERO
                        };
                        let next_event = events_rx.recv_timeout(timeout);

                        // Hold the write lock for the duration of this loop iteration
                        let mut lock = info.write().expect("Could not acquire session lock");
                        match next_event {
                            Ok(Event::ShutDown(ShutDown { reason })) => {
                                // Shutting down, save the session duration and exit
                                update_session_duration(&mut lock);
                                return reason;
                            }

                            Ok(Event::DeviceRemoved(_)) => {
                                lock.current_device_count -= 1;
                            }

                            Ok(Event::DeviceAdded(_)) => {
                                // update the current_device_count and peak device usage
                                lock.current_device_count += 1;
                                let current_device_count = lock.current_device_count;
                                // incremement total number of devices started
                                let device_count = lock.stats_proto.device_count();
                                lock.stats_proto.set_device_count(device_count + 1);
                                // track the peak device usage
                                if current_device_count > lock.stats_proto.peak_concurrent_devices()
                                {
                                    lock.stats_proto
                                        .set_peak_concurrent_devices(current_device_count);
                                }
                            }

                            Ok(Event::ChipRemoved(ChipRemoved {
                                radio_stats, device_id, ..
                            })) => {
                                // Update the radio stats proto when a
                                // chip is removed.  In the case of
                                // bluetooth there will be 2 radios,
                                // otherwise 1
                                for mut r in radio_stats {
                                    r.set_device_id(device_id);
                                    lock.stats_proto.radio_stats.push(r);
                                }
                            }

                            Ok(Event::ChipAdded(_)) => {
                                // No session stat update required when Chip is added but do proceed to write stats
                            }
                            _ => {
                                // other events are ignored, check to perform periodic write
                                if next_instant > Instant::now() {
                                    write_stats = false;
                                }
                            }
                        }
                        // End of event match - write current stats to json
                        if write_stats {
                            update_session_duration(&mut lock);
                            if lock.write_json {
                                let current_stats = get_current_stats(lock.stats_proto.clone());
                                if let Err(err) = write_stats_to_json(current_stats) {
                                    error!("Failed to write stats to json: {err:?}");
                                }
                            }
                            next_instant = Instant::now() + WRITE_INTERVAL;
                        }
                    }
                })
                .expect("failed to start session monitor thread"),
        );
        self
    }

    // Stop the netsim stats session.
    //
    // Waits for the session monitor thread to finish and writes
    // the session proto to a json file. Consumes the session.
    pub fn stop(mut self) -> anyhow::Result<()> {
        if !self.handle.as_ref().expect("no session monitor").is_finished() {
            info!("session monitor active, waiting...");
        }

        // Synchronize on session monitor thread
        self.handle.take().map(JoinHandle::join);

        let lock = self.info.read().expect("Could not acquire session lock");
        if lock.write_json {
            write_stats_to_json(lock.stats_proto.clone())?;
        }
        Ok(())
    }
}

/// Update session duration
fn update_session_duration(session_lock: &mut RwLockWriteGuard<'_, SessionInfo>) {
    let duration_secs = session_lock.session_start.elapsed().as_secs();
    session_lock.stats_proto.set_duration_secs(duration_secs);
}

/// Construct current radio stats
fn get_current_stats(mut current_stats: NetsimStats) -> NetsimStats {
    current_stats.radio_stats.extend(get_radio_stats());
    current_stats
}

/// Write netsim stats to json file
fn write_stats_to_json(stats_proto: NetsimStats) -> anyhow::Result<()> {
    let filename = netsimd_temp_dir().join("session_stats.json");
    let mut file = File::create(filename)?;
    let json = print_to_string(&stats_proto)?;
    file.write(json.as_bytes()).context("Unable to write json session stats")?;
    file.flush()?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::events;
    use crate::events::{
        ChipAdded, ChipRemoved, DeviceAdded, DeviceRemoved, Event, Events, ShutDown,
    };
    use netsim_proto::stats::NetsimRadioStats;
    use std::sync::Mutex;

    #[test]
    fn test_new() {
        let session: Session = Session::new();
        assert!(session.handle.is_none());
        let lock = session.info.read().expect("Could not acquire session lock");
        assert_eq!(lock.current_device_count, 0);
        assert!(matches!(
            lock.stats_proto,
            NetsimStats { version: Some(_), duration_secs: None, .. }
        ));
        assert_eq!(lock.stats_proto.version.clone().unwrap(), get_version().clone());
        assert_eq!(lock.stats_proto.radio_stats.len(), 0);
    }

    fn setup_session_start_test() -> (Session, Arc<Mutex<Events>>) {
        let mut session = Session::new_internal(false);
        let mut events = events::test::new();
        let events_rx = events::test::subscribe(&mut events);
        session.start(events_rx);
        (session, events)
    }

    fn get_stats_proto(session: &Session) -> NetsimStats {
        session.info.read().expect("Could not acquire session lock").stats_proto.clone()
    }

    #[test]
    fn test_start_and_shutdown() {
        let (mut session, mut events) = setup_session_start_test();

        // we want to be able to check the session time gets incremented
        std::thread::sleep(std::time::Duration::from_secs(1));

        // publish the shutdown afterwards to cause the separate thread to stop
        events::test::publish(
            &mut events,
            Event::ShutDown(ShutDown { reason: "Stop the session".to_string() }),
        );

        // join the handle
        session.handle.take().map(JoinHandle::join);

        let stats_proto = get_stats_proto(&session);

        // check device counts are missing if no device add/remove events occurred
        assert!(stats_proto.device_count.is_none());

        assert!(stats_proto.peak_concurrent_devices.is_none());

        // check the session time is > 0
        assert!(stats_proto.duration_secs.unwrap() > 0u64);
    }

    #[test]
    fn test_start_and_stop() {
        let (session, mut events) = setup_session_start_test();

        // we want to be able to check the session time gets incremented
        std::thread::sleep(std::time::Duration::from_secs(1));

        // publish the shutdown which is required when using `session.stop()`
        events::test::publish(
            &mut events,
            Event::ShutDown(ShutDown { reason: "Stop the session".to_string() }),
        );

        // should not panic or deadlock
        session.stop().unwrap();
    }

    #[test]
    fn test_start_and_device_add() {
        let (mut session, mut events) = setup_session_start_test();

        // we want to be able to check the session time gets incremented
        std::thread::sleep(std::time::Duration::from_secs(1));

        events::test::publish(
            &mut events,
            Event::DeviceAdded(DeviceAdded { builtin: false, ..Default::default() }),
        );

        // publish the shutdown afterwards to cause the separate thread to stop
        events::test::publish(
            &mut events,
            Event::ShutDown(ShutDown { reason: "Stop the session".to_string() }),
        );

        // join the handle
        session.handle.take().map(JoinHandle::join);

        // check device counts were incremented
        assert_eq!(
            session.info.read().expect("Could not acquire session lock").current_device_count,
            1i32
        );
        let stats_proto = get_stats_proto(&session);

        assert_eq!(stats_proto.device_count.unwrap(), 1i32);
        assert_eq!(stats_proto.peak_concurrent_devices.unwrap(), 1i32);
        // check the session time is > 0
        assert!(stats_proto.duration_secs.unwrap() > 0u64);
    }

    #[test]
    fn test_start_and_device_add_and_remove() {
        let (mut session, mut events) = setup_session_start_test();

        // we want to be able to check the session time gets incremented
        std::thread::sleep(std::time::Duration::from_secs(1));

        events::test::publish(
            &mut events,
            Event::DeviceAdded(DeviceAdded { builtin: false, id: 1, ..Default::default() }),
        );

        events::test::publish(
            &mut events,
            Event::DeviceRemoved(DeviceRemoved { builtin: false, id: 1, ..Default::default() }),
        );

        events::test::publish(
            &mut events,
            Event::DeviceAdded(DeviceAdded { builtin: false, id: 2, ..Default::default() }),
        );

        events::test::publish(
            &mut events,
            Event::DeviceRemoved(DeviceRemoved { builtin: false, id: 2, ..Default::default() }),
        );

        // publish the shutdown afterwards to cause the separate thread to stop
        events::test::publish(
            &mut events,
            Event::ShutDown(ShutDown { reason: "Stop the session".to_string() }),
        );

        // join the handle
        session.handle.take().map(JoinHandle::join);

        // check the different device counts were incremented as expected
        assert_eq!(
            session.info.read().expect("Could not acquire session lock").current_device_count,
            0i32
        );
        let stats_proto = get_stats_proto(&session);
        assert_eq!(stats_proto.device_count.unwrap(), 2i32);
        assert_eq!(stats_proto.peak_concurrent_devices.unwrap(), 1i32);

        // check the session time is > 0
        assert!(stats_proto.duration_secs.unwrap() > 0u64);
    }

    #[test]
    fn test_start_and_chip_add_and_remove() {
        let (mut session, mut events) = setup_session_start_test();

        // we want to be able to check the session time gets incremented
        std::thread::sleep(std::time::Duration::from_secs(1));

        events::test::publish(
            &mut events,
            Event::ChipAdded(ChipAdded { builtin: false, chip_id: 0, ..Default::default() }),
        );

        std::thread::sleep(std::time::Duration::from_secs(1));

        // no radio stats until after we remove the chip
        assert_eq!(get_stats_proto(&session).radio_stats.len(), 0usize);

        events::test::publish(
            &mut events,
            Event::ChipRemoved(ChipRemoved {
                chip_id: 0,
                radio_stats: vec![NetsimRadioStats { ..Default::default() }],
                ..Default::default()
            }),
        );

        // publish the shutdown afterwards to cause the separate thread to stop
        events::test::publish(
            &mut events,
            Event::ShutDown(ShutDown { reason: "Stop the session".to_string() }),
        );

        // join the handle
        session.handle.take().map(JoinHandle::join);

        // check devices were not incremented (here we only added and removed the chip)
        assert_eq!(
            session.info.read().expect("Could not acquire session lock").current_device_count,
            0i32
        );

        let stats_proto = get_stats_proto(&session);
        assert_eq!(stats_proto.radio_stats.len(), 1usize);

        // these will still be none since no device level events were processed
        assert!(stats_proto.device_count.is_none());

        assert!(stats_proto.peak_concurrent_devices.is_none());

        // check the session time is > 0
        assert!(stats_proto.duration_secs.unwrap() > 0u64);
    }
}