1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <stdarg.h>
8 
9 #include <fbl/macros.h>
10 #include <fbl/vector.h>
11 #include <fbl/unique_ptr.h>
12 #include <zircon/status.h>
13 #include <zircon/syscalls.h>
14 
15 class StressTest {
16 public:
StressTest()17     StressTest() {
18         tests_.push_back(this);
19     }
20 
21     virtual ~StressTest() = default;
22 
23     DISALLOW_COPY_ASSIGN_AND_MOVE(StressTest);
24 
25     // Called once before starting the test. Allocate resources needed for
26     // the test here.
27     //
28     // If overridden in a subclass, call through to this version first.
Init(bool verbose,const zx_info_kmem_stats & stats)29     virtual zx_status_t Init(bool verbose, const zx_info_kmem_stats& stats) {
30         verbose_ = verbose;
31 
32         // gather some info about the system
33         kmem_stats_ = stats;
34         num_cpus_ = zx_system_get_num_cpus();
35         return ZX_OK;
36     }
37 
38     // Called once to start the test. Must return immediately.
39     virtual zx_status_t Start() = 0;
40 
41     // Called to stop the individual test. Must wait until test has
42     // been shut down.
43     virtual zx_status_t Stop() = 0;
44 
45     // Return the name of the test in C string format
46     virtual const char* name() const = 0;
47 
48     // get a ref to the master test list
tests()49     static fbl::Vector<StressTest*>& tests() { return tests_; }
50 
51 protected:
52     // global list of all the stress tests, registered at app start
53     static fbl::Vector<StressTest*> tests_;
54 
55     // wrapper around printf that enables/disables based on verbose flag
Printf(const char * fmt,...)56     void Printf(const char *fmt, ...) {
57         if (!verbose_) {
58             return;
59         }
60 
61         va_list ap;
62         va_start(ap, fmt);
63 
64         vprintf(fmt, ap);
65 
66         va_end(ap);
67     }
68 
PrintfAlways(const char * fmt,...)69     void PrintfAlways(const char *fmt, ...) {
70         va_list ap;
71         va_start(ap, fmt);
72 
73         vprintf(fmt, ap);
74 
75         va_end(ap);
76     }
77 
78     bool verbose_{false};
79     zx_info_kmem_stats_t kmem_stats_{};
80     uint32_t num_cpus_{};
81 };
82 
83 // factories for local tests
84 fbl::unique_ptr<StressTest> CreateVmStressTest();
85