Click
Loading...
Searching...
No Matches
numa.hh
1// -*- c-basic-offset: 4 -*-
2#ifndef CLICK_NUMA_HH
3#define CLICK_NUMA_HH
4
5#include <click/bitvector.hh>
6#include <fcntl.h>
7#include <unistd.h>
8
9extern "C" {
10#include <numa.h>
11}
12
13
14class NumaCpuBitmask {
15 NumaCpuBitmask() {
16 numa_available();
17 b = numa_allocate_cpumask();
18 };
19
20public:
21 ~NumaCpuBitmask() {
22 numa_free_cpumask(b);
23 }
24
25 struct bitmask* bitmask() {
26 return b;
27 }
28
29 bool isBitSet(unsigned int n) {
30 return numa_bitmask_isbitset(b,n);
31 }
32
33
34 static NumaCpuBitmask allocate() {
35 numa_available();
36 NumaCpuBitmask bcp;
37 return bcp;
38 }
39
40 NumaCpuBitmask& operator=(const NumaCpuBitmask& other) {
41 copy_bitmask_to_bitmask(other.b,b);
42 return *this;
43 }
44
45 NumaCpuBitmask& operator|=(const NumaCpuBitmask& bmp) {
46 bmp.print();
47 for (int i = 0; i < numa_num_configured_cpus(); i++)
48 if (bmp.get(i))
49 set(i);
50 return *this;
51 }
52
53 NumaCpuBitmask(const NumaCpuBitmask& other ) {
54 NumaCpuBitmask bcp = NumaCpuBitmask::allocate();
55 copy_bitmask_to_bitmask(other.b,bcp.b);
56 }
57
58
59 void set(int i) {
60 numa_bitmask_setbit(b,i);
61 }
62
63 bool get(int i) const {
64 return numa_bitmask_isbitset(b, i);
65 }
66
67 void print() const {
68 char s[numa_num_configured_cpus() + 1];
69 for (int i = 0; i < numa_num_configured_cpus(); i++) {
70 if (get(i))
71 s[i] = '1';
72 else
73 s[i] = '0';
74 }
75 s[numa_num_configured_cpus()] = '\0';
76 click_chatter("%d : %s",numa_num_configured_cpus(),s);
77 }
78
79 void toBitvector(Bitvector &b) {
80 if (numa_num_configured_cpus() > b.size())
81 b.resize(numa_num_configured_cpus());
82 for (int i = 0; i < numa_num_configured_cpus(); i++) {
83 if (get(i))
84 b[i] = 1;
85 }
86
87 }
88
89private:
90 struct bitmask* b;
91
92};
93
94class Numa {
95public:
96
97
98 static NumaCpuBitmask all_cpu() {
99 NumaCpuBitmask bcp = NumaCpuBitmask::allocate();
100 copy_bitmask_to_bitmask(numa_all_cpus_ptr,bcp.bitmask());
101 return bcp;
102 }
103
104 static NumaCpuBitmask node_to_cpus(int node) {
105 NumaCpuBitmask bcp = NumaCpuBitmask::allocate();
106 numa_node_to_cpus(node,bcp.bitmask());
107 return bcp;
108 }
109
110 static int get_max_cpus() {
111 return numa_num_configured_cpus();
112 }
113
114 static int get_max_numas() {
115 return numa_num_configured_nodes();
116 }
117
118 static int get_numa_node_of_cpu(int cpuid) {
119 return numa_node_of_cpu(cpuid);
120 }
121
122 static int get_device_node(const char* device) {
123
124 char path[100];
125 sprintf(path, "/sys/class/net/%s/device/numa_node", device);
126 int fd = open(path, O_RDONLY);
127 int i = read(fd, path,100);
128 if (i <= 0)
129 return 0;
130
131 path[i] = '\0';
132 sscanf(path,"%d",&i);
133 close(fd);
134 if (i == -1)
135 return 0;
136 return i;
137 }
138
139 static void print(Bitvector& b) {
140 char s[numa_num_configured_cpus() + 1];
141 for (int i = 0; i < numa_num_configured_cpus(); i++) {
142 if (b[i])
143 s[i] = '1';
144 else
145 s[i] = '0';
146 }
147 s[numa_num_configured_cpus()] = '\0';
148 click_chatter("%d : %s",numa_num_configured_cpus(),s);
149 }
150};
151
152#endif
Click's bitvector class.
Vector of bits.
Definition bitvector.hh:21
void resize(int n)
Resize the bitvector to n bits.
Definition bitvector.cc:70
int size() const
Return the number of bits in the bitvector.
Definition bitvector.hh:192