Solaris上修改进程能够打开的最大文件句柄数

进程能够打开的最大文件句柄数决定了每个进程能够同时打开的文件数量。Solaris10上缺省值是256,对于某些应用而言,缺省值太小,需要手工修改。

  • 检查用户进程能够打开的最大文件句柄数

有两种方式,一是使用ulimit命令,二是使用prctl命令

1.  ulimit命令

jyu@jumpstart:~ $>id
uid=100(jyu) gid=1(other)
jyu@jumpstart:~ $>ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 10
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 11445
virtual memory          (kbytes, -v) unlimited

open files表示进程能够打开的最大文件句柄数量。

 

2. prctl命令

jyu@jumpstart:~ $>prctl -i process $$
process: 1079: -bash
NAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT

……
process.max-file-descriptor
        basic             256       -   deny                              1079
        privileged      65.5K       -   deny                                 -
        system          2.15G     max   deny                                 -

……

process.max-file-descriptor表示进程能够打开的最大文件句柄数,其中basic表示软限制,privileged表示硬限制。非root用户可以在硬限制的范围内自行调整软硬限制值。

  • 修改用户进程能够打开的最大文件句柄数

修改此参数通常有以下几种方法。

  1. 使用ulimit命令或plimit命令修改

ulimit命令只能修改当前SHELL及其子进程的设置,设置后立即生效,一旦当前SHELL退出设置即失效。-S参数用于设置软限制,-H参数用于设置硬限制。

  • 设置软限制:

jyu@jumpstart:~ $>ulimit -S -n 1024
jyu@jumpstart:~ $>ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 10
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 11445
virtual memory          (kbytes, -v) unlimited

jyu@jumpstart:~ $>prctl -i process $$
process: 1079: -bash
NAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT

……

process.max-file-descriptor
        basic           1.02K       -   deny                              1079
        privileged      65.5K       -   deny                                 -
        system          2.15G     max   deny                                 -

……

jyu@jumpstart:~ $>ulimit -S -n 65536
jyu@jumpstart:~ $>ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 65536
pipe size            (512 bytes, -p) 10
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 11445
virtual memory          (kbytes, -v) unlimited
jyu@jumpstart:~ $>ulimit -S -n 65537
-bash: ulimit: open files: cannot modify limit: Invalid argument

软限制只能在privileged的值以下调整,此例中所能调整的最大值是65536。

  • 设置硬限制

jyu@jumpstart:~ $>ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 10
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 11445
virtual memory          (kbytes, -v) unlimited

ulimit命令中open files显示的是软限制,可以用prctl命令显示硬限制,即privileged值。

jyu@jumpstart:~ $>prctl -i process $$
process: 1139: -bash
NAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT

……

process.max-file-descriptor
        basic             256       -   deny                              1139
        privileged      65.5K       -   deny                                 -
        system          2.15G     max   deny                                 -

……

jyu@jumpstart:~ $>ulimit -H -n 65537
-bash: ulimit: open files: cannot modify limit: Not owner
jyu@jumpstart:~ $>ulimit -H -n 32768
jyu@jumpstart:~ $>ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 10
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 11445
virtual memory          (kbytes, -v) unlimited

jyu@jumpstart:~ $>prctl -i process $$
process: 1139: -bash
NAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT

process.max-file-descriptor
        basic             256       -   deny                              1139
        privileged      32.8K       -   deny                                 -
        system          2.15G     max   deny
                                 -

非root用户调整硬限制时只能往小调,不能往大调。

 

2. 修改/etc/system参数

在Solaris10上,这种方法已经不建议使用,但这种方式仍然有效。/etc/system中设置参数是全局有效的,即所有用户均会受影响。并且设置后,需要重启系统才能生效。

设置方法是在/etc/system文件中增加以下两个参数,然后重启系统。

set rlim_fd_cur=1024

set rlim_fd_max=65535

以下是两个参数的说明:

rlim_fd_max (硬限制)
Description : Specifies the “hard” limit on file descriptors that a single process might have open.Overriding this limit requires superuser privilege.
Data Type : Signed integer
Default : 65,536
Range : 1 to MAXINT
Units : File descriptors
Dynamic? : No
Validation : None
When to Change : When the maximum number of open files for a process is not enough. Other limitations in system facilities can mean that a larger number of file descriptors is not as useful as it might be. For example:
■ A 32-bit program using standard I/O is limited to 256 file descriptors. A 64-bit program using standard I/O can use up to 2 billion descriptors. Specifically, standard I/O refers to the
stdio(3C) functions in libc(3LIB).
■ select is by default limited to 1024 descriptors per fd_set. For more information, see select(3C). Starting with the Solaris 7 release, 32-bit application code can be recompiled with a larger fd_set size (less than or equal to 65,536). A 64-bit application uses an fd_set size of 65,536, which cannot be changed.

An alternative to changing this on a system wide basis is to use the plimit(1) command. If a parent process has its limits changed by plimit, all children inherit the increased limit. This alternative is useful for daemons such as inetd.

Commitment Level : Unstable
ChangeHistory : For information, see “rlim_fd_max (Solaris 8 Release)” on page 184.

 

rlim_fd_cur (软限制)
Description : Defines the “soft” limit on file descriptors that a single process can have open. A process might adjust its file descriptor limit to any value up to the “hard” limit defined by rlim_fd_max by using the setrlimit() call or by issuing the limit command in whatever shell it is running. You do not require superuser privilege to adjust the limit to any value less than or equal to the hard limit.
Data Type : Signed integer
Default : 256
Range : 1 to MAXINT
Units : File descriptors
Dynamic? : No
Validation : Compared to rlim_fd_max. If rlim_fd_cur is greater than rlim_fd_max, rlim_fd_cur is reset to rlim_fd_max.
When to Change : When the default number of open files for a process is not enough. Increasing this value means only that it might not be necessary for a program to use setrlimit to increase the maximum number of file descriptors available to it.
Commitment Level : Unstable

以下是调整示例:

在/etc/system中设置参数然后重启系统。

root@jumpstart:/ #>tail -2 /etc/system
set rlim_fd_cur=1024
set rlim_fd_max=65535

root@jumpstart:/ #>plimit $$
1041:   -bash
   resource              current         maximum
  time(seconds)         unlimited       unlimited
  file(blocks)          unlimited       unlimited
  data(kbytes)          unlimited       unlimited
  stack(kbytes)         10240           unlimited
  coredump(blocks)      unlimited       unlimited
  nofiles(descriptors)  1024            65535
  vmemory(kbytes)       unlimited       unlimited

root@jumpstart:/ #>prctl -i process $$
process: 1041: -bash
NAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT

……

process.max-file-descriptor
        basic           1.02K       -   deny                              1041
        privileged      65.5K       -   deny                                 -
        system          2.15G     max   deny                                 -

……

root@jumpstart:/ #>su - jyu
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
jyu@jumpstart:~ $>plimit $$
1049:   -bash
   resource              current         maximum
  time(seconds)         unlimited       unlimited
  file(blocks)          unlimited       unlimited
  data(kbytes)          unlimited       unlimited
  stack(kbytes)         10240           unlimited
  coredump(blocks)      unlimited       unlimited
  nofiles(descriptors)  1024            65535
  vmemory(kbytes)       unlimited       unlimited

jyu@jumpstart:~ $>prctl -i process $$
process: 1049: -bash
NAME    PRIVILEGE       VALUE    FLAG   ACTION                       RECIPIENT

……

process.max-file-descriptor
        basic           1.02K       -   deny                              1049
        privileged      65.5K       -   deny                                 -
        system          2.15G     max   deny                                 -

……

 

3. 使用project

project是Solaris10新增加的特性,可以通过设置project参数为一个用户或一组用户设置参数值。设置后可立即生效。

以下是设置示例:

root@jumpstart:/ #>projadd user.jyu  (创建project user.jyu)

root@jumpstart:/ #>id -p jyu
uid=100(jyu) gid=1(other) projid=100(user.jyu)   (jyu用户属于project user.jyu)

root@jumpstart:/ #>projmod -a -K "process.max-file-descriptor=(basic,65537,deny)" user.jyu
root@jumpstart:/ #>projmod -a -K "process.max-file-descriptor=(priv,65538,deny)" user.jyu
root@jumpstart:/ #>grep 'user.jyu' /etc/project
user.jyu:100::::process.max-file-descriptor=(basic,65537,deny),(priv,65538,deny)

设置basic和privilege值分别为65537和65538,越过/etc/system中的最大硬限制

root@jumpstart:/ #>tail -2 /etc/system
set rlim_fd_cur=1024
set rlim_fd_max=65535

root@jumpstart:/ #>plimit $$
1041:   -bash
   resource              current         maximum
  time(seconds)         unlimited       unlimited
  file(blocks)          unlimited       unlimited
  data(kbytes)          unlimited       unlimited
  stack(kbytes)         10240           unlimited
  coredump(blocks)      unlimited       unlimited
  nofiles(descriptors)  1024            65535
  vmemory(kbytes)       unlimited       unlimited

root用户的结果只受/etc/system里参数的影响,而不受project user.jyu影响,root用户不属于此project.

root@jumpstart:/ #>su - jyu
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
jyu@jumpstart:~ $>plimit $$
1091:   -bash
   resource              current         maximum
  time(seconds)         unlimited       unlimited
  file(blocks)          unlimited       unlimited
  data(kbytes)          unlimited       unlimited
  stack(kbytes)         10240           unlimited
  coredump(blocks)      unlimited       unlimited
  nofiles(descriptors)  65535           65535
  vmemory(kbytes)       unlimited       unlimited
jyu@jumpstart:~ $>id -p
uid=100(jyu) gid=1(other) projid=100(user.jyu)

用户jyu当前值和最大值都是65535,而project user.jyu里设置的值分别是65537和65538,用户结果与project设置值不一致,这是因为project中设置的值超过了/etc/system里设置的最大硬限制数是65535,此时系统自动将用户结果调整为/etc/system中设置的最大硬限制数。

root@jumpstart:/ #>projmod -s -K "process.max-file-descriptor=(basic,32767,deny),(priv,32768,deny)" user.jyu
root@jumpstart:/ #>
root@jumpstart:/ #>plimit $$
1041:   -bash
   resource              current         maximum
  time(seconds)         unlimited       unlimited
  file(blocks)          unlimited       unlimited
  data(kbytes)          unlimited       unlimited
  stack(kbytes)         10240           unlimited
  coredump(blocks)      unlimited       unlimited
  nofiles(descriptors)  1024            65535
  vmemory(kbytes)       unlimited       unlimited

root用户未受project user.jyu调整影响。
root@jumpstart:/ #>su - jyu
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
jyu@jumpstart:~ $>plimit $$
1099:   -bash
   resource              current         maximum
  time(seconds)         unlimited       unlimited
  file(blocks)          unlimited       unlimited
  data(kbytes)          unlimited       unlimited
  stack(kbytes)         10240           unlimited
  coredump(blocks)      unlimited       unlimited
  nofiles(descriptors)  32767           32768
  vmemory(kbytes)       unlimited       unlimited

用户jyu当前值和最大值均与project user.jyu中的设置一致。

如果在系统里同时设置/etc/system和project里的参数时要注意以下几点:

1. /etc/system的设置是全局设置,会影响所有用户,而project的设置仅影响属于此project的用户。

2. /etc/system的设置需要重启系统才能生效,而project的设置立即生效(新进程)。

3. /etc/system的硬限制值是所有用户的最大限制值。如果project中的设置值超过了/etc/system的硬限制值,则project设置无效,相应用户值会被设置为/etc/system的硬限制值。

posted @ 2012-01-16 10:08  生命的力量在于不顺从  阅读(2894)  评论(0编辑  收藏  举报