6N/A/*
6N/A * CDDL HEADER START
6N/A *
14N/A * The contents of this file are subject to the terms of the
14N/A * Common Development and Distribution License (the "License").
14N/A * You may not use this file except in compliance with the License.
6N/A *
14N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
6N/A * or http://www.opensolaris.org/os/licensing.
14N/A * See the License for the specific language governing permissions
14N/A * and limitations under the License.
6N/A *
14N/A * When distributing Covered Code, include this CDDL HEADER in each
14N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14N/A * If applicable, add the following below this CDDL HEADER, with the
14N/A * fields enclosed by brackets "[]" replaced with your own identifying
14N/A * information: Portions Copyright [yyyy] [name of copyright owner]
6N/A *
6N/A * CDDL HEADER END
6N/A */
6N/A
0N/A/*
14N/A * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/A#include <sys/mman.h>
14N/A#include <sys/sysmacros.h>
14N/A#include <sys/types.h>
0N/A#include <unistd.h>
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A#include <fcntl.h>
0N/A#include <strings.h>
0N/A
0N/A#include "libmicro.h"
0N/A
0N/Atypedef volatile char vchar_t;
0N/A
0N/Atypedef struct {
0N/A int ts_once;
0N/A vchar_t ** ts_map;
14N/A vchar_t ts_foo;
0N/A} tsd_t;
0N/A
0N/A#define DEFF "/dev/zero"
14N/A#define GIGABYTE (1024 * 1024 * 1024)
0N/A
0N/Astatic char *optf = DEFF;
14N/Astatic long long optl = 0;
14N/Astatic long long optp = 0;
0N/Astatic int optr = 0;
0N/Astatic int opts = 0;
0N/Astatic int optw = 0;
0N/Astatic int fd = -1;
0N/Astatic int anon = 0;
0N/A
14N/Astatic long long def_optl;
14N/Astatic long long def_optp;
14N/Astatic int npagesizes = 1;
14N/Astatic size_t *pagesizes;
14N/A
0N/Aint
0N/Abenchmark_init()
0N/A{
0N/A lm_tsdsize = sizeof (tsd_t);
14N/A int i;
0N/A
14N/A (void) sprintf(lm_optstr, "f:l:p:rsw");
14N/A
14N/A#ifdef __sun
14N/A /* determine available page sizes before parsing options */
14N/A npagesizes = getpagesizes(NULL, 0);
14N/A if (npagesizes <= 0) {
14N/A return (-1);
14N/A }
14N/A#endif
14N/A
14N/A pagesizes = malloc(npagesizes * sizeof (size_t));
14N/A if (pagesizes == NULL) {
14N/A return (-1);
14N/A }
14N/A
14N/A pagesizes[0] = sysconf(_SC_PAGESIZE);
14N/A
14N/A#ifdef __sun
14N/A if (getpagesizes(pagesizes, npagesizes) != npagesizes) {
14N/A return (-1);
14N/A }
14N/A#endif
14N/A
14N/A /* pick a default for mapping length and page size */
14N/A def_optp = pagesizes[0];
14N/A
14N/A /* set mapping length so we map at least 2 pages */
14N/A def_optl = def_optp * 2;
14N/A optp = def_optp;
14N/A optl = def_optl;
0N/A
0N/A (void) sprintf(lm_usage,
0N/A " [-f file-to-map (default %s)]\n"
14N/A " [-l mapping-length (default %lld)]\n"
14N/A " [-p page-size (default %lld)]\n"
0N/A " [-r] (read a byte from each page)\n"
0N/A " [-w] (write a byte on each page)\n"
0N/A " [-s] (use MAP_SHARED)\n"
0N/A "notes: measures mmap()\n",
14N/A DEFF, def_optl, def_optp);
0N/A
14N/A (void) sprintf(lm_header, "%8s %5s %8s", "length", "flags", "pgsz");
14N/A
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_optswitch(int opt, char *optarg)
0N/A{
0N/A switch (opt) {
0N/A case 'f':
0N/A optf = optarg;
0N/A anon = strcmp(optf, "MAP_ANON") == 0;
0N/A break;
0N/A case 'l':
0N/A optl = sizetoll(optarg);
0N/A break;
14N/A case 'p':
14N/A optp = sizetoll(optarg);
14N/A break;
0N/A case 'r':
0N/A optr = 1;
0N/A break;
0N/A case 's':
0N/A opts = 1;
0N/A break;
0N/A case 'w':
0N/A optw = 1;
0N/A break;
0N/A default:
0N/A return (-1);
0N/A }
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initrun()
0N/A{
14N/A int i;
14N/A /*
14N/A * If optp or optl were set by caller, make sure that they point to a
14N/A * valid pagesize. If not, return an error.
14N/A */
14N/A if (optp != def_optp) {
14N/A for (i = 0; i < npagesizes; i++) {
14N/A if (pagesizes[i] == optp) {
14N/A break;
14N/A }
14N/A }
14N/A if (i == npagesizes) {
14N/A printf("Invalid page size: %lld\n", optp);
14N/A exit(1);
14N/A }
14N/A }
14N/A
14N/A /*
14N/A * If the benchmark is running 32-bit, return an error if a page
14N/A * size >= 1G is selected.
14N/A */
14N/A if ((optp >= GIGABYTE) && (sizeof (void *) == 4)) {
14N/A printf("Page sizes >= 1G not supported on 32-bit\n");
14N/A exit(1);
14N/A }
14N/A
14N/A if (optl == def_optl) {
14N/A optl = optp * 2;
14N/A } else if (optl % optp != 0) {
14N/A printf("map length %lld must be aligned to page size %lld\n",
14N/A optl, optp);
14N/A exit(1);
14N/A }
14N/A
0N/A if (!anon)
0N/A fd = open(optf, O_RDWR);
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initbatch(void *tsd)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A int errors = 0;
0N/A
0N/A if (ts->ts_once++ == 0) {
0N/A ts->ts_map = (vchar_t **)malloc(lm_optB * sizeof (void *));
0N/A if (ts->ts_map == NULL) {
0N/A errors++;
0N/A }
0N/A }
0N/A
0N/A return (errors);
0N/A}
0N/A
0N/Aint
0N/Abenchmark(void *tsd, result_t *res)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
14N/A int i;
14N/A unsigned long j;
14N/A#ifdef __sun
14N/A struct memcntl_mha marg;
14N/A
14N/A marg.mha_cmd = MHA_MAPSIZE_VA;
14N/A marg.mha_flags = 0;
14N/A marg.mha_pagesize = optp;
14N/A#endif
0N/A
0N/A for (i = 0; i < lm_optB; i++) {
0N/A if (anon) {
0N/A ts->ts_map[i] = (vchar_t *)mmap(NULL, optl,
0N/A PROT_READ | PROT_WRITE,
0N/A MAP_ANON | (opts ? MAP_SHARED : MAP_PRIVATE),
0N/A -1, 0L);
0N/A } else {
0N/A ts->ts_map[i] = (vchar_t *)mmap(NULL, optl,
0N/A PROT_READ | PROT_WRITE,
0N/A opts ? MAP_SHARED : MAP_PRIVATE,
0N/A fd, 0L);
0N/A }
0N/A
0N/A if (ts->ts_map[i] == MAP_FAILED) {
0N/A res->re_errors++;
0N/A continue;
0N/A }
14N/A#ifdef __sun
14N/A if ((optp != def_optp) &&
14N/A memcntl((caddr_t)ts->ts_map[i], optl, MC_HAT_ADVISE,
14N/A (caddr_t)&marg, 0, 0) == -1) {
14N/A res->re_errors++;
14N/A continue;
14N/A }
14N/A#endif
0N/A
0N/A if (optr) {
14N/A for (j = 0; j < optl; j += optp) {
0N/A ts->ts_foo += ts->ts_map[i][j];
0N/A }
0N/A }
0N/A if (optw) {
14N/A for (j = 0; j < optl; j += optp) {
0N/A ts->ts_map[i][j] = 1;
0N/A }
0N/A }
0N/A }
0N/A res->re_count = i;
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_finibatch(void *tsd)
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 (void) munmap((void *)ts->ts_map[i], optl);
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 char flags[5];
0N/A
0N/A flags[0] = anon ? 'a' : '-';
0N/A flags[1] = optr ? 'r' : '-';
0N/A flags[2] = optw ? 'w' : '-';
0N/A flags[3] = opts ? 's' : '-';
0N/A flags[4] = 0;
0N/A
14N/A (void) sprintf(result, "%8lld %5s %8lld", optl, flags, optp);
0N/A
0N/A return (result);
0N/A}