mac中修改系统限制量--ulimit和sysctl

来源:互联网 发布:手机淘宝新手怎么拿货 编辑:程序博客网 时间:2024/06/05 03:01

在*nux中,对于每个用户,系统限制其最大进程数、文件数……。为提高性能,可以根据设备资源情况,设置各用户的最大进程数,文件数等等
在mac中,要设置这些系统值,也使用这些命令。

可以用ulimit -a 来显示当前的各种用户进程限制。

$ulimit -acore file size          (blocks, -c) 0data seg size           (kbytes, -d) unlimitedfile size               (blocks, -f) unlimitedmax locked memory       (kbytes, -l) unlimitedmax memory size         (kbytes, -m) unlimitedopen files                      (-n) 256pipe size            (512 bytes, -p) 1stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 709virtual memory          (kbytes, -v) unlimited$ ulimit -n 1000$ ulimit -acore file size          (blocks, -c) 0data seg size           (kbytes, -d) unlimitedfile size               (blocks, -f) unlimitedmax locked memory       (kbytes, -l) unlimitedmax memory size         (kbytes, -m) unlimitedopen files                      (-n) 1000pipe size            (512 bytes, -p) 1stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 709virtual memory          (kbytes, -v) unlimited$ 

在服务器类中,可能要对可以使用的socket端口等限制放大,修改其值

$ ulimit -n 1000$

如果修改太大,并不一定成功。

$ ulimit -n 100000-bash: ulimit: open files: cannot modify limit: Operation not permitted

这是,需要使用的命令是launchctl和sysctl

  • sysctl 命令:
NAME     sysctl -- get or set kernel stateSYNOPSIS     sysctl [-bdehiNnoqx] name[=value] ...     sysctl [-bdehNnoqx] -aDESCRIPTION     The sysctl utility retrieves kernel state and allows processes with appropriate privilege to set kernel     state.  The state to be retrieved or set is described using a ``Management Information Base'' (``MIB'')     style name, described as a dotted set of components.     The following options are available:     -A      Equivalent to -o -a (for compatibility).     -a      List all the currently available non-opaque values.  This option is ignored if one or more variable             names are specified on the command line.    ... ...          

可以尝试使用sysctl -a 来列出系统状态值,这是一个超长的列表。

  • launchctl 命令
NAME     launchctl -- Interfaces with launchdSYNOPSIS     launchctl subcommand [arguments ...]DESCRIPTION     launchctl interfaces with launchd to manage and inspect daemons, agents and XPC services.SUBCOMMANDS     launchctl allows for detailed examination of launchd endpoints. A domain manages the execution policy for a     collection of services.  A service may be thought of as a virtual process that is always available to be     spawned in response to demand. Each service has a collection of endpoints, and sending a message to one of     those endpoints will cause the service to launch on demand. Domains advertise these endpoints in a shared     namespace and may be thought of as synonymous with Mach bootstrap subsets.     Many subcommands in launchctl take a specifier which indicates the target domain or service for the subcom-     mand. This specifier may take one of the following forms:     system/[service-name]              Targets the system domain or a service within the system domain. The system domain manages the root              Mach bootstrap and is considered a privileged execution context. Anyone may read or query the sys-              tem domain, but root privileges are required to make modifications.     user/<uid>/[service-name]              Targets the user domain for the given UID or a service within that domain. A user domain may exist              independently of a logged-in user. User domains do not exist on iOS.     login/<asid>/[service-name]              Targets a user-login domain or service within that domain. A user-login domain is created when the              user logs in at the GUI and is identified by the audit session identifier associated with that              login. If a user domain has an associated login domain, the print subcommand will display the ASID              of that login domain. User-login domains do not exist on iOS.     gui/<uid>/[service-name]              Another form of the login specifier. Rather than specifying a user-login domain by its ASID, this              specifier targets the domain based on which user it is associated with and is generally more conve-              nient.              Note: GUI domains and user domains share many resources. For the purposes of the Mach bootstrap              name lookups, they are "flat", so they share the same set of registered names. But they still have              discrete sets of services. So when printing the user domain's contents, you may see many Mach boot-              strap name registrations from services that exist in the GUI domain for that user, but you will not              see the services themselves in that list.     session/<asid>/[service-name]              Targets the session domain for the given audit session ID or a service within that domain. For more              information about audit sessions, see auditon(2) and libbsm(3)     pid/<pid>/[service-name]              Targets the domain for the given PID or a service within that domain. Each process on the system              will have a PID domain associated with it that consists of the XPC services visible to that process              which can be reached with xpc_connection_create(3).     ... ... ... ... 
  • 设置打开文件数
$sudo launchctl limit maxfiles 100000 500000$sudo ulimit -n 100000
  • 设置进程数
$sudo launchctl limit maxproc 100000 100000$ launchctl limit     cpu         unlimited      unlimited          filesize    unlimited      unlimited          data        unlimited      unlimited          stack       8388608        67104768           core        0              unlimited          rss         unlimited      unlimited          memlock     unlimited      unlimited          maxproc     709            1064               maxfiles    100000         100000 $sysctl -a  | grep "files"kern.maxfiles: 100000kern.maxfilesperproc: 100000kern.num_files: 7622

要查看有哪些项是可以修改的,请使用命令man 3 sysctl,其列出了各种类型的配置变量,还有哪些是可以修改,哪些不可以修改。

$sudo ulimit -u 1064
  • 设置端口
$sudo sysctl net.inet.ip.portrange.first=10000

要想支持更高数量的TCP并发连接的通讯处理程序,就必须修改系统对当前用户的进程同时打开的文件数量的软限制(soft limit)和硬限制(hardlimit)。其中软限制是指Linux在当前系统能够承受的告警范围内进一步限制用户同时打开的文件数,超过则会告警;硬限制则是根据系统硬件资源状况(主要是系统内存)计算出来的系统最多可同时打开的文件数量,超过则无法打开了。

查看网络的设置

$ sysctl -a | grep "net.inet.ip"net.inet.ip.portrange.lowfirst: 1023net.inet.ip.portrange.lowlast: 600net.inet.ip.portrange.first: 49152net.inet.ip.portrange.last: 65535net.inet.ip.portrange.hifirst: 49152net.inet.ip.portrange.hilast: 65535net.inet.ip.forwarding: 0net.inet.ip.redirect: 1net.inet.ip.ttl: 64net.inet.ip.rtexpire: 2400net.inet.ip.rtminexpire: 10net.inet.ip.rtmaxcache: 128net.inet.ip.sourceroute: 0net.inet.ip.accept_sourceroute: 0net.inet.ip.gifttl: 30net.inet.ip.subnets_are_local: 0net.inet.ip.mcast.maxgrpsrc: 512net.inet.ip.mcast.maxsocksrc: 128net.inet.ip.mcast.loop: 1net.inet.ip.dummynet.hash_size: 64net.inet.ip.dummynet.curr_time: 0net.inet.ip.dummynet.ready_heap: 0net.inet.ip.dummynet.extract_heap: 0net.inet.ip.dummynet.searches: 0net.inet.ip.dummynet.search_steps: 0net.inet.ip.dummynet.expire: 1net.inet.ip.dummynet.max_chain_len: 16net.inet.ip.dummynet.red_lookup_depth: 256net.inet.ip.dummynet.red_avg_pkt_size: 512net.inet.ip.dummynet.red_max_pkt_size: 1500net.inet.ip.dummynet.debug: 0net.inet.ip.fw.enable: 1net.inet.ip.fw.autoinc_step: 100net.inet.ip.fw.one_pass: 0net.inet.ip.fw.debug: 0net.inet.ip.fw.verbose: 0net.inet.ip.fw.verbose_limit: 0net.inet.ip.fw.dyn_buckets: 256net.inet.ip.fw.curr_dyn_buckets: 256net.inet.ip.fw.dyn_count: 0net.inet.ip.fw.dyn_max: 4096net.inet.ip.fw.static_count: 1net.inet.ip.fw.dyn_ack_lifetime: 300net.inet.ip.fw.dyn_syn_lifetime: 20net.inet.ip.fw.dyn_fin_lifetime: 1net.inet.ip.fw.dyn_rst_lifetime: 1net.inet.ip.fw.dyn_udp_lifetime: 10net.inet.ip.fw.dyn_short_lifetime: 5net.inet.ip.fw.dyn_keepalive: 1net.inet.ip.random_id_statistics: 0net.inet.ip.random_id_collisions: 0net.inet.ip.random_id_total: 0net.inet.ip.sendsourcequench: 0net.inet.ip.maxfragpackets: 2048net.inet.ip.fragpackets: 0net.inet.ip.maxfragsperpacket: 128net.inet.ip.scopedroute: 1net.inet.ip.adj_clear_hwcksum: 0net.inet.ip.check_interface: 0net.inet.ip.rx_chaining: 1net.inet.ip.rx_chainsz: 6net.inet.ip.input_perf: 0net.inet.ip.input_perf_bins: 0net.inet.ip.linklocal.in.allowbadttl: 1net.inet.ip.random_id: 1net.inet.ip.maxchainsent: 11net.inet.ip.select_srcif_debug: 0net.inet.ip.output_perf: 0net.inet.ip.output_perf_bins: 0net.inet.ipsec.def_policy: 1net.inet.ipsec.esp_trans_deflev: 1net.inet.ipsec.esp_net_deflev: 1net.inet.ipsec.ah_trans_deflev: 1net.inet.ipsec.ah_net_deflev: 1net.inet.ipsec.ah_cleartos: 1net.inet.ipsec.ah_offsetmask: 0net.inet.ipsec.dfbit: 0net.inet.ipsec.ecn: 0net.inet.ipsec.debug: 0net.inet.ipsec.esp_randpad: -1net.inet.ipsec.bypass: 1net.inet.ipsec.esp_port: 4500
0 0