]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/splat/splat-atomic.c
Imported Upstream version 0.6.2
[mirror_spl-debian.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://zfsonlinux.org/>.
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 <sys/atomic.h>
28 #include <sys/thread.h>
29 #include <linux/slab.h>
30 #include "splat-internal.h"
31
32 #define SPLAT_ATOMIC_NAME "atomic"
33 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
34
35 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
36 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
37 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
38
39 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
40 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
41
42 typedef enum {
43 SPLAT_ATOMIC_INC_64 = 0,
44 SPLAT_ATOMIC_DEC_64 = 1,
45 SPLAT_ATOMIC_ADD_64 = 2,
46 SPLAT_ATOMIC_SUB_64 = 3,
47 SPLAT_ATOMIC_ADD_64_NV = 4,
48 SPLAT_ATOMIC_SUB_64_NV = 5,
49 SPLAT_ATOMIC_COUNT_64 = 6
50 } atomic_op_t;
51
52 typedef struct atomic_priv {
53 unsigned long ap_magic;
54 struct file *ap_file;
55 struct mutex ap_lock;
56 wait_queue_head_t ap_waitq;
57 volatile uint64_t ap_atomic;
58 volatile uint64_t ap_atomic_exited;
59 atomic_op_t ap_op;
60
61 } atomic_priv_t;
62
63 static void
64 splat_atomic_work(void *priv)
65 {
66 atomic_priv_t *ap;
67 atomic_op_t op;
68 int i;
69
70 ap = (atomic_priv_t *)priv;
71 ASSERT(ap->ap_magic == SPLAT_ATOMIC_TEST_MAGIC);
72
73 mutex_lock(&ap->ap_lock);
74 op = ap->ap_op;
75 wake_up(&ap->ap_waitq);
76 mutex_unlock(&ap->ap_lock);
77
78 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
79 "Thread %d successfully started: %lu/%lu\n", op,
80 (long unsigned)ap->ap_atomic,
81 (long unsigned)ap->ap_atomic_exited);
82
83 for (i = 0; i < SPLAT_ATOMIC_INIT_VALUE / 10; i++) {
84
85 /* Periodically sleep to mix up the ordering */
86 if ((i % (SPLAT_ATOMIC_INIT_VALUE / 100)) == 0) {
87 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
88 "Thread %d sleeping: %lu/%lu\n", op,
89 (long unsigned)ap->ap_atomic,
90 (long unsigned)ap->ap_atomic_exited);
91 set_current_state(TASK_INTERRUPTIBLE);
92 schedule_timeout(HZ / 100);
93 }
94
95 switch (op) {
96 case SPLAT_ATOMIC_INC_64:
97 atomic_inc_64(&ap->ap_atomic);
98 break;
99 case SPLAT_ATOMIC_DEC_64:
100 atomic_dec_64(&ap->ap_atomic);
101 break;
102 case SPLAT_ATOMIC_ADD_64:
103 atomic_add_64(&ap->ap_atomic, 3);
104 break;
105 case SPLAT_ATOMIC_SUB_64:
106 atomic_sub_64(&ap->ap_atomic, 3);
107 break;
108 case SPLAT_ATOMIC_ADD_64_NV:
109 atomic_add_64_nv(&ap->ap_atomic, 5);
110 break;
111 case SPLAT_ATOMIC_SUB_64_NV:
112 atomic_sub_64_nv(&ap->ap_atomic, 5);
113 break;
114 default:
115 PANIC("Undefined op %d\n", op);
116 }
117 }
118
119 atomic_inc_64(&ap->ap_atomic_exited);
120
121 splat_vprint(ap->ap_file, SPLAT_ATOMIC_TEST1_NAME,
122 "Thread %d successfully exited: %lu/%lu\n", op,
123 (long unsigned)ap->ap_atomic,
124 (long unsigned)ap->ap_atomic_exited);
125
126 wake_up(&ap->ap_waitq);
127 thread_exit();
128 }
129
130 static int
131 splat_atomic_test1_cond(atomic_priv_t *ap, int started)
132 {
133 return (ap->ap_atomic_exited == started);
134 }
135
136 static int
137 splat_atomic_test1(struct file *file, void *arg)
138 {
139 atomic_priv_t ap;
140 DEFINE_WAIT(wait);
141 kthread_t *thr;
142 int i, rc = 0;
143
144 ap.ap_magic = SPLAT_ATOMIC_TEST_MAGIC;
145 ap.ap_file = file;
146 mutex_init(&ap.ap_lock);
147 init_waitqueue_head(&ap.ap_waitq);
148 ap.ap_atomic = SPLAT_ATOMIC_INIT_VALUE;
149 ap.ap_atomic_exited = 0;
150
151 for (i = 0; i < SPLAT_ATOMIC_COUNT_64; i++) {
152 mutex_lock(&ap.ap_lock);
153 ap.ap_op = i;
154
155 thr = (kthread_t *)thread_create(NULL, 0, splat_atomic_work,
156 &ap, 0, &p0, TS_RUN,
157 minclsyspri);
158 if (thr == NULL) {
159 rc = -ESRCH;
160 mutex_unlock(&ap.ap_lock);
161 break;
162 }
163
164 /* Prepare to wait, the new thread will wake us once it
165 * has made a copy of the unique private passed data */
166 prepare_to_wait(&ap.ap_waitq, &wait, TASK_UNINTERRUPTIBLE);
167 mutex_unlock(&ap.ap_lock);
168 schedule();
169 }
170
171 wait_event(ap.ap_waitq, splat_atomic_test1_cond(&ap, i));
172
173 if (rc) {
174 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME, "Only started "
175 "%d/%d test threads\n", i, SPLAT_ATOMIC_COUNT_64);
176 return rc;
177 }
178
179 if (ap.ap_atomic != SPLAT_ATOMIC_INIT_VALUE) {
180 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
181 "Final value %lu does not match initial value %lu\n",
182 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
183 return -EINVAL;
184 }
185
186 splat_vprint(file, SPLAT_ATOMIC_TEST1_NAME,
187 "Success initial and final values match, %lu == %lu\n",
188 (long unsigned)ap.ap_atomic, SPLAT_ATOMIC_INIT_VALUE);
189
190 return 0;
191 }
192
193 splat_subsystem_t *
194 splat_atomic_init(void)
195 {
196 splat_subsystem_t *sub;
197
198 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
199 if (sub == NULL)
200 return NULL;
201
202 memset(sub, 0, sizeof(*sub));
203 strncpy(sub->desc.name, SPLAT_ATOMIC_NAME, SPLAT_NAME_SIZE);
204 strncpy(sub->desc.desc, SPLAT_ATOMIC_DESC, SPLAT_DESC_SIZE);
205 INIT_LIST_HEAD(&sub->subsystem_list);
206 INIT_LIST_HEAD(&sub->test_list);
207 spin_lock_init(&sub->test_lock);
208 sub->desc.id = SPLAT_SUBSYSTEM_ATOMIC;
209
210 SPLAT_TEST_INIT(sub, SPLAT_ATOMIC_TEST1_NAME, SPLAT_ATOMIC_TEST1_DESC,
211 SPLAT_ATOMIC_TEST1_ID, splat_atomic_test1);
212
213 return sub;
214 }
215
216 void
217 splat_atomic_fini(splat_subsystem_t *sub)
218 {
219 ASSERT(sub);
220 SPLAT_TEST_FINI(sub, SPLAT_ATOMIC_TEST1_ID);
221
222 kfree(sub);
223 }
224
225 int
226 splat_atomic_id(void) {
227 return SPLAT_SUBSYSTEM_ATOMIC;
228 }