File: sysinfo_darwin_module.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (120 lines) | stat: -rw-r--r-- 3,241 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2009      Cisco Systems, Inc.  All rights reserved.
 *
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */

#include "opal_config.h"
#include "opal/constants.h"

/* This component will only be compiled on Mac OSX, where we are
   guaranteed to have these headers */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#include <assert.h>
#include <time.h>
#include <string.h>

#include "opal/mca/base/mca_base_param.h"
#include "opal/mca/sysinfo/sysinfo.h"
#include "opal/mca/sysinfo/base/base.h"

#include "sysinfo_darwin.h"

static int init(void);
static int query(char **keys, opal_list_t *values);
static int fini(void);

/*
 * Darwin sysinfo module
 */
const opal_sysinfo_base_module_t opal_sysinfo_darwin_module = {
    init,
    query,
    fini
};

static int init(void)
{
    return OPAL_SUCCESS;
}

static int fini(void)
{
    return OPAL_SUCCESS;
}

/* Mac OSX does things a little differently than Linux
 * by providing process stats via an API. This means we
 * don't have to parse files that could change!
 */
static int query(char **keys, opal_list_t *values)
{
    int mib[2], i;
    size_t len;
    int64_t i64;
    int iint;
    opal_sysinfo_value_t *data;
    char strval[128];
    
    mib[0] = CTL_HW;
    
    /* cycle through the requested keys */
    for (i=0; NULL != keys[i]; i++) {
        if (0 == strcmp(keys[i], OPAL_SYSINFO_CPU_TYPE)) {
            mib[1] = HW_MACHINE;
            len = 128;
            sysctl(mib, 2, &strval, &len, NULL, 0);
            data = OBJ_NEW(opal_sysinfo_value_t);
            data->key = strdup(OPAL_SYSINFO_CPU_TYPE);
            data->type = OPAL_STRING;
            data->data.str = strdup(strval);
            opal_list_append(values, &data->super);
            continue;
        }
        if (0 == strcmp(keys[i], OPAL_SYSINFO_CPU_MODEL)) {
            mib[1] = HW_MODEL;
            len = 128;
            sysctl(mib, 2, &strval, &len, NULL, 0);
            data = OBJ_NEW(opal_sysinfo_value_t);
            data->key = strdup(OPAL_SYSINFO_CPU_MODEL);
            data->type = OPAL_STRING;
            data->data.str = strdup(strval);
            opal_list_append(values, &data->super);
            continue;
        }
        if (0 == strcmp(keys[i], OPAL_SYSINFO_NUM_CPUS)) {
            mib[1] = HW_NCPU;
            len = sizeof(int);
            sysctl(mib, 2, &iint, &len, NULL, 0);
            data = OBJ_NEW(opal_sysinfo_value_t);
            data->key = strdup(OPAL_SYSINFO_NUM_CPUS);
            data->type = OPAL_INT64;
            data->data.i64 = (int64_t)iint;
            opal_list_append(values, &data->super);
            continue;
        }
        if (0 == strcmp(keys[i], OPAL_SYSINFO_MEM_SIZE)) {
            mib[1] = HW_MEMSIZE;
            len = sizeof(int64_t);
            sysctl(mib, 2, &i64, &len, NULL, 0);
            data = OBJ_NEW(opal_sysinfo_value_t);
            data->key = strdup(OPAL_SYSINFO_MEM_SIZE);
            data->type = OPAL_INT64;
            data->data.i64 = i64 / (1 << 20);
            opal_list_append(values, &data->super);
            continue;
        }
    }

    return OPAL_SUCCESS;
}