nginx 地址對齊(ngx_align_ptr)

內存池,要在大塊連續內存上,分配小塊內存,指向小內存塊的地址是否對齊,對系統性能有一定影響:因爲 cpu 從主存上讀取數據很慢的,合理的地址對齊可以減少訪問次數,提高訪問效率。看看 nginx 的[內存池地址對齊操作:

// p 是內存指針,a 是對齊字節數
#define ngx_align_ptr(p, a)                                                   \
    (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))

該宏的原理詳細證明,請參考 《高效算法的奧祕》(第二版)第三章 2 的冪邊界


  • 當 $ a = 2^n$ 時,~((uintptr_t) a - 1)) 的 64 位二進制數,最右邊 nn 位數是 0。所以 x & ~((uintptr_t) a - 1)) 能被 2n2^n 整除。
a 對齊字節數 2 的冪 64位二進制
1 202^0 1111111111111111111111111111111111111111111111111111111111111111
2 212^1 1111111111111111111111111111111111111111111111111111111111111110
4 222^2 1111111111111111111111111111111111111111111111111111111111111100
8 232^3 1111111111111111111111111111111111111111111111111111111111111000
16 242^4 1111111111111111111111111111111111111111111111111111111111110000
32 252^5 1111111111111111111111111111111111111111111111111111111111100000
64 262^6 1111111111111111111111111111111111111111111111111111111111000000

測試

測試源碼

測試 ~((uintptr_t)a - 1))

// 測試 ~((uintptr_t)a - 1))
void test_a() {
    int i, len;
    uintptr_t l;
    char* p;
    char test[128];

    int aligns[] = {1, 2, 4, 8, 16, 32, 64};
    len = sizeof(aligns) / sizeof(int);

    for (i = 0; i < len; i++) {
        l = ~((uintptr_t)aligns[i] - 1);
        p = i2bin(l, test, 128);
        printf("a: %2d,  d: %s\n", aligns[i], p);
    }
}

結果:

a:  1,  d: 1111111111111111111111111111111111111111111111111111111111111111
a:  2,  d: 1111111111111111111111111111111111111111111111111111111111111110
a:  4,  d: 1111111111111111111111111111111111111111111111111111111111111100
a:  8,  d: 1111111111111111111111111111111111111111111111111111111111111000
a: 16,  d: 1111111111111111111111111111111111111111111111111111111111110000
a: 32,  d: 1111111111111111111111111111111111111111111111111111111111100000
a: 64,  d: 1111111111111111111111111111111111111111111111111111111111000000

地址添加隨機數,測試不同的對齊方式

// 測試數值是否對齊
void test_align_mod() {
    char bin[128];
    u_char *p, *a, *r;
    int i, len, alignment;
    int aligns[] = {1, 2, 4, 8, 16, 32, 64};

    len = sizeof(aligns) / sizeof(int);
    srand(time(NULL));

    p = (u_char*)malloc(1024 * sizeof(u_char));
    printf("p: %p\n", p);

    r = p;

    for (i = 0; i < len; i++) {
        alignment = aligns[i];
        r = p + rand() % 64;
        a = ngx_align_ptr(r, alignment);
        printf("a: %2d, r: %p, align: %p, abin: %s, mod: %lu\n", alignment, r,
               a, i2bin((unsigned long long)a, bin, 128),
               (uintptr_t)a % alignment);
    }
    free(p);
}

結果:

p: 0x7fd035800600
a:  1, r: 0x7fd03580062f, align: 0x7fd03580062f, abin: 11111111101000000110101100000000000011000101111, mod: 0
a:  2, r: 0x7fd03580061a, align: 0x7fd03580061a, abin: 11111111101000000110101100000000000011000011010, mod: 0
a:  4, r: 0x7fd035800635, align: 0x7fd035800638, abin: 11111111101000000110101100000000000011000111000, mod: 0
a:  8, r: 0x7fd035800613, align: 0x7fd035800618, abin: 11111111101000000110101100000000000011000011000, mod: 0
a: 16, r: 0x7fd035800633, align: 0x7fd035800640, abin: 11111111101000000110101100000000000011001000000, mod: 0
a: 32, r: 0x7fd035800602, align: 0x7fd035800620, abin: 11111111101000000110101100000000000011000100000, mod: 0
a: 64, r: 0x7fd03580061b, align: 0x7fd035800640, abin: 11111111101000000110101100000000000011001000000, mod: 0

測試對齊效率

申請兩塊內存,一塊內存是對齊處理,另外一塊不對齊查看效率(測試代碼)。

#define ALIGN 1
#define UN_ALIGN 0
#define READ 0
#define WRITE 1
#define ALIGN_COUNT (1024 * 1024 * 64)
#define UN_ALIGN_COUNT ALIGN_COUNT

typedef int type_t;
#define ngx_align_ptr(p, a) \
    (u_char*)(((uintptr_t)(p) + ((uintptr_t)a - 1)) & ~((uintptr_t)a - 1))

// 申請兩塊內存,一塊內存是對齊處理,另外一塊不對齊。
void test_align(u_char* p, int size, int alignment, int is_align,
                int is_write) {
    u_char* end;
    long long start, stop;
    type_t *wirte, read;
    int count;

    count = 0;
    srand(time(NULL));

    end = p + size;
    p = (u_char*)ngx_align_ptr(p, alignment);
    p += is_align ? 0 : 1;  //製造不對齊地址

    start = mstime();
    while (p + sizeof(type_t) < end) {
        if (is_write) {
            wirte = (type_t*)p;
            *wirte = (type_t)rand();
        } else {
            read = (type_t)rand();
        }
        p += sizeof(type_t);

        count++;
    }
    stop = mstime();

    printf(
        "is_align: %d, is_write: %d, alignment: %d, count: %d, cost: %lld ms,"
        " avg: %lf ms\n",
        is_align, is_write, alignment, count, stop - start,
        (float)(stop - start) / count);
}

void test_alloc_mem(int argc, char** argv, int alignment, int is_align) {
    u_char *aligns, *ualigns;
    int alen, ualen;

    alen = ALIGN_COUNT * sizeof(type_t);
    aligns = (u_char*)malloc(alen);
    ualen = UN_ALIGN_COUNT * sizeof(type_t);
    ualigns = (u_char*)malloc(ualen);

    if (is_align) {
        test_align(aligns, alen, alignment, ALIGN, WRITE);
        test_align(aligns, alen, alignment, ALIGN, READ);
    } else {
        test_align(ualigns, ualen, alignment, UN_ALIGN, WRITE);
        test_align(ualigns, ualen, alignment, UN_ALIGN, READ);
    }

    free(aligns);
    free(ualigns);
    return;
}

int main(int argc, char* argv[]) {
    int alignment, is_align;

    alignment = (argc >= 2) ? atoi(argv[1]) : 4;
    is_align = (argc == 3 && !strcasecmp(argv[2], "1")) ? 1 : 0;
    test_alloc_mem(argc, argv, alignment, is_align);
    return 0;
}

結果:

# ./test_align.sh

is_align: 1, is_write: 1, alignment: 16, count: 67108862, cost: 1016 ms, avg: 0.000015 ms
is_align: 1, is_write: 0, alignment: 16, count: 67108862, cost: 214 ms, avg: 0.000003 ms

real    0m1.244s
user    0m1.177s
sys     0m0.066s
-------
is_align: 0, is_write: 1, alignment: 16, count: 67108862, cost: 919 ms, avg: 0.000014 ms
is_align: 0, is_write: 0, alignment: 16, count: 67108862, cost: 223 ms, avg: 0.000003 ms

real    0m1.159s
user    0m1.084s
sys     0m0.075s

總結

從測試例子中,對齊和不對齊效率沒有明顯差距(cost 耗費時間),反而對齊的地址有時候花的時間還多,實踐和理論對不上啊!——不知道問題出在哪裏,能力有限,歡迎指正。


參考

[nginx 源碼走讀] 內存池
NGINX 內存池 — 對齊
Nginx - CPU Cacheline 深思
C語言字節對齊問題詳解
談談內存對齊一

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章