summaryrefslogtreecommitdiffstats
path: root/src/hardware/lscpu.c
blob: 3407ceacee39b472fbe1471a975d6dea064ba7b3 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (C) 2013-2014 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Peter Schiffer <pschiffe@redhat.com>
 */

#include "lscpu.h"


/*
 * Initialize LscpuProcessor attributes.
 * @param cpu
 */
void init_lscpuprocessor_struct(LscpuProcessor *cpu)
{
    cpu->data_width = 0;
    cpu->processors = 0;
    cpu->cores = 1;
    cpu->threads_per_core = 1;
    cpu->stepping = NULL;
    cpu->current_speed = 0;
}

/*
 * Check attributes of cpu structure and fill in defaults if needed.
 * @param cpu
 * @return 0 if success, negative value otherwise
 */
short check_lscpuprocessor_attributes(LscpuProcessor *cpu)
{
    short ret = -1;

    if (!cpu->stepping) {
        if (!(cpu->stepping = strdup(""))) {
            goto done;
        }
    }

    ret = 0;

done:
    if (ret != 0) {
        lmi_warn("Failed to allocate memory.");
    }

    return ret;
}

short lscpu_get_processor(LscpuProcessor *cpu)
{
    short ret = -1;
    unsigned i, buffer_size = 0;
    char **buffer = NULL, *buf;

    /* get lscpu output */
    if (run_command("lscpu", &buffer, &buffer_size) != 0) {
        goto done;
    }

    init_lscpuprocessor_struct(cpu);

    /* parse information about processor */
    for (i = 0; i < buffer_size; i++) {
        /* Data Width */
        buf = copy_string_part_after_delim(buffer[i], "CPU op-mode(s):");
        if (buf) {
            if (strstr(buf, "64")) {
                cpu->data_width = 64;
            } else if (strstr(buf, "32")) {
                cpu->data_width = 32;
            }
            free(buf);
            buf = NULL;
            continue;
        }
        /* Threads per core */
        buf = copy_string_part_after_delim(buffer[i], "Thread(s) per core:");
        if (buf) {
            sscanf(buf, "%u", &cpu->threads_per_core);
            free(buf);
            buf = NULL;
            continue;
        }
        /* Cores per processor */
        buf = copy_string_part_after_delim(buffer[i], "Core(s) per socket:");
        if (buf) {
            sscanf(buf, "%u", &cpu->cores);
            free(buf);
            buf = NULL;
            continue;
        }
        /* Number of processors */
        buf = copy_string_part_after_delim(buffer[i], "Socket(s):");
        if (buf) {
            sscanf(buf, "%u", &cpu->processors);
            free(buf);
            buf = NULL;
            continue;
        }
        /* Stepping */
        buf = copy_string_part_after_delim(buffer[i], "Stepping:");
        if (buf) {
            cpu->stepping = buf;
            buf = NULL;
            continue;
        }
        /* Current speed in MHz */
        buf = copy_string_part_after_delim(buffer[i], "CPU MHz:");
        if (buf) {
            sscanf(buf, "%u", &cpu->current_speed);
            free(buf);
            buf = NULL;
            continue;
        }
    }

    if (check_lscpuprocessor_attributes(cpu) != 0) {
        goto done;
    }

    ret = 0;

done:
    free_2d_buffer(&buffer, &buffer_size);

    if (ret != 0) {
        lscpu_free_processor(cpu);
    }

    return ret;
}

void lscpu_free_processor(LscpuProcessor *cpus)
{
    if (!cpus) {
        return;
    }

    free(cpus->stepping);
    cpus->stepping = NULL;
}