
madvise()系统掉用,用于向内核提供对于起始地址为 addr ,长度为 length 的内存空间的 *** 作建议或者指示。在大多数情况下,此类建议的目标是提高系统或者应用程序的性能。
最初,此系统调用,仅仅支持一组“常规的(conventional)”建议值,这些建议值在各种系统中也有实现,(但是请注意,POSIX中并没有指定madvise()),后来,又添加了许多特定于Linux的建议值。
常规建议值
以下建议值允许应用程序告诉内核,它期望如何使用某些映射或者共享内存区域,以便内核可以选择适当的预读以及缓存技术。这些建议并不会影响应用程序的寓意( MADV_DONTNEED 除外),但是可能会影响其性能。同样除 MADV_DONTNEED 之外,下列所有的建议值都与posix_madvise(3)函数类似,且具有相同的含义。
这些建议值,被指示为参数 advice ,参数值是以下之一:
Linux 特定建议值
以下是Linux下的特定建议值,在posix_madvise(3)中不存在对应项,并且在其他系统的实现中,可能有也可能没有对应项。同时,,其中一些 *** 作会更改内存访问的语义。
On success, madvise() returns zero. On error, it returns -1 and errno is set to indicate the error.
Since Linux 3.18, support for this system call is optional, depending on the setting of the CONFIG_ADVISE_SYSCALLS configuration option.
madvise() is not specified by any standards. Versions of this system call, implementing a wide variety of advice values, exist on many other implementations. Other implementations typically implement at least the flags listed above under Conventional advice flags, albeit with some variation in semantics.
POSIX.1-2001 describes posix_madvise(3) with constants POSIX_MADV_NORMAL, POSIX_MADV_RANDOM, POSIX_MADV_SEQUENTIAL, POSIX_MADV_WILLNEED, and POSIX_MADV_DONTNEED, and so on, with behavior close to the similarly named flags listed above.
Linux 实现要求地址 addr 是页对齐的,并允许长度为零。 如果指定地址范围的某些部分未映射,则 madvise() 的 Linux 版本将忽略它们并将调用应用于其余部分(但应从系统调用中返回 ENOMEM)。
第一个问题: Increased maximum number of open files to 10032 (it was originally set to 1024). 原因分析:即进程能打开的最大文件描述符太小了。系统默认设置的值一般是 1024 。 解决方法: 1.系统级别,即 linux 内核能分配的最大文件描述符数量(参考:https://www.kernel.org/doc/Documentation/sysctl/fs.txt)。 # vi /etc/sysctl.conf fs.file-max = 102400 # sysctl -p 查看方法: # cat /proc/sys/fs/file-max # sysctl fs.file-max 2.用户级别,即用户进程能打开的最大文件描述符数量(参考 man limits.conf 或者 https://linux.die.net/man/5/limits.conf)。 设置方法: # vi /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 查看方法(无需重启系统。当前用户重新登录后生效。或者切换到具体的用户,立刻生效): # su - username $ ulimit -Hn $ ulimit -Sn 注意: CentOS 还需开启 pam_limits 模块,因为 limits.conf 文件相当于 pam_limits 模块的配置文件。 # vi /etc/pam.d/login session required pam_limits.so 第二个问题: WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.原因分析:net.core.somaxconn 参数的值太小了。即全连接(即 accept 连接)的最大队列长度太小了。解决方法: # vi /etc/sysctl.conf net.core.somaxconn = 511 # sysctl -p 查看方法: # cat /proc/sys/net/core/somaxconn # sysctl net.core.somaxconn 第三个问题:WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.原因分析:vm.overcommit_memory 指的是进程申请的内存大小。当参数值为 0 时,如果一次性申请的内存大小超过了系统总内存,有可能被拒绝。当参数值为 0 时,有可能会导致 redis 执行 bgsave *** 作保存 rdb 文件失败。(参考:http://linuxperf.com/?p=102)。 解决方法: # vi /etc/sysctl.conf vm.overcommit_memory = 1 # sysctl -p 查看方法: # cat /proc/sys/vm/overcommit_memory # sysctl vm.overcommit_memory 第四个问题:WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never >/sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.原因分析:Transparent Huge Pages (THP) 开启后会使 redis 时延变大和造成内存使用问题(参考:https://redis.io/topics/latency)。 解决方法(CentOS 6.X): echo never >/sys/kernel/mm/transparent_hugepage/enabled # vim /etc/rc.local echo never >/sys/kernel/mm/transparent_hugepage/enabled 解决方法(CentOS 7.X): # vim /etc/systemd/system/disable-transparent-huge-pages.service [Unit] Description=Disable Transparent Huge Pages (THP) DefaultDependencies=no After=sysinit.target local-fs.target [Service] Type=oneshot ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled >/dev/null' [Install] WantedBy=basic.target # systemctl enable disable-transparent-huge-pages # systemctl start disable-transparent-huge-pages 查看方法: # cat /sys/kernel/mm/transparent_hugepage/enabled always madvise [never][root@localhost ~]# grep Huge /proc/meminfoAnonHugePages: 2048 kB
HugePages_Total: 0
HugePages_Free:0
HugePages_Rsvd:0
HugePages_Surp:0
Hugepagesize: 2048 kB
如果输出包含类似于"AnonHugepages: xxxx kB",并且AnonHugePages >0kB,则表明内核使用的是Transparent HugePages(透明巨大页面)
它的值我们可以从/proc/meminfo从找到它正在被内核使用的AnonHugePages当前值
# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] never
若显示的不是never则证明Transparent HugePages正在被使用。
下面的 *** 作是如何disable Transparent HugePages *** 作,
[root@localhost ~]# vim /etc/grub.conf ///在kernel /最后添加transparent_hugepage=never,保存退出
title Oracle Linux Server (2.6.32-300.25.1.el6uek.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-300.25.1.el6uek.x86_64 ro root=LABEL=/ transparent_hugepage=never
initrd /initramfs-2.6.32-300.25.1.el6uek.x86_64.img
重启下,
[root@localhost ~]# grep Huge /proc/meminfo
AnonHugePages: 0 kB
HugePages_Total: 2
HugePages_Free:2
HugePages_Rsvd:0
HugePages_Surp:0
Hugepagesize: 2048 kB
[root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)