PERFORCE change 164326 for review
Edward Tomasz Napierala
trasz at FreeBSD.org
Sun Jun 14 10:49:09 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=164326
Change 164326 by trasz at trasz_victim on 2009/06/14 10:48:25
Working resource accounting retrieval.
Affected files ...
.. //depot/projects/soc2009/trasz_limits/lib/libc/sys/Symbol.map#3 edit
.. //depot/projects/soc2009/trasz_limits/sys/kern/kern_hrl.c#8 edit
.. //depot/projects/soc2009/trasz_limits/sys/sys/hrl.h#7 edit
.. //depot/projects/soc2009/trasz_limits/sys/sys/proc.h#4 edit
.. //depot/projects/soc2009/trasz_limits/sys/sys/resourcevar.h#3 edit
.. //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#7 edit
Differences ...
==== //depot/projects/soc2009/trasz_limits/lib/libc/sys/Symbol.map#3 (text) ====
@@ -357,8 +357,7 @@
setfib;
symlinkat;
unlinkat;
- hrl_get;
- hrl_set;
+ hrl;
};
FBSDprivate_1.0 {
==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_hrl.c#8 (text+ko) ====
@@ -34,6 +34,7 @@
#include <sys/kernel.h>
#include <sys/priv.h>
#include <sys/proc.h>
+#include <sys/resourcevar.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/systm.h>
@@ -107,7 +108,8 @@
/*
* XXX: Obviously wrong, fix later.
*/
- p->p_resources[resource] += amount;
+ p->p_accounting.ha_resources[resource] += amount;
+ p->p_ucred->cr_ruidinfo->ui_accounting.ha_resources[resource] += amount;
/*
* XXX: When denying, return proper errno - EFSIZ, ENOMEM etc.
@@ -119,6 +121,12 @@
int
hrl_allocated_proc(struct proc *p, int resource, uint64_t amount)
{
+ int64_t diff;
+
+ diff = amount - p->p_accounting.ha_resources[resource];
+ p->p_accounting.ha_resources[resource] += diff;
+ p->p_ucred->cr_ruidinfo->ui_accounting.ha_resources[resource] += diff;
+
/*
* XXX: Make sure process can lower its resource consumption,
* even when already above the limit.
@@ -133,7 +141,8 @@
KASSERT(amount > 0, ("invalid amount"));
- p->p_resources[resource] -= amount;
+ p->p_accounting.ha_resources[resource] -= amount;
+ p->p_ucred->cr_ruidinfo->ui_accounting.ha_resources[resource] -= amount;
}
int
@@ -221,7 +230,7 @@
static int
hrl_get_rules(struct thread *td, void *bufp, size_t buflen)
{
- int error, copied = 0;
+ int error = 0, copied = 0;
struct hrl_rule *buf;
struct hrl_node *node;
@@ -266,23 +275,78 @@
return (error);
}
+static int
+hrl_get_acc_pid(struct thread *td, id_t pid, void *bufp, size_t buflen)
+{
+ int error;
+ struct proc *p;
+
+ if ((p = pfind(pid)) == NULL) {
+ if ((p = zpfind(pid)) == NULL)
+ return (ESRCH);
+ }
+ error = copyout(&p->p_accounting, bufp, sizeof(p->p_accounting));
+ PROC_UNLOCK(p);
+
+ return (error);
+}
+
+static int
+hrl_get_acc_uid(struct thread *td, id_t pid, void *bufp, size_t buflen)
+{
+ int error;
+ struct uidinfo *uip;
+
+ uip = uifind(pid);
+ if (uip == NULL)
+ return (ESRCH);
+ error = copyout(&uip->ui_accounting, bufp, sizeof(uip->ui_accounting));
+ uifree(uip);
+
+ return (error);
+}
+
int
hrl(struct thread *td, struct hrl_args *uap)
{
- switch (uap->op) {
- case HRL_OP_GET_RULES:
+ int error;
+ id_t id;
+
+ if (uap->op == HRL_OP_GET_RULES)
return (hrl_get_rules(td, uap->outbufp, uap->outbuflen));
- case HRL_OP_GET_ACC_PID:
- return (hrl_get_acc_pid(td, uap->outbufp, uap->outbuflen));
- default:
+
+ if (uap->inbuflen != sizeof(id_t))
+ return (EINVAL);
+
+ error = copyin(uap->inbufp, &id, sizeof(id_t));
+ if (error)
+ return (error);
+
+ if (id <= 0)
return (EINVAL);
- }
+
+ if (uap->outbuflen < sizeof(struct hrl_acc))
+ return (EFBIG);
+
+ if (uap->op == HRL_OP_GET_ACC_PID)
+ return (hrl_get_acc_pid(td, id, uap->outbufp, uap->outbuflen));
+
+ if (uap->op == HRL_OP_GET_ACC_UID)
+ return (hrl_get_acc_uid(td, id, uap->outbufp, uap->outbuflen));
+
+ return (EINVAL);
}
static void
hrl_proc_exit(void *arg __unused, struct proc *p)
{
+ int i;
struct hrl_node *node, *next;
+ struct uidinfo *uip;
+
+ uip = p->p_ucred->cr_ruidinfo;
+ for (i = 0; i < HRL_RESOURCE_MAX + 1; i++)
+ uip->ui_accounting.ha_resources[i] -= p->p_accounting.ha_resources[i];
/*
* Remove temporary rules created via setrlimit(2).
==== //depot/projects/soc2009/trasz_limits/sys/sys/hrl.h#7 (text+ko) ====
@@ -90,13 +90,21 @@
#define HRL_ACTION_SIGXFSZ 0x0009
#define HRL_ACTION_MAX HRL_ACTION_SIGXFSZ
-#define HRL_MAX_LIMITS 128
+#define HRL_MAX_LIMITS 1024
#define HRL_OP_GET_RULES 1
#define HRL_OP_GET_ACC_PID 2
#define HRL_OP_GET_ACC_UID 3
#define HRL_OP_GET_ACC_GID 4
+/*
+ * 'hrl_acc' defines resource consumption for a particular
+ * object, such as process or user.
+ */
+struct hrl_acc {
+ int64_t ha_resources[HRL_RESOURCE_MAX + 1];
+};
+
#ifdef _KERNEL
struct proc;
==== //depot/projects/soc2009/trasz_limits/sys/sys/proc.h#4 (text+ko) ====
@@ -513,6 +513,7 @@
int p_boundary_count;/* (c) Num threads at user boundary */
int p_pendingcnt; /* how many signals are pending */
struct itimers *p_itimers; /* (c) POSIX interval timers. */
+ struct hrl_acc p_accounting; /* (*) HRL resource accounting */
/* End area that is zeroed on creation. */
#define p_endzero p_magic
@@ -546,7 +547,6 @@
LIST_HEAD(, mqueue_notifier) p_mqnotifier; /* (c) mqueue notifiers.*/
struct kdtrace_proc *p_dtrace; /* (*) DTrace-specific data. */
struct cv p_pwait; /* (*) wait cv for exit/exec */
- int64_t p_resources[HRL_RESOURCE_MAX + 1];
};
#define p_session p_pgrp->pg_session
==== //depot/projects/soc2009/trasz_limits/sys/sys/resourcevar.h#3 (text+ko) ====
@@ -95,7 +95,7 @@
long ui_ptscnt; /* (b) number of pseudo-terminals */
uid_t ui_uid; /* (a) uid */
u_int ui_ref; /* (b) reference count */
- int64_t ui_resources[HRL_RESOURCE_MAX + 1];
+ struct hrl_acc ui_accounting; /* (*) HRL resource accounting */
};
struct proc;
==== //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#7 (text+ko) ====
@@ -248,9 +248,9 @@
err(1, "realloc");
error = hrl(HRL_OP_GET_RULES, NULL, 0, rules, ruleslen);
- if (error != 0 && error != EFBIG)
- err(1, "hrl_get");
- } while (error == EFBIG);
+ if (error && errno != EFBIG)
+ err(1, "hrl");
+ } while (error && errno == EFBIG);
printf("Defined resource limits:\n");
@@ -282,46 +282,19 @@
static void
print_accounting(int op, id_t id)
{
- int error;
- size_t ruleslen, i;
- struct hrl_rule *rules = NULL;
+ int error, i;
+ struct hrl_acc acc;
- ruleslen = BUFLEN_DEFAULT / 4;
- do {
- ruleslen *= 4;
- rules = realloc(rules, ruleslen);
- if (rules == NULL)
- err(1, "realloc");
-
- error = hrl(op, &id, sizeof(id), rules, ruleslen);
- if (error != 0 && error != EFBIG)
- err(1, "hrl_get");
- } while (error == EFBIG);
+ error = hrl(op, &id, sizeof(id), &acc, sizeof(acc));
+ if (error)
+ err(1, "hrl");
printf("Resource utilisation:\n");
- if (rules[0].hr_subject == 0) {
- printf("No resource utilisation recorded.\n");
-
- return;
- }
-
- for (i = 0; i < ruleslen / sizeof(*rules); i++) {
- /* NULL entry terminating the table? */
- if (rules[i].hr_subject == 0)
- break;
- print_subject(rules[i].hr_subject, rules[i].hr_subject_id);
- printf(":");
- print_resource(rules[i].hr_resource);
- printf(":");
- print_action(rules[i].hr_action);
+ for (i = 1; i < HRL_RESOURCE_MAX + 1; i++) {
+ print_resource(i);
printf("=");
- printf("%jd", rules[i].hr_amount);
- if (rules[i].hr_subject != rules[i].hr_per) {
- printf("/");
- print_per(rules[i].hr_per);
- }
- printf("\n");
+ printf("%jd\n", acc.ha_resources[i]);
}
}
More information about the p4-projects
mailing list