/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* usr/src/OPENSOLARIS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* benchmarking routines
*/
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <pthread.h>
#include <dlfcn.h>
#include <errno.h>
#include <sys/resource.h>
#include <math.h>
#include <limits.h>
#ifdef __sun
#endif
#include "libmicro.h"
/*
* user visible globals
*/
int lm_argc = 0;
int lm_opt1;
int lm_optA;
int lm_optB;
int lm_optD;
int lm_optE;
int lm_optH;
int lm_optI;
int lm_optL = 0;
int lm_optM = 0;
char *lm_optN;
int lm_optP;
int lm_optS;
int lm_optT;
int lm_optW;
int lm_def1 = 0;
int lm_defH = 0;
int lm_defS = 0;
/*
* default on fast platform, should be overridden by individual
* benchmarks if significantly wrong in either direction.
*/
char *lm_procpath;
/*
* Globals we do not export to the user
*/
#ifdef USE_RDTSC
static long long lm_hz = 0;
#endif
/*
* Forward references
*/
static void worker_process();
static void usage();
static void print_stats(barrier_t *);
static void print_histo(barrier_t *);
static int remove_outliers(double *, int, stats_t *);
static long long nsecs_overhead;
static long long nsecs_resolution;
static long long get_nsecs_overhead();
static int crunch_stats(double *, int, stats_t *);
static void compute_stats(barrier_t *);
/*
* main routine; renamed in this file to allow linking with other
* files
*/
int
{
int i;
int opt;
extern char *optarg;
char *tmp;
barrier_t *b;
long long startnsecs;
#ifdef USE_RDTSC
(void) printf("LIBMICRO_HZ needed but not set\n");
exit(1);
}
#endif
startnsecs = getnsecs();
/* before we do anything */
(void) benchmark_init();
/*
* Set defaults
*/
/*
* squirrel away the path to the current
* binary in a way that works on both
* Linux and Solaris
*/
if (*argv[0] == '/') {
} else {
}
/*
* name of binary
*/
else
}
/*
* Parse command line arguments
*/
switch (opt) {
case '1':
lm_opt1 = 1;
break;
case 'A':
lm_optA = 1;
break;
case 'B':
break;
case 'C':
break;
case 'D':
break;
case 'E':
lm_optE = 1;
break;
case 'H':
lm_optH = 1;
break;
case 'I':
break;
case 'L':
lm_optL = 1;
break;
case 'M':
lm_optM = 1;
break;
case 'N':
break;
case 'P':
break;
case 'S':
lm_optS = 1;
break;
case 'T':
break;
case 'V':
exit(0);
break;
case 'W':
lm_optW = 1;
lm_optS = 1;
break;
case '?':
usage();
exit(0);
break;
default:
usage();
exit(0);
}
}
}
/* deal with implicit and overriding options */
lm_optP = 1;
(void) printf("warning: -1 overrides -P\n");
}
if (lm_optE) {
}
if (lm_optB == 0) {
/*
* neither benchmark or user has specified the number
*/
if (lm_optI)
if (lm_optB == 0)
lm_optB = 1;
}
/*
* now that the options are set
*/
if (benchmark_initrun() == -1) {
exit(1);
}
/* allocate dynamic data */
perror("malloc(pids)");
exit(1);
}
perror("malloc(tids)");
exit(1);
}
/* check that the case defines lm_tsdsize before proceeding */
"lm_tsdsize not set\n");
exit(1);
}
/* round up tsdsize to nearest 128 to eliminate false sharing */
/* allocate sufficient TSD for each thread in each process */
perror("mmap(tsd)");
exit(1);
}
/* initialise worker synchronisation */
if (b == NULL) {
perror("barrier_create()");
exit(1);
}
lm_barrier = b;
b->ba_flag = 1;
/* need this here so that parent and children can call exit() */
/* when we started and when to stop */
b->ba_starttime = getnsecs();
/* do the work */
if (lm_opt1) {
/* single process, non-fork mode */
pindex = 0;
} else {
/* create worker processes */
for (i = 0; i < lm_optP; i++) {
switch (pids[i]) {
case 0:
pindex = i;
exit(0);
break;
case -1:
perror("fork");
exit(1);
break;
default:
continue;
}
}
/* wait for worker processes */
for (i = 0; i < lm_optP; i++) {
if (pids[i] > 0) {
}
}
}
b->ba_endtime = getnsecs();
/* compute results */
compute_stats(b);
/* print arguments benchmark was invoked with ? */
if (lm_optL) {
int l;
for (l = 1; l < argc; l++) {
}
(void) printf("\n");
}
/* print result header (unless suppressed) */
if (!lm_optH) {
(void) printf("%12s %3s %3s %12s %12s %8s %8s %s\n",
"", "prc", "thr",
}
/* print result */
(void) printf("%-12s %3d %3d %12.5f %12d %8lld %8d %s\n",
benchmark_result());
if (lm_optS) {
print_stats(b);
}
/* just incase something goes awry */
/* cleanup by stages */
(void) benchmark_finirun();
(void) barrier_destroy(b);
(void) benchmark_fini();
if (lm_optE) {
(double)(getnsecs() - startnsecs) /
1.e9);
}
return (0);
}
void *
{
result_t r;
long long last_sleep = 0;
long long t;
while (lm_barrier->ba_flag) {
r.re_count = 0;
/* sync to clock */
(void) poll(0, 0, 10);
last_sleep = t;
}
/* wait for it ... */
/* time the test */
/* time to stop? */
lm_barrier->ba_flag = 0;
}
/* record results and sync */
(void) barrier_queue(lm_barrier, &r);
(void) benchmark_finibatch(arg);
r.re_errors = 0;
}
(void) benchmark_finiworker(arg);
return (0);
}
void
{
int i;
void *tsd;
for (i = 1; i < lm_optT; i++) {
perror("pthread_create");
exit(1);
}
}
(void) worker_thread(tsd);
for (i = 1; i < lm_optT; i++) {
}
}
void
usage()
{
(void) printf(
"usage: %s\n"
" [-1] (single process; overrides -P > 1)\n"
" [-A] (align with clock)\n"
" [-B batch-size (default %d)]\n"
" [-C minimum number of samples (default 0)]\n"
" [-D duration in msecs (default %ds)]\n"
" [-E (echo name to stderr)]\n"
" [-H] (suppress headers)\n"
" [-I] nsecs per op (used to compute batch size)"
" [-L] (print argument line)\n"
" [-M] (reports mean rather than median)\n"
" [-N test-name (default '%s')]\n"
" [-P processes (default %d)]\n"
" [-S] (print detailed stats)\n"
" [-T threads (default %d)]\n"
" [-V] (print the libMicro version and exit)\n"
" [-W] (flag possible benchmark problems)\n"
"%s\n",
lm_usage);
}
void
{
int head = 0;
int increase;
if (b->ba_quant) {
if (!head++) {
(void) printf("#\n# WARNINGS\n");
}
1.0);
(void) printf("# Quantization error likely;"
"increase batch size (-B option) %dX to avoid.\n",
increase);
}
/*
* XXX should warn on median != mean by a lot
*/
if (b->ba_errors) {
if (!head++) {
(void) printf("#\n# WARNINGS\n");
}
(void) printf("# Errors occured during benchmark.\n");
}
}
void
{
(void) printf("#\n");
(void) printf("# STATISTICS %12s %12s\n",
if (b->ba_count == 0) {
(void) printf("zero samples\n");
return;
}
(void) printf("# min %12.5f %12.5f\n",
b->ba_corrected.st_min);
(void) printf("# max %12.5f %12.5f\n",
b->ba_corrected.st_max);
(void) printf("# mean %12.5f %12.5f\n",
b->ba_corrected.st_mean);
(void) printf("# median %12.5f %12.5f\n",
b->ba_corrected.st_median);
(void) printf("# stddev %12.5f %12.5f\n",
b->ba_corrected.st_stddev);
(void) printf("# standard error %12.5f %12.5f\n",
b->ba_corrected.st_stderr);
(void) printf("# 99%% confidence level %12.5f %12.5f\n",
(void) printf("# skew %12.5f %12.5f\n",
b->ba_corrected.st_skew);
(void) printf("# kurtosis %12.5f %12.5f\n",
b->ba_raw.st_kurtosis,
b->ba_corrected.st_kurtosis);
(void) printf("# time correlation %12.5f %12.5f\n",
b->ba_raw.st_timecorr,
b->ba_corrected.st_timecorr);
(void) printf("#\n");
b->ba_starttime) / 1.0e9);
(void) printf("#\n");
(void) printf("# DISTRIBUTION\n");
print_histo(b);
if (lm_optW) {
print_warnings(b);
}
}
void
{
double time;
double nsecs_per_call;
if (b->ba_waiters == 0) {
/* first thread only */
b->ba_count0 = 0;
b->ba_errors0 = 0;
} else {
/* all but first thread */
}
}
}
b->ba_errors0 += r->re_errors;
/* last thread only */
(double)nsecs_overhead;
b->ba_quant++;
/*
* normalize by procs * threads if not -U
*/
b->ba_errors += b->ba_errors0;
b->ba_batches++;
}
}
#ifdef USE_SEMOP
{
struct sembuf s[1];
barrier_t *b;
/*LINTED*/
if (b == (barrier_t *)MAP_FAILED) {
return (NULL);
}
b->ba_datasize = datasize;
b->ba_flag = 0;
if (b->ba_semid == -1) {
return (NULL);
}
/* [hwm - 1, 0, 0] */
s[0].sem_num = 0;
s[0].sem_flg = 0;
perror("semop(1)");
return (NULL);
}
b->ba_waiters = 0;
b->ba_phase = 0;
b->ba_count = 0;
b->ba_errors = 0;
return (b);
}
int
{
return (0);
}
int
{
struct sembuf s[2];
/*
* {s0(-(hwm-1))}
* if ! nowait {s1(-(hwm-1))}
* (all other threads)
* update shared stats
* {s0(hwm-1), s1(1)}
* {s0(1), s2(-1)}
* else
* (last thread)
* update shared stats
* {s2(hwm-1)}
*/
s[0].sem_num = 0;
s[0].sem_flg = 0;
perror("semop(2)");
return (-1);
}
s[0].sem_num = 1;
s[0].sem_flg = IPC_NOWAIT;
perror("semop(3)");
return (-1);
}
/* all but the last thread */
if (r != NULL) {
update_stats(b, r);
}
b->ba_waiters++;
s[0].sem_num = 0;
s[0].sem_flg = 0;
s[1].sem_flg = 0;
perror("semop(4)");
return (-1);
}
s[0].sem_num = 0;
s[0].sem_op = 1;
s[0].sem_flg = 0;
s[1].sem_flg = 0;
perror("semop(5)");
return (-1);
}
} else {
/* the last thread */
if (r != NULL) {
update_stats(b, r);
}
b->ba_waiters = 0;
b->ba_phase++;
s[0].sem_num = 2;
s[0].sem_flg = 0;
perror("semop(6)");
return (-1);
}
}
return (0);
}
#else /* USE_SEMOP */
{
barrier_t *b;
/*LINTED*/
if (b == (barrier_t *)MAP_FAILED) {
return (NULL);
}
b->ba_datasize = datasize;
b->ba_flag = 0;
(void) pthread_mutexattr_init(&attr);
(void) pthread_condattr_init(&cattr);
b->ba_waiters = 0;
b->ba_phase = 0;
b->ba_count = 0;
b->ba_errors = 0;
return (b);
}
int
{
return (0);
}
int
{
int phase;
(void) pthread_mutex_lock(&b->ba_lock);
if (r != NULL) {
update_stats(b, r);
}
b->ba_waiters++;
if (b->ba_hwm == b->ba_waiters) {
b->ba_waiters = 0;
b->ba_phase++;
(void) pthread_cond_broadcast(&b->ba_cv);
}
}
(void) pthread_mutex_unlock(&b->ba_lock);
return (0);
}
#endif /* USE_SEMOP */
int
{
int i;
return (-1);
}
for (i = 1; i < lm_optT; i++) {
if (pthread_self() == tids[i]) {
return (i);
}
}
return (0);
}
int
{
return (pindex);
}
void *
gettsd(int p, int t)
{
return (NULL);
return ((void *)((unsigned long)tsdseg +
}
#ifdef USE_GETHRTIME
long long
getnsecs()
{
return (gethrtime());
}
long long
getusecs()
{
return (gethrtime() / 1000);
}
__inline__ long long
rdtsc(void)
{
unsigned long long x;
return (x);
}
long long
getusecs()
{
}
long long
getnsecs()
{
}
#else /* USE_GETHRTIME */
long long
getusecs()
{
}
long long
getnsecs()
{
}
#endif /* USE_GETHRTIME */
int
{
perror("getrlimit");
exit(1);
}
return (0); /* no worries */
perror("setrlimit");
exit(3);
}
return (0);
}
long long
{
int i;
case 'k':
case 'K':
break;
case 'm':
case 'M':
break;
case 'g':
case 'G':
break;
default:
return (-1);
}
for (i = 0; i < len - 1; i++)
return (-1);
}
}
int
{
int i;
case 'k':
case 'K':
break;
case 'm':
case 'M':
break;
case 'g':
case 'G':
break;
default:
return (-1);
}
for (i = 0; i < len - 1; i++)
return (-1);
}
}
static void
{
int i;
(void) putchar_unlocked('*');
for (; i < 32; i++)
(void) putchar_unlocked(' ');
}
static int
{
double a = *((double *)p1);
double b = *((double *)p2);
if (a > b)
return (1);
if (a < b)
return (-1);
return (0);
}
static void
{
int n;
int i;
int j;
int last;
long long maxcount;
double sum;
long long min;
long long scale;
double x;
long long y;
long long count;
int i95;
double p95;
double r95;
double m95;
"", "means");
/* calculate how much data we've captured */
/* find the 95th percentile - index, value and range */
/* find a suitable min and scale */
i = 0;
while (x >= 10.0) {
x /= 10.0;
i++;
}
y = x + 0.9999999999;
while (i > 0) {
y *= 10;
i--;
}
min /= y;
min *= y;
}
/* create and initialise the histogram */
for (i = 0; i < HISTOSIZE; i++) {
}
/* populate the histogram */
last = 0;
sum = 0.0;
count = 0;
for (i = 0; i < i95; i++) {
if (j >= HISTOSIZE) {
(void) printf("panic!\n");
j = HISTOSIZE - 1;
}
count++;
}
/* find the larges bucket */
maxcount = 0;
for (i = 0; i < HISTOSIZE; i++)
last = i;
}
/* print the buckets */
for (i = 0; i <= last; i++) {
(void) printf("%12.5f\n",
else
}
/* find the mean of values beyond the 95th percentile */
sum = 0.0;
count = 0;
for (i = i95; i < n; i++) {
count++;
}
/* print the >95% bucket summary */
(void) printf("#\n");
if (count > 0)
else
(void) printf("#\n");
/* quantify any buffer overflow */
if (b->ba_batches > b->ba_datasize)
b->ba_batches - b->ba_datasize);
}
static void
{
int i;
if (b->ba_batches > b->ba_datasize)
b->ba_batches = b->ba_datasize;
/*
*/
for (i = 0; i < b->ba_batches; i++)
b->ba_data[i] /= 1000.0;
/*
* do raw stats
*/
/*
* recursively apply 3 sigma rule to remove outliers
*/
b->ba_corrected = b->ba_raw;
b->ba_outliers = 0;
int removed;
do {
&b->ba_corrected);
b->ba_outliers += removed;
b->ba_batches -= removed;
&b->ba_corrected);
}
}
/*
* routine to compute various statistics on array of doubles.
*/
static int
{
double a;
double std;
double diff;
double sk;
double ku;
double mean;
int i;
int bytes;
double *dupdata;
/*
* first we need the mean
*/
mean = 0.0;
for (i = 0; i < count; i++) {
}
/*
* malloc and sort so we can do median
*/
/*
* reuse dupdata to compute time correlation of data to
* detect interesting time-based trends
*/
for (i = 0; i < count; i++)
dupdata[i] = (double)i;
std = 0.0;
sk = 0.0;
ku = 0.0;
for (i = 0; i < count; i++) {
}
(double)(count) - 3;
return (0);
}
/*
* does a least squares fit to the set of points x, y and
* fits a line y = a + bx. Returns a, b
*/
int
{
double denom;
int i;
for (i = 0; i < count; i++) {
sumx += x[i];
sumx2 += x[i] * x[i];
sumy += y[i];
sumxy += x[i] * y[i];
}
if (denom == 0.0)
return (-1);
return (0);
}
/*
* empty function for measurement purposes
*/
int
nop()
{
return (1);
}
static long long
{
long long s;
int i;
int count;
int outliers;
(void) getnsecs(); /* warmup */
(void) getnsecs(); /* warmup */
(void) getnsecs(); /* warmup */
i = 0;
for (i = 0; i < count; i++) {
s = getnsecs();
}
}
}
/*
* Determine the resolution of the system's high resolution counter.
* Most hardware has a nanosecond resolution counter, but some systems still
* use course resolution (e.g. derived instead by a periodic interrupt).
*
* Algorithm:
* Determine a busy loop that is long enough for successive nanosecond counter
* reads to report different times. Then take 1000 samples with busy loop
* interval successively increases by i. The counter resolution is assumed
* to be the smallest non-zero time delta between these 1000 samples.
*
* One last wrinkle is all 1000 samples may have the same delta on a system
* with a very fast and consistent hardware counter based getnsecs().
* In that case assume the resolution is 1ns.
*/
long long
{
long long y[1000];
volatile int i, j;
/*
* first, figure out how many nops to use
* to get any delta between time measurements.
* use a minimum of one.
*/
/*
* warm cache
*/
for (i = 1; i < 10000000; i++) {
for (j = i; j; j--)
;
break;
}
nops = i;
/*
* now collect data at linearly varying intervals
*/
for (i = 0; i < 1000; i++) {
for (j = nops * i; j; j--)
;
}
/*
* find smallest positive difference between samples;
* this is the counter resolution
*/
res = y[0];
for (i = 1; i < 1000; i++) {
}
if (res == 0)
res = 1;
return (res);
}
/*
* remove any data points from the array more than 3 sigma out
*/
static int
{
int i, j, outliers;
outliers++;
else
return (outliers);
}