]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/cascade_fcntl.c
4 * The contents of this file are subject to the terms
5 * of the Common Development and Distribution License
6 * (the "License"). You may not use this file except
7 * in compliance with the License.
9 * You can obtain a copy of the license at
10 * src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing
13 * permissions and limitations under the License.
15 * When distributing Covered Code, include this CDDL
16 * HEADER in each file and include the License file at
17 * usr/src/OPENSOLARIS.LICENSE. If applicable,
18 * add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your
20 * own identifying information: Portions Copyright [yyyy]
21 * [name of copyright owner]
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
32 * The "cascade" test case is a multiprocess/multithread batten-passing model
33 * using lock primitives alone for synchronisation. Threads are arranged in a
34 * ring. Each thread has two locks of its own on which it blocks, and is able
35 * to manipulate the two locks belonging to the thread which follows it in the
38 * The number of threads (nthreads) is specified by the generic libMicro -P/-T
39 * options. With nthreads == 1 (the default) the uncontended case can be timed.
41 * The main logic is generic and allows any simple blocking API to be tested.
42 * The API-specific component is clearly indicated.
55 int ts_us0
; /* our lock indices */
57 int ts_them0
; /* their lock indices */
64 * API-specific code BEGINS here
67 #define DEFD "/private/tmp"
69 static char *optd
= DEFD
;
76 lm_tsdsize
= sizeof (tsd_t
);
78 (void) sprintf(lm_optstr
, "d:");
80 lm_defN
= "cscd_fcntl";
82 (void) sprintf(lm_usage
,
83 " [-d directory for temp file (default %s)]\n"
84 "notes: thread cascade using fcntl region locking\n",
91 benchmark_optswitch(int opt
, char *optarg
)
109 nthreads
= lm_optP
* lm_optT
;
110 nlocks
= nthreads
* 2;
112 (void) sprintf(fname
, "%s/cascade.%ld", optd
, getpid());
114 file
= open(fname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
123 if (ftruncate(file
, nlocks
* 3) == -1) {
136 fl
.l_whence
= SEEK_SET
;
139 return (fcntl(file
, F_SETLKW
, &fl
) == -1);
148 fl
.l_whence
= SEEK_SET
;
151 return (fcntl(file
, F_SETLK
, &fl
) == -1);
155 * API-specific code ENDS here
159 benchmark_initbatch(void *tsd
)
161 tsd_t
*ts
= (tsd_t
*)tsd
;
164 if (ts
->ts_once
== 0) {
167 #if !defined(__APPLE__)
168 us
= (getpindex() * lm_optT
) + gettindex();
170 us
= gettsdindex(tsd
);
171 #endif /* __APPLE__ */
173 them
= (us
+ 1) % (lm_optP
* lm_optT
);
177 /* lock index asignment for us and them */
178 ts
->ts_us0
= (us
* 4);
179 ts
->ts_us1
= (us
* 4) + 2;
180 if (us
< nthreads
- 1) {
181 /* straight-thru connection to them */
182 ts
->ts_them0
= (them
* 4);
183 ts
->ts_them1
= (them
* 4) + 2;
185 /* cross-over connection to them */
186 ts
->ts_them0
= (them
* 4) + 2;
187 ts
->ts_them1
= (them
* 4);
193 /* block their first move */
194 e
+= block(ts
->ts_them0
);
200 benchmark(void *tsd
, result_t
*res
)
202 tsd_t
*ts
= (tsd_t
*)tsd
;
206 /* wait to be unblocked (id == 0 will not block) */
207 e
+= block(ts
->ts_us0
);
209 for (i
= 0; i
< lm_optB
; i
+= 2) {
210 /* allow them to block us again */
211 e
+= unblock(ts
->ts_us0
);
213 /* block their next + 1 move */
214 e
+= block(ts
->ts_them1
);
216 /* unblock their next move */
217 e
+= unblock(ts
->ts_them0
);
219 /* wait for them to unblock us */
220 e
+= block(ts
->ts_us1
);
222 /* repeat with locks reversed */
223 e
+= unblock(ts
->ts_us1
);
224 e
+= block(ts
->ts_them0
);
225 e
+= unblock(ts
->ts_them1
);
226 e
+= block(ts
->ts_us0
);
229 /* finish batch with nothing blocked */
230 e
+= unblock(ts
->ts_them0
);
231 e
+= unblock(ts
->ts_us0
);