]> git.proxmox.com Git - mirror_spl.git/blob - module/splat/splat-atomic.c
Public Release Prep
[mirror_spl.git] / module / splat / splat-atomic.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting LAyer Tests (SPLAT) Atomic Tests.
25 \*****************************************************************************/
26
27 #include "splat-internal.h"
28
29 #define SPLAT_ATOMIC_NAME "atomic"
30 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
31
32 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
33 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
34 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
35
36 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
37 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
38
39 typedef enum {
40 SPLAT_ATOMIC_INC_64 = 0,
41 SPLAT_ATOMIC_DEC_64 = 1,
42 SPLAT_ATOMIC_ADD_64 = 2,
43 SPLAT_ATOMIC_SUB_64 = 3,
44 SPLAT_ATOMIC_ADD_64_NV = 4,
45 SPLAT_ATOMIC_SUB_64_NV = 5,
46 SPLAT_ATOMIC_COUNT_64 = 6
47 } atomic_op_t;
48
49 typedef struct atomic_priv {
50 unsigned long ap_magic;
51 struct file *ap_file;
52 spinlock_t ap_lock;
53 wait_queue_head_t ap_waitq;
54 volatile uint64_t ap_atomic;
55 volatile uint64_t ap_atomic_exited;
56 atomic_op_t ap_op;
57
58 } atomic_priv_t;
59
60 static void
61 splat_atomic_work(void *priv)
62 {
63 atomic_priv_t *ap;
64 atomic_op_t op;
65 int i;
66
67 ap = (atomic_priv_t *)priv;
68 ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);
69
70 spin_lock(&ap->ap_lock);
71 op = ap->ap_op;
72 wake_up(&ap->ap_waitq);
73 spin_unlock(&ap->ap_lock);
74
75 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
76 "Thread %d successfully started: %lu/%lu\n", op,
77 (long unsigned)ap->ap_atomic,
78 (long unsigned)ap->ap_atomic_exited);
79
80 for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {
81
82 /* Periodically sleep to mix up the ordering */
83 if ((i % (SPLAT_ATOMIC_INIT_VALUE / 100)) == 0) {
84 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
85 "Thread %d sleeping: %lu/%lu\n", op,
86 (long unsigned)ap->ap_atomic,
87 (long unsigned)ap->ap_atomic_exited);
88 set_current_state(TASK_INTERRUPTIBLE);
89 schedule_timeout(HZ / 100);
90 }
91
92 switch (op) {
93 case SPLAT_ATOMIC_INC_64:
94 atomic_inc_64(&ap->ap_atomic);
95 break;
96 case SPLAT_ATOMIC_DEC_64:
97 atomic_dec_64(&ap->ap_atomic);
98 break;
99 case SPLAT_ATOMIC_ADD_64:
100 atomic_add_64(&ap->ap_atomic, 3);
101 break;
102 case SPLAT_ATOMIC_SUB_64:
103 atomic_sub_64(&ap->ap_atomic, 3);
104 break;
105 case SPLAT_ATOMIC_ADD_64_NV:
106 atomic_add_64_nv(&ap->ap_atomic, 5);
107 break;
108 case SPLAT_ATOMIC_SUB_64_NV:
109 atomic_sub_64_nv(&ap->ap_atomic, 5);
110 break;
111 default:
112 SBUG();
113 }
114 }
115
116 atomic_inc_64(&ap->ap_atomic_exited);
117
118 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
119 "Thread %d successfully exited: %lu/%lu\n", op,
120 (long unsigned)ap->ap_atomic,
121 (long unsigned)ap->ap_atomic_exited);
122
123 wake_up(&ap->ap_waitq);
124 thread_exit();
125 }
126
127 static int
128 splat_atomic_test1_cond(atomic_priv_t *ap, int started)
129 {
130 return (ap->ap_atomic_exited == started);
131 }
132
133 static int
134 splat_atomic_test1(struct file *file, void *arg)
135 {
136 atomic_priv_t ap;
137 DEFINE_WAIT(wait);
138 kthread_t *thr;
139 int i, rc = 0;
140
141 ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
142 ap.ap_file = file;
143 spin_lock_init(&ap.ap_lock);
144 init_waitqueue_head(&ap.ap_waitq);
145 ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
146 ap.ap_atomic_exited = 0;
147
148 for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
149 spin_lock(&ap.ap_lock);
150 ap.ap_op = i;
151
152 thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
153 &ap, 0, &p0, TS_RUN,
154 minclsyspri);
155 if (thr == NULL) {
156 rc = -ESRCH;
157 spin_unlock(&ap.ap_lock);
158 break;
159 }
160
161 /* Prepare to wait, the new thread will wake us once it
162 * has made a copy of the unique private passed data */
163 prepare_to_wait(&ap.ap_waitq, &wait, TASK_UNINTERRUPTIBLE);
164 spin_unlock(&ap.ap_lock);
165 schedule();
166 }
167
168 wait_event_interruptible(ap.ap_waitq, splat_atomic_test1_cond(&ap, i));
169
170 if (rc) {
171 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME, "Only started "
172 "%d/%d test threads\n", i, SPLAT_ATOMIC_COUNT_64);
173 return rc;
174 }
175
176 if (ap.ap_atomic != SPLAT_ATOMIC_INIT_VALUE) {
177 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
178 "Final value %lu does not match initial value %lu\n",
179 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
180 return -EINVAL;
181 }
182
183 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
184 "Success initial and final values match, %lu == %lu\n",
185 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
186
187 return 0;
188 }
189
190 splat_subsystem_t *
191 splat_atomic_init(void)
192 {
193 splat_subsystem_t *sub;
194
195 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
196 if (sub == NULL)
197 return NULL;
198
199 memset(sub, 0, sizeof(*sub));
200 strncpy(sub->desc.name, SPLAT_ATOMIC_NAME, SPLAT_NAME_SIZE);
201 strncpy(sub->desc.desc, SPLAT_ATOMIC_DESC, SPLAT_DESC_SIZE);
202 INIT_LIST_HEAD(&sub->subsystem_list);
203 INIT_LIST_HEAD(&sub->test_list);
204 spin_lock_init(&sub->test_lock);
205 sub->desc.id = SPLAT_SUBSYSTEM_ATOMIC;
206
207 SPLAT_TEST_INIT(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
208 SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);
209
210 return sub;
211 }
212
213 void
214 splat_atomic_fini(splat_subsystem_t *sub)
215 {
216 ASSERT(sub);
217 SPLAT_TEST_FINI(sub, SPLAT_ATOMIC_TEST1_ID);
218
219 kfree(sub);
220 }
221
222 int
223 splat_atomic_id(void) {
224 return SPLAT_SUBSYSTEM_ATOMIC;
225 }