PAPI 7.1.0.0
Loading...
Searching...
No Matches
pfmlib_os_macos.c
Go to the documentation of this file.
1/*
2 * pfmlib_os_macos.c: set of functions for MacOS (Tiger)
3 *
4 * Copyright (c) 2008 Stephane Eranian
5 * Contributed by Stephane Eranian <eranian@gmail.com>
6 * As a sign of friendship to my friend Eric, big fan of MacOS
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
22 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
23 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25#include <sys/types.h>
26#include <stdint.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/sysctl.h>
31
32#include "pfmlib_priv.h"
33
34typedef enum {
39
40/*
41 * helper function to retrieve one value from /proc/cpuinfo
42 * for internal libpfm use only
43 * attr: the attribute (line) to look for
44 * ret_buf: a buffer to store the value of the attribute (as a string)
45 * maxlen : number of bytes of capacity in ret_buf
46 *
47 * ret_buf is null terminated.
48 *
49 * Return:
50 * 0 : attribute found, ret_buf populated
51 * -1: attribute not found
52 */
53int
54__pfm_getcpuinfo_attr(const char *attr, char *ret_buf, size_t maxlen)
55{
57 union {
58 char str[32];
59 int val;
60 } value;
61 char *name = NULL;
62 int mib[16];
63 int ret = -1;
64 size_t len, mib_len;
65
66 if (attr == NULL || ret_buf == NULL || maxlen < 1)
67 return -1;
68
69 *ret_buf = '\0';
70
71 if (!strcmp(attr, "vendor_id")) {
72 name = "machdep.cpu.vendor";
73 type = TYPE_STR;
74 } else if (!strcmp(attr, "model")) {
75 name = "machdep.cpu.model";
76 type = TYPE_INT;
77 } else if (!strcmp(attr, "cpu family")) {
78 name = "machdep.cpu.family";
79 type = TYPE_INT;
80 }
81
82 mib_len = 16;
83 ret = sysctlnametomib(name, mib, &mib_len);
84 if (ret)
85 return -1;
86
87 len = sizeof(value);
88 ret = sysctl(mib, mib_len, &value, &len, NULL, 0);
89 if (ret)
90 return ret;
91
92 if (type == TYPE_STR)
93 strncpy(ret_buf, value.str, maxlen);
94 else if (type == TYPE_INT)
95 snprintf(ret_buf, maxlen, "%d", value.val);
96
97 __pfm_vbprintf("attr=%s ret=%d ret_buf=%s\n", attr, ret, ret_buf);
98
99 return ret;
100}
101
102void
104{
105}
uint16_t type
int __pfm_getcpuinfo_attr(const char *attr, char *ret_buf, size_t maxlen)
void pfm_init_syscalls(void)
mib_name_t
@ TYPE_NONE
@ TYPE_STR
@ TYPE_INT
void __pfm_vbprintf(const char *fmt,...)
Definition: pfmlib_priv.c:52
const char * name
Definition: rocs.c:225