4

与 C 代码互操作时,我无法直接转换结构,我不得不在 Go 中定义一个等效的结构。来自的 C 函数libproc.h

int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize)

的 C 结构flavor==PROC_PIDTASKINFO定义proc_taskinfosys/proc_info.h(包含在中libproc.h):

struct proc_taskinfo {
    uint64_t        pti_virtual_size;   /* virtual memory size (bytes) */
    uint64_t        pti_resident_size;  /* resident memory size (bytes) */
    uint64_t        pti_total_user;     /* total time */
    uint64_t        pti_total_system;
    uint64_t        pti_threads_user;   /* existing threads only */
    uint64_t        pti_threads_system;
    int32_t         pti_policy;     /* default policy for new threads */
    int32_t         pti_faults;     /* number of page faults */
    int32_t         pti_pageins;        /* number of actual pageins */
    int32_t         pti_cow_faults;     /* number of copy-on-write faults */
    int32_t         pti_messages_sent;  /* number of messages sent */
    int32_t         pti_messages_received;  /* number of messages received */
    int32_t         pti_syscalls_mach;  /* number of mach system calls */
    int32_t         pti_syscalls_unix;  /* number of unix system calls */
    int32_t         pti_csw;            /* number of context switches */
    int32_t         pti_threadnum;      /* number of threads in the task */
    int32_t         pti_numrunning;     /* number of running threads */
    int32_t         pti_priority;       /* task priority*/
};

即使 Go 代码确实有效,我也无法C.proc_taskinfo直接使用。Go 函数是propertiesOf():完整的源代码在这里

如果我引用 C 结构,我会收到与我关于该主题的最新问题could not determine kind of name for C.proc_taskinfo中报告的类似错误: ,但这次我确定定义是使用#include.

4

1 回答 1

7

根据文档

要直接访问 struct、union 或 enum 类型,请在其前面加上 struct_、union_ 或 enum_,如 C.struct_stat 中所示。

使用C.struct_proc_taskinfo.

于 2015-05-29T14:52:34.530 回答