【问题标题】:GoLang / CGO: Problems calling Mach API host_statistics() from GoGoLang / CGO:从 Go 调用 Mach API host_statistics() 时出现问题
【发布时间】:2013-10-08 00:26:49
【问题描述】:

我使用以下 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 标头中定义为:

 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
 );

【问题讨论】:

  • 我找不到问题的根源,但是将 var host_info_out 类型从 C.host_info_t 更改为 C.integer_t 对我有用。

标签: go cgo mach


【解决方案1】:

函数原型表明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包在不兼容的指针类型之间进行转换)。

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多