3 #include <linux/bitmap.h>
8 struct rb_node rb_node
;
14 static void phys_entry__insert(struct phys_entry
*entry
, struct rb_root
*root
)
16 struct rb_node
**p
= &root
->rb_node
;
17 struct rb_node
*parent
= NULL
;
22 e
= rb_entry(parent
, struct phys_entry
, rb_node
);
24 if (entry
->start
< e
->start
)
30 rb_link_node(&entry
->rb_node
, parent
, p
);
31 rb_insert_color(&entry
->rb_node
, root
);
35 phys_entry__init(struct phys_entry
*entry
, u64 start
, u64 bsize
, u64 node
)
38 entry
->end
= start
+ bsize
;
40 RB_CLEAR_NODE(&entry
->rb_node
);
43 int mem2node__init(struct mem2node
*map
, struct perf_env
*env
)
45 struct memory_node
*n
, *nodes
= &env
->memory_nodes
[0];
46 struct phys_entry
*entries
, *tmp_entries
;
47 u64 bsize
= env
->memory_bsize
;
48 int i
, j
= 0, max
= 0;
50 memset(map
, 0x0, sizeof(*map
));
53 for (i
= 0; i
< env
->nr_memory_nodes
; i
++) {
55 max
+= bitmap_weight(n
->set
, n
->size
);
58 entries
= zalloc(sizeof(*entries
) * max
);
62 for (i
= 0; i
< env
->nr_memory_nodes
; i
++) {
67 for (bit
= 0; bit
< n
->size
; bit
++) {
70 if (!test_bit(bit
, n
->set
))
76 * Merge nearby areas, we walk in order
77 * through the bitmap, so no need to sort.
80 struct phys_entry
*prev
= &entries
[j
- 1];
82 if ((prev
->end
== start
) &&
83 (prev
->node
== n
->node
)) {
89 phys_entry__init(&entries
[j
++], start
, bsize
, n
->node
);
93 /* Cut unused entries, due to merging. */
94 tmp_entries
= realloc(entries
, sizeof(*entries
) * j
);
96 entries
= tmp_entries
;
98 for (i
= 0; i
< j
; i
++) {
99 pr_debug("mem2node %03" PRIu64
" [0x%016" PRIx64
"-0x%016" PRIx64
"]\n",
100 entries
[i
].node
, entries
[i
].start
, entries
[i
].end
);
102 phys_entry__insert(&entries
[i
], &map
->root
);
105 map
->entries
= entries
;
109 void mem2node__exit(struct mem2node
*map
)
111 zfree(&map
->entries
);
114 int mem2node__node(struct mem2node
*map
, u64 addr
)
116 struct rb_node
**p
, *parent
= NULL
;
117 struct phys_entry
*entry
;
119 p
= &map
->root
.rb_node
;
122 entry
= rb_entry(parent
, struct phys_entry
, rb_node
);
123 if (addr
< entry
->start
)
125 else if (addr
>= entry
->end
)
133 return entry
? (int) entry
->node
: -1;