mirror of
https://github.com/AetherDroid/android_kernel_samsung_on5xelte.git
synced 2025-10-29 07:18:51 +01:00
Fixed MTP to work with TWRP
This commit is contained in:
commit
f6dfaef42e
50820 changed files with 20846062 additions and 0 deletions
48
tools/perf/bench/bench.h
Normal file
48
tools/perf/bench/bench.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#ifndef BENCH_H
|
||||
#define BENCH_H
|
||||
|
||||
/*
|
||||
* The madvise transparent hugepage constants were added in glibc
|
||||
* 2.13. For compatibility with older versions of glibc, define these
|
||||
* tokens if they are not already defined.
|
||||
*
|
||||
* PA-RISC uses different madvise values from other architectures and
|
||||
* needs to be special-cased.
|
||||
*/
|
||||
#ifdef __hppa__
|
||||
# ifndef MADV_HUGEPAGE
|
||||
# define MADV_HUGEPAGE 67
|
||||
# endif
|
||||
# ifndef MADV_NOHUGEPAGE
|
||||
# define MADV_NOHUGEPAGE 68
|
||||
# endif
|
||||
#else
|
||||
# ifndef MADV_HUGEPAGE
|
||||
# define MADV_HUGEPAGE 14
|
||||
# endif
|
||||
# ifndef MADV_NOHUGEPAGE
|
||||
# define MADV_NOHUGEPAGE 15
|
||||
# endif
|
||||
#endif
|
||||
|
||||
extern int bench_numa(int argc, const char **argv, const char *prefix);
|
||||
extern int bench_sched_messaging(int argc, const char **argv, const char *prefix);
|
||||
extern int bench_sched_pipe(int argc, const char **argv, const char *prefix);
|
||||
extern int bench_mem_memcpy(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused);
|
||||
extern int bench_mem_memset(int argc, const char **argv, const char *prefix);
|
||||
extern int bench_futex_hash(int argc, const char **argv, const char *prefix);
|
||||
extern int bench_futex_wake(int argc, const char **argv, const char *prefix);
|
||||
extern int bench_futex_requeue(int argc, const char **argv, const char *prefix);
|
||||
|
||||
#define BENCH_FORMAT_DEFAULT_STR "default"
|
||||
#define BENCH_FORMAT_DEFAULT 0
|
||||
#define BENCH_FORMAT_SIMPLE_STR "simple"
|
||||
#define BENCH_FORMAT_SIMPLE 1
|
||||
|
||||
#define BENCH_FORMAT_UNKNOWN -1
|
||||
|
||||
extern int bench_format;
|
||||
extern unsigned int bench_repeat;
|
||||
|
||||
#endif
|
||||
215
tools/perf/bench/futex-hash.c
Normal file
215
tools/perf/bench/futex-hash.c
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
|
||||
*
|
||||
* futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
|
||||
*
|
||||
* This program is particularly useful for measuring the kernel's futex hash
|
||||
* table/function implementation. In order for it to make sense, use with as
|
||||
* many threads and futexes as possible.
|
||||
*/
|
||||
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/stat.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../util/header.h"
|
||||
#include "bench.h"
|
||||
#include "futex.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
static unsigned int nthreads = 0;
|
||||
static unsigned int nsecs = 10;
|
||||
/* amount of futexes per thread */
|
||||
static unsigned int nfutexes = 1024;
|
||||
static bool fshared = false, done = false, silent = false;
|
||||
static int futex_flag = 0;
|
||||
|
||||
struct timeval start, end, runtime;
|
||||
static pthread_mutex_t thread_lock;
|
||||
static unsigned int threads_starting;
|
||||
static struct stats throughput_stats;
|
||||
static pthread_cond_t thread_parent, thread_worker;
|
||||
|
||||
struct worker {
|
||||
int tid;
|
||||
u_int32_t *futex;
|
||||
pthread_t thread;
|
||||
unsigned long ops;
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
|
||||
OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
|
||||
OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
|
||||
OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
|
||||
OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
static const char * const bench_futex_hash_usage[] = {
|
||||
"perf bench futex hash <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
static void *workerfn(void *arg)
|
||||
{
|
||||
int ret;
|
||||
unsigned int i;
|
||||
struct worker *w = (struct worker *) arg;
|
||||
|
||||
pthread_mutex_lock(&thread_lock);
|
||||
threads_starting--;
|
||||
if (!threads_starting)
|
||||
pthread_cond_signal(&thread_parent);
|
||||
pthread_cond_wait(&thread_worker, &thread_lock);
|
||||
pthread_mutex_unlock(&thread_lock);
|
||||
|
||||
do {
|
||||
for (i = 0; i < nfutexes; i++, w->ops++) {
|
||||
/*
|
||||
* We want the futex calls to fail in order to stress
|
||||
* the hashing of uaddr and not measure other steps,
|
||||
* such as internal waitqueue handling, thus enlarging
|
||||
* the critical region protected by hb->lock.
|
||||
*/
|
||||
ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
|
||||
if (!silent &&
|
||||
(!ret || errno != EAGAIN || errno != EWOULDBLOCK))
|
||||
warn("Non-expected futex return call");
|
||||
}
|
||||
} while (!done);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void toggle_done(int sig __maybe_unused,
|
||||
siginfo_t *info __maybe_unused,
|
||||
void *uc __maybe_unused)
|
||||
{
|
||||
/* inform all threads that we're done for the day */
|
||||
done = true;
|
||||
gettimeofday(&end, NULL);
|
||||
timersub(&end, &start, &runtime);
|
||||
}
|
||||
|
||||
static void print_summary(void)
|
||||
{
|
||||
unsigned long avg = avg_stats(&throughput_stats);
|
||||
double stddev = stddev_stats(&throughput_stats);
|
||||
|
||||
printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
|
||||
!silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
|
||||
(int) runtime.tv_sec);
|
||||
}
|
||||
|
||||
int bench_futex_hash(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused)
|
||||
{
|
||||
int ret = 0;
|
||||
cpu_set_t cpu;
|
||||
struct sigaction act;
|
||||
unsigned int i, ncpus;
|
||||
pthread_attr_t thread_attr;
|
||||
struct worker *worker = NULL;
|
||||
|
||||
argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
|
||||
if (argc) {
|
||||
usage_with_options(bench_futex_hash_usage, options);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
|
||||
sigfillset(&act.sa_mask);
|
||||
act.sa_sigaction = toggle_done;
|
||||
sigaction(SIGINT, &act, NULL);
|
||||
|
||||
if (!nthreads) /* default to the number of CPUs */
|
||||
nthreads = ncpus;
|
||||
|
||||
worker = calloc(nthreads, sizeof(*worker));
|
||||
if (!worker)
|
||||
goto errmem;
|
||||
|
||||
if (!fshared)
|
||||
futex_flag = FUTEX_PRIVATE_FLAG;
|
||||
|
||||
printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
|
||||
getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
|
||||
|
||||
init_stats(&throughput_stats);
|
||||
pthread_mutex_init(&thread_lock, NULL);
|
||||
pthread_cond_init(&thread_parent, NULL);
|
||||
pthread_cond_init(&thread_worker, NULL);
|
||||
|
||||
threads_starting = nthreads;
|
||||
pthread_attr_init(&thread_attr);
|
||||
gettimeofday(&start, NULL);
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
worker[i].tid = i;
|
||||
worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
|
||||
if (!worker[i].futex)
|
||||
goto errmem;
|
||||
|
||||
CPU_ZERO(&cpu);
|
||||
CPU_SET(i % ncpus, &cpu);
|
||||
|
||||
ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
|
||||
if (ret)
|
||||
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
|
||||
|
||||
ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
|
||||
(void *)(struct worker *) &worker[i]);
|
||||
if (ret)
|
||||
err(EXIT_FAILURE, "pthread_create");
|
||||
|
||||
}
|
||||
pthread_attr_destroy(&thread_attr);
|
||||
|
||||
pthread_mutex_lock(&thread_lock);
|
||||
while (threads_starting)
|
||||
pthread_cond_wait(&thread_parent, &thread_lock);
|
||||
pthread_cond_broadcast(&thread_worker);
|
||||
pthread_mutex_unlock(&thread_lock);
|
||||
|
||||
sleep(nsecs);
|
||||
toggle_done(0, NULL, NULL);
|
||||
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
ret = pthread_join(worker[i].thread, NULL);
|
||||
if (ret)
|
||||
err(EXIT_FAILURE, "pthread_join");
|
||||
}
|
||||
|
||||
/* cleanup & report results */
|
||||
pthread_cond_destroy(&thread_parent);
|
||||
pthread_cond_destroy(&thread_worker);
|
||||
pthread_mutex_destroy(&thread_lock);
|
||||
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
unsigned long t = worker[i].ops/runtime.tv_sec;
|
||||
update_stats(&throughput_stats, t);
|
||||
if (!silent) {
|
||||
if (nfutexes == 1)
|
||||
printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
|
||||
worker[i].tid, &worker[i].futex[0], t);
|
||||
else
|
||||
printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
|
||||
worker[i].tid, &worker[i].futex[0],
|
||||
&worker[i].futex[nfutexes-1], t);
|
||||
}
|
||||
|
||||
free(worker[i].futex);
|
||||
}
|
||||
|
||||
print_summary();
|
||||
|
||||
free(worker);
|
||||
return ret;
|
||||
errmem:
|
||||
err(EXIT_FAILURE, "calloc");
|
||||
}
|
||||
211
tools/perf/bench/futex-requeue.c
Normal file
211
tools/perf/bench/futex-requeue.c
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
* Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
|
||||
*
|
||||
* futex-requeue: Block a bunch of threads on futex1 and requeue them
|
||||
* on futex2, N at a time.
|
||||
*
|
||||
* This program is particularly useful to measure the latency of nthread
|
||||
* requeues without waking up any tasks -- thus mimicking a regular futex_wait.
|
||||
*/
|
||||
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/stat.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../util/header.h"
|
||||
#include "bench.h"
|
||||
#include "futex.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
static u_int32_t futex1 = 0, futex2 = 0;
|
||||
|
||||
/*
|
||||
* How many tasks to requeue at a time.
|
||||
* Default to 1 in order to make the kernel work more.
|
||||
*/
|
||||
static unsigned int nrequeue = 1;
|
||||
|
||||
static pthread_t *worker;
|
||||
static bool done = false, silent = false, fshared = false;
|
||||
static pthread_mutex_t thread_lock;
|
||||
static pthread_cond_t thread_parent, thread_worker;
|
||||
static struct stats requeuetime_stats, requeued_stats;
|
||||
static unsigned int ncpus, threads_starting, nthreads = 0;
|
||||
static int futex_flag = 0;
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
|
||||
OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
|
||||
OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
|
||||
OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
static const char * const bench_futex_requeue_usage[] = {
|
||||
"perf bench futex requeue <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
static void print_summary(void)
|
||||
{
|
||||
double requeuetime_avg = avg_stats(&requeuetime_stats);
|
||||
double requeuetime_stddev = stddev_stats(&requeuetime_stats);
|
||||
unsigned int requeued_avg = avg_stats(&requeued_stats);
|
||||
|
||||
printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
|
||||
requeued_avg,
|
||||
nthreads,
|
||||
requeuetime_avg/1e3,
|
||||
rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
|
||||
}
|
||||
|
||||
static void *workerfn(void *arg __maybe_unused)
|
||||
{
|
||||
pthread_mutex_lock(&thread_lock);
|
||||
threads_starting--;
|
||||
if (!threads_starting)
|
||||
pthread_cond_signal(&thread_parent);
|
||||
pthread_cond_wait(&thread_worker, &thread_lock);
|
||||
pthread_mutex_unlock(&thread_lock);
|
||||
|
||||
futex_wait(&futex1, 0, NULL, futex_flag);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void block_threads(pthread_t *w,
|
||||
pthread_attr_t thread_attr)
|
||||
{
|
||||
cpu_set_t cpu;
|
||||
unsigned int i;
|
||||
|
||||
threads_starting = nthreads;
|
||||
|
||||
/* create and block all threads */
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
CPU_ZERO(&cpu);
|
||||
CPU_SET(i % ncpus, &cpu);
|
||||
|
||||
if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
|
||||
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
|
||||
|
||||
if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
|
||||
err(EXIT_FAILURE, "pthread_create");
|
||||
}
|
||||
}
|
||||
|
||||
static void toggle_done(int sig __maybe_unused,
|
||||
siginfo_t *info __maybe_unused,
|
||||
void *uc __maybe_unused)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
|
||||
int bench_futex_requeue(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused)
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned int i, j;
|
||||
struct sigaction act;
|
||||
pthread_attr_t thread_attr;
|
||||
|
||||
argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
|
||||
if (argc)
|
||||
goto err;
|
||||
|
||||
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
|
||||
sigfillset(&act.sa_mask);
|
||||
act.sa_sigaction = toggle_done;
|
||||
sigaction(SIGINT, &act, NULL);
|
||||
|
||||
if (!nthreads)
|
||||
nthreads = ncpus;
|
||||
|
||||
worker = calloc(nthreads, sizeof(*worker));
|
||||
if (!worker)
|
||||
err(EXIT_FAILURE, "calloc");
|
||||
|
||||
if (!fshared)
|
||||
futex_flag = FUTEX_PRIVATE_FLAG;
|
||||
|
||||
printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
|
||||
"%d at a time.\n\n", getpid(), nthreads,
|
||||
fshared ? "shared":"private", &futex1, &futex2, nrequeue);
|
||||
|
||||
init_stats(&requeued_stats);
|
||||
init_stats(&requeuetime_stats);
|
||||
pthread_attr_init(&thread_attr);
|
||||
pthread_mutex_init(&thread_lock, NULL);
|
||||
pthread_cond_init(&thread_parent, NULL);
|
||||
pthread_cond_init(&thread_worker, NULL);
|
||||
|
||||
for (j = 0; j < bench_repeat && !done; j++) {
|
||||
unsigned int nrequeued = 0;
|
||||
struct timeval start, end, runtime;
|
||||
|
||||
/* create, launch & block all threads */
|
||||
block_threads(worker, thread_attr);
|
||||
|
||||
/* make sure all threads are already blocked */
|
||||
pthread_mutex_lock(&thread_lock);
|
||||
while (threads_starting)
|
||||
pthread_cond_wait(&thread_parent, &thread_lock);
|
||||
pthread_cond_broadcast(&thread_worker);
|
||||
pthread_mutex_unlock(&thread_lock);
|
||||
|
||||
usleep(100000);
|
||||
|
||||
/* Ok, all threads are patiently blocked, start requeueing */
|
||||
gettimeofday(&start, NULL);
|
||||
for (nrequeued = 0; nrequeued < nthreads; nrequeued += nrequeue) {
|
||||
/*
|
||||
* Do not wakeup any tasks blocked on futex1, allowing
|
||||
* us to really measure futex_wait functionality.
|
||||
*/
|
||||
futex_cmp_requeue(&futex1, 0, &futex2, 0,
|
||||
nrequeue, futex_flag);
|
||||
}
|
||||
gettimeofday(&end, NULL);
|
||||
timersub(&end, &start, &runtime);
|
||||
|
||||
if (nrequeued > nthreads)
|
||||
nrequeued = nthreads;
|
||||
|
||||
update_stats(&requeued_stats, nrequeued);
|
||||
update_stats(&requeuetime_stats, runtime.tv_usec);
|
||||
|
||||
if (!silent) {
|
||||
printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
|
||||
j + 1, nrequeued, nthreads, runtime.tv_usec/1e3);
|
||||
}
|
||||
|
||||
/* everybody should be blocked on futex2, wake'em up */
|
||||
nrequeued = futex_wake(&futex2, nthreads, futex_flag);
|
||||
if (nthreads != nrequeued)
|
||||
warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
|
||||
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
ret = pthread_join(worker[i], NULL);
|
||||
if (ret)
|
||||
err(EXIT_FAILURE, "pthread_join");
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup & report results */
|
||||
pthread_cond_destroy(&thread_parent);
|
||||
pthread_cond_destroy(&thread_worker);
|
||||
pthread_mutex_destroy(&thread_lock);
|
||||
pthread_attr_destroy(&thread_attr);
|
||||
|
||||
print_summary();
|
||||
|
||||
free(worker);
|
||||
return ret;
|
||||
err:
|
||||
usage_with_options(bench_futex_requeue_usage, options);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
198
tools/perf/bench/futex-wake.c
Normal file
198
tools/perf/bench/futex-wake.c
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
/*
|
||||
* Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
|
||||
*
|
||||
* futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
|
||||
*
|
||||
* This program is particularly useful to measure the latency of nthread wakeups
|
||||
* in non-error situations: all waiters are queued and all wake calls wakeup
|
||||
* one or more tasks, and thus the waitqueue is never empty.
|
||||
*/
|
||||
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/stat.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../util/header.h"
|
||||
#include "bench.h"
|
||||
#include "futex.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
/* all threads will block on the same futex */
|
||||
static u_int32_t futex1 = 0;
|
||||
|
||||
/*
|
||||
* How many wakeups to do at a time.
|
||||
* Default to 1 in order to make the kernel work more.
|
||||
*/
|
||||
static unsigned int nwakes = 1;
|
||||
|
||||
pthread_t *worker;
|
||||
static bool done = false, silent = false, fshared = false;
|
||||
static pthread_mutex_t thread_lock;
|
||||
static pthread_cond_t thread_parent, thread_worker;
|
||||
static struct stats waketime_stats, wakeup_stats;
|
||||
static unsigned int ncpus, threads_starting, nthreads = 0;
|
||||
static int futex_flag = 0;
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
|
||||
OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"),
|
||||
OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
|
||||
OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
static const char * const bench_futex_wake_usage[] = {
|
||||
"perf bench futex wake <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
static void *workerfn(void *arg __maybe_unused)
|
||||
{
|
||||
pthread_mutex_lock(&thread_lock);
|
||||
threads_starting--;
|
||||
if (!threads_starting)
|
||||
pthread_cond_signal(&thread_parent);
|
||||
pthread_cond_wait(&thread_worker, &thread_lock);
|
||||
pthread_mutex_unlock(&thread_lock);
|
||||
|
||||
futex_wait(&futex1, 0, NULL, futex_flag);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void print_summary(void)
|
||||
{
|
||||
double waketime_avg = avg_stats(&waketime_stats);
|
||||
double waketime_stddev = stddev_stats(&waketime_stats);
|
||||
unsigned int wakeup_avg = avg_stats(&wakeup_stats);
|
||||
|
||||
printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
|
||||
wakeup_avg,
|
||||
nthreads,
|
||||
waketime_avg/1e3,
|
||||
rel_stddev_stats(waketime_stddev, waketime_avg));
|
||||
}
|
||||
|
||||
static void block_threads(pthread_t *w,
|
||||
pthread_attr_t thread_attr)
|
||||
{
|
||||
cpu_set_t cpu;
|
||||
unsigned int i;
|
||||
|
||||
threads_starting = nthreads;
|
||||
|
||||
/* create and block all threads */
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
CPU_ZERO(&cpu);
|
||||
CPU_SET(i % ncpus, &cpu);
|
||||
|
||||
if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
|
||||
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
|
||||
|
||||
if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
|
||||
err(EXIT_FAILURE, "pthread_create");
|
||||
}
|
||||
}
|
||||
|
||||
static void toggle_done(int sig __maybe_unused,
|
||||
siginfo_t *info __maybe_unused,
|
||||
void *uc __maybe_unused)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
|
||||
int bench_futex_wake(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused)
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned int i, j;
|
||||
struct sigaction act;
|
||||
pthread_attr_t thread_attr;
|
||||
|
||||
argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
|
||||
if (argc) {
|
||||
usage_with_options(bench_futex_wake_usage, options);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
|
||||
sigfillset(&act.sa_mask);
|
||||
act.sa_sigaction = toggle_done;
|
||||
sigaction(SIGINT, &act, NULL);
|
||||
|
||||
if (!nthreads)
|
||||
nthreads = ncpus;
|
||||
|
||||
worker = calloc(nthreads, sizeof(*worker));
|
||||
if (!worker)
|
||||
err(EXIT_FAILURE, "calloc");
|
||||
|
||||
if (!fshared)
|
||||
futex_flag = FUTEX_PRIVATE_FLAG;
|
||||
|
||||
printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
|
||||
"waking up %d at a time.\n\n",
|
||||
getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
|
||||
|
||||
init_stats(&wakeup_stats);
|
||||
init_stats(&waketime_stats);
|
||||
pthread_attr_init(&thread_attr);
|
||||
pthread_mutex_init(&thread_lock, NULL);
|
||||
pthread_cond_init(&thread_parent, NULL);
|
||||
pthread_cond_init(&thread_worker, NULL);
|
||||
|
||||
for (j = 0; j < bench_repeat && !done; j++) {
|
||||
unsigned int nwoken = 0;
|
||||
struct timeval start, end, runtime;
|
||||
|
||||
/* create, launch & block all threads */
|
||||
block_threads(worker, thread_attr);
|
||||
|
||||
/* make sure all threads are already blocked */
|
||||
pthread_mutex_lock(&thread_lock);
|
||||
while (threads_starting)
|
||||
pthread_cond_wait(&thread_parent, &thread_lock);
|
||||
pthread_cond_broadcast(&thread_worker);
|
||||
pthread_mutex_unlock(&thread_lock);
|
||||
|
||||
usleep(100000);
|
||||
|
||||
/* Ok, all threads are patiently blocked, start waking folks up */
|
||||
gettimeofday(&start, NULL);
|
||||
while (nwoken != nthreads)
|
||||
nwoken += futex_wake(&futex1, nwakes, futex_flag);
|
||||
gettimeofday(&end, NULL);
|
||||
timersub(&end, &start, &runtime);
|
||||
|
||||
update_stats(&wakeup_stats, nwoken);
|
||||
update_stats(&waketime_stats, runtime.tv_usec);
|
||||
|
||||
if (!silent) {
|
||||
printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
|
||||
j + 1, nwoken, nthreads, runtime.tv_usec/1e3);
|
||||
}
|
||||
|
||||
for (i = 0; i < nthreads; i++) {
|
||||
ret = pthread_join(worker[i], NULL);
|
||||
if (ret)
|
||||
err(EXIT_FAILURE, "pthread_join");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* cleanup & report results */
|
||||
pthread_cond_destroy(&thread_parent);
|
||||
pthread_cond_destroy(&thread_worker);
|
||||
pthread_mutex_destroy(&thread_lock);
|
||||
pthread_attr_destroy(&thread_attr);
|
||||
|
||||
print_summary();
|
||||
|
||||
free(worker);
|
||||
return ret;
|
||||
}
|
||||
71
tools/perf/bench/futex.h
Normal file
71
tools/perf/bench/futex.h
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Glibc independent futex library for testing kernel functionality.
|
||||
* Shamelessly stolen from Darren Hart <dvhltc@us.ibm.com>
|
||||
* http://git.kernel.org/cgit/linux/kernel/git/dvhart/futextest.git/
|
||||
*/
|
||||
|
||||
#ifndef _FUTEX_H
|
||||
#define _FUTEX_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <linux/futex.h>
|
||||
|
||||
/**
|
||||
* futex() - SYS_futex syscall wrapper
|
||||
* @uaddr: address of first futex
|
||||
* @op: futex op code
|
||||
* @val: typically expected value of uaddr, but varies by op
|
||||
* @timeout: typically an absolute struct timespec (except where noted
|
||||
* otherwise). Overloaded by some ops
|
||||
* @uaddr2: address of second futex for some ops\
|
||||
* @val3: varies by op
|
||||
* @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
|
||||
*
|
||||
* futex() is used by all the following futex op wrappers. It can also be
|
||||
* used for misuse and abuse testing. Generally, the specific op wrappers
|
||||
* should be used instead. It is a macro instead of an static inline function as
|
||||
* some of the types over overloaded (timeout is used for nr_requeue for
|
||||
* example).
|
||||
*
|
||||
* These argument descriptions are the defaults for all
|
||||
* like-named arguments in the following wrappers except where noted below.
|
||||
*/
|
||||
#define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
|
||||
syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)
|
||||
|
||||
/**
|
||||
* futex_wait() - block on uaddr with optional timeout
|
||||
* @timeout: relative timeout
|
||||
*/
|
||||
static inline int
|
||||
futex_wait(u_int32_t *uaddr, u_int32_t val, struct timespec *timeout, int opflags)
|
||||
{
|
||||
return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);
|
||||
}
|
||||
|
||||
/**
|
||||
* futex_wake() - wake one or more tasks blocked on uaddr
|
||||
* @nr_wake: wake up to this many tasks
|
||||
*/
|
||||
static inline int
|
||||
futex_wake(u_int32_t *uaddr, int nr_wake, int opflags)
|
||||
{
|
||||
return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
|
||||
}
|
||||
|
||||
/**
|
||||
* futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
|
||||
* @nr_wake: wake up to this many tasks
|
||||
* @nr_requeue: requeue up to this many tasks
|
||||
*/
|
||||
static inline int
|
||||
futex_cmp_requeue(u_int32_t *uaddr, u_int32_t val, u_int32_t *uaddr2, int nr_wake,
|
||||
int nr_requeue, int opflags)
|
||||
{
|
||||
return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,
|
||||
val, opflags);
|
||||
}
|
||||
|
||||
#endif /* _FUTEX_H */
|
||||
12
tools/perf/bench/mem-memcpy-arch.h
Normal file
12
tools/perf/bench/mem-memcpy-arch.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
#ifdef HAVE_ARCH_X86_64_SUPPORT
|
||||
|
||||
#define MEMCPY_FN(fn, name, desc) \
|
||||
extern void *fn(void *, const void *, size_t);
|
||||
|
||||
#include "mem-memcpy-x86-64-asm-def.h"
|
||||
|
||||
#undef MEMCPY_FN
|
||||
|
||||
#endif
|
||||
|
||||
12
tools/perf/bench/mem-memcpy-x86-64-asm-def.h
Normal file
12
tools/perf/bench/mem-memcpy-x86-64-asm-def.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
MEMCPY_FN(__memcpy,
|
||||
"x86-64-unrolled",
|
||||
"unrolled memcpy() in arch/x86/lib/memcpy_64.S")
|
||||
|
||||
MEMCPY_FN(memcpy_c,
|
||||
"x86-64-movsq",
|
||||
"movsq-based memcpy() in arch/x86/lib/memcpy_64.S")
|
||||
|
||||
MEMCPY_FN(memcpy_c_e,
|
||||
"x86-64-movsb",
|
||||
"movsb-based memcpy() in arch/x86/lib/memcpy_64.S")
|
||||
12
tools/perf/bench/mem-memcpy-x86-64-asm.S
Normal file
12
tools/perf/bench/mem-memcpy-x86-64-asm.S
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#define memcpy MEMCPY /* don't hide glibc's memcpy() */
|
||||
#define altinstr_replacement text
|
||||
#define globl p2align 4; .globl
|
||||
#define Lmemcpy_c globl memcpy_c; memcpy_c
|
||||
#define Lmemcpy_c_e globl memcpy_c_e; memcpy_c_e
|
||||
#include "../../../arch/x86/lib/memcpy_64.S"
|
||||
/*
|
||||
* We need to provide note.GNU-stack section, saying that we want
|
||||
* NOT executable stack. Otherwise the final linking will assume that
|
||||
* the ELF stack should not be restricted at all and set it RWX.
|
||||
*/
|
||||
.section .note.GNU-stack,"",@progbits
|
||||
312
tools/perf/bench/mem-memcpy.c
Normal file
312
tools/perf/bench/mem-memcpy.c
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
/*
|
||||
* mem-memcpy.c
|
||||
*
|
||||
* memcpy: Simple memory copy in various ways
|
||||
*
|
||||
* Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
|
||||
*/
|
||||
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../util/header.h"
|
||||
#include "../util/cloexec.h"
|
||||
#include "bench.h"
|
||||
#include "mem-memcpy-arch.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define K 1024
|
||||
|
||||
static const char *length_str = "1MB";
|
||||
static const char *routine = "default";
|
||||
static int iterations = 1;
|
||||
static bool use_cycle;
|
||||
static int cycle_fd;
|
||||
static bool only_prefault;
|
||||
static bool no_prefault;
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_STRING('l', "length", &length_str, "1MB",
|
||||
"Specify length of memory to copy. "
|
||||
"Available units: B, KB, MB, GB and TB (upper and lower)"),
|
||||
OPT_STRING('r', "routine", &routine, "default",
|
||||
"Specify routine to copy"),
|
||||
OPT_INTEGER('i', "iterations", &iterations,
|
||||
"repeat memcpy() invocation this number of times"),
|
||||
OPT_BOOLEAN('c', "cycle", &use_cycle,
|
||||
"Use cycles event instead of gettimeofday() for measuring"),
|
||||
OPT_BOOLEAN('o', "only-prefault", &only_prefault,
|
||||
"Show only the result with page faults before memcpy()"),
|
||||
OPT_BOOLEAN('n', "no-prefault", &no_prefault,
|
||||
"Show only the result without page faults before memcpy()"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
typedef void *(*memcpy_t)(void *, const void *, size_t);
|
||||
|
||||
struct routine {
|
||||
const char *name;
|
||||
const char *desc;
|
||||
memcpy_t fn;
|
||||
};
|
||||
|
||||
struct routine routines[] = {
|
||||
{ "default",
|
||||
"Default memcpy() provided by glibc",
|
||||
memcpy },
|
||||
#ifdef HAVE_ARCH_X86_64_SUPPORT
|
||||
|
||||
#define MEMCPY_FN(fn, name, desc) { name, desc, fn },
|
||||
#include "mem-memcpy-x86-64-asm-def.h"
|
||||
#undef MEMCPY_FN
|
||||
|
||||
#endif
|
||||
|
||||
{ NULL,
|
||||
NULL,
|
||||
NULL }
|
||||
};
|
||||
|
||||
static const char * const bench_mem_memcpy_usage[] = {
|
||||
"perf bench mem memcpy <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
static struct perf_event_attr cycle_attr = {
|
||||
.type = PERF_TYPE_HARDWARE,
|
||||
.config = PERF_COUNT_HW_CPU_CYCLES
|
||||
};
|
||||
|
||||
static void init_cycle(void)
|
||||
{
|
||||
cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1,
|
||||
perf_event_open_cloexec_flag());
|
||||
|
||||
if (cycle_fd < 0 && errno == ENOSYS)
|
||||
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
|
||||
else
|
||||
BUG_ON(cycle_fd < 0);
|
||||
}
|
||||
|
||||
static u64 get_cycle(void)
|
||||
{
|
||||
int ret;
|
||||
u64 clk;
|
||||
|
||||
ret = read(cycle_fd, &clk, sizeof(u64));
|
||||
BUG_ON(ret != sizeof(u64));
|
||||
|
||||
return clk;
|
||||
}
|
||||
|
||||
static double timeval2double(struct timeval *ts)
|
||||
{
|
||||
return (double)ts->tv_sec +
|
||||
(double)ts->tv_usec / (double)1000000;
|
||||
}
|
||||
|
||||
static void alloc_mem(void **dst, void **src, size_t length)
|
||||
{
|
||||
*dst = zalloc(length);
|
||||
if (!*dst)
|
||||
die("memory allocation failed - maybe length is too large?\n");
|
||||
|
||||
*src = zalloc(length);
|
||||
if (!*src)
|
||||
die("memory allocation failed - maybe length is too large?\n");
|
||||
/* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */
|
||||
memset(*src, 0, length);
|
||||
}
|
||||
|
||||
static u64 do_memcpy_cycle(memcpy_t fn, size_t len, bool prefault)
|
||||
{
|
||||
u64 cycle_start = 0ULL, cycle_end = 0ULL;
|
||||
void *src = NULL, *dst = NULL;
|
||||
int i;
|
||||
|
||||
alloc_mem(&src, &dst, len);
|
||||
|
||||
if (prefault)
|
||||
fn(dst, src, len);
|
||||
|
||||
cycle_start = get_cycle();
|
||||
for (i = 0; i < iterations; ++i)
|
||||
fn(dst, src, len);
|
||||
cycle_end = get_cycle();
|
||||
|
||||
free(src);
|
||||
free(dst);
|
||||
return cycle_end - cycle_start;
|
||||
}
|
||||
|
||||
static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
|
||||
{
|
||||
struct timeval tv_start, tv_end, tv_diff;
|
||||
void *src = NULL, *dst = NULL;
|
||||
int i;
|
||||
|
||||
alloc_mem(&src, &dst, len);
|
||||
|
||||
if (prefault)
|
||||
fn(dst, src, len);
|
||||
|
||||
BUG_ON(gettimeofday(&tv_start, NULL));
|
||||
for (i = 0; i < iterations; ++i)
|
||||
fn(dst, src, len);
|
||||
BUG_ON(gettimeofday(&tv_end, NULL));
|
||||
|
||||
timersub(&tv_end, &tv_start, &tv_diff);
|
||||
|
||||
free(src);
|
||||
free(dst);
|
||||
return (double)((double)len / timeval2double(&tv_diff));
|
||||
}
|
||||
|
||||
#define pf (no_prefault ? 0 : 1)
|
||||
|
||||
#define print_bps(x) do { \
|
||||
if (x < K) \
|
||||
printf(" %14lf B/Sec", x); \
|
||||
else if (x < K * K) \
|
||||
printf(" %14lfd KB/Sec", x / K); \
|
||||
else if (x < K * K * K) \
|
||||
printf(" %14lf MB/Sec", x / K / K); \
|
||||
else \
|
||||
printf(" %14lf GB/Sec", x / K / K / K); \
|
||||
} while (0)
|
||||
|
||||
int bench_mem_memcpy(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused)
|
||||
{
|
||||
int i;
|
||||
size_t len;
|
||||
double result_bps[2];
|
||||
u64 result_cycle[2];
|
||||
|
||||
argc = parse_options(argc, argv, options,
|
||||
bench_mem_memcpy_usage, 0);
|
||||
|
||||
if (no_prefault && only_prefault) {
|
||||
fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (use_cycle)
|
||||
init_cycle();
|
||||
|
||||
len = (size_t)perf_atoll((char *)length_str);
|
||||
|
||||
result_cycle[0] = result_cycle[1] = 0ULL;
|
||||
result_bps[0] = result_bps[1] = 0.0;
|
||||
|
||||
if ((s64)len <= 0) {
|
||||
fprintf(stderr, "Invalid length:%s\n", length_str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* same to without specifying either of prefault and no-prefault */
|
||||
if (only_prefault && no_prefault)
|
||||
only_prefault = no_prefault = false;
|
||||
|
||||
for (i = 0; routines[i].name; i++) {
|
||||
if (!strcmp(routines[i].name, routine))
|
||||
break;
|
||||
}
|
||||
if (!routines[i].name) {
|
||||
printf("Unknown routine:%s\n", routine);
|
||||
printf("Available routines...\n");
|
||||
for (i = 0; routines[i].name; i++) {
|
||||
printf("\t%s ... %s\n",
|
||||
routines[i].name, routines[i].desc);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (bench_format == BENCH_FORMAT_DEFAULT)
|
||||
printf("# Copying %s Bytes ...\n\n", length_str);
|
||||
|
||||
if (!only_prefault && !no_prefault) {
|
||||
/* show both of results */
|
||||
if (use_cycle) {
|
||||
result_cycle[0] =
|
||||
do_memcpy_cycle(routines[i].fn, len, false);
|
||||
result_cycle[1] =
|
||||
do_memcpy_cycle(routines[i].fn, len, true);
|
||||
} else {
|
||||
result_bps[0] =
|
||||
do_memcpy_gettimeofday(routines[i].fn,
|
||||
len, false);
|
||||
result_bps[1] =
|
||||
do_memcpy_gettimeofday(routines[i].fn,
|
||||
len, true);
|
||||
}
|
||||
} else {
|
||||
if (use_cycle) {
|
||||
result_cycle[pf] =
|
||||
do_memcpy_cycle(routines[i].fn,
|
||||
len, only_prefault);
|
||||
} else {
|
||||
result_bps[pf] =
|
||||
do_memcpy_gettimeofday(routines[i].fn,
|
||||
len, only_prefault);
|
||||
}
|
||||
}
|
||||
|
||||
switch (bench_format) {
|
||||
case BENCH_FORMAT_DEFAULT:
|
||||
if (!only_prefault && !no_prefault) {
|
||||
if (use_cycle) {
|
||||
printf(" %14lf Cycle/Byte\n",
|
||||
(double)result_cycle[0]
|
||||
/ (double)len);
|
||||
printf(" %14lf Cycle/Byte (with prefault)\n",
|
||||
(double)result_cycle[1]
|
||||
/ (double)len);
|
||||
} else {
|
||||
print_bps(result_bps[0]);
|
||||
printf("\n");
|
||||
print_bps(result_bps[1]);
|
||||
printf(" (with prefault)\n");
|
||||
}
|
||||
} else {
|
||||
if (use_cycle) {
|
||||
printf(" %14lf Cycle/Byte",
|
||||
(double)result_cycle[pf]
|
||||
/ (double)len);
|
||||
} else
|
||||
print_bps(result_bps[pf]);
|
||||
|
||||
printf("%s\n", only_prefault ? " (with prefault)" : "");
|
||||
}
|
||||
break;
|
||||
case BENCH_FORMAT_SIMPLE:
|
||||
if (!only_prefault && !no_prefault) {
|
||||
if (use_cycle) {
|
||||
printf("%lf %lf\n",
|
||||
(double)result_cycle[0] / (double)len,
|
||||
(double)result_cycle[1] / (double)len);
|
||||
} else {
|
||||
printf("%lf %lf\n",
|
||||
result_bps[0], result_bps[1]);
|
||||
}
|
||||
} else {
|
||||
if (use_cycle) {
|
||||
printf("%lf\n", (double)result_cycle[pf]
|
||||
/ (double)len);
|
||||
} else
|
||||
printf("%lf\n", result_bps[pf]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* reaching this means there's some disaster: */
|
||||
die("unknown format: %d\n", bench_format);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
12
tools/perf/bench/mem-memset-arch.h
Normal file
12
tools/perf/bench/mem-memset-arch.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
#ifdef HAVE_ARCH_X86_64_SUPPORT
|
||||
|
||||
#define MEMSET_FN(fn, name, desc) \
|
||||
extern void *fn(void *, int, size_t);
|
||||
|
||||
#include "mem-memset-x86-64-asm-def.h"
|
||||
|
||||
#undef MEMSET_FN
|
||||
|
||||
#endif
|
||||
|
||||
12
tools/perf/bench/mem-memset-x86-64-asm-def.h
Normal file
12
tools/perf/bench/mem-memset-x86-64-asm-def.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
MEMSET_FN(__memset,
|
||||
"x86-64-unrolled",
|
||||
"unrolled memset() in arch/x86/lib/memset_64.S")
|
||||
|
||||
MEMSET_FN(memset_c,
|
||||
"x86-64-stosq",
|
||||
"movsq-based memset() in arch/x86/lib/memset_64.S")
|
||||
|
||||
MEMSET_FN(memset_c_e,
|
||||
"x86-64-stosb",
|
||||
"movsb-based memset() in arch/x86/lib/memset_64.S")
|
||||
13
tools/perf/bench/mem-memset-x86-64-asm.S
Normal file
13
tools/perf/bench/mem-memset-x86-64-asm.S
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#define memset MEMSET /* don't hide glibc's memset() */
|
||||
#define altinstr_replacement text
|
||||
#define globl p2align 4; .globl
|
||||
#define Lmemset_c globl memset_c; memset_c
|
||||
#define Lmemset_c_e globl memset_c_e; memset_c_e
|
||||
#include "../../../arch/x86/lib/memset_64.S"
|
||||
|
||||
/*
|
||||
* We need to provide note.GNU-stack section, saying that we want
|
||||
* NOT executable stack. Otherwise the final linking will assume that
|
||||
* the ELF stack should not be restricted at all and set it RWX.
|
||||
*/
|
||||
.section .note.GNU-stack,"",@progbits
|
||||
304
tools/perf/bench/mem-memset.c
Normal file
304
tools/perf/bench/mem-memset.c
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
/*
|
||||
* mem-memset.c
|
||||
*
|
||||
* memset: Simple memory set in various ways
|
||||
*
|
||||
* Trivial clone of mem-memcpy.c.
|
||||
*/
|
||||
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../util/header.h"
|
||||
#include "../util/cloexec.h"
|
||||
#include "bench.h"
|
||||
#include "mem-memset-arch.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define K 1024
|
||||
|
||||
static const char *length_str = "1MB";
|
||||
static const char *routine = "default";
|
||||
static int iterations = 1;
|
||||
static bool use_cycle;
|
||||
static int cycle_fd;
|
||||
static bool only_prefault;
|
||||
static bool no_prefault;
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_STRING('l', "length", &length_str, "1MB",
|
||||
"Specify length of memory to set. "
|
||||
"Available units: B, KB, MB, GB and TB (upper and lower)"),
|
||||
OPT_STRING('r', "routine", &routine, "default",
|
||||
"Specify routine to set"),
|
||||
OPT_INTEGER('i', "iterations", &iterations,
|
||||
"repeat memset() invocation this number of times"),
|
||||
OPT_BOOLEAN('c', "cycle", &use_cycle,
|
||||
"Use cycles event instead of gettimeofday() for measuring"),
|
||||
OPT_BOOLEAN('o', "only-prefault", &only_prefault,
|
||||
"Show only the result with page faults before memset()"),
|
||||
OPT_BOOLEAN('n', "no-prefault", &no_prefault,
|
||||
"Show only the result without page faults before memset()"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
typedef void *(*memset_t)(void *, int, size_t);
|
||||
|
||||
struct routine {
|
||||
const char *name;
|
||||
const char *desc;
|
||||
memset_t fn;
|
||||
};
|
||||
|
||||
static const struct routine routines[] = {
|
||||
{ "default",
|
||||
"Default memset() provided by glibc",
|
||||
memset },
|
||||
#ifdef HAVE_ARCH_X86_64_SUPPORT
|
||||
|
||||
#define MEMSET_FN(fn, name, desc) { name, desc, fn },
|
||||
#include "mem-memset-x86-64-asm-def.h"
|
||||
#undef MEMSET_FN
|
||||
|
||||
#endif
|
||||
|
||||
{ NULL,
|
||||
NULL,
|
||||
NULL }
|
||||
};
|
||||
|
||||
static const char * const bench_mem_memset_usage[] = {
|
||||
"perf bench mem memset <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
static struct perf_event_attr cycle_attr = {
|
||||
.type = PERF_TYPE_HARDWARE,
|
||||
.config = PERF_COUNT_HW_CPU_CYCLES
|
||||
};
|
||||
|
||||
static void init_cycle(void)
|
||||
{
|
||||
cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1,
|
||||
perf_event_open_cloexec_flag());
|
||||
|
||||
if (cycle_fd < 0 && errno == ENOSYS)
|
||||
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
|
||||
else
|
||||
BUG_ON(cycle_fd < 0);
|
||||
}
|
||||
|
||||
static u64 get_cycle(void)
|
||||
{
|
||||
int ret;
|
||||
u64 clk;
|
||||
|
||||
ret = read(cycle_fd, &clk, sizeof(u64));
|
||||
BUG_ON(ret != sizeof(u64));
|
||||
|
||||
return clk;
|
||||
}
|
||||
|
||||
static double timeval2double(struct timeval *ts)
|
||||
{
|
||||
return (double)ts->tv_sec +
|
||||
(double)ts->tv_usec / (double)1000000;
|
||||
}
|
||||
|
||||
static void alloc_mem(void **dst, size_t length)
|
||||
{
|
||||
*dst = zalloc(length);
|
||||
if (!*dst)
|
||||
die("memory allocation failed - maybe length is too large?\n");
|
||||
}
|
||||
|
||||
static u64 do_memset_cycle(memset_t fn, size_t len, bool prefault)
|
||||
{
|
||||
u64 cycle_start = 0ULL, cycle_end = 0ULL;
|
||||
void *dst = NULL;
|
||||
int i;
|
||||
|
||||
alloc_mem(&dst, len);
|
||||
|
||||
if (prefault)
|
||||
fn(dst, -1, len);
|
||||
|
||||
cycle_start = get_cycle();
|
||||
for (i = 0; i < iterations; ++i)
|
||||
fn(dst, i, len);
|
||||
cycle_end = get_cycle();
|
||||
|
||||
free(dst);
|
||||
return cycle_end - cycle_start;
|
||||
}
|
||||
|
||||
static double do_memset_gettimeofday(memset_t fn, size_t len, bool prefault)
|
||||
{
|
||||
struct timeval tv_start, tv_end, tv_diff;
|
||||
void *dst = NULL;
|
||||
int i;
|
||||
|
||||
alloc_mem(&dst, len);
|
||||
|
||||
if (prefault)
|
||||
fn(dst, -1, len);
|
||||
|
||||
BUG_ON(gettimeofday(&tv_start, NULL));
|
||||
for (i = 0; i < iterations; ++i)
|
||||
fn(dst, i, len);
|
||||
BUG_ON(gettimeofday(&tv_end, NULL));
|
||||
|
||||
timersub(&tv_end, &tv_start, &tv_diff);
|
||||
|
||||
free(dst);
|
||||
return (double)((double)len / timeval2double(&tv_diff));
|
||||
}
|
||||
|
||||
#define pf (no_prefault ? 0 : 1)
|
||||
|
||||
#define print_bps(x) do { \
|
||||
if (x < K) \
|
||||
printf(" %14lf B/Sec", x); \
|
||||
else if (x < K * K) \
|
||||
printf(" %14lfd KB/Sec", x / K); \
|
||||
else if (x < K * K * K) \
|
||||
printf(" %14lf MB/Sec", x / K / K); \
|
||||
else \
|
||||
printf(" %14lf GB/Sec", x / K / K / K); \
|
||||
} while (0)
|
||||
|
||||
int bench_mem_memset(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused)
|
||||
{
|
||||
int i;
|
||||
size_t len;
|
||||
double result_bps[2];
|
||||
u64 result_cycle[2];
|
||||
|
||||
argc = parse_options(argc, argv, options,
|
||||
bench_mem_memset_usage, 0);
|
||||
|
||||
if (no_prefault && only_prefault) {
|
||||
fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (use_cycle)
|
||||
init_cycle();
|
||||
|
||||
len = (size_t)perf_atoll((char *)length_str);
|
||||
|
||||
result_cycle[0] = result_cycle[1] = 0ULL;
|
||||
result_bps[0] = result_bps[1] = 0.0;
|
||||
|
||||
if ((s64)len <= 0) {
|
||||
fprintf(stderr, "Invalid length:%s\n", length_str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* same to without specifying either of prefault and no-prefault */
|
||||
if (only_prefault && no_prefault)
|
||||
only_prefault = no_prefault = false;
|
||||
|
||||
for (i = 0; routines[i].name; i++) {
|
||||
if (!strcmp(routines[i].name, routine))
|
||||
break;
|
||||
}
|
||||
if (!routines[i].name) {
|
||||
printf("Unknown routine:%s\n", routine);
|
||||
printf("Available routines...\n");
|
||||
for (i = 0; routines[i].name; i++) {
|
||||
printf("\t%s ... %s\n",
|
||||
routines[i].name, routines[i].desc);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (bench_format == BENCH_FORMAT_DEFAULT)
|
||||
printf("# Copying %s Bytes ...\n\n", length_str);
|
||||
|
||||
if (!only_prefault && !no_prefault) {
|
||||
/* show both of results */
|
||||
if (use_cycle) {
|
||||
result_cycle[0] =
|
||||
do_memset_cycle(routines[i].fn, len, false);
|
||||
result_cycle[1] =
|
||||
do_memset_cycle(routines[i].fn, len, true);
|
||||
} else {
|
||||
result_bps[0] =
|
||||
do_memset_gettimeofday(routines[i].fn,
|
||||
len, false);
|
||||
result_bps[1] =
|
||||
do_memset_gettimeofday(routines[i].fn,
|
||||
len, true);
|
||||
}
|
||||
} else {
|
||||
if (use_cycle) {
|
||||
result_cycle[pf] =
|
||||
do_memset_cycle(routines[i].fn,
|
||||
len, only_prefault);
|
||||
} else {
|
||||
result_bps[pf] =
|
||||
do_memset_gettimeofday(routines[i].fn,
|
||||
len, only_prefault);
|
||||
}
|
||||
}
|
||||
|
||||
switch (bench_format) {
|
||||
case BENCH_FORMAT_DEFAULT:
|
||||
if (!only_prefault && !no_prefault) {
|
||||
if (use_cycle) {
|
||||
printf(" %14lf Cycle/Byte\n",
|
||||
(double)result_cycle[0]
|
||||
/ (double)len);
|
||||
printf(" %14lf Cycle/Byte (with prefault)\n ",
|
||||
(double)result_cycle[1]
|
||||
/ (double)len);
|
||||
} else {
|
||||
print_bps(result_bps[0]);
|
||||
printf("\n");
|
||||
print_bps(result_bps[1]);
|
||||
printf(" (with prefault)\n");
|
||||
}
|
||||
} else {
|
||||
if (use_cycle) {
|
||||
printf(" %14lf Cycle/Byte",
|
||||
(double)result_cycle[pf]
|
||||
/ (double)len);
|
||||
} else
|
||||
print_bps(result_bps[pf]);
|
||||
|
||||
printf("%s\n", only_prefault ? " (with prefault)" : "");
|
||||
}
|
||||
break;
|
||||
case BENCH_FORMAT_SIMPLE:
|
||||
if (!only_prefault && !no_prefault) {
|
||||
if (use_cycle) {
|
||||
printf("%lf %lf\n",
|
||||
(double)result_cycle[0] / (double)len,
|
||||
(double)result_cycle[1] / (double)len);
|
||||
} else {
|
||||
printf("%lf %lf\n",
|
||||
result_bps[0], result_bps[1]);
|
||||
}
|
||||
} else {
|
||||
if (use_cycle) {
|
||||
printf("%lf\n", (double)result_cycle[pf]
|
||||
/ (double)len);
|
||||
} else
|
||||
printf("%lf\n", result_bps[pf]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* reaching this means there's some disaster: */
|
||||
die("unknown format: %d\n", bench_format);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
1744
tools/perf/bench/numa.c
Normal file
1744
tools/perf/bench/numa.c
Normal file
File diff suppressed because it is too large
Load diff
331
tools/perf/bench/sched-messaging.c
Normal file
331
tools/perf/bench/sched-messaging.c
Normal file
|
|
@ -0,0 +1,331 @@
|
|||
/*
|
||||
*
|
||||
* sched-messaging.c
|
||||
*
|
||||
* messaging: Benchmark for scheduler and IPC mechanisms
|
||||
*
|
||||
* Based on hackbench by Rusty Russell <rusty@rustcorp.com.au>
|
||||
* Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../builtin.h"
|
||||
#include "bench.h"
|
||||
|
||||
/* Test groups of 20 processes spraying to 20 receivers */
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/time.h>
|
||||
#include <poll.h>
|
||||
#include <limits.h>
|
||||
#include <err.h>
|
||||
|
||||
#define DATASIZE 100
|
||||
|
||||
static bool use_pipes = false;
|
||||
static unsigned int loops = 100;
|
||||
static bool thread_mode = false;
|
||||
static unsigned int num_groups = 10;
|
||||
|
||||
struct sender_context {
|
||||
unsigned int num_fds;
|
||||
int ready_out;
|
||||
int wakefd;
|
||||
int out_fds[0];
|
||||
};
|
||||
|
||||
struct receiver_context {
|
||||
unsigned int num_packets;
|
||||
int in_fds[2];
|
||||
int ready_out;
|
||||
int wakefd;
|
||||
};
|
||||
|
||||
static void fdpair(int fds[2])
|
||||
{
|
||||
if (use_pipes) {
|
||||
if (pipe(fds) == 0)
|
||||
return;
|
||||
} else {
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
|
||||
return;
|
||||
}
|
||||
|
||||
err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
|
||||
}
|
||||
|
||||
/* Block until we're ready to go */
|
||||
static void ready(int ready_out, int wakefd)
|
||||
{
|
||||
char dummy;
|
||||
struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
|
||||
|
||||
/* Tell them we're ready. */
|
||||
if (write(ready_out, &dummy, 1) != 1)
|
||||
err(EXIT_FAILURE, "CLIENT: ready write");
|
||||
|
||||
/* Wait for "GO" signal */
|
||||
if (poll(&pollfd, 1, -1) != 1)
|
||||
err(EXIT_FAILURE, "poll");
|
||||
}
|
||||
|
||||
/* Sender sprays loops messages down each file descriptor */
|
||||
static void *sender(struct sender_context *ctx)
|
||||
{
|
||||
char data[DATASIZE];
|
||||
unsigned int i, j;
|
||||
|
||||
ready(ctx->ready_out, ctx->wakefd);
|
||||
|
||||
/* Now pump to every receiver. */
|
||||
for (i = 0; i < loops; i++) {
|
||||
for (j = 0; j < ctx->num_fds; j++) {
|
||||
int ret, done = 0;
|
||||
|
||||
again:
|
||||
ret = write(ctx->out_fds[j], data + done,
|
||||
sizeof(data)-done);
|
||||
if (ret < 0)
|
||||
err(EXIT_FAILURE, "SENDER: write");
|
||||
done += ret;
|
||||
if (done < DATASIZE)
|
||||
goto again;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* One receiver per fd */
|
||||
static void *receiver(struct receiver_context* ctx)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (!thread_mode)
|
||||
close(ctx->in_fds[1]);
|
||||
|
||||
/* Wait for start... */
|
||||
ready(ctx->ready_out, ctx->wakefd);
|
||||
|
||||
/* Receive them all */
|
||||
for (i = 0; i < ctx->num_packets; i++) {
|
||||
char data[DATASIZE];
|
||||
int ret, done = 0;
|
||||
|
||||
again:
|
||||
ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
|
||||
if (ret < 0)
|
||||
err(EXIT_FAILURE, "SERVER: read");
|
||||
done += ret;
|
||||
if (done < DATASIZE)
|
||||
goto again;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static pthread_t create_worker(void *ctx, void *(*func)(void *))
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
pthread_t childid;
|
||||
int ret;
|
||||
|
||||
if (!thread_mode) {
|
||||
/* process mode */
|
||||
/* Fork the receiver. */
|
||||
switch (fork()) {
|
||||
case -1:
|
||||
err(EXIT_FAILURE, "fork()");
|
||||
break;
|
||||
case 0:
|
||||
(*func) (ctx);
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return (pthread_t)0;
|
||||
}
|
||||
|
||||
if (pthread_attr_init(&attr) != 0)
|
||||
err(EXIT_FAILURE, "pthread_attr_init:");
|
||||
|
||||
#ifndef __ia64__
|
||||
if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
|
||||
err(EXIT_FAILURE, "pthread_attr_setstacksize");
|
||||
#endif
|
||||
|
||||
ret = pthread_create(&childid, &attr, func, ctx);
|
||||
if (ret != 0)
|
||||
err(EXIT_FAILURE, "pthread_create failed");
|
||||
|
||||
return childid;
|
||||
}
|
||||
|
||||
static void reap_worker(pthread_t id)
|
||||
{
|
||||
int proc_status;
|
||||
void *thread_status;
|
||||
|
||||
if (!thread_mode) {
|
||||
/* process mode */
|
||||
wait(&proc_status);
|
||||
if (!WIFEXITED(proc_status))
|
||||
exit(1);
|
||||
} else {
|
||||
pthread_join(id, &thread_status);
|
||||
}
|
||||
}
|
||||
|
||||
/* One group of senders and receivers */
|
||||
static unsigned int group(pthread_t *pth,
|
||||
unsigned int num_fds,
|
||||
int ready_out,
|
||||
int wakefd)
|
||||
{
|
||||
unsigned int i;
|
||||
struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
|
||||
+ num_fds * sizeof(int));
|
||||
|
||||
if (!snd_ctx)
|
||||
err(EXIT_FAILURE, "malloc()");
|
||||
|
||||
for (i = 0; i < num_fds; i++) {
|
||||
int fds[2];
|
||||
struct receiver_context *ctx = malloc(sizeof(*ctx));
|
||||
|
||||
if (!ctx)
|
||||
err(EXIT_FAILURE, "malloc()");
|
||||
|
||||
|
||||
/* Create the pipe between client and server */
|
||||
fdpair(fds);
|
||||
|
||||
ctx->num_packets = num_fds * loops;
|
||||
ctx->in_fds[0] = fds[0];
|
||||
ctx->in_fds[1] = fds[1];
|
||||
ctx->ready_out = ready_out;
|
||||
ctx->wakefd = wakefd;
|
||||
|
||||
pth[i] = create_worker(ctx, (void *)receiver);
|
||||
|
||||
snd_ctx->out_fds[i] = fds[1];
|
||||
if (!thread_mode)
|
||||
close(fds[0]);
|
||||
}
|
||||
|
||||
/* Now we have all the fds, fork the senders */
|
||||
for (i = 0; i < num_fds; i++) {
|
||||
snd_ctx->ready_out = ready_out;
|
||||
snd_ctx->wakefd = wakefd;
|
||||
snd_ctx->num_fds = num_fds;
|
||||
|
||||
pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
|
||||
}
|
||||
|
||||
/* Close the fds we have left */
|
||||
if (!thread_mode)
|
||||
for (i = 0; i < num_fds; i++)
|
||||
close(snd_ctx->out_fds[i]);
|
||||
|
||||
/* Return number of children to reap */
|
||||
return num_fds * 2;
|
||||
}
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_BOOLEAN('p', "pipe", &use_pipes,
|
||||
"Use pipe() instead of socketpair()"),
|
||||
OPT_BOOLEAN('t', "thread", &thread_mode,
|
||||
"Be multi thread instead of multi process"),
|
||||
OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
|
||||
OPT_UINTEGER('l', "loop", &loops, "Specify number of loops"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
static const char * const bench_sched_message_usage[] = {
|
||||
"perf bench sched messaging <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
int bench_sched_messaging(int argc, const char **argv,
|
||||
const char *prefix __maybe_unused)
|
||||
{
|
||||
unsigned int i, total_children;
|
||||
struct timeval start, stop, diff;
|
||||
unsigned int num_fds = 20;
|
||||
int readyfds[2], wakefds[2];
|
||||
char dummy;
|
||||
pthread_t *pth_tab;
|
||||
|
||||
argc = parse_options(argc, argv, options,
|
||||
bench_sched_message_usage, 0);
|
||||
|
||||
pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t));
|
||||
if (!pth_tab)
|
||||
err(EXIT_FAILURE, "main:malloc()");
|
||||
|
||||
fdpair(readyfds);
|
||||
fdpair(wakefds);
|
||||
|
||||
total_children = 0;
|
||||
for (i = 0; i < num_groups; i++)
|
||||
total_children += group(pth_tab+total_children, num_fds,
|
||||
readyfds[1], wakefds[0]);
|
||||
|
||||
/* Wait for everyone to be ready */
|
||||
for (i = 0; i < total_children; i++)
|
||||
if (read(readyfds[0], &dummy, 1) != 1)
|
||||
err(EXIT_FAILURE, "Reading for readyfds");
|
||||
|
||||
gettimeofday(&start, NULL);
|
||||
|
||||
/* Kick them off */
|
||||
if (write(wakefds[1], &dummy, 1) != 1)
|
||||
err(EXIT_FAILURE, "Writing to start them");
|
||||
|
||||
/* Reap them all */
|
||||
for (i = 0; i < total_children; i++)
|
||||
reap_worker(pth_tab[i]);
|
||||
|
||||
gettimeofday(&stop, NULL);
|
||||
|
||||
timersub(&stop, &start, &diff);
|
||||
|
||||
switch (bench_format) {
|
||||
case BENCH_FORMAT_DEFAULT:
|
||||
printf("# %d sender and receiver %s per group\n",
|
||||
num_fds, thread_mode ? "threads" : "processes");
|
||||
printf("# %d groups == %d %s run\n\n",
|
||||
num_groups, num_groups * 2 * num_fds,
|
||||
thread_mode ? "threads" : "processes");
|
||||
printf(" %14s: %lu.%03lu [sec]\n", "Total time",
|
||||
diff.tv_sec,
|
||||
(unsigned long) (diff.tv_usec/1000));
|
||||
break;
|
||||
case BENCH_FORMAT_SIMPLE:
|
||||
printf("%lu.%03lu\n", diff.tv_sec,
|
||||
(unsigned long) (diff.tv_usec/1000));
|
||||
break;
|
||||
default:
|
||||
/* reaching here is something disaster */
|
||||
fprintf(stderr, "Unknown format:%d\n", bench_format);
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
|
||||
free(pth_tab);
|
||||
|
||||
return 0;
|
||||
}
|
||||
184
tools/perf/bench/sched-pipe.c
Normal file
184
tools/perf/bench/sched-pipe.c
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
*
|
||||
* sched-pipe.c
|
||||
*
|
||||
* pipe: Benchmark for pipe()
|
||||
*
|
||||
* Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
|
||||
* http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
|
||||
* Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
|
||||
*/
|
||||
#include "../perf.h"
|
||||
#include "../util/util.h"
|
||||
#include "../util/parse-options.h"
|
||||
#include "../builtin.h"
|
||||
#include "bench.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <linux/unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
struct thread_data {
|
||||
int nr;
|
||||
int pipe_read;
|
||||
int pipe_write;
|
||||
pthread_t pthread;
|
||||
};
|
||||
|
||||
#define LOOPS_DEFAULT 1000000
|
||||
static int loops = LOOPS_DEFAULT;
|
||||
|
||||
/* Use processes by default: */
|
||||
static bool threaded;
|
||||
|
||||
static const struct option options[] = {
|
||||
OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
|
||||
OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
static const char * const bench_sched_pipe_usage[] = {
|
||||
"perf bench sched pipe <options>",
|
||||
NULL
|
||||
};
|
||||
|
||||
static void *worker_thread(void *__tdata)
|
||||
{
|
||||
struct thread_data *td = __tdata;
|
||||
int m = 0, i;
|
||||
int ret;
|
||||
|
||||
for (i = 0; i < loops; i++) {
|
||||
if (!td->nr) {
|
||||
ret = read(td->pipe_read, &m, sizeof(int));
|
||||
BUG_ON(ret != sizeof(int));
|
||||
ret = write(td->pipe_write, &m, sizeof(int));
|
||||
BUG_ON(ret != sizeof(int));
|
||||
} else {
|
||||
ret = write(td->pipe_write, &m, sizeof(int));
|
||||
BUG_ON(ret != sizeof(int));
|
||||
ret = read(td->pipe_read, &m, sizeof(int));
|
||||
BUG_ON(ret != sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused)
|
||||
{
|
||||
struct thread_data threads[2], *td;
|
||||
int pipe_1[2], pipe_2[2];
|
||||
struct timeval start, stop, diff;
|
||||
unsigned long long result_usec = 0;
|
||||
int nr_threads = 2;
|
||||
int t;
|
||||
|
||||
/*
|
||||
* why does "ret" exist?
|
||||
* discarding returned value of read(), write()
|
||||
* causes error in building environment for perf
|
||||
*/
|
||||
int __maybe_unused ret, wait_stat;
|
||||
pid_t pid, retpid __maybe_unused;
|
||||
|
||||
argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
|
||||
|
||||
BUG_ON(pipe(pipe_1));
|
||||
BUG_ON(pipe(pipe_2));
|
||||
|
||||
gettimeofday(&start, NULL);
|
||||
|
||||
for (t = 0; t < nr_threads; t++) {
|
||||
td = threads + t;
|
||||
|
||||
td->nr = t;
|
||||
|
||||
if (t == 0) {
|
||||
td->pipe_read = pipe_1[0];
|
||||
td->pipe_write = pipe_2[1];
|
||||
} else {
|
||||
td->pipe_write = pipe_1[1];
|
||||
td->pipe_read = pipe_2[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (threaded) {
|
||||
|
||||
for (t = 0; t < nr_threads; t++) {
|
||||
td = threads + t;
|
||||
|
||||
ret = pthread_create(&td->pthread, NULL, worker_thread, td);
|
||||
BUG_ON(ret);
|
||||
}
|
||||
|
||||
for (t = 0; t < nr_threads; t++) {
|
||||
td = threads + t;
|
||||
|
||||
ret = pthread_join(td->pthread, NULL);
|
||||
BUG_ON(ret);
|
||||
}
|
||||
|
||||
} else {
|
||||
pid = fork();
|
||||
assert(pid >= 0);
|
||||
|
||||
if (!pid) {
|
||||
worker_thread(threads + 0);
|
||||
exit(0);
|
||||
} else {
|
||||
worker_thread(threads + 1);
|
||||
}
|
||||
|
||||
retpid = waitpid(pid, &wait_stat, 0);
|
||||
assert((retpid == pid) && WIFEXITED(wait_stat));
|
||||
}
|
||||
|
||||
gettimeofday(&stop, NULL);
|
||||
timersub(&stop, &start, &diff);
|
||||
|
||||
switch (bench_format) {
|
||||
case BENCH_FORMAT_DEFAULT:
|
||||
printf("# Executed %d pipe operations between two %s\n\n",
|
||||
loops, threaded ? "threads" : "processes");
|
||||
|
||||
result_usec = diff.tv_sec * 1000000;
|
||||
result_usec += diff.tv_usec;
|
||||
|
||||
printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
|
||||
diff.tv_sec,
|
||||
(unsigned long) (diff.tv_usec/1000));
|
||||
|
||||
printf(" %14lf usecs/op\n",
|
||||
(double)result_usec / (double)loops);
|
||||
printf(" %14d ops/sec\n",
|
||||
(int)((double)loops /
|
||||
((double)result_usec / (double)1000000)));
|
||||
break;
|
||||
|
||||
case BENCH_FORMAT_SIMPLE:
|
||||
printf("%lu.%03lu\n",
|
||||
diff.tv_sec,
|
||||
(unsigned long) (diff.tv_usec / 1000));
|
||||
break;
|
||||
|
||||
default:
|
||||
/* reaching here is something disaster */
|
||||
fprintf(stderr, "Unknown format:%d\n", bench_format);
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue