// linux-gnu uses u_int as resource enum, which is implemented in libc as // well. // // https://gcc.gnu.org/legacy-ml/gcc/2015-08/msg00441.html // https://github.com/rust-lang/libc/blob/master/src/unix/linux_like/linux/gnu/mod.rs #[cfg_attr(any(
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")),
target_os = "hurd"
), repr(u32))] #[cfg_attr(any(
bsd,
target_os = "android",
target_os = "aix",
all(target_os = "linux", not(any(target_env = "gnu", target_env = "uclibc")))
), repr(i32))] #[non_exhaustive] pubenum Resource { #[cfg(not(any(target_os = "freebsd", netbsdlike)))] /// The maximum amount (in bytes) of virtual memory the process is /// allowed to map.
RLIMIT_AS, /// The largest size (in bytes) core(5) file that may be created.
RLIMIT_CORE, /// The maximum amount of cpu time (in seconds) to be used by each /// process.
RLIMIT_CPU, /// The maximum size (in bytes) of the data segment for a process
RLIMIT_DATA, /// The largest size (in bytes) file that may be created.
RLIMIT_FSIZE, /// The maximum number of open files for this process.
RLIMIT_NOFILE, /// The maximum size (in bytes) of the stack segment for a process.
RLIMIT_STACK,
#[cfg(target_os = "freebsd")] /// The maximum number of kqueues this user id is allowed to create.
RLIMIT_KQUEUES,
#[cfg(linux_android)] /// A limit on the combined number of flock locks and fcntl leases that /// this process may establish.
RLIMIT_LOCKS,
#[cfg(any(linux_android, target_os = "freebsd", netbsdlike))] /// The maximum size (in bytes) which a process may lock into memory /// using the mlock(2) system call.
RLIMIT_MEMLOCK,
#[cfg(linux_android)] /// A limit on the number of bytes that can be allocated for POSIX /// message queues for the real user ID of the calling process.
RLIMIT_MSGQUEUE,
#[cfg(linux_android)] /// A ceiling to which the process's nice value can be raised using /// setpriority or nice.
RLIMIT_NICE,
#[cfg(any(
linux_android,
target_os = "freebsd",
netbsdlike,
target_os = "aix",
))] /// The maximum number of simultaneous processes for this user id.
RLIMIT_NPROC,
#[cfg(target_os = "freebsd")] /// The maximum number of pseudo-terminals this user id is allowed to /// create.
RLIMIT_NPTS,
#[cfg(any(linux_android,
target_os = "freebsd",
netbsdlike,
target_os = "aix",
))] /// When there is memory pressure and swap is available, prioritize /// eviction of a process' resident pages beyond this amount (in bytes).
RLIMIT_RSS,
#[cfg(linux_android)] /// A ceiling on the real-time priority that may be set for this process /// using sched_setscheduler and sched_set‐ param.
RLIMIT_RTPRIO,
#[cfg(any(target_os = "linux"))] /// A limit (in microseconds) on the amount of CPU time that a process /// scheduled under a real-time scheduling policy may con‐ sume without /// making a blocking system call.
RLIMIT_RTTIME,
#[cfg(linux_android)] /// A limit on the number of signals that may be queued for the real /// user ID of the calling process.
RLIMIT_SIGPENDING,
#[cfg(freebsdlike)] /// The maximum size (in bytes) of socket buffer usage for this user.
RLIMIT_SBSIZE,
#[cfg(target_os = "freebsd")] /// The maximum size (in bytes) of the swap space that may be reserved /// or used by all of this user id's processes.
RLIMIT_SWAP,
#[cfg(target_os = "freebsd")] /// An alias for RLIMIT_AS.
RLIMIT_VMEM,
}
}
/// Get the current processes resource limits /// /// The special value [`RLIM_INFINITY`] indicates that no limit will be /// enforced. /// /// # Parameters /// /// * `resource`: The [`Resource`] that we want to get the limits of. /// /// # Examples /// /// ``` /// # use nix::sys::resource::{getrlimit, Resource}; /// /// let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap(); /// println!("current soft_limit: {}", soft_limit); /// println!("current hard_limit: {}", hard_limit); /// ``` /// /// # References /// /// [getrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215) /// /// [`Resource`]: enum.Resource.html pubfn getrlimit(resource: Resource) -> Result<(rlim_t, rlim_t)> { letmut old_rlim = mem::MaybeUninit::<rlimit>::uninit();
cfg_if! { if#[cfg(any(
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")),
target_os = "hurd"
))] { let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, old_rlim.as_mut_ptr()) };
} else { let res = unsafe { libc::getrlimit(resource as c_int, old_rlim.as_mut_ptr()) };
}
}
/// Set the current processes resource limits /// /// # Parameters /// /// * `resource`: The [`Resource`] that we want to set the limits of. /// * `soft_limit`: The value that the kernel enforces for the corresponding /// resource. /// * `hard_limit`: The ceiling for the soft limit. Must be lower or equal to /// the current hard limit for non-root users. /// /// The special value [`RLIM_INFINITY`] indicates that no limit will be /// enforced. /// /// # Examples /// /// ``` /// # use nix::sys::resource::{setrlimit, Resource}; /// /// let soft_limit = 512; /// let hard_limit = 1024; /// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap(); /// ``` /// /// # References /// /// [setrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215) /// /// [`Resource`]: enum.Resource.html /// /// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`. pubfn setrlimit(
resource: Resource,
soft_limit: rlim_t,
hard_limit: rlim_t,
) -> Result<()> { let new_rlim = rlimit {
rlim_cur: soft_limit,
rlim_max: hard_limit,
};
cfg_if! { if#[cfg(any(
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")),
target_os = "hurd",
))]{ let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &new_rlim as *const rlimit) };
}else{ let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const rlimit) };
}
}
Errno::result(res).map(drop)
}
libc_enum! { /// Whose resource usage should be returned by [`getrusage`]. #[repr(i32)] #[non_exhaustive] pubenum UsageWho { /// Resource usage for the current process.
RUSAGE_SELF,
/// Resource usage for all the children that have terminated and been waited for.
RUSAGE_CHILDREN,
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] /// Resource usage for the calling thread.
RUSAGE_THREAD,
}
}
/// Output of `getrusage` with information about resource usage. Some of the fields /// may be unused in some platforms, and will be always zeroed out. See their manuals /// for details. #[repr(transparent)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pubstruct Usage(rusage);
impl Usage { /// Total amount of time spent executing in user mode. pubfn user_time(&self) -> TimeVal {
TimeVal::from(self.0.ru_utime)
}
/// Total amount of time spent executing in kernel mode. pubfn system_time(&self) -> TimeVal {
TimeVal::from(self.0.ru_stime)
}
/// The resident set size at its peak, #[cfg_attr(apple_targets, doc = " in bytes.")] #[cfg_attr(not(apple_targets), doc = " in kilobytes.")] pubfn max_rss(&self) -> c_long { self.0.ru_maxrss
}
/// Integral value expressed in kilobytes times ticks of execution indicating /// the amount of text memory shared with other processes. pubfn shared_integral(&self) -> c_long { self.0.ru_ixrss
}
/// Integral value expressed in kilobytes times ticks of execution indicating /// the amount of unshared memory used by data. pubfn unshared_data_integral(&self) -> c_long { self.0.ru_idrss
}
/// Integral value expressed in kilobytes times ticks of execution indicating /// the amount of unshared memory used for stack space. pubfn unshared_stack_integral(&self) -> c_long { self.0.ru_isrss
}
/// Number of page faults that were served without resorting to I/O, with pages /// that have been allocated previously by the kernel. pubfn minor_page_faults(&self) -> c_long { self.0.ru_minflt
}
/// Number of page faults that were served through I/O (i.e. swap). pubfn major_page_faults(&self) -> c_long { self.0.ru_majflt
}
/// Number of times all of the memory was fully swapped out. pubfn full_swaps(&self) -> c_long { self.0.ru_nswap
}
/// Number of times a read was done from a block device. pubfn block_reads(&self) -> c_long { self.0.ru_inblock
}
/// Number of times a write was done to a block device. pubfn block_writes(&self) -> c_long { self.0.ru_oublock
}
/// Number of IPC messages sent. pubfn ipc_sends(&self) -> c_long { self.0.ru_msgsnd
}
/// Number of IPC messages received. pubfn ipc_receives(&self) -> c_long { self.0.ru_msgrcv
}
/// Number of signals received. pubfn signals(&self) -> c_long { self.0.ru_nsignals
}
/// Number of times a context switch was voluntarily invoked. pubfn voluntary_context_switches(&self) -> c_long { self.0.ru_nvcsw
}
/// Number of times a context switch was imposed by the kernel (usually due to /// time slice expiring or preemption by a higher priority process). pubfn involuntary_context_switches(&self) -> c_long { self.0.ru_nivcsw
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.