0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms
0N/A * of the Common Development and Distribution License
0N/A * (the "License"). You may not use this file except
0N/A * in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at
0N/A * src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing
0N/A * permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL
0N/A * HEADER in each file and include the License file at
0N/A * usr/src/OPENSOLARIS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your
0N/A * own identifying information: Portions Copyright [yyyy]
0N/A * [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/A/*
0N/A * mutex
0N/A */
0N/A
0N/A#include <unistd.h>
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <pthread.h>
0N/A#include <sys/mman.h>
0N/A
0N/A#include "libmicro.h"
0N/A
0N/Astatic int optt = 0;
0N/Astatic int optp = 0;
0N/Astatic int opth = 0;
0N/Astatic int opto = 0;
0N/A
0N/Apthread_mutex_t *lock;
0N/A
0N/Atypedef struct {
0N/A int ts_once;
0N/A pthread_mutex_t *ts_lock;
0N/A} tsd_t;
0N/A
0N/Aint
0N/Abenchmark_init()
0N/A{
0N/A lm_tsdsize = sizeof (tsd_t);
0N/A
0N/A (void) sprintf(lm_usage,
0N/A " [-t] (create dummy thread so we are multithreaded)\n"
0N/A " [-p] (use inter-process mutex (not support everywhere))\n"
0N/A " [-h usecs] (specify mutex hold time (default 0)\n"
0N/A "notes: measures uncontended pthread_mutex_[un,]lock\n");
0N/A
0N/A (void) sprintf(lm_optstr, "tph:o:");
0N/A
0N/A (void) sprintf(lm_header, "%8s", "holdtime");
0N/A
0N/A return (0);
0N/A}
0N/A
0N/A/*ARGSUSED*/
0N/Aint
0N/Abenchmark_optswitch(int opt, char *optarg)
0N/A{
0N/A switch (opt) {
0N/A case 'p':
0N/A optp = 1;
0N/A break;
0N/A
0N/A case 't':
0N/A optt = 1;
0N/A break;
0N/A
0N/A case 'h':
0N/A opth = sizetoint(optarg);
0N/A break;
0N/A
0N/A case 'o':
0N/A opto = sizetoint(optarg);
0N/A break;
0N/A
0N/A default:
0N/A return (-1);
0N/A }
0N/A return (0);
0N/A}
0N/A
0N/Avoid *
0N/Adummy(void *arg)
0N/A{
0N/A (void) pause();
0N/A return (arg);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initrun()
0N/A{
0N/A pthread_mutexattr_t attr;
0N/A int errors = 0;
0N/A
0N/A /*LINTED*/
0N/A lock = (pthread_mutex_t *)mmap(NULL,
0N/A getpagesize(),
0N/A PROT_READ | PROT_WRITE,
0N/A optp?(MAP_ANON | MAP_SHARED):MAP_ANON|MAP_PRIVATE,
0N/A -1, 0L) + opto;
0N/A
0N/A if (lock == MAP_FAILED) {
0N/A errors++;
0N/A } else {
0N/A (void) pthread_mutexattr_init(&attr);
0N/A if (optp)
0N/A (void) pthread_mutexattr_setpshared(&attr,
0N/A PTHREAD_PROCESS_SHARED);
0N/A
0N/A if (pthread_mutex_init(lock, &attr) != 0)
0N/A errors++;
0N/A }
0N/A
0N/A return (errors);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initworker(void *tsd)
0N/A{
0N/A int errors = 0;
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A
0N/A
0N/A if (optt) {
0N/A pthread_t tid;
0N/A
0N/A
0N/A
0N/A if (pthread_create(&tid, NULL, dummy, NULL) != 0) {
0N/A errors++;
0N/A }
0N/A }
0N/A
0N/A ts->ts_lock = lock;
0N/A
0N/A return (errors);
0N/A}
0N/A
0N/Avoid
0N/Aspinme(int usecs)
0N/A{
0N/A long long s = getusecs();
0N/A
0N/A while (getusecs() - s < usecs)
0N/A ;
0N/A}
0N/A
0N/Aint
0N/Abenchmark(void *tsd, result_t *res)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A int i;
0N/A
0N/A for (i = 0; i < lm_optB; i ++) {
0N/A
0N/A (void) pthread_mutex_lock(ts->ts_lock);
0N/A if (opth)
0N/A spinme(opth);
0N/A (void) pthread_mutex_unlock(ts->ts_lock);
0N/A
0N/A }
0N/A
0N/A res->re_count = lm_optB;
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Achar *
0N/Abenchmark_result()
0N/A{
0N/A static char result[256];
0N/A
0N/A (void) sprintf(result, "%8d", opth);
0N/A
0N/A return (result);
0N/A}