Go 语言/CGO : Problems calling Mach API host_statistics() from Go

标签 go cgo mach

我使用以下 C 代码片段来获取 OS X 上的 CPU 负载:

    #include <mach/message.h> 
    #include <mach/mach_host.h>
    #include <mach/host_info.h>

    [...]

    mach_msg_type_number_t  count = HOST_CPU_LOAD_INFO_COUNT;
    kern_return_t error;
    host_cpu_load_info_data_t r_load;

    mach_port_t host_port = mach_host_self();
    error = host_statistics(host_port, HOST_CPU_LOAD_INFO, (host_info_t)&r_load, &count);

看完cgo教程后,我尝试将这段代码移植到Go。生成的代码如下所示:

package main

/*
#include <stdlib.h>
#include <mach/message.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
*/
import "C"

func main() {
    var err C.kern_return_t
    var host_info_out C.host_info_t
    var host_port C.mach_port_t = C.mach_host_self()

    count := C.mach_msg_type_number_t(C.HOST_CPU_LOAD_INFO_COUNT)

    err = C.host_statistics(C.host_t(host_port), C.HOST_CPU_LOAD_INFO, &host_info_out, &count)
}

但是,当我尝试构建代码时,我得到了以下错误消息

go build cputimes.go 
# command-line-arguments
cputimes.go:33: cannot use &host_info_out (type *_Ctype_host_info_t) as type *_Ctype_integer_t in function argument

我不明白为什么 cgo 会提示这个类型。 host_statistics() 的签名在 mach header 中定义为:

 kern_return_t host_statistics
 (
      host_t host_priv,
      host_flavor_t flavor,
      host_info_t host_info_out,
      mach_msg_type_number_t *host_info_outCnt
 );

最佳答案

函数原型(prototype)说 host_statistics 的第三个参数是一个 host_info_t 变量,而你传递一个指向 host_info_t 变量的指针你的示例程序。

查看 mach/host_info.h header file ,我们可以看到host_info_t是一个指针类型:

typedef integer_t   *host_info_t;       /* varying array of int. */

这就解释了为什么您会收到有关类型与 integer_t 不匹配的错误消息。

在处理该参数时,您的 Go 代码看起来并不等同于 C 代码。您可能想要更像这样的东西:

...
var r_load C.host_cpu_load_info_data_t
...
err = C.host_statistics(C.host_t(host_port), C.HOST_CPU_LOAD_INFO, C.host_info_t(unsafe.Pointer(&r_load)), &count)
...

(您需要使用 unsafe 包在不兼容的指针类型之间进行转换)。

关于Go 语言/CGO : Problems calling Mach API host_statistics() from Go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19123059/

相关文章:

go - 等待一个协程完成

C调用Go导出函数

iphone - iPhone 中的 Mach 异常

c - 在 XNU 项目中找到 __proc_info 符号

go - 如何设置 go-swagger 以从注释生成规范

c++ - 释放 cgo 库的内存

amazon-web-services - 在 Go 中测试 lambda 处理程序时如何模拟 AWS Lambda 上下文?

go - CGO中的C NULL类型

go - 在Golang中导入C错误: cc1.exe: error: too many filenames given

swift - 无法让 XPC 从客户端接收数据