File: memory_matrix.c

package info (click to toggle)
memkind 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,508 kB
  • sloc: ansic: 72,572; cpp: 39,493; sh: 4,594; perl: 4,250; xml: 2,044; python: 1,753; makefile: 1,393; csh: 7
file content (124 lines) | stat: -rw-r--r-- 3,843 bytes parent folder | download
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
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (C) 2021 Intel Corporation. */

#include "config.h"
#include <stdio.h>

#ifdef MEMKIND_HWLOC
#include "hwloc.h"
#include "numa.h"

static struct bitmask *node_cpumask;
static hwloc_topology_t topology;

static void print_attr(hwloc_memattr_id_t mem_attr)
{
    int max_node_id = numa_max_node();
    int init_id, target_id;

    if (mem_attr == HWLOC_MEMATTR_ID_BANDWIDTH) {
        printf("Node bandwidth [MiB/s]:\n");
    } else if (mem_attr == HWLOC_MEMATTR_ID_LATENCY) {
        printf("Node latency [ns]:\n");
    } else {
        printf("[ERROR] Unknown memory attribute\n");
        numa_free_cpumask(node_cpumask);
        hwloc_topology_destroy(topology);
        exit(-1);
    }

    printf("node");
    for (init_id = 0; init_id <= max_node_id; ++init_id) {
        if (numa_bitmask_isbitset(numa_all_nodes_ptr, init_id)) {
            printf("%6d ", init_id);
        }
    }
    printf("\n");
    for (init_id = 0; init_id <= max_node_id; ++init_id) {
        if (numa_bitmask_isbitset(numa_all_nodes_ptr, init_id)) {
            printf("%3d:", init_id);
            if (numa_node_to_cpus(init_id, node_cpumask)) {
                printf("[ERROR] numa_node_to_cpus\n");
                numa_free_cpumask(node_cpumask);
                hwloc_topology_destroy(topology);
                exit(-1);
            }
            if (numa_bitmask_weight(node_cpumask) == 0) {
                for (target_id = 0; target_id <= max_node_id; ++target_id) {
                    if (numa_bitmask_isbitset(numa_all_nodes_ptr, target_id)) {
                        printf("%6s ", "-");
                    }
                }
            } else {
                hwloc_uint64_t attr_val;
                struct hwloc_location initiator;
                hwloc_obj_t init_node =
                    hwloc_get_numanode_obj_by_os_index(topology, init_id);
                initiator.type = HWLOC_LOCATION_TYPE_CPUSET;
                initiator.location.cpuset = init_node->cpuset;
                for (target_id = 0; target_id <= max_node_id; ++target_id) {
                    if (numa_bitmask_isbitset(numa_all_nodes_ptr, target_id)) {
                        hwloc_obj_t target_node =
                            hwloc_get_numanode_obj_by_os_index(topology,
                                                               target_id);
                        int err = hwloc_memattr_get_value(
                            topology, mem_attr, target_node, &initiator, 0,
                            &attr_val);
                        if (err) {
                            printf("%6s ", "X");
                        } else {
                            printf("%6lu ", attr_val);
                        }
                    }
                }
            }
            printf("\n");
        }
    }
}

static int print_all(void)
{
    int err = numa_available();
    if (err == -1) {
        printf("[ERROR] NUMA not available\n");
        return -1;
    }
    err = hwloc_topology_init(&topology);
    if (err) {
        printf("[ERROR] hwloc initialization failed\n");
        return -1;
    }
    err = hwloc_topology_load(topology);
    if (err) {
        printf("[ERROR] hwloc topology load failed\n");
        hwloc_topology_destroy(topology);
        return -1;
    }
    node_cpumask = numa_allocate_cpumask();
    if (!node_cpumask) {
        printf("[ERROR] numa_allocate_cpumask\n");
        hwloc_topology_destroy(topology);
        return -1;
    }

    print_attr(HWLOC_MEMATTR_ID_BANDWIDTH);
    print_attr(HWLOC_MEMATTR_ID_LATENCY);

    numa_free_cpumask(node_cpumask);

    hwloc_topology_destroy(topology);
    return 0;
}
#else
static int print_all(void)
{
    printf("[ERROR] Libhwloc is not supported\n");
    return -1;
}
#endif

int main()
{
    return print_all();
}