]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/cascade_lockf.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_lockf";
82 (void) sprintf(lm_usage
,
83 " [-d directory for temp files (default %s)]\n"
84 "notes: thread cascade using lockf file locking\n",
91 benchmark_optswitch(int opt
, char *optarg
)
110 nthreads
= lm_optP
* lm_optT
;
111 nfiles
= nthreads
* 2;
112 (void) setfdlimit(nfiles
+ 10);
113 files
= (int *)malloc(nfiles
* sizeof (int));
118 (void) sprintf(fname
, "%s/cascade.%ld", optd
, getpid());
120 for (i
= 0; i
< nfiles
; i
++) {
121 files
[i
] = open(fname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
122 if (files
[i
] == -1) {
136 return (lockf(files
[index
], F_LOCK
, 0) == -1);
142 return (lockf(files
[index
], F_ULOCK
, 0) == -1);
146 * API-specific code ENDS here
150 benchmark_initbatch(void *tsd
)
152 tsd_t
*ts
= (tsd_t
*)tsd
;
155 if (ts
->ts_once
== 0) {
158 #if !defined(__APPLE__)
159 us
= (getpindex() * lm_optT
) + gettindex();
161 us
= gettsdindex(tsd
);
162 #endif /* __APPLE__ */
164 them
= (us
+ 1) % (lm_optP
* lm_optT
);
168 /* lock index asignment for us and them */
169 ts
->ts_us0
= (us
* 2);
170 ts
->ts_us1
= (us
* 2) + 1;
171 if (us
< nthreads
- 1) {
172 /* straight-thru connection to them */
173 ts
->ts_them0
= (them
* 2);
174 ts
->ts_them1
= (them
* 2) + 1;
176 /* cross-over connection to them */
177 ts
->ts_them0
= (them
* 2) + 1;
178 ts
->ts_them1
= (them
* 2);
184 /* block their first move */
185 e
+= block(ts
->ts_them0
);
191 benchmark(void *tsd
, result_t
*res
)
193 tsd_t
*ts
= (tsd_t
*)tsd
;
197 /* wait to be unblocked (id == 0 will not block) */
198 e
+= block(ts
->ts_us0
);
200 for (i
= 0; i
< lm_optB
; i
+= 2) {
201 /* allow them to block us again */
202 e
+= unblock(ts
->ts_us0
);
204 /* block their next + 1 move */
205 e
+= block(ts
->ts_them1
);
207 /* unblock their next move */
208 e
+= unblock(ts
->ts_them0
);
210 /* wait for them to unblock us */
211 e
+= block(ts
->ts_us1
);
213 /* repeat with locks reversed */
214 e
+= unblock(ts
->ts_us1
);
215 e
+= block(ts
->ts_them0
);
216 e
+= unblock(ts
->ts_them1
);
217 e
+= block(ts
->ts_us0
);
220 /* finish batch with nothing blocked */
221 e
+= unblock(ts
->ts_them0
);
222 e
+= unblock(ts
->ts_us0
);