`
lzy.je
  • 浏览: 150565 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

RLIMIT_*

阅读更多

用于取得 *nix 平台上的 RLIMITs 值,我猜应该是完整列表吧。参考了 apue2e。

 

#if defined(SOLARIS)
#define _XOPEN_SOURCE	500	/* Single UNIX Specification, Version 2  for Solaris 9 */
#define CMSG_LEN(x)	_CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))
#elif !defined(BSD)
#define _XOPEN_SOURCE	600	/* Single UNIX Specification, Version 3 */
#endif

#include <sys/types.h>	/* some systems still require this */
#include <sys/stat.h>
#include <sys/termios.h>	/* for winsize */
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif
#include <stdio.h>		/* for convenience */
#include <stdlib.h>		/* for convenience */
#include <stddef.h>		/* for offsetof */
#include <string.h>		/* for convenience */
#include <unistd.h>		/* for convenience */
#include <errno.h>		/* for definition of errno */
#include <stdarg.h>		/* ISO C variable aruments */

#include <sys/resource.h>

#define	MAXLINE	4096			/* max line length */

#if defined(BSD) || defined(MACOS)
#include <sys/time.h>
#define FMT "%10lld	"
#else
#define FMT "%10ld	"
#endif	/* define(BSD) || defined(MACOS) */

#define doit(name) pr_limits(#name, name)

static void pr_limits(char*, int);

static void err_doit(int, int, const char *, va_list);
static void err_sys(const char *, ...);

int main(void)
{
#ifdef RLIMIT_AS
	doit(RLIMIT_AS);
#endif

	doit(RLIMIT_CORE);
	doit(RLIMIT_CPU);
	doit(RLIMIT_DATA);
	doit(RLIMIT_FSIZE);

#ifdef RLIMIT_LOCKS
	doit(RLIMIT_LOCKS);
#endif

#ifdef RLIMIT_MEMLOCK
	doit(RLIMIT_MEMLOCK);
#endif

	doit(RLIMIT_NOFILE);

#ifdef RLIMIT_NPROC
	doit(RLIMIT_NPROC);
#endif

#ifdef RLIMIT_RSS
	doit(RLIMIT_RSS);
#endif

#ifdef RLIMIT_SBSIZE
	doit(RLIMIT_SBSIZE);
#endif

	doit(RLIMIT_STACK);

#ifdef RLIMIT_VMEM
	doit(RLIMIT_VMEM);
#endif

#ifdef RLIMIT_SIGPENDING
	doit(RLIMIT_SIGPENDING);
#endif

#ifdef RLIMIT_MSGQUEUE
	doit(RLIMIT_MSGQUEUE);
#endif

#ifdef RLIMIT_NICE
	doit(RLIMIT_NICE);
#endif

#ifdef RLIMIT_RTPRIO
	doit(RLIMIT_RTPRIO);
#endif

#ifdef RLIMIT_TCACHE
	doit(RLIMIT_TCACHE);
#endif

#ifdef RLIMIT_AIO_OPS
	doit(RLIMIT_AIO_OPS);
#endif

#ifdef RLIMIT_AIO_MEM
	doit(RLIMIT_AIO_MEM);
#endif

#ifdef RLIMIT_RSESTACK
	doit(RLIMIT_RSESTACK);
#endif

	return 0;	
}

static void pr_limits(char* name, int resource)
{
	struct rlimit limit;

	if (getrlimit(resource, &limit) < 0)
		err_sys("getrlimit error for %s", name);

	printf("%-20s\t", name);

	if (limit.rlim_cur == RLIM_INFINITY)
		printf("(infinite)\t");
	else
		printf(FMT, limit.rlim_cur);

	if (limit.rlim_max == RLIM_INFINITY)
		printf("(infinite)");
	else
		printf(FMT, limit.rlim_max);

	putchar((int)'\n');
}

static void err_sys(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	err_doit(1, errno, fmt, ap);
	va_end(ap);
	exit(1);
}

static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
	char	buf[MAXLINE];

	vsnprintf(buf, MAXLINE, fmt, ap);
	if (errnoflag)
		snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
		strerror(error));
	strcat(buf, "\n");
	fflush(stdout);		/* in case stdout and stderr are the same */
	fputs(buf, stderr);
	fflush(NULL);		/* flushes all stdio output streams */
}

 

Resource must be one of:

 

  • RLIMIT_CPU

          CPU time limit in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program. If the process continues to consume CPU time, it will be sent SIGXCPU once per second until the hard limit is reached, at which time it is sent SIGKILL. (This latter point describes Linux 2.2 and 2.4 behaviour. Implementations vary in how they treat processes which continue to consume CPU time after reaching the soft limit. Portable applications that need to catch this signal should perform an orderly termination upon first receipt of SIGXCPU.)

 

  • RLIMIT_DATA

          The maximum size of the process's data segment (initialized data, uninitialized data, and heap). This limit affects calls to brk() and sbrk(), which fail with the error ENOMEM upon encountering the soft limit of this resource.

 

  • RLIMIT_FSIZE

          The maximum size of files that the process may create. Attempts to extend a file beyond this limit result in delivery of a SIGXFSZ signal. By default, this signal terminates a process, but a process can catch this signal instead, in which case the relevant system call (e.g., write(), truncate()) fails with the error EFBIG.

 

  • RLIMIT_LOCKS

          A limit on the combined number of flock() locks and fcntl() leases that this process may establish (Linux 2.4 and later).

 

  • RLIMIT_MEMLOCK

          The maximum number of bytes of virtual memory that may be locked into RAM using mlock() and mlockall().

 

  • RLIMIT_NOFILE

          Specifies a value one greater than the maximum file descriptor number that can be opened by this process. Attempts (open(), pipe(), dup(), etc.) to exceed this limit yield the error EMFILE.

 

  • RLIMIT_NPROC

          The maximum number of processes that can be created for the real user ID of the calling process. Upon encountering this limit, fork() fails with the error EAGAIN.

 

  • RLIMIT_RSS

          Specifies the limit (in pages) of the process's resident set (the number of virtual pages resident in RAM). This limit only has effect in Linux 2.4 onwatrds, and there only affects calls to madvise() specifying MADVISE_WILLNEED.

 

  • RLIMIT_STACK

          The maximum size of the process stack, in bytes. Upon reaching this limit, a SIGSEGV signal is generated. To handle this signal, a process must employ an alternate signal stack (sigaltstack(2)).

 

  • RLIMIT_OFILE is the BSD name for RLIMIT_NOFILE.

 

/*
 * AIX 5.3 resource limits
 * /usr/include/sys/resource.h
 */
#define RLIMIT_CPU      0               /* cpu time in milliseconds */
#define RLIMIT_FSIZE    1               /* maximum file size */
#define RLIMIT_DATA     2               /* data size */
#define RLIMIT_STACK    3               /* stack size */
#define RLIMIT_CORE     4               /* core file size */
#define RLIMIT_RSS      5               /* resident set size */

#define RLIMIT_AS       6               /* max size proc's total memory--not enforced */
#define RLIMIT_NOFILE   7               /* max # allocated fds--not enforced */
 
/*
 * SunOS 5.10 resource limits
 * /usr/include/sys/resource.h
 */
#define RLIMIT_CPU      0               /* cpu time in seconds */
#define RLIMIT_FSIZE    1               /* maximum file size */
#define RLIMIT_DATA     2               /* data size */
#define RLIMIT_STACK    3               /* stack size */
#define RLIMIT_CORE     4               /* core file size */
#define RLIMIT_NOFILE   5               /* file descriptors */
#define RLIMIT_VMEM     6               /* maximum mapped memory */
#define RLIMIT_AS       RLIMIT_VMEM

#define RLIM_NLIMITS    7               /* number of resource limits */

#if defined(_LP64)

typedef unsigned long   rlim_t;

#define RLIM_INFINITY   (-3l)
#define RLIM_SAVED_MAX  (-2l)
#define RLIM_SAVED_CUR  (-1l)

#else   /* _LP64 */

/*
 * The definitions of the following types and constants differ between the
 * regular and large file compilation environments.
 */
#if _FILE_OFFSET_BITS == 32

typedef unsigned long   rlim_t;

#define RLIM_INFINITY   0x7fffffff
#define RLIM_SAVED_MAX  0x7ffffffe
#define RLIM_SAVED_CUR  0x7ffffffd

#else   /* _FILE_OFFSET_BITS == 32 */

typedef u_longlong_t    rlim_t;

#define RLIM_INFINITY   ((rlim_t)-3)
#define RLIM_SAVED_MAX  ((rlim_t)-2)
#define RLIM_SAVED_CUR  ((rlim_t)-1)

#endif  /* _FILE_OFFSET_BITS == 32 */

#endif  /* _LP64 */
 
/*
 * linux 2.6 (ubuntu 8.10)
 * /usr/include/bits/resource.h
 */
/* Kinds of resource limit.  */
enum __rlimit_resource
{
  /* Per-process CPU limit, in seconds.  */
  RLIMIT_CPU = 0,
#define RLIMIT_CPU RLIMIT_CPU

  /* Largest file that can be created, in bytes.  */
  RLIMIT_FSIZE = 1,
#define RLIMIT_FSIZE RLIMIT_FSIZE

  /* Maximum size of data segment, in bytes.  */
  RLIMIT_DATA = 2,
#define RLIMIT_DATA RLIMIT_DATA

  /* Maximum size of stack segment, in bytes.  */
  RLIMIT_STACK = 3,
#define RLIMIT_STACK RLIMIT_STACK

  /* Largest core file that can be created, in bytes.  */
  RLIMIT_CORE = 4,
#define RLIMIT_CORE RLIMIT_CORE

  /* Largest resident set size, in bytes.
     This affects swapping; processes that are exceeding their
     resident set size will be more likely to have physical memory
     taken from them.  */
  __RLIMIT_RSS = 5,
#define RLIMIT_RSS __RLIMIT_RSS

  /* Number of open files.  */
  RLIMIT_NOFILE = 7,
  __RLIMIT_OFILE = RLIMIT_NOFILE, /* BSD name for same.  */
#define RLIMIT_NOFILE RLIMIT_NOFILE
#define RLIMIT_OFILE __RLIMIT_OFILE

  /* Address space limit.  */
  RLIMIT_AS = 9,
#define RLIMIT_AS RLIMIT_AS

  /* Number of processes.  */
  __RLIMIT_NPROC = 6,
#define RLIMIT_NPROC __RLIMIT_NPROC

  /* Locked-in-memory address space.  */
  __RLIMIT_MEMLOCK = 8,
#define RLIMIT_MEMLOCK __RLIMIT_MEMLOCK

  /* Maximum number of file locks.  */
  __RLIMIT_LOCKS = 10,
#define RLIMIT_LOCKS __RLIMIT_LOCKS

  /* Maximum number of pending signals.  */
  __RLIMIT_SIGPENDING = 11,
#define RLIMIT_SIGPENDING __RLIMIT_SIGPENDING

  /* Maximum bytes in POSIX message queues.  */
  __RLIMIT_MSGQUEUE = 12,
#define RLIMIT_MSGQUEUE __RLIMIT_MSGQUEUE

  /* Maximum nice priority allowed to raise to.
     Nice levels 19 .. -20 correspond to 0 .. 39
     values of this resource limit.  */
  __RLIMIT_NICE = 13,
#define RLIMIT_NICE __RLIMIT_NICE

  /* Maximum realtime priority allowed for non-priviledged
     processes.  */
  __RLIMIT_RTPRIO = 14,
#define RLIMIT_RTPRIO __RLIMIT_RTPRIO

  __RLIMIT_NLIMITS = 15,
  __RLIM_NLIMITS = __RLIMIT_NLIMITS
#define RLIMIT_NLIMITS __RLIMIT_NLIMITS
#define RLIM_NLIMITS __RLIM_NLIMITS
};

/* Value to indicate that there is no limit.  */
#ifndef __USE_FILE_OFFSET64
# define RLIM_INFINITY ((unsigned long int)(~0UL))
#else
# define RLIM_INFINITY 0xffffffffffffffffuLL
#endif

#ifdef __USE_LARGEFILE64
# define RLIM64_INFINITY 0xffffffffffffffffuLL
#endif

 

作者:lzy.je
出处:http://lzy.iteye.com
本文版权归作者所有,只允许以摘要和完整全文两种形式转载,不允许对文字进行裁剪。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

// 2009.04.29 添加 ////

 

/*
 * HP-UX 11.23I Resource limits
 * /usr/include/sys/resource.h
 */

#if defined(_LARGEFILE64_SOURCE)
#  if defined(__L64_MODE__)
#    define  RLIM64_INFINITY  (uint64_t) 0x7fffffffffffffffL
#  else /* __L64_MODE__ */
#    if defined(__LL_MODE__)
#       define  RLIM64_INFINITY  (uint64_t) 0x7fffffffffffffffLL
#    endif /* __LL_MODE__ */
#  endif /* __L64_MODE__ */
#  define  RLIM32_INFINITY  (uint32_t) 0x7fffffff
#endif /* _LARGEFILE64_SOURCE */

#if defined(_KERNEL)
#  define  RLIM_INFINITY  RLIM64_INFINITY
#  define  K_RLIM_INFINITY  RLIM_INFINITY
#  define  ATT_BYTESPERBLOCK    512
#  define  LOG_ATT_BYTESPERBLOCK        9
/* bytes to 1/2k blocks and 1/2k  blocks to bytes conversion macro. */
#  define bt2bk(x)      (((unsigned long) (x)) >> (LOG_ATT_BYTESPERBLOCK))
#  define bk2bt(x)      (((unsigned long) (x)) << (LOG_ATT_BYTESPERBLOCK))
#  define ULIMIT_MAX    bt2bk(0x7fffffff)
#  if defined(__L64_MODE__)
#    define  ULIMIT64_MAX bt2bk(0x7fffffffffffffffL)
#  endif /* __L64_MODE__ */
#  define  ULIMIT32_MAX bt2bk(0x7fffffff)
#else
#if defined(__STDC__)
#  if defined(__L64_MODE__)
#    define  RLIM_INFINITY    0x7fffffffffffffffUL
#  else /* __L64_MODE__ */
#    if defined(__64BIT_OFF_T)
#      if defined(__LL_MODE__)
#        define  RLIM_INFINITY    0x7fffffffffffffffULL
#      endif /* __LL_MODE__ */
#    else /* __64BIT_OFF_T */
#      define  RLIM_INFINITY    0x7fffffffUL
#    endif /* __64BIT_OFF_T */
#  endif /* __L64_MODE__ */
#else
#  if defined(__L64_MODE__)
#    define  RLIM_INFINITY   (uint64_t) 0x7fffffffffffffffL
#  else /* __L64_MODE__ */
#    if defined(__64BIT_OFF_T)
#      if defined(__LL_MODE__)
#        define  RLIM_INFINITY   (uint64_t) 0x7fffffffffffffffLL
#      endif /* __LL_MODE__ */
#    else /* __64BIT_OFF_T */
#      define  RLIM_INFINITY   (uint32_t) 0x7fffffff
#    endif /* __64BIT_OFF_T */
#  endif /* __L64_MODE__ */
#endif /* __STDC__ */
#endif /* _KERNEL */

#define	RLIMIT_CPU	0		/* cpu time in milliseconds */
#define	RLIMIT_DATA	2		/* data size */
#define	RLIMIT_STACK	3		/* stack size */
#define	RLIMIT_RSS	5		/* resident set size */

#define	RLIMIT_FSIZE	1		/* maximum file size */
#define	RLIMIT_CORE	4		/* core file size */
#define RLIMIT_NOFILE   6               /* maximum number of open files */
#define RLIMIT_OPEN_MAX RLIMIT_NOFILE   /* maximum number of open files */
#define RLIMIT_AS       7               /* maximum number of open files */
#define RLIMIT_TCACHE   8		/* maximum number of cached threads */
#define RLIMIT_AIO_OPS  9		/* maximum number of POSIX AIO ops */
#define RLIMIT_AIO_MEM  10		/* maximum bytes locked for POSIX AIO */
#define RLIMIT_RSESTACK	11		/* RSE stack size */
#define	RLIM_NLIMITS	12		/* number of resource limits */

#endif /* _INCLUDE_XOPEN_SOURCE_EXTENDED */

#endif /* _SYS_RESOURCE_INCLUDED */

 

这几个 limit 不明白意思:

 

RLIMIT_TCACHE 8 /* maximum number of cached threads */
RLIMIT_AIO_OPS 9 /* maximum number of POSIX AIO ops */
RLIMIT_AIO_MEM 10 /* maximum bytes locked for POSIX AIO */
RLIMIT_RSESTACK 11 /* RSE stack size */
RLIM_NLIMITS 12 /* number of resource limits */

 

5
0
分享到:
评论

相关推荐

    linux下 rlimit

    在Linux系统中,rlimit机制是用于控制进程资源限制的关键组件。通过rlimit,系统管理员或用户能够设定进程可以消耗的资源上限,这对于防止资源过度消耗、提高系统稳定性及安全性至关重要。本文将深入探讨rlimit的...

    nginx优化.docx

    3. **worker_rlimit_nofile**: 设置每个Nginx进程能打开的最大文件描述符数量,通常应与`ulimit -n`的值一致,以防止文件描述符耗尽。 4. **use epoll**: 在Linux系统中,使用epoll作为I/O模型可以显著提高Nginx的...

    nginx优化 突破十万并发1

    3. **worker_rlimit_nofile**:设定每个Nginx进程能打开的最大文件描述符数。通常建议与系统的最大文件描述符数(ulimit -n)保持一致,如65535,以确保在高并发下不因文件描述符限制而影响性能。 4. **use epoll**...

    Nginx安装+nginx_upstream_check_module后端健康检查

    - **worker_rlimit_nofile**: 设置每个工作进程可以打开的最大文件描述符数量。 - **error_log**: 日志级别配置。 - **events**: 事件处理模型。 - **http**: HTTP配置块。 具体示例配置如下: ```nginx worker_...

    详解Nginx服务器中的nginx.conf配置文件

    Nginx 配置文件主要分成四部分... worker_rlimit_nofile #  指定所有 worker 进程能够打开的最大文件数  worker_cpu_affinity  设置 worker 进程的 CPU 粘性,以避免进程在 CPU 间切换带来的性能消耗。如 worker_cp

    Nginx性能配置[参照].pdf

    2. **worker_rlimit_nofile**:它限制了每个工作进程可以打开的最大文件描述符数。提高这个值可以允许Nginx处理更多的并发连接,避免“too many open files”的错误。 3. **events模块**: - **worker_connections...

    nginx 10w并发优化项

    - **建议值:** `max`参数应与`worker_rlimit_nofile`一致,`inactive`参数根据实际负载情况进行调整。 - **配置示例:** `open_file_cache max=65535 inactive=60s;` **9. **`open_file_cache_valid`**:** - **...

    elasticsearch启动警告无法锁定JVM内存

    Elasticsearch启动警告无法锁定JVM内存的问题是由于Linux系统的内存管理机制引起的,可以通过增加RLIMIT_MEMLOCK的值来解决这个问题。 知识点: * Linux系统的内存管理机制 * RLIMIT_MEMLOCK限制 * ulimit命令 * ...

    nginx优化.pdf

    3. **`worker_rlimit_nofile`**:设置每个工作进程能打开的最大文件描述符数量。通常,该值应与系统级的最大文件描述符数保持一致,可以通过`ulimit -n`命令查看系统当前的最大文件描述符数。 4. **`use epoll;`**...

    第16章-实战nginx调优-v8.pdf

    现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。 这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过...

    linux core文件生产设置与编码

    具体到core文件,需要使用RLIMIT_CORE作为resource参数。这两个函数的声明如下: ```c #include int getrlimit(int resource, struct rlimit *rlptr); // 获取当前进程的限制值 int setrlimit(int resource, ...

    2.0线上nginx搭建及优化.docx

    - **worker_rlimit_nofile**: 设置每个 Nginx 进程的最大文件描述符数量,应与系统允许的最大值保持一致,例如 `worker_rlimit_nofile 65535;`。 - **use epoll**: Nginx 使用最新的 `epoll`(Linux 2.6内核)网络 ...

    Python限制内存和CPU使用量的方法(Unix系统适用)

    值得注意的是,`setrlimit`函数可以用来调整多种资源的限制,例如文件描述符的数量(`RLIMIT_NOFILE`),子进程的最大数量(`RLIMIT_NPROC`)等。具体的资源限制类型可以在`resource`模块文档中找到。 然而,必须...

    Nginx_ppt_zhanghui

    Nginx初认识,总结了user,worker_process,worker_rlimit_nofile,event,http等相关的配置节点。

    一些优化Nginx服务器的技巧简介

    ### worker_processes和worker_rlimit_nofile的优化配置 - **worker_processes auto;** 此参数设置worker进程的数量。合理设置worker进程数可以充分利用多核CPU的计算能力。在自动模式下,Nginx会尝试检测可用的CPU...

    Tengine(Nginx)配置参考

    #运行用户 #user nobody; #启动进程,通常设置成和cpu的...worker_rlimit_nofile 100000; #全局错误日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;

    /etc/php-fpm.d/www.conf 配置注意事项

    1. **rlimit_files 的设置**: PHP-FPM的`rlimit_files`配置项决定了可以同时打开的文件描述符数量。这应该与操作系统的限制保持一致,以避免资源限制问题。通过命令`ulimit -n`可以查看当前系统允许的最大文件描述...

    基于Linux系统调用--getrlimit()与setrlimit()函数的方法

    参数`resource`是一个枚举值,代表要查询的资源类型,如`RLIMIT_AS`(最大虚拟内存)、`RLIMIT_CPU`(最大CPU时间)等。`rlim`是一个指向`struct rlimit`结构体的指针,该结构体包含两个成员:`rlim_cur`(软限制)和`...

Global site tag (gtag.js) - Google Analytics