Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Heap Allocation/Deallocation Statistics On QNX 7.0: (13 Items)
   
Heap Allocation/Deallocation Statistics On QNX 7.0  
Hey all, thanks for taking the time to read through.

My problem is the following: 
I have a QNX process that I've created that keeps track of some system performance statistics such as CPU usage for 
various processes, threads, etc. I'd also like to keep track of the number of heap allocations/deallocations that are 
used within different system processes (ie calls to malloc, free, etc); and I'd like to do this programatically, either 
by means of ioctl/devct or any other means you can think of. I'm currently looking into the following devctl commands:

devctl(fd, DCMD_PROC_MAPINFO, mbuffers, sizeof(mbuffers), &num_segments);
devctl(fd, DCMD_PROC_PAGEDATA, mbuffers, sizeof(mbuffers), &num_segments);

which return some number of memory segments associated with the process, including stack, data, shared object memory 
spaces, etc. 

I'm not sure if this is the correct approach, and any help/guidance is greatly appreciated. For some more context I am 
using QNX 7.0
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Heap allocations and mappings are two different things, so these devctl()s will not tell you what you want to know (or 
at least said you want to know).
Every address space is composed of a set of regions that are created by mmap() calls. There are regions for code, for 
various data segments, shared objects, mapped files, etc. The process heap (the one from which memory is malloc()ed) is 
just a user-space allocator that uses mapped memory but further divides it into chunks as requested by the caller. Most 
calls to malloc(), free() et al do not go through the operating system.
The first question you need to answer is whether you are really interested in knowing what processes do in terms of heap
 calls, or what they do in terms of system memory usage.

--Elad
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Hey Elad, thanks for your reply and for the clarifications. 

I am interested in the following: either the number of calls to malloc() / free(), or the actual amount of memory 
dynamically allocated by a certain process. There are QNX tools (for example: https://www.qnx.com/developers/docs/6.4.0/
ide_en/user_guide/memory.html#allocations) that provide this functionality, but as you know this requires using some 
external tools/IDEs. I'd like to implement some subset of these metrics along with a QNX process programatically, let's 
call it memory monitor. What is your recommended approach? Again thank you for your time.
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
That's the incorrect version of the docs.
see here
http://www.qnx.com/developers/docs/7.0.0/#com.qnx.doc.ide.userguide/topic/malloc_information.html

The only 3 readily accessible methods are 
1. to use momentics system information->malloc information.
2. http://www.qnx.com/developers/docs/7.0.0/#com.qnx.doc.neutrino.lib_ref/topic/m/mallopt.html#
mallopt__mallopt_MALLOC_STATS
3. attach to the process with gdb and print _malloc_stats

Doing it on target from outside the process isn't currently supported.
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Hi John,
When you say "Doing it on target from outside the process isn't currently supported", how does qconn obtain that 
information to pass it to the IDE System Information Perspective?

Regards,
-Al
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
It walks the dynamic symbol  in the process remotely to find the symbol for _malloc_stats.
Then reads the contents.
doing this over the procfs. effectively the same way that gdb does.
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
How do you recommend finding the malloc_stats symbol via procfs? Seems like mallopt(...) provides a system-wide 
allocation statistics. I'm looking for a per-process malloc_stats.
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
No that's per process it's the same info that momentics displays.

As for finding the symbols... 
That's involved. 
typically it looks something like.
look in /proc/<pid>/pmap for MAP_ELF PROT_EXEC entries.
Then the start of that mapping should be an elf header.
You can access that memory via /proc/<pid>/as where the file offset is the vaddr in the process aspace.

Now you need code that can walk the dynamic symbol table, without using sections.
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Hi John,

If I understood you correctly, calls to mallopt(MALLOC_STATS, ...) return statistics for the current process, in which 
this call is made. Without having to traverse the symbols (as you described earlier), is there another way to extract 
the malloc stats from other processes from one central "memory monitor" process?

Thanks again for your time
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Aside from the methods I already mentioned, no straightforward ways.


I guess you could use a preload lib to either periodically dump the values or possible just dump the vaddr of the 
structure in memory on process start. Your program could then recover and use those values.
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Taking a shot in the dark here,

devctl(fd, DCMD_PROC_MAPDEBUG_BASE, ...) gets me the base address of the executable address space, which you mentioned 
earlier. 

Is this a good starting point for symbol traversal? In theory, malloc_stats symbol should be somewhere in that 
addressable space correct? I'm thinking about just starting at that addr and dumping everything until I find something 
useful? Heh.. might take a bit though, should I pursue this or totally foolish?

Thanks again!
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
Google is your friend.

"Locating ELF shared library exports at runtime"
Re: Heap Allocation/Deallocation Statistics On QNX 7.0  
I'll take a look, thanks for all the suggestions, really appreciate it