2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "splat-internal.h"
29 #define SPLAT_SUBSYSTEM_ATOMIC 0x0b00
30 #define SPLAT_ATOMIC_NAME "atomic"
31 #define SPLAT_ATOMIC_DESC "Kernel Atomic Tests"
33 #define SPLAT_ATOMIC_TEST1_ID 0x0b01
34 #define SPLAT_ATOMIC_TEST1_NAME "64-bit"
35 #define SPLAT_ATOMIC_TEST1_DESC "Validate 64-bit atomic ops"
37 #define SPLAT_ATOMIC_TEST_MAGIC 0x43435454UL
38 #define SPLAT_ATOMIC_INIT_VALUE 10000000UL
41 SPLAT_ATOMIC_INC_64
= 0,
42 SPLAT_ATOMIC_DEC_64
= 1,
43 SPLAT_ATOMIC_ADD_64
= 2,
44 SPLAT_ATOMIC_SUB_64
= 3,
45 SPLAT_ATOMIC_ADD_64_NV
= 4,
46 SPLAT_ATOMIC_SUB_64_NV
= 5,
47 SPLAT_ATOMIC_COUNT_64
= 6
50 typedef struct atomic_priv
{
51 unsigned long ap_magic
;
54 wait_queue_head_t ap_waitq
;
55 volatile uint64_t ap_atomic
;
56 volatile uint64_t ap_atomic_exited
;
62 splat_atomic_work(void *priv
)
68 ap
= (atomic_priv_t
*)priv
;
69 ASSERT(ap
->ap_magic
== SPLAT_ATOMIC_TEST_MAGIC
);
71 spin_lock(&ap
->ap_lock
);
73 wake_up(&ap
->ap_waitq
);
74 spin_unlock(&ap
->ap_lock
);
76 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
77 "Thread %d successfully started: %lu/%lu\n", op
,
78 (long unsigned)ap
->ap_atomic
,
79 (long unsigned)ap
->ap_atomic_exited
);
81 for (i
= 0; i
< SPLAT_ATOMIC_INIT_VALUE
/ 10; i
++) {
83 /* Periodically sleep to mix up the ordering */
84 if ((i
% (SPLAT_ATOMIC_INIT_VALUE
/ 100)) == 0) {
85 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
86 "Thread %d sleeping: %lu/%lu\n", op
,
87 (long unsigned)ap
->ap_atomic
,
88 (long unsigned)ap
->ap_atomic_exited
);
89 set_current_state(TASK_INTERRUPTIBLE
);
90 schedule_timeout(HZ
/ 100);
94 case SPLAT_ATOMIC_INC_64
:
95 atomic_inc_64(&ap
->ap_atomic
);
97 case SPLAT_ATOMIC_DEC_64
:
98 atomic_dec_64(&ap
->ap_atomic
);
100 case SPLAT_ATOMIC_ADD_64
:
101 atomic_add_64(&ap
->ap_atomic
, 3);
103 case SPLAT_ATOMIC_SUB_64
:
104 atomic_sub_64(&ap
->ap_atomic
, 3);
106 case SPLAT_ATOMIC_ADD_64_NV
:
107 atomic_add_64_nv(&ap
->ap_atomic
, 5);
109 case SPLAT_ATOMIC_SUB_64_NV
:
110 atomic_sub_64_nv(&ap
->ap_atomic
, 5);
117 atomic_inc_64(&ap
->ap_atomic_exited
);
119 splat_vprint(ap
->ap_file
, SPLAT_ATOMIC_TEST1_NAME
,
120 "Thread %d successfully exited: %lu/%lu\n", op
,
121 (long unsigned)ap
->ap_atomic
,
122 (long unsigned)ap
->ap_atomic_exited
);
124 wake_up(&ap
->ap_waitq
);
129 splat_atomic_test1_cond(atomic_priv_t
*ap
)
131 return (ap
->ap_atomic_exited
== SPLAT_ATOMIC_COUNT_64
);
135 splat_atomic_test1(struct file
*file
, void *arg
)
142 ap
.ap_magic
= SPLAT_ATOMIC_TEST_MAGIC
;
144 spin_lock_init(&ap
.ap_lock
);
145 init_waitqueue_head(&ap
.ap_waitq
);
146 ap
.ap_atomic
= SPLAT_ATOMIC_INIT_VALUE
;
147 ap
.ap_atomic_exited
= 0;
149 for (i
= 0; i
< SPLAT_ATOMIC_COUNT_64
; i
++) {
150 spin_lock(&ap
.ap_lock
);
153 thr
= (kthread_t
*)thread_create(NULL
, 0, splat_atomic_work
,
158 /* Prepare to wait, the new thread will wake us once it
159 * has made a copy of the unique private passed data */
160 prepare_to_wait(&ap
.ap_waitq
, &wait
, TASK_UNINTERRUPTIBLE
);
161 spin_unlock(&ap
.ap_lock
);
165 wait_event_interruptible(ap
.ap_waitq
, splat_atomic_test1_cond(&ap
));
167 if (ap
.ap_atomic
!= SPLAT_ATOMIC_INIT_VALUE
) {
168 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
,
169 "Final value %lu does not match initial value %lu\n",
170 (long unsigned)ap
.ap_atomic
, SPLAT_ATOMIC_INIT_VALUE
);
174 splat_vprint(file
, SPLAT_ATOMIC_TEST1_NAME
,
175 "Success initial and final values match, %lu == %lu\n",
176 (long unsigned)ap
.ap_atomic
, SPLAT_ATOMIC_INIT_VALUE
);
182 splat_atomic_init(void)
184 splat_subsystem_t
*sub
;
186 sub
= kmalloc(sizeof(*sub
), GFP_KERNEL
);
190 memset(sub
, 0, sizeof(*sub
));
191 strncpy(sub
->desc
.name
, SPLAT_ATOMIC_NAME
, SPLAT_NAME_SIZE
);
192 strncpy(sub
->desc
.desc
, SPLAT_ATOMIC_DESC
, SPLAT_DESC_SIZE
);
193 INIT_LIST_HEAD(&sub
->subsystem_list
);
194 INIT_LIST_HEAD(&sub
->test_list
);
195 spin_lock_init(&sub
->test_lock
);
196 sub
->desc
.id
= SPLAT_SUBSYSTEM_ATOMIC
;
198 SPLAT_TEST_INIT(sub
, SPLAT_ATOMIC_TEST1_NAME
, SPLAT_ATOMIC_TEST1_DESC
,
199 SPLAT_ATOMIC_TEST1_ID
, splat_atomic_test1
);
205 splat_atomic_fini(splat_subsystem_t
*sub
)
208 SPLAT_TEST_FINI(sub
, SPLAT_ATOMIC_TEST1_ID
);
214 splat_atomic_id(void) {
215 return SPLAT_SUBSYSTEM_ATOMIC
;