jemalooc 学习

jemalloc 的编译

./autogen.sh 

开启 prof

./configure --enable-prof

JEMALLOC_PREFIX    : 
JEMALLOC_PRIVATE_NAMESPACE
                   : je_
install_suffix     : 
malloc_conf        : 
autogen            : 0
debug              : 0
stats              : 1
prof               : 1
prof-libunwind     : 0
prof-libgcc        : 1
prof-gcc           : 0
thp                : 1
fill               : 1
utrace             : 0
xmalloc            : 0
lazy_lock          : 0
cache-oblivious    : 1
cxx                : 1
===============================================================================

开启 prof

export MALLOC_CONF="prof:true"

ptmalloc, tcmalloc 和 jemalloc 内存分配策略研究

http://www.360doc.com/content/13/0915/09/8363527_314549128.shtml

安装画图工具

yum install graphviz*
yum install ghostscript
g++ main.c -o main -Llib -ljemalloc -ldl -lpthread
./jemalloc-master/bin/jeprof --pdf ./main ./jeprof.11734.0.m0.heap > 1.pdf
#include <stdio.h>
#include <stdlib.h>
#include "include/jemalloc/jemalloc.h"

typedef struct T1
{
    char c[4];
} T1;

void print_my_jemalloc_data(void *opaque, const char *buf);

int main()
{
    //for (int i=0; i<50; ++i)
    //{
    //    void* v = malloc(sizeof(T1));
    //}

    void* v = malloc(sizeof(T1));

    malloc_stats_print(NULL, NULL, NULL);

    return 0;
}

void print_my_jemalloc_data(void *opaque,const char *buf)
{
   printf("%s", buf);
}

性能测试

g++ main.cpp -o main -std=c++0x -Lnormal/lib -ljemalloc -lrt -lpthread
#include <unistd.h>
#include <list>
#include <memory>
#include <iostream>
#include <sys/time.h>
#include "normal/include/jemalloc.h"

#define SMALL_LOOP_COUNT 50000000
#define LARGE_LOOP_COUNT 50000000
#define HUGE_LOOP_COUNT 50000000

// --------------------------------------------------

typedef struct Small8 { char c[8]; } Small8;
typedef struct Small64 { char c[64]; } Small64;
typedef struct Small128 { char c[128]; } Small128;

typedef struct Small256 { char c[256]; } Small256;
typedef struct Small320 { char c[320]; } Small320;
typedef struct Small512 { char c[512]; } Small512;

typedef struct Small1024 { char c[1024]; } Small1024;
typedef struct Small2048 { char c[2048]; } Small2048;
typedef struct Small3840 { char c[3840]; } Small3840;

// --------------------------------------------------

typedef struct Large4k { char c[4096]; } Large4k;
typedef struct Large32k { char c[32768]; } Large32k;
typedef struct Large128k { char c[131072]; } Large128k;

typedef struct Large256k { char c[262144]; } Large256k;
typedef struct Large512k { char c[524288]; } Large512k;
typedef struct Large1024k { char c[1048576]; } Large1024k;

typedef struct Large2048k { char c[2097152]; } Large2048k;
typedef struct Large3840k { char c[3932160]; } Large3840k;
typedef struct Large4072k { char c[4169728]; } Large4072k;

// --------------------------------------------------

typedef struct Large4m { char c[1048576]; } Huge4m;
typedef struct Large8m { char c[2097152]; } Huge8m;
typedef struct Large12m { char c[3145728]; } Huge12m;

template<typename T>
void test_malloc(uint32_t n)
{
    for (int i = 0; i < n; ++i)
    {
        void* p = malloc(sizeof(T));
    }
}

template<typename T>
void test_new(uint32_t n)
{
    for (int i = 0; i < n; ++i)
    {
        T* p = new T();
    }
}

template<typename T>
void test_allocate(uint32_t n)
{
    std::allocator<T> talloc;
    for (int i = 0; i < n; ++i)
    {
        T* p = talloc.allocate(1);
    }
}

template<typename T>
void test_makeshared(uint32_t n)
{
    for (int i = 0; i < n; ++i)
    {
        std::shared_ptr<T> p = std::make_shared<T>();
    }
}

//printf("start tv_sec:%lld, tv_usec:%lld\n", start.tv_sec, start.tv_usec); \
//printf("end tv_sec:%lld, tv_usec:%lld\n", end.tv_sec, end.tv_usec); \

#define CLOCK_FUNC_TIME(func, n, t) \
    do {\
        struct timeval start, end; \
        int64_t interval; \
        gettimeofday(&start, NULL); \
        func<t>(n); \
        gettimeofday(&end, NULL); \
        interval = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec); \
        printf("%zu, interval = %lld\n", sizeof(t), interval); \
    } while(0);

int main(int argc, char *argv[])
{
    int n =  atoi(argv[1]);
    switch(n)
    {
        case 1: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small8);
            break;
        case 2: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small64);
            break;
        case 3: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small128);
            break;
        case 4: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small256);
            break;
        case 5: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small320);
            break;
        case 6: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small512);
            break;
        case 7: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small1024);
            break;
        case 8: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small2048);
            break;
        case 9: CLOCK_FUNC_TIME(test_malloc, SMALL_LOOP_COUNT, Small3840);
            break;
        case 10: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large4k);
            break;
        case 11: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large32k);
            break;
        case 12: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large128k);
            break;
        case 13: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large256k);
            break;
        case 14: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large512k);
            break;
        case 15: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large1024k);
            break;
        case 16: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large2048k);
            break;
        case 17: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large3840k);
            break;
        case 18: CLOCK_FUNC_TIME(test_malloc, LARGE_LOOP_COUNT, Large4072k);
            break;
        case 19: CLOCK_FUNC_TIME(test_malloc, HUGE_LOOP_COUNT, Huge4m);
            break;
        case 20: CLOCK_FUNC_TIME(test_malloc, HUGE_LOOP_COUNT, Huge8m);
            break;
        case 21: CLOCK_FUNC_TIME(test_malloc, HUGE_LOOP_COUNT, Huge12m);
            break;
        default:
            break;
    }

    mallctl("prof.dump", NULL, NULL, NULL, 0);

    return 0;
}

posted @ 2018-03-25 23:08  tomren  阅读(434)  评论(0编辑  收藏  举报