Thread memory allocation

I am working on a tracing tool for multi-threaded applications, especially about memory allocation.

I would like to highlight the flow distribution. I know that when a thread executes malloc, the memory used is a global heap. I would like to track which thread allocated how much memory.

I wrapped on malloc, incrementing the values ​​every time there is malloc as:

void *mymalloc(size_t size) {
    mem_used[thread_id] += size;
    return malloc(size);
}

It works well. The problem is a method freethat does not return the amount of freed memory.

Do not take my decision into account, it is just to show what I tried.

EDIT:

As mentioned above, saving my table is too hard.

+3
source share
3

mymalloc:

int* mymem = malloc(size + sizeof(int)*2);
mymem[0] = thread_id;
mymem[1] = size;
mem_used[thread_id] += size;
return &mymem[2].

myfree (void * mem) :

void myfree(void* mem)
{
    int* imem = (int*)(mem - sizeof(int)*2);
    int thread_id = imem[0];
    int size = imem[1];
    mem_used[thread_id] -= size;
    free(imem);
}

, , ...

+8

, ( , , ), :

  • ThreadID ()

malloc , , .

? .

+1

. :

  • , .
  • my_malloc size.
  • Create your own wrapper for freeby subtracting size(which you extract by looking at the pointer value) for this stream.
0
source

Source: https://habr.com/ru/post/1706956/


All Articles