summaryrefslogtreecommitdiff
path: root/test/test_perfmon_torture.c
blob: 7b54129c3cac1c62a78cf98495026903373343fb (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
/*
 * Copyright (c) 2014 Remy Noel.
 * Copyright (c) 2014 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * This is a stress test for perfmon thread monitoring.
 * The goal is to pass through a maximum of code paths of the perfmon module.
 * We therefore try to get a high migration rate by schedueling at least as
 * many threads as CPUs.
 * Also, we stop and restart some threads along the way in order to check wether
 * stopped threads are propery handled.
 *
 * TODO: replace thread selection with a proper pseudo-random function once we
 * get one.
 */

#include <stddef.h>
#include <stdio.h>

#include <kern/error.h>
#include <kern/kmem.h>
#include <kern/panic.h>
#include <kern/perfmon.h>
#include <kern/thread.h>
#include <test/test.h>

enum test_thread_state {
    TEST_LAUNCHED = 0,
    TEST_RUNNING,
    TEST_STOPPING,
    TEST_STOPPED,
};

struct test_thread {
    struct thread *thread;
    struct perfmon_group *group;
    struct perfmon_event *event;

    bool monitored;
    enum test_thread_state state;
    unsigned long long count;
};

struct threads_stats {
    size_t num_group_start;
    size_t num_group_started;
    size_t num_thread_start;
    size_t num_thread_started;
};

static struct test_thread **test_threads;

static struct thread *test_control;

#define TEST_WAIT_INSTRUCT_COUNT 1000UL
#define TEST_NUM_LOOP_STATUS_PRINT 200000

static void
test_wait(void)
{
    volatile unsigned long i;

    /* TODO: Do something a a bit more clever once timers are here */

    for (i = 0; i < TEST_WAIT_INSTRUCT_COUNT; i++);
}

static void
test_thread_run(void *arg)
{
    struct test_thread *thread = arg;
    unsigned long num_loops;

    assert(thread->state == TEST_LAUNCHED);
    num_loops = 0;

    thread->state = TEST_RUNNING;

    for (;;) {
        barrier();
        if (thread->state == TEST_STOPPING) {
            break;
        }
        /* Invividual threads waits twice as much as control one in order to
         * induce some asynchronism between control and treads.
         */
        test_wait();
        test_wait();
        num_loops++;
    }
    thread->state = TEST_STOPPED;
}

static void
test_thread_toggle_monitor(struct test_thread *thread,
                           struct threads_stats *stats)
{
    int error;
    struct perfmon_group *group;

    group = thread->group;

    if (!thread->monitored) {
        error = perfmon_group_start(group);
        error_check(error, "perfmon_group_start");
        thread->monitored = true;
        stats->num_group_start++;
        stats->num_group_started++;
    } else {
        perfmon_group_update(group);
        thread->count = perfmon_event_read(thread->event);
        error = perfmon_group_stop(group);
        error_check(error, "perfmon_group_stop");
        thread->monitored = false;
        stats->num_group_started--;
    }
}

static int
test_thread_create_monitored_thread(struct thread **thread, size_t index,
                                    void *arg)
{
    struct thread_attr attr;
    char name[THREAD_NAME_SIZE];

    snprintf(name, sizeof(name), THREAD_KERNEL_PREFIX
             "test_monitored_thread:%zu", index);
    thread_attr_init(&attr, name);
    thread_attr_set_detached(&attr);

    return thread_create(thread, &attr, test_thread_run, arg);
}

static void
test_thread_toggle_state(size_t index,
                         struct threads_stats *stats)
{
    int error;
    struct perfmon_group *group;
    struct test_thread *thread;

    thread = test_threads[index];
    group = thread->group;

    switch (thread->state) {
    case TEST_RUNNING:
        thread->state = TEST_STOPPING;
        stats->num_thread_started--;
        break;
    case TEST_STOPPED:
        /* restart thread and attach it to the group of the previous thread.
         */
        if (thread->monitored) {
            test_thread_toggle_monitor(thread, stats);
        }
        error = perfmon_group_detach(group);
        error_check(error, "perfmon_group_detach");
        thread->state = TEST_LAUNCHED;
        error = test_thread_create_monitored_thread(&thread->thread, index,
                                                    thread);
        error_check(error, "thread_recreate monitored");
        error = perfmon_group_attach(group, thread->thread);
        error_check(error, "perfmon_group_attach");
        stats->num_thread_start++;
        stats->num_thread_started++;
        break;
    default:
        /* Do nothing if the thread is not in a stable state */
        break;
    }
}

static void
test_x15_test_control_run(void *arg)
{
    size_t selected_thread;
    size_t stopped_thread;
    struct test_thread *thread;
    size_t nr_threads;
    size_t loop_since_status;
    struct threads_stats stats;

    (void)arg;
    nr_threads = MAX(cpu_count() - 1, 1);
    selected_thread = 0;
    stopped_thread = 0;
    loop_since_status = 0;
    stats.num_group_start = 0;
    stats.num_group_started = 0;
    stats.num_thread_start = nr_threads;
    stats.num_thread_started = nr_threads;

    printf("monitoring %zu threads\n", nr_threads);

    for (;;) {
        /* Dummy `random` thread selection. */
        selected_thread = (selected_thread + 7) % nr_threads;
        thread = test_threads[selected_thread];
        test_thread_toggle_monitor(thread, &stats);

        /* only half of the threads may be stopped / restarted */
        stopped_thread = (stopped_thread + 11) % ((nr_threads  + 1) / 2);
        test_thread_toggle_state(stopped_thread, &stats);

        test_wait();
        if (!(++loop_since_status % TEST_NUM_LOOP_STATUS_PRINT)) {
            printf("===============================\n");
            printf("%zu groups started (%zu total)\n", stats.num_group_started,
                   stats.num_group_start);
            printf("%zu threads started (%zu total)\n",
                   stats.num_thread_started, stats.num_thread_start);
            printf("monitor value: ");
            for (size_t i = 0; i < nr_threads; i++) {
                printf("%zu: %llu, ", i, test_threads[i]->count);
            }
            printf("\n");
        }
    }
}

static struct test_thread *
test_thread_create(size_t index)
{
    struct test_thread *thread;
    int error;

    thread = kmem_zalloc(sizeof(*thread));

    if (thread == NULL) {
        panic("thread allocation failed");
    }

    error = test_thread_create_monitored_thread(&thread->thread, index, thread);
    error_check(error, "thread_create");
    error = perfmon_group_create(&thread->group);
    error_check(error, "perfmon_group_create");
    error = perfmon_event_create(&thread->event, PERFMON_ET_GENERIC,
                                 PERFMON_EV_CYCLE, PERFMON_EF_KERN);
    error_check(error, "perfmon_event_create");

    perfmon_group_add(thread->group, thread->event);
    error = perfmon_group_attach(thread->group, thread->thread);
    error_check(error, "perfmon_group_attach");

    return thread;
}

void
test_setup(void)
{
    struct thread_attr attr;
    size_t nr_threads;
    size_t i;
    int error;

    nr_threads = MAX(cpu_count() - 1, 1);

    test_threads = kmem_alloc(nr_threads * sizeof(*test_threads));
    for (i = 0; i < nr_threads; i++) {
        test_threads[i] =  test_thread_create(i);
    }

    thread_attr_init(&attr, "15_test_control_thread");
    thread_attr_set_detached(&attr);
    error = thread_create(&test_control, &attr, test_x15_test_control_run,
                          NULL);
    error_check(error, "thread_create control");
}