0

I am working on an embedded device with only 512MB of RAM and the device is running Linux kernel. I want to do the memory management of all the processes running in the userspace by my own library. is it possible to do so. from my understanding, the memory management is done by kernel, Is it possible to have that functionality in User space.

jsaji
  • 900
  • 1
  • 15
  • 31
  • 1
    I don't follow your question, if you just use vmstat command that will display the entire memory, page info. You don't have to do anything for the memory management. Kernel will handle and make sure your application is not leaking the memory. – danglingpointer May 26 '17 at 07:53
  • 1
    What kind of issues are you trying to fix ? – Ôrel May 26 '17 at 07:55
  • I don't want kernel to do the memory management, Instead, My library should do the memory management. – jsaji May 26 '17 at 07:57
  • @Ôrel I am trying to come up with an SDK for Linux based embedded devices. If this is possible, I could virtually divide the memory into different segment and limit each application to use memory from corresponding portions. – jsaji May 26 '17 at 08:00
  • 1
    Your trying to achieve something is not possible. You want to change the core feature that is memory management, handled by Kernel. It's not the easy work. If you want to keep monitor of memory usage of the system my application process then there are several utils available to do. – danglingpointer May 26 '17 at 08:04
  • `man setrlimit` will help you – Ôrel May 26 '17 at 08:10
  • or more details here http://coldattic.info/shvedsky/pro/blogs/a-foo-walks-into-a-bar/posts/40 – Ôrel May 26 '17 at 08:11

3 Answers3

1

If your embedded device runs Linux, it has an MMU. Controling the MMU is normally a privileged operation, so only an operating system kernel has access to it. Therefore the answer is: No, you can't.

Of course you can write software running directly on the device, without operating system, but I guess that's not what you wanted. You should probably take one step back, ask yourself what gave you the idea about the memory management and what could be a better way to solve this original problem.

0

You can consider using setrlimit. Refer another Q&A.

I wrote the test code and run it on my PC. I can see that memory usage is limited. The exact relationship of units requires further analysis.

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>

int main(int argc, char* argv)
{
    long limitSize =      1;
    long testSize  = 140000;

    // 1. BEFORE: getrlimit
    {
        struct rlimit asLimit;
        getrlimit(RLIMIT_AS, &asLimit);
        printf("BEFORE: rlimit(RLIMIT_AS) = %ld,%ld\n", asLimit.rlim_cur, asLimit.rlim_max);
    }

    // 2. BEFORE: test malloc
    {
        char *xx = malloc(testSize);
        if (xx == NULL) 
            perror("malloc FAIL");
        else
            printf("malloc(%ld) OK\n", testSize);
        free(xx);
    }

    // 3. setrlimit
    {
        struct rlimit new;
        new.rlim_cur = limitSize; 
        new.rlim_max = limitSize;
        setrlimit(RLIMIT_AS, &new);
    }

    // 4. AFTER: getrlimit
    {
        struct rlimit asLimit;
        getrlimit(RLIMIT_AS, &asLimit);
        printf("AFTER: rlimit(RLIMIT_AS) = %ld,%ld\n", asLimit.rlim_cur, asLimit.rlim_max);
    }

    // 5. AFTER: test malloc
    {
        char *xx = malloc(testSize);
        if (xx == NULL) 
            perror("malloc FAIL");
        else
            printf("malloc(%ld) OK\n", testSize);
        free(xx);
    }

    return 0;
}

Result:

BEFORE: rlimit(RLIMIT_AS) = -1,-1
malloc(140000) OK
AFTER: rlimit(RLIMIT_AS) = 1,1
malloc FAIL: Cannot allocate memory
thatseeyou
  • 1,822
  • 13
  • 10
0

From what I understand of your question, you want to somehow use your own library for handling memory of kernel processes. I presume you are doing this to make sure that rogue processes don't use too much memory, which allows your process to use as much memory as is available. I believe this idea is flawed.

For example, imagine this scenario:

  • Total memory 512MB
  • Process 1 limit of 128MB - Uses 64MB
  • Process 2 imit of 128MB - Uses 64MB
  • Process 3 limit of 256MB - Uses 256MB then runs out of memory, when in fact 128MB is still available.

I know you THINK this is the answer to your problem, and on 'normal' embedded systems, this would probably work, but you are using a complex kernel, running processes you don't have total control over. You should write YOUR software to be robust when memory gets tight because that is all you can control.

Neil
  • 11,059
  • 3
  • 31
  • 56