sysinfo(2) 手册页中是否存在错误?

sysinfo(2) 手册页中是否存在错误?

联机帮助页(还)记录了 struct sysinfo。该结构体的大小在 32 位和 64 位系统上有所不同。 (因为该结构是由 long 组成的)。

您可以在联机帮助页中阅读这些评论:

...
/* Pads structure to 64 bytes */
...
/* Padding to 64 bytes */
...

但这并不完全正确。在 64 位上,结构的大小是 112 而不是 64。所以我认为这是一个错误?

答案1

手册页绝对是不准确的,即使在其历史介绍中也是如此。

正如您所提到的,在 32 位平台上,struct最终的大小为 64 字节,如文档所述,但在 64 位平台上,最终的大小为 112 字节(因为 long 的长度为 8 字节,并且struct成员最终以 8 字节间隔对齐)。

就历史而言,2.3.16,struct声明如下:

struct sysinfo {
    long uptime;             /* Seconds since boot */
    unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
    unsigned long totalram;  /* Total usable main memory size */
    unsigned long freeram;   /* Available memory size */
    unsigned long sharedram; /* Amount of shared memory */
    unsigned long bufferram; /* Memory used by buffers */
    unsigned long totalswap; /* Total swap space size */
    unsigned long freeswap;  /* Swap space still available */
    unsigned short procs;    /* Number of current processes */
    char _f[22];             /* Pads structure to 64 bytes */
};

2.3.16中,增加了两个字段(totalbigfreebig),填充为已删除

struct sysinfo {
    long uptime;             /* Seconds since boot */
    unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
    unsigned long totalram;  /* Total usable main memory size */
    unsigned long freeram;   /* Available memory size */
    unsigned long sharedram; /* Amount of shared memory */
    unsigned long bufferram; /* Memory used by buffers */
    unsigned long totalbig;  /* Total big memory size */
    unsigned long freebig;   /* Available big memory size */
    unsigned long totalswap; /* Total swap space size */
    unsigned long freeswap;  /* Swap space still available */
    unsigned short procs;    /* Number of current processes */
};

在 2.3.17 中,新字段被移到末尾(大概有人意识到 2.3.16 的更改破坏了用户空间 ABI),并且重新引入了填充,因为libc5(但没有总体大小指示):

struct sysinfo {
    long uptime;                /* Seconds since boot */
    unsigned long loads[3];     /* 1, 5, and 15 minute load averages */
    unsigned long totalram;     /* Total usable main memory size */
    unsigned long freeram;      /* Available memory size */
    unsigned long sharedram;    /* Amount of shared memory */
    unsigned long bufferram;    /* Memory used by buffers */
    unsigned long totalswap;    /* Total swap space size */
    unsigned long freeswap;     /* Swap space still available */
    unsigned short procs;       /* Number of current processes */
    unsigned long totalbig;     /* Total big memory size */
    unsigned long freebig;      /* Available big memory size */
    char _f[22-2*sizeof(long)]; /* Padding: libc5 uses this.. */
};

2.3.18 将填充更改为char _f[20-2*sizeof(long)];, 以考虑procs和之间的填充totalbig

2.3.23 将totalbigand重命名freebigtotalhighand freehigh,并添加mem_unit(相应调整填充),留下几乎结构就像今天一样;从那时起,为 Motorola 68k 添加了一些显式填充,并且更改了类型以确保使用适合内核的类型,而不管“标准”类型定义如何:

struct sysinfo {
    __kernel_long_t uptime;     /* Seconds since boot */
    __kernel_ulong_t loads[3];  /* 1, 5, and 15 minute load averages */
    __kernel_ulong_t totalram;  /* Total usable main memory size */
    __kernel_ulong_t freeram;   /* Available memory size */
    __kernel_ulong_t sharedram; /* Amount of shared memory */
    __kernel_ulong_t bufferram; /* Memory used by buffers */
    __kernel_ulong_t totalswap; /* Total swap space size */
    __kernel_ulong_t freeswap;  /* swap space still available */
    __u16 procs;            /* Number of current processes */
    __u16 pad;          /* Explicit padding for m68k */
    __kernel_ulong_t totalhigh; /* Total high memory size */
    __kernel_ulong_t freehigh;  /* Available high memory size */
    __u32 mem_unit;         /* Memory unit size in bytes */
    char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /* Padding: libc5 uses this.. */
};

/* Padding to 64 bytes */我在内核历史记录中没有发现任何单独行的迹象。这似乎是由手册页贡献者引入的,在手册页项目的版本 3.41 中将“Padding for libc5”替换为“Padding to 64 bytes”,可能基于libc5例如使用的标头副本中的相同注释在 Debian 中。

我会提交一个补丁,除非你想处理它!

相关内容