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