首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cgroups内存上限为16

cgroups内存上限为16
EN

Server Fault用户
提问于 2014-02-24 19:58:50
回答 1查看 1.1K关注 0票数 4

我试图使用cgroup来限制服务器上大量内存(128 GB或更多)上的用户进程的内存使用。我们想要实现的是为操作系统和根进程预留大约6GB的ram,剩下的留给用户。我们希望确保我们在任何时候都有空闲的内存,并且我们不希望服务器进行积极的交换。

如果将限制设置得足够低(<16 is ),则可以正常工作。用户进程由cgred正确地分配给正确的cgroup,一旦达到限制,oom将终止需要内存的进程。

当我们把限制定得更高时,问题就出现了。然后,如果一个进程使用超过16G的ram,那么服务器将开始交换,即使内存使用率仍然远远低于限制,并且有大量的ram可用。

是否有任何设置或某种最大值来限制我们可以在cgroup下授予访问权限的内存量?

以下是更多信息:

我使用下面的代码来模拟用户进程占用内存。代码跟踪链接列表中分配的内存,以便在程序中使用和访问内存,而不仅仅是使用malloc (每次重写指针)。

/*含量(英文)

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>



struct testlink {
  void *ram;
  struct testlink *next;
};

int main (int argc, char *argv[]) {

    int block=8192;
    char buf[block];
    void *ram=NULL;
    FILE *frandom;
    int nbproc,i;
    pid_t pID;
  struct testlink *pstart, *pcurr, *pnew;

    if (argc < 2) {
        //nbproc = 1 by default
        nbproc=1;
    } else {
        if (sscanf(argv[1], "%d", &nbproc) != 1) {
                /* it is an error */
            printf("Failed to set number of child processes\n");
            return -1;
            } 
    }

    // open /dev/urandom for reading
    frandom = fopen("/dev/urandom", "r");
    if ( frandom == NULL ) {
        printf("I can't open /dev/urandom, giving up\n");
        return -1;
    }

    fread(&buf, block, 1, frandom); 
    if ( ferror(frandom) ) {
        // we read less than 1 byte, get out of the loop
        printf("Error reading from urandom\n");
        return -1;
    } 
    fclose (frandom);

    // pID=0 => child pID <0 => error, pID > 0 => parent
    for (i=1; i<nbproc; i++){ 
            pID = fork();
        // break out of the loop  if a child
        if (pID == 0)
            break;
        // exit if fork fails
        if (pID < 0) {
            printf("fork() failed, dying \n");
            return -1;
        }

    }
  pstart = (struct testlink*)malloc(sizeof(struct testlink));
  pstart->ram=NULL;
  pstart->next=NULL;
  pcurr = pstart;

    while ( 1==1 ) {
        ram = (void *)malloc(block);
        if (ram == NULL) {
                    printf("can't allocate memory\n");
                    return -1;
        }

        memcpy(ram, &buf, block);

    // store allocated blocks of ram in a linked list
    // so no one think we are not using them
    pcurr->ram = ram;
    pnew = (struct testlink*)malloc(sizeof(struct testlink));
    pnew->ram=NULL;
    pnew->next=NULL;
    pcurr->next=pnew;
    pcurr=pnew;

    }

    return 0;   

}

到目前为止,我尝试设置以下调谐器:

vm.overcommit_memory

vm.overcommit_ratio

vm.swappiness

vm.dirty_ratio

vm.dirty_background_ratio

vm.vfs_cache_pressure

这些sysctl设置似乎没有任何效果。当我上面的代码超过16 go的屏障后,服务器将开始交换,即使swappiness设置为0,overcommit被禁用,等等。我甚至试图关闭交换,但没有效果。即使没有交换,kswapd仍然被触发,性能下降。

最后,介绍了cgconfig.conf文件的相关内容。

代码语言:javascript
复制
mount {
  cpuset  = /cgroup/computenodes;
  cpu = /cgroup/computenodes;
  memory  = /cgroup/computenodes;
}


#limit = 120G
group computenodes {
# set memory.memsw the same so users can't use swap
  memory {
    memory.limit_in_bytes = 120G;
    memory.memsw.limit_in_bytes = 120G;
    memory.swappiness = 0;
#    memory.use_hierarchy = 1;
  }

# No alternate memory nodes if the system is not NUMA
# On computenodes use all available cores
    cpuset {
        cpuset.mems="0";
        cpuset.cpus="0-47";
    }
}

最后,我们使用Centos 6,内核2.6.32。

谢谢

EN

回答 1

Server Fault用户

回答已采纳

发布于 2014-02-24 21:13:36

**注:删除后人**

你的问题就在这里

代码语言:javascript
复制
# No alternate memory nodes if the system is not NUMA
# On computenodes use all available cores
    cpuset {
        cpuset.mems="0";
        cpuset.cpus="0-47";
    }
}

您只使用一个内存节点。您需要将其设置为使用内存的所有节点。

我也认为下面的内容也适用,除非你知道下面的情况,否则你还是会看到问题的。所以为了子孙后代而离开。

这个问题基本上可以归结为硬件的使用。内核有一个启发式来确定这个开关的值。这改变了内核如何确定NUMA系统上的内存压力。

代码语言:javascript
复制
zone_reclaim_mode:

Zone_reclaim_mode allows someone to set more or less aggressive approaches to
reclaim memory when a zone runs out of memory. If it is set to zero then no
zone reclaim occurs. Allocations will be satisfied from other zones / nodes
in the system.

This is value ORed together of

1   = Zone reclaim on
2   = Zone reclaim writes dirty pages out
4   = Zone reclaim swaps pages

zone_reclaim_mode is set during bootup to 1 if it is determined that pages
from remote zones will cause a measurable performance reduction. The
page allocator will then reclaim easily reusable pages (those page
cache pages that are currently not used) before allocating off node pages.

It may be beneficial to switch off zone reclaim if the system is
used for a file server and all of memory should be used for caching files
from disk. In that case the caching effect is more important than
data locality.

Allowing zone reclaim to write out pages stops processes that are
writing large amounts of data from dirtying pages on other nodes. Zone
reclaim will write out dirty pages if a zone fills up and so effectively
throttle the process. This may decrease the performance of a single process
since it cannot use all of system memory to buffer the outgoing writes
anymore but it preserve the memory on other nodes so that the performance
of other processes running on other nodes will not be affected.

Allowing regular swap effectively restricts allocations to the local
node unless explicitly overridden by memory policies or cpuset
configurations.

为了让您了解正在发生的事情,内存被分解为区域,这在NUMA系统中特别有用,因为RAM与特定的CPU相关联。在这些主机中,内存局部性可能是性能的一个重要因素。例如,如果将内存组1和2分配给物理CPU 0,CPU 1可以访问该内存,但代价是从CPU 0锁定该RAM,从而导致性能下降。

在linux上,分区反映了物理机器的NUMA布局。每个区域的大小为16 is。

目前在区域回收中发生的事情是内核选择在一个完整区域(16 GB)中回收(向磁盘写入脏页、删除文件缓存、交换内存),而不是允许进程在另一个区域中分配内存(这将影响该CPU的性能。这就是为什么在16 is之后你会注意到交换。

如果关闭此值,这将改变内核的行为,而不是积极地回收区域数据,而是从另一个节点分配。

尝试通过在系统上运行zone_reclaim_mode来关闭sysctl -w vm.zone_reclaim_mode=0,然后重新运行测试。

注意,长时间运行在这样的配置上运行的高内存进程(在关闭zone_reclaim_mode的情况下)将随着时间的推移变得越来越昂贵。

如果允许许多不同的进程在许多不同的CPU上运行--所有这些进程都使用大量内存--使用任何有空闲页的节点,则可以有效地将主机的性能呈现为类似于只有一个物理CPU的东西。

票数 5
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/577989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档