memory: extend "info mtree" with flat view dump
v2 cover memory: extend "info mtree" with flat view dump
v2:
- fix a size error in patch 2
- add r-b for Marc-André in patch 1
Each address space has its own flatview. It's another way to observe memory info besides the default memory region hierachy, for example, if we want to know which memory region will handle the write to specific address, a flatview will suite more here than the default hierachical dump.
I used it to debug a vt-d memory region overlap issue. Do we need this? I think we can at least consider patch 1, which is a cleanup of existing codes. :)
Please review. Thanks,
Peter Xu (2):
memory: provide common macros for mtree_print_mr()
memory: hmp: dump flat view for 'info mtree'
memory.c | 65 +++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 19 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/2 memory: provide common macros for mtree_print_mr()
mtree_print_mr() has some common codes. Generalize it.
--- memory.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-)
memory.c +15 -19
@@ -2450,6 +2450,13 @@ struct MemoryRegionList { typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead; +#define MR_CHAR_RD (mr) ((mr)->romd_mode ? 'R' : '-') +#define MR_CHAR_WR (mr) (!(mr)->readonly && !((mr)->rom_device && \ + (mr)->romd_mode) ? 'W' : '-') +#define MR_SIZE (size) (int128_nz(size) ? (hwaddr)int128_get64( \ + int128_sub((size), int128_one())) : 0) +#define MTREE_INDENT " " + static void mtree_print_mr(fprintf_function mon_printf, void *f, const MemoryRegion *mr, unsigned int level, hwaddr base,
@@ -2465,7 +2472,7 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f, }for (i =0 ; i < level; i++) { -mon_printf (f," " ); +mon_printf (f,MTREE_INDENT ); }if (mr->alias) {
@@ -2488,34 +2495,23 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f," (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx"-" TARGET_FMT_plx"%s\n" , base + mr->addr, - base + mr->addr - + (int128_nz (mr->size) ? - (hwaddr )int128_get64 (int128_sub (mr->size, -int128_one ())) :0 ), + base + mr->addr +MR_SIZE (mr->size), mr->priority, - mr->romd_mode ?'R' :'-' , - !mr->readonly && !(mr->rom_device && mr->romd_mode) ?'W' - :'-' , +MR_CHAR_RD (mr), +MR_CHAR_WR (mr),memory_region_name (mr),memory_region_name (mr->alias), mr->alias_offset, - mr->alias_offset - + (int128_nz (mr->size) ? - (hwaddr )int128_get64 (int128_sub (mr->size, -int128_one ())) :0 ), + mr->alias_offset +MR_SIZE (mr->size), mr->enabled ? "" : " [disabled]"); }else {mon_printf (f, TARGET_FMT_plx"-" TARGET_FMT_plx" (prio %d, %c%c): %s%s\n" , base + mr->addr, - base + mr->addr - + (int128_nz (mr->size) ? - (hwaddr )int128_get64 (int128_sub (mr->size, -int128_one ())) :0 ), + base + mr->addr +MR_SIZE (mr->size), mr->priority, - mr->romd_mode ?'R' :'-' , - !mr->readonly && !(mr->rom_device && mr->romd_mode) ?'W' - :'-' , +MR_CHAR_RD (mr), +MR_CHAR_WR (mr),memory_region_name (mr), mr->enabled ?"" :" [disabled]" ); }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 1/2] memory: provide common macros for mtree_print_mr()
memory.c static void mtree_print_mr(fprintf_function mon_printf, void *f, · line 2501
Quoted 5 lines · Peter Xu
memory.c +2
-3- mr->romd_mode ?'R' :'-' , - !mr->readonly && !(mr->rom_device && mr->romd_mode) ?'W' - : '-', +MR_CHAR_RD (mr), + MR_CHAR_WR(mr),
An alternative definition could be
memory_access_is_direct(mr, false) ? 'R' : '-' memory_access_is_direct(mr, true) ? 'W' : '-'
for MR_CHAR_RD and MR_CHAR_WR. With this change, I think the small code duplication in the "? :" operator is tolerable and the code is clearer.
Paolo
Re: [Qemu-devel] [PATCH v2 1/2] memory: provide common macros for mtree_print_mr()
Quoted 9 lines · Paolo Bonzini
Review
Quoted 5 lines · Peter Xu
(quoted diff) +2
-3- mr->romd_mode ? 'R' : '-', - !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' - : '-', + MR_CHAR_RD(mr), + MR_CHAR_WR(mr),
Quoted 7 lines
An alternative definition could be
memory_access_is_direct(mr, false) ? 'R' : '-' memory_access_is_direct(mr, true) ? 'W' : '-'for
MR_CHAR_RDandMR_CHAR_WR. With this change, I think the small code duplication in the "? :" operator is tolerable and the code is clearer.
memory_access_is_direct() will check against whether mr is RAM, is that what we want here? In that case we'll get most of the regions as "--" as long as they are not RAM, while in fact IMHO we should want to know the rw permission for all cases.
How about I add one more patch at the beginning to provide some more memory_region_is_*() helpers (meanwhile refactor memory_access_is_direct() a bit), like:
--------8<--------
include/exec/memory.h +17 -5
@@ -1619,14 +1619,27 @@ MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, uint8_t *buf, int len);void *qemu_map_ram_ptr (RAMBlock *ram_block,ram_addr_t addr); +static inline bool memory_region_is_readable (MemoryRegion *mr) +{ +return mr->rom_device ? mr->romd_mode : true; +} + +static inline bool memory_region_is_writable (MemoryRegion *mr) +{ +return !mr->rom_device && !mr->readonly; +} + +static inline bool memory_region_is_direct (MemoryRegion *mr) +{ +return memory_region_is_ram (mr) && !memory_region_is_ram_device (mr); +} +static inline bool memory_access_is_direct (MemoryRegion *mr,bool is_write) {if (is_write) { -return memory_region_is_ram (mr) && - !mr->readonly && !memory_region_is_ram_device (mr); +return memory_region_is_direct (mr) &&memory_region_is_writable (mr); }else { -return (memory_region_is_ram (mr) && !memory_region_is_ram_device (mr)) || -memory_region_is_romd (mr); +return memory_region_is_direct (mr) &&memory_region_is_readable (mr); } } -------->8--------
Then, I can throw away MR_CHAR_* macros and use:
memory_access_is_readable(mr, false) ? 'R' : '-'
memory_access_is_writable(mr, true) ? 'W' : '-'Do you like this approach?
-- peterx
Re: [Qemu-devel] [PATCH v2 1/2] memory: provide common macros for mtree_print_mr()
Quoted 15 lines · Peter Xu
Quoted 14 lines · Paolo Bonzini
Review
Quoted 5 lines · Peter Xu
(quoted diff) +2
-3- mr->romd_mode ? 'R' : '-', - !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' - : '-', + MR_CHAR_RD(mr), + MR_CHAR_WR(mr),An alternative definition could be
memory_access_is_direct(mr, false) ? 'R' : '-' memory_access_is_direct(mr, true) ? 'W' : '-'
Quoted 7 lines
Quoted 2 lines
for
MR_CHAR_RDandMR_CHAR_WR. With this change, I think the small code duplication in the "? :" operator is tolerable and the code is clearer.
memory_access_is_direct()will check against whether mr is RAM, is that what we want here? In that case we'll get most of the regions as "--" as long as they are not RAM, while in fact IMHO we should want to know the rw permission for all cases.
Indeed. My idea was that the RW permission is not well defined for non-RAM memory regions, and ROMD regions in MMIO mode shows as "--" while MMIO regions show as "RW". But perhaps it's confusing.
What about writing one of "ram", "rom", "ramd", "romd", "i/o" (with I/O also covering rom_device && !romd_mode)?
If you disagree, the below patch looks good.
Paolo
Quoted 48 lines
--------8<--------
include/exec/memory.h +17
-5@@ -1619,14 +1619,27 @@ MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, uint8_t *buf, int len);void *qemu_map_ram_ptr (RAMBlock *ram_block,ram_addr_t addr); +static inline bool memory_region_is_readable (MemoryRegion *mr) +{ +return mr->rom_device ? mr->romd_mode : true; +} + +static inline bool memory_region_is_writable (MemoryRegion *mr) +{ +return !mr->rom_device && !mr->readonly; +} + +static inline bool memory_region_is_direct (MemoryRegion *mr) +{ +return memory_region_is_ram (mr) && !memory_region_is_ram_device (mr); +} +static inline bool memory_access_is_direct (MemoryRegion *mr,bool is_write) {if (is_write) { -return memory_region_is_ram (mr) && - !mr->readonly && !memory_region_is_ram_device (mr); +return memory_region_is_direct (mr) &&memory_region_is_writable (mr); }else { -return (memory_region_is_ram (mr) && !memory_region_is_ram_device (mr)) || -memory_region_is_romd (mr); +return memory_region_is_direct (mr) &&memory_region_is_readable (mr); } } -------->8--------Then, I can throw away MR_CHAR_* macros and use:
memory_access_is_readable(mr, false) ? 'R' : '-' memory_access_is_writable(mr, true) ? 'W' : '-'Do you like this approach?
-- peterx
Re: [Qemu-devel] [PATCH v2 1/2] memory: provide common macros for mtree_print_mr()
Quoted 26 lines · Paolo Bonzini
Quoted 15 lines · Peter Xu
Quoted 14 lines · Paolo Bonzini
Review
Quoted 5 lines · Peter Xu
(quoted diff) +2
-3- mr->romd_mode ? 'R' : '-', - !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' - : '-', + MR_CHAR_RD(mr), + MR_CHAR_WR(mr),An alternative definition could be
memory_access_is_direct(mr, false) ? 'R' : '-' memory_access_is_direct(mr, true) ? 'W' : '-'Quoted 7 lines
Quoted 2 lines
for
MR_CHAR_RDandMR_CHAR_WR. With this change, I think the small code duplication in the "? :" operator is tolerable and the code is clearer.
memory_access_is_direct()will check against whether mr is RAM, is that what we want here? In that case we'll get most of the regions as "--" as long as they are not RAM, while in fact IMHO we should want to know the rw permission for all cases.
Quoted 8 lines
Indeed. My idea was that the RW permission is not well defined for non-RAM memory regions, and ROMD regions in MMIO mode shows as "--" while MMIO regions show as "RW". But perhaps it's confusing.
What about writing one of "ram", "rom", "ramd", "romd", "i/o" (with I/O also covering
rom_device&& !romd_mode)?If you disagree, the below patch looks good.
Yes, above suggestion makes sense to me, since after all the RW permissions are derived from the type of memory regions, and the type itself tells more things than the RW bits. So I totally agree we can replace the "RW" chars with its type directly (if no one else disagree, of course).
While for below patch, do you want me to include it as well as a standalone patch, for the purpose of refactoring memory_access_is_direct()? Since IMHO it's tiny clearer and more readable than before.
Thanks!
Quoted 43 lines
Paolo
Quoted 40 lines
--------8<--------
include/exec/memory.h +17
-5@@ -1619,14 +1619,27 @@ MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, uint8_t *buf, int len);void *qemu_map_ram_ptr (RAMBlock *ram_block,ram_addr_t addr); +static inline bool memory_region_is_readable (MemoryRegion *mr) +{ +return mr->rom_device ? mr->romd_mode : true; +} + +static inline bool memory_region_is_writable (MemoryRegion *mr) +{ +return !mr->rom_device && !mr->readonly; +} + +static inline bool memory_region_is_direct (MemoryRegion *mr) +{ +return memory_region_is_ram (mr) && !memory_region_is_ram_device (mr); +} +static inline bool memory_access_is_direct (MemoryRegion *mr,bool is_write) {if (is_write) { -return memory_region_is_ram (mr) && - !mr->readonly && !memory_region_is_ram_device (mr); +return memory_region_is_direct (mr) &&memory_region_is_writable (mr); }else { -return (memory_region_is_ram (mr) && !memory_region_is_ram_device (mr)) || -memory_region_is_romd (mr); +return memory_region_is_direct (mr) &&memory_region_is_readable (mr); } } -------->8--------Then, I can throw away MR_CHAR_* macros and use:
Quoted 8 lines
Quoted 8 lines
memory_access_is_readable(mr, false) ? 'R' : '-' memory_access_is_writable(mr, true) ? 'W' : '-'Do you like this approach?
-- peterx
-- peterx
Re: [Qemu-devel] [PATCH v2 1/2] memory: provide common macros for mtree_print_mr()
Quoted 10 lines · Peter Xu
Yes, above suggestion makes sense to me, since after all the RW permissions are derived from the type of memory regions, and the type itself tells more things than the RW bits. So I totally agree we can replace the "RW" chars with its type directly (if no one else disagree, of course).
While for below patch, do you want me to include it as well as a standalone patch, for the purpose of refactoring
memory_access_is_direct()? Since IMHO it's tiny clearer and more readable than before.
It is more readable, but my plan was to turn these fields into a single field (with bits) to speed up memory_access_is_direct. For that we'd need to undo your change. So I'm undecided.
Paolo
Re: [Qemu-devel] [PATCH v2 1/2] memory: provide common macros for mtree_print_mr()
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Peter Xu
Yes, above suggestion makes sense to me, since after all the RW permissions are derived from the type of memory regions, and the type itself tells more things than the RW bits. So I totally agree we can replace the "RW" chars with its type directly (if no one else disagree, of course).
Quoted 8 lines
Quoted 4 lines
While for below patch, do you want me to include it as well as a standalone patch, for the purpose of refactoring
memory_access_is_direct()? Since IMHO it's tiny clearer and more readable than before.It is more readable, but my plan was to turn these fields into a single field (with bits) to speed up
memory_access_is_direct. For that we'd need to undo your change. So I'm undecided.
No problem. Then I'll just ignore it and repost with above. Thanks!
-- peterx
PATCH v2 2/2 memory: hmp: dump flat view for 'info mtree'
In reply to Peter XuDumping flat view will be useful to debug the memory rendering logic, also it'll be much easier with it to know what memory region is handling what address range.
--- memory.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
memory.c +31 -0
@@ -2545,6 +2545,36 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f, } } +static void mtree_print_flatview (fprintf_function p,void *f, +AddressSpace *as) +{ +FlatView *view =address_space_get_flatview (as); +FlatRange *range = &view->ranges[0 ]; +MemoryRegion *mr; +int n = view->nr; + +if (n <=0 ) { +p (f,MTREE_INDENT "No rendered FlatView for " +"address space '%s'\n" , as->name); +return ; + } + +p (f,MTREE_INDENT "FlatView (address space '%s'):\n" , as->name); + +while (n--) { + mr = range->mr; + p(f, MTREE_INDENT MTREE_INDENT TARGET_FMT_plx "-" + TARGET_FMT_plx" (prio %d, %c%c): %s\n" , +int128_get64 (range->addr.start), +int128_get64 (range->addr.start) +MR_SIZE (range->addr.size), + mr->priority,MR_CHAR_RD (mr),MR_CHAR_WR (mr), +memory_region_name (mr)); + range++; + } + +flatview_unref (view); +} +void mtree_info (fprintf_function mon_printf,void *f) {MemoryRegionListHead ml_head;
@@ -2556,6 +2586,7 @@ void mtree_info(fprintf_function mon_printf, void *f) QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {mon_printf (f,"address-space: %s\n" , as->name);mtree_print_mr (mon_printf, f, as->root,1 ,0 , &ml_head); +mtree_print_flatview (mon_printf, f, as);mon_printf (f,"\n" ); }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v2 2/2] memory: hmp: dump flat view for 'info mtree'
Quoted 5 lines · Peter Xu
Dumping flat view will be useful to debug the memory rendering logic, also it'll be much easier with it to know what memory region is handling what address range.
Signed-off-by Peter Xu <[email protected]>
This is useful, but dumping both makes the output very long. What about adding a -f option to "info mtree"?
Paolo
Quoted 54 lines
--- memory.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)memory.c +31
-0@@ -2545,6 +2545,36 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f, } } +static void mtree_print_flatview (fprintf_function p,void *f, +AddressSpace *as) +{ +FlatView *view =address_space_get_flatview (as); +FlatRange *range = &view->ranges[0 ]; +MemoryRegion *mr; +int n = view->nr; + +if (n <=0 ) { +p (f,MTREE_INDENT "No rendered FlatView for " +"address space '%s'\n" , as->name); +return ; + } + +p (f,MTREE_INDENT "FlatView (address space '%s'):\n" , as->name); + +while (n--) { + mr = range->mr; + p(f, MTREE_INDENT MTREE_INDENT TARGET_FMT_plx "-" + TARGET_FMT_plx" (prio %d, %c%c): %s\n" , +int128_get64 (range->addr.start), +int128_get64 (range->addr.start) +MR_SIZE (range->addr.size), + mr->priority,MR_CHAR_RD (mr),MR_CHAR_WR (mr), +memory_region_name (mr)); + range++; + } + +flatview_unref (view); +} +void mtree_info (fprintf_function mon_printf,void *f) {MemoryRegionListHead ml_head;@@ -2556,6 +2586,7 @@ void mtree_info(fprintf_function mon_printf, void *f) QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {mon_printf (f,"address-space: %s\n" , as->name);mtree_print_mr (mon_printf, f, as->root,1 ,0 , &ml_head); +mtree_print_flatview (mon_printf, f, as);mon_printf (f,"\n" ); }
Re: [Qemu-devel] [PATCH v2 2/2] memory: hmp: dump flat view for 'info mtree'
Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · Peter Xu
Dumping flat view will be useful to debug the memory rendering logic, also it'll be much easier with it to know what memory region is handling what address range.
Signed-off-by Peter Xu <[email protected]>This is useful, but dumping both makes the output very long. What about adding a -f option to "info mtree"?
Sure. :)
After I confirm how I should improve on the first patch, I'll cook another version for the series with "-f".
Thanks,
-- peterx
Quoted 3 lines · Peter Xu
v2:
- fix a size error in patch 2
- add r-b for Marc-André in patch 1
Ping? :)
-- peterx