olealgoritme / gddr6

Linux​ based GDDR6/GDDR6X VRAM temperature reader for NVIDIA RTX 3000/4000 series GPUs.
79 stars 30 forks source link

unmap map_base after each iteration to prevent memory leak #8

Closed stevetracvc closed 1 year ago

stevetracvc commented 1 year ago

without calling munmap after each iteration, the virtual address for map_base changes on each iteration. Adding this unmap call allows the same address for map_base for each iteration.

stevetracvc commented 1 year ago

Since I'm in the process of figuring out the A5000 Mobile, I'm dumping a bunch of extra values to the screen. Without this patch, I get:

base_addr: b4000000 | bar0: b4000000Device: RTX A5000 Mobile GDDR6 (GA104 / 0x24b6) pci=1:0:0
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2483000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2456000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2455000 |   63°c | ( 14312, 55.91 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2454000 |   62°c | ( 14280, 55.78 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2453000 |   64°c | ( 14352, 56.06 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2452000 |   62°c | ( 14288, 55.81 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2451000 |   62°c | ( 14288, 55.81 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2450000 |   64°c | ( 14344, 56.03 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac244f000 |   62°c | ( 14296, 55.84 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac244e000 |   63°c | ( 14320, 55.94 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac244d000 |   64°c | ( 14344, 56.03 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac244c000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac244b000 |   62°c | ( 14280, 55.78 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac244a000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2449000 |   62°c | ( 14296, 55.84 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2448000 |   62°c | ( 14296, 55.84 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2447000 |   64°c | ( 14336, 56.00 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2446000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2445000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2444000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7fcac2443000 |   62°c | ( 14280, 55.78 )

Note how map_base keeps decreasing with every loop iteration. With this PR, map_base stays the same:

base_addr: b4000000 | bar0: b4000000Device: RTX A5000 Mobile GDDR6 (GA104 / 0x24b6) pci=1:0:0
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   60°c | ( 14232, 55.59 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   63°c | ( 14304, 55.88 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   61°c | ( 14256, 55.69 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   60°c | ( 14216, 55.53 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   62°c | ( 14296, 55.84 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   61°c | ( 14256, 55.69 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   61°c | ( 14240, 55.62 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   62°c | ( 14296, 55.84 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   60°c | ( 14232, 55.59 )
VRAM Temps: |custom offset: 204b8 | phys_addr: b40204b8 | base_offset: b4020000 | map_base: 0x7f4077e88000 |   60°c | ( 14232, 55.59 )
stevetracvc commented 1 year ago

Sorry, forgot to include how I was printing those:

printf("phys_addr: %x | base_offset: %x | map_base: %p | virt_addr: %p | ", phys_addr, base_offset, (void *)map_base, (void *) virt_addr);

just before printing the temperature

olealgoritme commented 1 year ago

Thank you for the PR.

The code is a bit messy now, as it mmaps() multiple times per iteration, depending on GPU count. Possibly a bit cleaner if the mapping is done only once. Based on this I created a test_mmap branch for you to test with.