1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Upgrade gperftools to 2.9 for GCP/GMC/GMP/GMD. (#2247)

This commit is contained in:
winlin 2021-12-12 15:38:30 +08:00
parent 63da0dca92
commit 44e9dc83e9
346 changed files with 169666 additions and 78 deletions

View file

@ -0,0 +1,108 @@
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
//
// Copied from
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=gpp&id=2
// and slightly modified (particularly by adding multi-threaded
// operation to hit malloc harder).
//
// This version of binary trees is mostly new/delete benchmark
//
// NOTE: copyright of this code is unclear, but we only distribute
// source.
/* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* Contributed by Jon Harrop
* Modified by Alex Mizrahi
* Adapted for gperftools and added threads by Aliaksei Kandratsenka
*/
#include <algorithm>
#include <errno.h>
#include <iostream>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
struct Node {
Node *l, *r;
int i;
Node(int i2) : l(0), r(0), i(i2) {}
Node(Node *l2, int i2, Node *r2) : l(l2), r(r2), i(i2) {}
~Node() { delete l; delete r; }
int check() const {
if (l) {
return l->check() + i - r->check();
} else {
return i;
}
}
};
Node *make(int i, int d) {
if (d == 0) return new Node(i);
return new Node(make(2*i-1, d-1), i, make(2*i, d-1));
}
void run(int given_depth) {
int min_depth = 4,
max_depth = std::max(min_depth+2,
given_depth),
stretch_depth = max_depth+1;
{
Node *c = make(0, stretch_depth);
std::cout << "stretch tree of depth " << stretch_depth << "\t "
<< "check: " << c->check() << std::endl;
delete c;
}
Node *long_lived_tree=make(0, max_depth);
for (int d=min_depth; d<=max_depth; d+=2) {
int iterations = 1 << (max_depth - d + min_depth), c=0;
for (int i=1; i<=iterations; ++i) {
Node *a = make(i, d), *b = make(-i, d);
c += a->check() + b->check();
delete a;
delete b;
}
std::cout << (2*iterations) << "\t trees of depth " << d << "\t "
<< "check: " << c << std::endl;
}
std::cout << "long lived tree of depth " << max_depth << "\t "
<< "check: " << (long_lived_tree->check()) << "\n";
delete long_lived_tree;
}
static void *run_tramp(void *_a) {
intptr_t a = reinterpret_cast<intptr_t>(_a);
run(a);
return 0;
}
int main(int argc, char *argv[]) {
int given_depth = argc >= 2 ? atoi(argv[1]) : 20;
int thread_count = std::max(1, argc >= 3 ? atoi(argv[2]) : 1) - 1;
std::vector<pthread_t> threads(thread_count);
for (int i = 0; i < thread_count; i++) {
int rv = pthread_create(&threads[i], NULL,
run_tramp,
reinterpret_cast<void *>(given_depth));
if (rv) {
errno = rv;
perror("pthread_create");
}
}
run_tramp(reinterpret_cast<void *>(given_depth));
for (int i = 0; i < thread_count; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}

View file

@ -0,0 +1,293 @@
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <algorithm>
#include "run_benchmark.h"
static void bench_fastpath_throughput(long iterations,
uintptr_t param)
{
size_t sz = 32;
for (; iterations>0; iterations--) {
void *p = malloc(sz);
if (!p) {
abort();
}
free(p);
// this makes next iteration use different free list. So
// subsequent iterations may actually overlap in time.
sz = ((sz * 8191) & 511) + 16;
}
}
static void bench_fastpath_dependent(long iterations,
uintptr_t param)
{
size_t sz = 32;
for (; iterations>0; iterations--) {
void *p = malloc(sz);
if (!p) {
abort();
}
free(p);
// this makes next iteration depend on current iteration. But this
// iteration's free may still overlap with next iteration's malloc
sz = ((sz | reinterpret_cast<size_t>(p)) & 511) + 16;
}
}
static void bench_fastpath_simple(long iterations,
uintptr_t param)
{
size_t sz = static_cast<size_t>(param);
for (; iterations>0; iterations--) {
void *p = malloc(sz);
if (!p) {
abort();
}
free(p);
// next iteration will use same free list as this iteration. So it
// should be prevent next iterations malloc to go too far before
// free done. But using same size will make free "too fast" since
// we'll hit size class cache.
}
}
#ifdef __GNUC__
#define HAVE_SIZED_FREE_OPTION
extern "C" void tc_free_sized(void *ptr, size_t size) __attribute__((weak));
extern "C" void *tc_memalign(size_t align, size_t size) __attribute__((weak));
static bool is_sized_free_available(void)
{
return tc_free_sized != NULL;
}
static bool is_memalign_available(void)
{
return tc_memalign != NULL;
}
static void bench_fastpath_simple_sized(long iterations,
uintptr_t param)
{
size_t sz = static_cast<size_t>(param);
for (; iterations>0; iterations--) {
void *p = malloc(sz);
if (!p) {
abort();
}
tc_free_sized(p, sz);
// next iteration will use same free list as this iteration. So it
// should be prevent next iterations malloc to go too far before
// free done. But using same size will make free "too fast" since
// we'll hit size class cache.
}
}
static void bench_fastpath_memalign(long iterations,
uintptr_t param)
{
size_t sz = static_cast<size_t>(param);
for (; iterations>0; iterations--) {
void *p = tc_memalign(32, sz);
if (!p) {
abort();
}
free(p);
// next iteration will use same free list as this iteration. So it
// should be prevent next iterations malloc to go too far before
// free done. But using same size will make free "too fast" since
// we'll hit size class cache.
}
}
#endif // __GNUC__
#define STACKSZ (1 << 16)
static void bench_fastpath_stack(long iterations,
uintptr_t _param)
{
void *stack[STACKSZ];
size_t sz = 64;
long param = static_cast<long>(_param);
param &= STACKSZ - 1;
param = param ? param : 1;
for (; iterations>0; iterations -= param) {
for (long k = param-1; k >= 0; k--) {
void *p = malloc(sz);
if (!p) {
abort();
}
stack[k] = p;
// this makes next iteration depend on result of this iteration
sz = ((sz | reinterpret_cast<size_t>(p)) & 511) + 16;
}
for (long k = 0; k < param; k++) {
free(stack[k]);
}
}
}
static void bench_fastpath_stack_simple(long iterations,
uintptr_t _param)
{
void *stack[STACKSZ];
size_t sz = 128;
long param = static_cast<long>(_param);
param &= STACKSZ - 1;
param = param ? param : 1;
for (; iterations>0; iterations -= param) {
for (long k = param-1; k >= 0; k--) {
void *p = malloc(sz);
if (!p) {
abort();
}
stack[k] = p;
}
for (long k = 0; k < param; k++) {
free(stack[k]);
}
}
}
static void bench_fastpath_rnd_dependent(long iterations,
uintptr_t _param)
{
static const uintptr_t rnd_c = 1013904223;
static const uintptr_t rnd_a = 1664525;
void *ptrs[STACKSZ];
size_t sz = 128;
if ((_param & (_param - 1))) {
abort();
}
if (_param > STACKSZ) {
abort();
}
int param = static_cast<int>(_param);
for (; iterations>0; iterations -= param) {
for (int k = param-1; k >= 0; k--) {
void *p = malloc(sz);
if (!p) {
abort();
}
ptrs[k] = p;
sz = ((sz | reinterpret_cast<size_t>(p)) & 511) + 16;
}
// this will iterate through all objects in order that is
// unpredictable to processor's prefetchers
uint32_t rnd = 0;
uint32_t free_idx = 0;
do {
free(ptrs[free_idx]);
rnd = rnd * rnd_a + rnd_c;
free_idx = rnd & (param - 1);
} while (free_idx != 0);
}
}
static void *randomize_buffer[13<<20];
void randomize_one_size_class(size_t size) {
int count = (100<<20) / size;
if (count * sizeof(randomize_buffer[0]) > sizeof(randomize_buffer)) {
abort();
}
for (int i = 0; i < count; i++) {
randomize_buffer[i] = malloc(size);
}
std::random_shuffle(randomize_buffer, randomize_buffer + count);
for (int i = 0; i < count; i++) {
free(randomize_buffer[i]);
}
}
void randomize_size_classes() {
randomize_one_size_class(8);
int i;
for (i = 16; i < 256; i += 16) {
randomize_one_size_class(i);
}
for (; i < 512; i += 32) {
randomize_one_size_class(i);
}
for (; i < 1024; i += 64) {
randomize_one_size_class(i);
}
for (; i < (4 << 10); i += 128) {
randomize_one_size_class(i);
}
for (; i < (32 << 10); i += 1024) {
randomize_one_size_class(i);
}
}
int main(void)
{
randomize_size_classes();
report_benchmark("bench_fastpath_throughput", bench_fastpath_throughput, 0);
report_benchmark("bench_fastpath_dependent", bench_fastpath_dependent, 0);
report_benchmark("bench_fastpath_simple", bench_fastpath_simple, 64);
report_benchmark("bench_fastpath_simple", bench_fastpath_simple, 2048);
report_benchmark("bench_fastpath_simple", bench_fastpath_simple, 16384);
#ifdef HAVE_SIZED_FREE_OPTION
if (is_sized_free_available()) {
report_benchmark("bench_fastpath_simple_sized", bench_fastpath_simple_sized, 64);
report_benchmark("bench_fastpath_simple_sized", bench_fastpath_simple_sized, 2048);
}
if (is_memalign_available()) {
report_benchmark("bench_fastpath_memalign", bench_fastpath_memalign, 64);
report_benchmark("bench_fastpath_memalign", bench_fastpath_memalign, 2048);
}
#endif
for (int i = 8; i <= 512; i <<= 1) {
report_benchmark("bench_fastpath_stack", bench_fastpath_stack, i);
}
report_benchmark("bench_fastpath_stack_simple", bench_fastpath_stack_simple, 32);
report_benchmark("bench_fastpath_stack_simple", bench_fastpath_stack_simple, 8192);
report_benchmark("bench_fastpath_rnd_dependent", bench_fastpath_rnd_dependent, 32);
report_benchmark("bench_fastpath_rnd_dependent", bench_fastpath_rnd_dependent, 8192);
return 0;
}

View file

@ -0,0 +1,112 @@
// -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*-
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "run_benchmark.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
struct internal_bench {
bench_body body;
uintptr_t param;
};
static void run_body(struct internal_bench *b, long iterations)
{
b->body(iterations, b->param);
}
static double measure_once(struct internal_bench *b, long iterations)
{
struct timeval tv_before, tv_after;
int rv;
double time;
rv = gettimeofday(&tv_before, NULL);
if (rv) {
perror("gettimeofday");
abort();
}
run_body(b, iterations);
rv = gettimeofday(&tv_after, NULL);
if (rv) {
perror("gettimeofday");
abort();
}
tv_after.tv_sec -= tv_before.tv_sec;
time = tv_after.tv_sec * 1E6 + tv_after.tv_usec;
time -= tv_before.tv_usec;
time *= 1000;
return time;
}
#define TRIAL_NSEC 0.3E9
#define TARGET_NSEC 3E9
static double run_benchmark(struct internal_bench *b)
{
long iterations = 128;
double nsec;
while (1) {
nsec = measure_once(b, iterations);
if (nsec > TRIAL_NSEC) {
break;
}
iterations <<= 1;
}
while (nsec < TARGET_NSEC) {
iterations = (long)(iterations * TARGET_NSEC * 1.1 / nsec);
nsec = measure_once(b, iterations);
}
return nsec / iterations;
}
void report_benchmark(const char *name, bench_body body, uintptr_t param)
{
int i;
struct internal_bench b = {.body = body, .param = param};
for (i = 0; i < 3; i++) {
double nsec = run_benchmark(&b);
int slen;
int padding_size;
slen = printf("Benchmark: %s", name);
if (param && name[strlen(name)-1] != ')') {
slen += printf("(%lld)", (long long)param);
}
padding_size = 60 - slen;
if (padding_size < 1) {
padding_size = 1;
}
printf("%*c%f nsec\n", padding_size, ' ', nsec);
fflush(stdout);
}
}

View file

@ -0,0 +1,43 @@
// -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*-
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _RUN_BENCHMARK_H_
#define _RUN_BENCHMARK_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*bench_body)(long iterations, uintptr_t param);
void report_benchmark(const char *name, bench_body body, uintptr_t param);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // _RUN_BENCHMARK_H_