Blame src/libpfm-3.y/lib/pfmlib_os_macos.c

Packit 577717
/*
Packit 577717
 * pfmlib_os_macos.c: set of functions for MacOS (Tiger)
Packit 577717
 *
Packit 577717
 * Copyright (c) 2008 Stephane Eranian
Packit 577717
 * Contributed by Stephane Eranian <eranian@gmail.com>
Packit 577717
 * As a sign of friendship to my friend Eric, big fan of MacOS
Packit 577717
 * 
Packit 577717
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 577717
 * of this software and associated documentation files (the "Software"), to deal
Packit 577717
 * in the Software without restriction, including without limitation the rights
Packit 577717
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 577717
 * of the Software, and to permit persons to whom the Software is furnished to do so,
Packit 577717
 * subject to the following conditions:
Packit 577717
 *
Packit 577717
 * The above copyright notice and this permission notice shall be included in all
Packit 577717
 * copies or substantial portions of the Software.
Packit 577717
 *
Packit 577717
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Packit 577717
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
Packit 577717
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 577717
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Packit 577717
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
Packit 577717
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 577717
 */
Packit 577717
#include <sys/types.h>
Packit 577717
#include <stdint.h>
Packit 577717
#include <stdio.h>
Packit 577717
#include <string.h>
Packit 577717
#include <unistd.h>
Packit 577717
#include <sys/sysctl.h>
Packit 577717
Packit 577717
#include "pfmlib_priv.h"
Packit 577717
Packit 577717
typedef enum {
Packit 577717
	TYPE_NONE,
Packit 577717
	TYPE_STR,
Packit 577717
	TYPE_INT
Packit 577717
} mib_name_t;
Packit 577717
Packit 577717
/*
Packit 577717
 * helper function to retrieve one value from /proc/cpuinfo
Packit 577717
 * for internal libpfm use only
Packit 577717
 * attr: the attribute (line) to look for
Packit 577717
 * ret_buf: a buffer to store the value of the attribute (as a string)
Packit 577717
 * maxlen : number of bytes of capacity in ret_buf
Packit 577717
 *
Packit 577717
 * ret_buf is null terminated.
Packit 577717
 *
Packit 577717
 * Return:
Packit 577717
 * 	0 : attribute found, ret_buf populated
Packit 577717
 * 	-1: attribute not found
Packit 577717
 */
Packit 577717
int
Packit 577717
__pfm_getcpuinfo_attr(const char *attr, char *ret_buf, size_t maxlen)
Packit 577717
{
Packit 577717
	mib_name_t type = TYPE_NONE;
Packit 577717
	union {
Packit 577717
		char str[32];
Packit 577717
		int val;
Packit 577717
	} value;
Packit 577717
	char *name = NULL;
Packit 577717
	int mib[16];
Packit 577717
	int ret = -1;
Packit 577717
	size_t len, mib_len;
Packit 577717
Packit 577717
	if (attr == NULL || ret_buf == NULL || maxlen < 1)
Packit 577717
		return -1;
Packit 577717
Packit 577717
	*ret_buf = '\0';
Packit 577717
Packit 577717
	if (!strcmp(attr, "vendor_id")) {
Packit 577717
		name = 	"machdep.cpu.vendor";
Packit 577717
		type = TYPE_STR;
Packit 577717
	} else if (!strcmp(attr, "model")) {
Packit 577717
		name = "machdep.cpu.model";
Packit 577717
		type = TYPE_INT;
Packit 577717
	} else if (!strcmp(attr, "cpu family")) {
Packit 577717
		name = "machdep.cpu.family";
Packit 577717
		type = TYPE_INT;
Packit 577717
	}
Packit 577717
Packit 577717
	mib_len = 16;
Packit 577717
	ret = sysctlnametomib(name, mib, &mib_len);
Packit 577717
	if (ret)
Packit 577717
		return -1;
Packit 577717
Packit 577717
	len = sizeof(value);
Packit 577717
	ret = sysctl(mib, mib_len, &value, &len, NULL, 0);
Packit 577717
	if (ret)
Packit 577717
		return ret;
Packit 577717
Packit 577717
	if (type == TYPE_STR)
Packit 577717
		strncpy(ret_buf, value.str, maxlen);
Packit 577717
	else if (type == TYPE_INT)
Packit 577717
		snprintf(ret_buf, maxlen, "%d", value.val);
Packit 577717
	
Packit 577717
	__pfm_vbprintf("attr=%s ret=%d ret_buf=%s\n", attr, ret, ret_buf);
Packit 577717
Packit 577717
	return ret;
Packit 577717
}
Packit 577717
Packit 577717
void
Packit 577717
pfm_init_syscalls(void)
Packit 577717
{
Packit 577717
}