Home | History | Annotate | Download | only in libmicro
      1 /*
      2  * CDDL HEADER START
      3  *
      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.
      8  *
      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.
     14  *
     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]
     22  *
     23  * CDDL HEADER END
     24  */
     25 
     26 /*
     27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
     28  * Use is subject to license terms.
     29  */
     30 
     31 /*
     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
     36  * ring.
     37  *
     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.
     40  *
     41  * The main logic is generic and allows any simple blocking API to be tested.
     42  * The API-specific component is clearly indicated.
     43  */
     44 
     45 #include <unistd.h>
     46 #include <stdlib.h>
     47 #include <stdio.h>
     48 #include <fcntl.h>
     49 
     50 #include "libmicro.h"
     51 
     52 typedef struct {
     53 	int			ts_once;
     54 	int			ts_id;
     55 	int			ts_us0;		/* our lock indices */
     56 	int			ts_us1;
     57 	int			ts_them0;	/* their lock indices */
     58 	int			ts_them1;
     59 } tsd_t;
     60 
     61 static int			nthreads;
     62 
     63 /*
     64  * API-specific code BEGINS here
     65  */
     66 
     67 #define	DEFD			"/tmp"
     68 
     69 static char			*optd = DEFD;
     70 static int			nfiles;
     71 static int			*files;
     72 
     73 int
     74 benchmark_init()
     75 {
     76 	lm_tsdsize = sizeof (tsd_t);
     77 
     78 	(void) sprintf(lm_optstr, "d:");
     79 
     80 	lm_defN = "cscd_lockf";
     81 
     82 	(void) sprintf(lm_usage,
     83 	    "       [-d directory for temp files (default %s)]\n"
     84 	    "notes: thread cascade using lockf file locking\n",
     85 	    DEFD);
     86 
     87 	return (0);
     88 }
     89 
     90 int
     91 benchmark_optswitch(int opt, char *optarg)
     92 {
     93 	switch (opt) {
     94 	case 'd':
     95 		optd = optarg;
     96 		break;
     97 	default:
     98 		return (-1);
     99 	}
    100 	return (0);
    101 }
    102 
    103 int
    104 benchmark_initrun()
    105 {
    106 	int			i;
    107 	int			errors = 0;
    108 	char			fname[1024];
    109 
    110 	nthreads = lm_optP * lm_optT;
    111 	nfiles = nthreads * 2;
    112 	(void) setfdlimit(nfiles + 10);
    113 	files = (int *)malloc(nfiles * sizeof (int));
    114 	if (files == NULL) {
    115 		return (1);
    116 	}
    117 
    118 	(void) sprintf(fname, "%s/cascade.%ld", optd, getpid());
    119 
    120 	for (i = 0; i < nfiles; i++) {
    121 		files[i] = open(fname, O_CREAT | O_TRUNC | O_RDWR, 0600);
    122 		if (files[i] == -1) {
    123 			errors++;
    124 		}
    125 		if (unlink(fname)) {
    126 			errors++;
    127 		}
    128 	}
    129 
    130 	return (errors);
    131 }
    132 
    133 int
    134 block(int index)
    135 {
    136 	return (lockf(files[index], F_LOCK, 0) == -1);
    137 }
    138 
    139 int
    140 unblock(int index)
    141 {
    142 	return (lockf(files[index], F_ULOCK, 0) == -1);
    143 }
    144 
    145 /*
    146  * API-specific code ENDS here
    147  */
    148 
    149 int
    150 benchmark_initbatch(void *tsd)
    151 {
    152 	tsd_t			*ts = (tsd_t *)tsd;
    153 	int			e = 0;
    154 
    155 	if (ts->ts_once == 0) {
    156 		int		us, them;
    157 
    158 		us = (getpindex() * lm_optT) + gettindex();
    159 		them = (us + 1) % (lm_optP * lm_optT);
    160 
    161 		ts->ts_id = us;
    162 
    163 		/* lock index asignment for us and them */
    164 		ts->ts_us0 = (us * 2);
    165 		ts->ts_us1 = (us * 2) + 1;
    166 		if (us < nthreads - 1) {
    167 			/* straight-thru connection to them */
    168 			ts->ts_them0 = (them * 2);
    169 			ts->ts_them1 = (them * 2) + 1;
    170 		} else {
    171 			/* cross-over connection to them */
    172 			ts->ts_them0 = (them * 2) + 1;
    173 			ts->ts_them1 = (them * 2);
    174 		}
    175 
    176 		ts->ts_once = 1;
    177 	}
    178 
    179 	/* block their first move */
    180 	e += block(ts->ts_them0);
    181 
    182 	return (e);
    183 }
    184 
    185 int
    186 benchmark(void *tsd, result_t *res)
    187 {
    188 	tsd_t			*ts = (tsd_t *)tsd;
    189 	int			i;
    190 	int			e = 0;
    191 
    192 	/* wait to be unblocked (id == 0 will not block) */
    193 	e += block(ts->ts_us0);
    194 
    195 	for (i = 0; i < lm_optB; i += 2) {
    196 		/* allow them to block us again */
    197 		e += unblock(ts->ts_us0);
    198 
    199 		/* block their next + 1 move */
    200 		e += block(ts->ts_them1);
    201 
    202 		/* unblock their next move */
    203 		e += unblock(ts->ts_them0);
    204 
    205 		/* wait for them to unblock us */
    206 		e += block(ts->ts_us1);
    207 
    208 		/* repeat with locks reversed */
    209 		e += unblock(ts->ts_us1);
    210 		e += block(ts->ts_them0);
    211 		e += unblock(ts->ts_them1);
    212 		e += block(ts->ts_us0);
    213 	}
    214 
    215 	/* finish batch with nothing blocked */
    216 	e += unblock(ts->ts_them0);
    217 	e += unblock(ts->ts_us0);
    218 
    219 	res->re_count = i;
    220 	res->re_errors = e;
    221 
    222 	return (0);
    223 }
    224