Reputation: 31
I use the following C code fragment to get the CPU load on OS X:
#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);
I have tried to port this piece of code to Go after reading the cgo tutorial. The resulting code looks like this:
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)
}
However, when I try to build the code, I end up with the following error message
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
I do not udnerstand why cgo complains about the type. The signature of host_statistics() is defined in the mach headers as:
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
);
Upvotes: 3
Views: 564
Reputation: 43949
The function prototype says that the third argument to host_statistics
is a host_info_t
variable, while you are passing a pointer to a host_info_t
variable in your sample program.
Looking at the mach/host_info.h
header file, we can see that host_info_t
is a pointer type:
typedef integer_t *host_info_t; /* varying array of int. */
That explains why you got the error message about a type mismatch with integer_t
.
Your Go code doesn't really look equivalent to the C code when handling that argument. You probably want something more like this:
...
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)
...
(you need to use the unsafe
package to cast between the incompatible pointer types).
Upvotes: 1