Aryaman Gupta
Aryaman Gupta

Reputation: 642

Differences between M_CACHE, M_DEVBUF, M_TEMP

I am trying to understand the difference in these three different memories. Code comments in kern_malloc.c

/*
 * Centrally define some common malloc types.
 */
MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");

What can be the major difference between the three memories in terms of allocation and deallocation and management?

Upvotes: 0

Views: 248

Answers (1)

josaphatv
josaphatv

Reputation: 643

The MALLOC_DECLARE and MALLOC_DEFINE macros are used to create malloc_type structures. These malloc_type structures are passed to the various kernel-level memory management functions for the purpose of gathering useful statistics about how the kernel memory is being used.

For example, when you use malloc(sizeof(my_type), M_DEVBUF, M_WAITOK) to allocate some memory, you can see information about this and all other allocations which pass along the M_DEVBUF type using vmstat -m. On my system:

$ vmstat -m | head -n 1 && vmstat -m | grep "devbuf"

  Type InUse MemUse HighUse Requests  Size(s)
devbuf 16806 34311K       -    16837  16,32,64,128,256,512,1024,2048,4096,8192,32768,65536

You can make your own malloc_types to track your own kernel level allocations.

MALLOC_DEFINE(M_MYDRIVER, "mydriver", "buffers used by my driver");

void d_foo() {
  void * x = malloc(sizeof(foo), M_MYDRIVER, M_WAITOK);
}
$ vmstat -m | head -n 1 && vmstat -m | grep "mydriver"
    Type InUse MemUse HighUse Requests  Size(s)
mydriver     1     4K       -        1

The man page for the kernel memory management functions is accessible via $ man 9 malloc.

Upvotes: 1

Related Questions