1*5ac9320dSKonstantin Belousov /*-
2*5ac9320dSKonstantin Belousov * SPDX-License-Identifier: BSD-2-Clause
3*5ac9320dSKonstantin Belousov *
4*5ac9320dSKonstantin Belousov * Copyright (c) 2024 The FreeBSD Foundation
5*5ac9320dSKonstantin Belousov *
6*5ac9320dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7*5ac9320dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation.
8*5ac9320dSKonstantin Belousov *
9*5ac9320dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without
10*5ac9320dSKonstantin Belousov * modification, are permitted provided that the following conditions
11*5ac9320dSKonstantin Belousov * are met:
12*5ac9320dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
13*5ac9320dSKonstantin Belousov * notice, this list of conditions and the following disclaimer.
14*5ac9320dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
15*5ac9320dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
16*5ac9320dSKonstantin Belousov * documentation and/or other materials provided with the distribution.
17*5ac9320dSKonstantin Belousov *
18*5ac9320dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*5ac9320dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*5ac9320dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*5ac9320dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*5ac9320dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*5ac9320dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*5ac9320dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*5ac9320dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*5ac9320dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*5ac9320dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*5ac9320dSKonstantin Belousov * SUCH DAMAGE.
29*5ac9320dSKonstantin Belousov */
30*5ac9320dSKonstantin Belousov
31*5ac9320dSKonstantin Belousov #include <sys/param.h>
32*5ac9320dSKonstantin Belousov #include <sys/sysctl.h>
33*5ac9320dSKonstantin Belousov #define _RLIMIT_IDENT
34*5ac9320dSKonstantin Belousov #include <sys/resource.h>
35*5ac9320dSKonstantin Belousov #include <sys/user.h>
36*5ac9320dSKonstantin Belousov
37*5ac9320dSKonstantin Belousov #include <err.h>
38*5ac9320dSKonstantin Belousov #include <errno.h>
39*5ac9320dSKonstantin Belousov #include <libprocstat.h>
40*5ac9320dSKonstantin Belousov #include <stdio.h>
41*5ac9320dSKonstantin Belousov #include <stdlib.h>
42*5ac9320dSKonstantin Belousov #include <stdint.h>
43*5ac9320dSKonstantin Belousov #include <libutil.h>
44*5ac9320dSKonstantin Belousov
45*5ac9320dSKonstantin Belousov #include "procstat.h"
46*5ac9320dSKonstantin Belousov
47*5ac9320dSKonstantin Belousov static const char ru[] = "resource_usage";
48*5ac9320dSKonstantin Belousov
49*5ac9320dSKonstantin Belousov void
procstat_rlimitusage(struct procstat * procstat,struct kinfo_proc * kipp)50*5ac9320dSKonstantin Belousov procstat_rlimitusage(struct procstat *procstat, struct kinfo_proc *kipp)
51*5ac9320dSKonstantin Belousov {
52*5ac9320dSKonstantin Belousov rlim_t *resuse;
53*5ac9320dSKonstantin Belousov unsigned int cnt, i;
54*5ac9320dSKonstantin Belousov
55*5ac9320dSKonstantin Belousov if ((procstat_opts & PS_OPT_NOHEADER) == 0)
56*5ac9320dSKonstantin Belousov xo_emit("{T:/%7s %12s %4s %18s}\n",
57*5ac9320dSKonstantin Belousov "PID", "RESOURCE", "ID", "USAGE");
58*5ac9320dSKonstantin Belousov
59*5ac9320dSKonstantin Belousov xo_emit("{ek:process_id/%d}", kipp->ki_pid);
60*5ac9320dSKonstantin Belousov
61*5ac9320dSKonstantin Belousov resuse = procstat_getrlimitusage(procstat, kipp, &cnt);
62*5ac9320dSKonstantin Belousov if (resuse == NULL)
63*5ac9320dSKonstantin Belousov return;
64*5ac9320dSKonstantin Belousov xo_open_list(ru);
65*5ac9320dSKonstantin Belousov for (i = 0; i < cnt; i++) {
66*5ac9320dSKonstantin Belousov xo_open_instance(ru);
67*5ac9320dSKonstantin Belousov xo_emit("{dk:process_id/%7d} ", kipp->ki_pid);
68*5ac9320dSKonstantin Belousov xo_emit("{:resource/%12s} ", i < nitems(rlimit_ident) ?
69*5ac9320dSKonstantin Belousov rlimit_ident[i] : "unknown");
70*5ac9320dSKonstantin Belousov xo_emit("{:resid/%4d} ", i);
71*5ac9320dSKonstantin Belousov xo_emit("{:usage/%18jd}\n", (intmax_t)resuse[i]);
72*5ac9320dSKonstantin Belousov xo_close_instance(ru);
73*5ac9320dSKonstantin Belousov }
74*5ac9320dSKonstantin Belousov xo_close_list(ru);
75*5ac9320dSKonstantin Belousov procstat_freerlimitusage(procstat, resuse);
76*5ac9320dSKonstantin Belousov }
77