仓库源文站点原文


layout: post title: 使用pahole進行c struct優化 categories:

- Linux

本來想找一個可以爬c struct的工具,進行自動轉換

結果發現pahole,不但可以秀出c struct,還可以秀出它的layout

linux man page裡面有它的說明

pahole shows data structure layouts encoded in debugging information formats, DWARF and CTF being supported. This is useful for, among other things: optimizing important data structures by reducing its size, figuring out what is the field sitting at an offset from the start of a data structure, investigating ABI changes and more generally understanding a new codebase you have to work with.

主要是分析加入debug information(-g)的執行檔或object檔來顯示資訊

但是ubuntu apt竟然找不到pahole..翻了一下原來在etfutils裡

xdmq裡的某個struct分析出的結果如下:

struct cli_ctx_s {
        char                       host[255];            /*     0   255 */

        /* XXX 1 byte hole, try to pack */

        /* --- cacheline 4 boundary (256 bytes) --- */
        int                        port;                 /*   256     4 */

        /* XXX 4 bytes hole, try to pack */

        uv_tcp_t                   client;               /*   264   248 */
        /* --- cacheline 8 boundary (512 bytes) --- */
        uv_connect_t               conn;                 /*   512    96 */
        /* --- cacheline 9 boundary (576 bytes) was 32 bytes ago --- */
        buffer_t                   buff;                 /*   608     8 */
        int                        node_id;              /*   616     4 */
        int                        self_id;              /*   620     4 */
        read_cb                    r_cb;                 /*   624     8 */
        msgpack_unpacker *         unp;                  /*   632     8 */
        /* --- cacheline 10 boundary (640 bytes) --- */
        int                        status;               /*   640     4 */

        /* XXX 4 bytes hole, try to pack */

        raft_node_t                node;                 /*   648     8 */

        /* size: 656, cachelines: 11, members: 11 */
        /* sum members: 647, holes: 3, sum holes: 9 */
        /* last cacheline: 16 bytes */
};