summaryrefslogtreecommitdiff
path: root/test/test_sref_dirty_zeroes.c
blob: 53183358ad30f581dff034fe4835983eb172f6bb (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
/*
 * 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/>.
 *
 *
 * The purpose of this test module is to produce dirty zeroes and make
 * sure they're correctly processed. It is a stress test that never ends,
 * except on failure. Two threads are created. The first increments a
 * scalable reference counter, then signals the second that it can decrement
 * it. Since these threads are likely to run on different processors, a
 * good amount of dirty zeroes should be produced, as reported by regularly
 * printing the relevant event counters. Since the true number of references
 * can never drop to 0, the no-reference function should never be called,
 * and panics if it is.
 */

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

#include <kern/condition.h>
#include <kern/error.h>
#include <kern/init.h>
#include <kern/kmem.h>
#include <kern/macros.h>
#include <kern/mutex.h>
#include <kern/panic.h>
#include <kern/sref.h>
#include <kern/syscnt.h>
#include <kern/thread.h>
#include <test/test.h>
#include <vm/vm_kmem.h>

static struct condition test_condition;
static struct mutex test_lock;
static struct sref_counter test_counter;
static unsigned long test_transient_ref;

static void
test_inc(void *arg)
{
    volatile unsigned long i;
    (void)arg;

    for (;;) {
        for (i = 0; i < 1000000; i++) {
            sref_counter_inc(&test_counter);

            mutex_lock(&test_lock);
            test_transient_ref++;
            condition_signal(&test_condition);

            while (test_transient_ref != 0) {
                condition_wait(&test_condition, &test_lock);
            }

            mutex_unlock(&test_lock);
        }

        printf("counter global value: %lu\n", test_counter.value);
        syscnt_info("sref_epoch");
        syscnt_info("sref_dirty_zero");
        syscnt_info("sref_true_zero");
    }
}

static void
test_dec(void *arg)
{
    (void)arg;

    for (;;) {
        mutex_lock(&test_lock);

        while (test_transient_ref == 0) {
            condition_wait(&test_condition, &test_lock);
        }

        test_transient_ref--;
        condition_signal(&test_condition);
        mutex_unlock(&test_lock);

        sref_counter_dec(&test_counter);
    }
}

static void
test_noref(struct sref_counter *counter)
{
    (void)counter;
    panic("0 references, page released\n");
}

void __init
test_setup(void)
{
    struct thread_attr attr;
    struct thread *thread;
    int error;

    condition_init(&test_condition);
    mutex_init(&test_lock);

    sref_counter_init(&test_counter, 1, NULL, test_noref);
    test_transient_ref = 0;

    thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_inc");
    thread_attr_set_detached(&attr);
    error = thread_create(&thread, &attr, test_inc, NULL);
    error_check(error, "thread_create");

    thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_dec");
    thread_attr_set_detached(&attr);
    error = thread_create(&thread, &attr, test_dec, NULL);
    error_check(error, "thread_create");
}