Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite mlockall logic? #22

Closed
hakavlad opened this issue Mar 15, 2019 · 8 comments
Closed

Rewrite mlockall logic? #22

hakavlad opened this issue Mar 15, 2019 · 8 comments

Comments

@hakavlad
Copy link

hakavlad commented Mar 15, 2019

  1. Use /dev/shm instead of /tmp.
    /dev/shm always uses tmpfs.

  2. Use mlockall() (works with python3):

from ctypes import CDLL

def mlockall():
    """Lock all memory to prevent swapping process."""

    MCL_CURRENT = 1
    MCL_FUTURE = 2
    MCL_ONFAULT = 4

    libc = CDLL('libc.so.6', use_errno=True)

    result = libc.mlockall(
        MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT
    )
    if result != 0:
        result = libc.mlockall(
            MCL_CURRENT | MCL_FUTURE
        )
        if result != 0:
            print('Cannot lock all memory')
        else:
            print('All memory locked with MCL_CURRENT | MCL_FUTURE')
    else:
        print('All memory locked with MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT')

mlockall()
@tobixen
Copy link
Owner

tobixen commented Mar 17, 2019

I've moved the first one to a separate issue.

The second suggestion, I'm not sure I understand it. Rewritten slightly, this is the gist of your suggestion:

from ctypes import CDLL
CDLL('libc.so.6', use_errno=True).mlockall(3)

While this is roughly the logic as it is in the code today:

from ctypes import cdll
cdll.LoadLibrary('libc.so.6').mlockall(3)

both approaches seems to work both under python2 and python3. Both yields 0 for me when I run it as root, and -1 when I run it as an ordinary user.

@tobixen tobixen changed the title Suggestions Rewrite mlockall logic? Mar 17, 2019
@hakavlad
Copy link
Author

hakavlad commented Mar 17, 2019

mlockall(3) is not good in Fedora 29, for example: the daemon will use more then 200 MiB VmRSS with mlockall(3). We should use mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT).
Same problem: golang/go#28114.

MCL_ONFAULT appeared in Linux 4.4.

@hakavlad
Copy link
Author

MCL_ONFAULT reduces memory usage on modern systems.

@hakavlad
Copy link
Author

status with MCL_ONFAULT on Debian 9:

cat /proc/21763/status
Name:	thrash-protect.
Umask:	0022
State:	S (sleeping)
Tgid:	21763
Ngid:	0
Pid:	21763
PPid:	21762
TracerPid:	0
Uid:	0	0	0	0
Gid:	0	0	0	0
FDSize:	64
Groups:	0 
NStgid:	21763
NSpid:	21763
NSpgid:	21762
NSsid:	21718
VmPeak:	   39480 kB
VmSize:	   39480 kB
VmLck:	   39464 kB
VmPin:	       0 kB
VmHWM:	   12276 kB
VmRSS:	   12140 kB

Without MCL_ONFAULT:

Name:	thrash-protect.
Umask:	0022
State:	S (sleeping)
Tgid:	21793
Ngid:	0
Pid:	21793
PPid:	21792
TracerPid:	0
Uid:	0	0	0	0
Gid:	0	0	0	0
FDSize:	64
Groups:	0 
NStgid:	21793
NSpid:	21793
NSpgid:	21792
NSsid:	21718
VmPeak:	   39480 kB
VmSize:	   39480 kB
VmLck:	   39464 kB
VmPin:	       0 kB
VmHWM:	   15372 kB
VmRSS:	   15372 kB

@hakavlad
Copy link
Author

hakavlad commented Mar 17, 2019

thrash-protect's VmRSS on Fedora 29: 222 MiB! Using MCL_ONFAULT fixes the problem.

@hakavlad
Copy link
Author

IMHO thrash-protect can be optimized to use only 10-12 MiB RSS.

@tobixen
Copy link
Owner

tobixen commented Mar 17, 2019

Is this sufficient?

@hakavlad
Copy link
Author

hakavlad commented Mar 18, 2019

Excellent, thanks.
Now VmRSS is 12.5 MiB on Fedora 29.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants