aboutsummaryrefslogtreecommitdiff
path: root/src/sysfs.c
blob: ea59c1bf3d853ea2d06953020d800909c641388a (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
#include <glib.h>
#include <stdio.h>
#include "sysfs.h"
#include "zenmonitor.h"

gshort* get_cpu_dev_ids(){
    gshort *cpu_dev_ids = NULL;
    gshort coreid, cpuid;
    GDir *dir;
    const gchar *entry;
    gchar *filename, *buffer;
    guint cores, i;

    cores = get_core_count();
    cpu_dev_ids = malloc(cores * sizeof (gshort));
    memset(cpu_dev_ids, -1, cores * sizeof (gshort));

    dir = g_dir_open(SYSFS_DIR_CPUS, 0, NULL);
    if (dir) {
        while ((entry = g_dir_read_name(dir))) {
            if (sscanf(entry, "cpu%hd", &cpuid) != 1) {
                continue;
            }

            filename = g_build_filename(SYSFS_DIR_CPUS, entry, "topology", "core_id", NULL);
            if (g_file_get_contents(filename, &buffer, NULL, NULL)) {
                coreid = (gshort) atoi(buffer);

                if (coreid < cores && cpu_dev_ids[coreid] == -1) {
                    cpu_dev_ids[coreid] = cpuid;
                }
            }

            g_free(filename);
            g_free(buffer);
        }
    }

    for (i = 0; i < cores; i++) {
        if (cpu_dev_ids[i] < 0) {
            cpu_dev_ids[i] = i;
        }
    }

    return cpu_dev_ids;
}