From owner-p4-projects@FreeBSD.ORG Mon Jun 29 20:58:19 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 4FCA31065677; Mon, 29 Jun 2009 20:58:19 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0FA1C1065672 for ; Mon, 29 Jun 2009 20:58:19 +0000 (UTC) (envelope-from trasz@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id F129A8FC15 for ; Mon, 29 Jun 2009 20:58:18 +0000 (UTC) (envelope-from trasz@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n5TKwIex087568 for ; Mon, 29 Jun 2009 20:58:18 GMT (envelope-from trasz@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n5TKwIuJ087566 for perforce@freebsd.org; Mon, 29 Jun 2009 20:58:18 GMT (envelope-from trasz@freebsd.org) Date: Mon, 29 Jun 2009 20:58:18 GMT Message-Id: <200906292058.n5TKwIuJ087566@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to trasz@freebsd.org using -f From: Edward Tomasz Napierala To: Perforce Change Reviews Cc: Subject: PERFORCE change 165445 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Jun 2009 20:58:20 -0000 http://perforce.freebsd.org/chv.cgi?CH=165445 Change 165445 by trasz@trasz_victim on 2009/06/29 20:57:56 Make syscall interface for resource usage completely strings based; no more passing of 'struct hrl_usage'. Affected files ... .. //depot/projects/soc2009/trasz_limits/sys/compat/svr4/svr4_filio.c#3 edit .. //depot/projects/soc2009/trasz_limits/sys/kern/kern_hrl.c#21 edit .. //depot/projects/soc2009/trasz_limits/sys/sys/hrl.h#16 edit .. //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#12 edit Differences ... ==== //depot/projects/soc2009/trasz_limits/sys/compat/svr4/svr4_filio.c#3 (text+ko) ==== @@ -78,7 +78,7 @@ } PROC_UNLOCK(td->td_proc); - hrl_alloc(HRL_RESOURCE_OPENFILES, uap->nfds); + hrl_alloc(HRL_RESOURCE_FILEDESCRIPTORS, uap->nfds); pa.fds = uap->fds; pa.nfds = uap->nfds; @@ -105,7 +105,7 @@ forget to update it if I add more code */ } done: - hrl_free(HRL_RESOURCE_OPENFILES, uap->nfds); + hrl_free(HRL_RESOURCE_FILEDESCRIPTORS, uap->nfds); free(pfd, M_TEMP); return error; } ==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_hrl.c#21 (text+ko) ==== @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -792,12 +793,27 @@ return (error); } +static struct sbuf * +hrl_usage_to_sbuf(struct hrl_usage *usage) +{ + int i; + struct sbuf *sb; + + sb = sbuf_new_auto(); + for (i = 1; i < HRL_RESOURCE_MAX; i++) { + sbuf_printf(sb, "%s=%jd,", hrl_resource_name(i), + usage->hu_resources[i]); + } + return (sb); +} + static int -hrl_get_usage_pid(struct thread *td, char *inputstr, void *bufp, size_t buflen) +hrl_get_usage_pid(struct thread *td, char *inputstr, struct sbuf **outputsbuf) { int error; id_t pid; struct proc *p; + struct hrl_usage usage; error = str2id(inputstr, &pid); if (error) @@ -806,14 +822,16 @@ if ((p = zpfind(pid)) == NULL) return (ESRCH); } - error = copyout(&p->p_usage, bufp, sizeof(p->p_usage)); + usage = p->p_usage; PROC_UNLOCK(p); + *outputsbuf = hrl_usage_to_sbuf(&usage); + return (error); } static int -hrl_get_usage_uid(struct thread *td, char *inputstr, void *bufp, size_t buflen) +hrl_get_usage_uid(struct thread *td, char *inputstr, struct sbuf **outputsbuf) { int error; id_t uid; @@ -825,14 +843,14 @@ uip = uifind_existing(uid); if (uip == NULL) return (ESRCH); - error = copyout(&uip->ui_usage, bufp, sizeof(uip->ui_usage)); + *outputsbuf = hrl_usage_to_sbuf(&uip->ui_usage); uifree(uip); return (error); } static int -hrl_get_usage_gid(struct thread *td, char *inputstr, void *bufp, size_t buflen) +hrl_get_usage_gid(struct thread *td, char *inputstr, struct sbuf **outputsbuf) { int error; id_t gid; @@ -844,14 +862,14 @@ gip = gifind_existing(gid); if (gip == NULL) return (ESRCH); - error = copyout(&gip->gi_usage, bufp, sizeof(gip->gi_usage)); + *outputsbuf = hrl_usage_to_sbuf(&gip->gi_usage); gifree(gip); return (error); } static int -hrl_get_usage_jid(struct thread *td, char *inputstr, void *bufp, size_t buflen) +hrl_get_usage_jid(struct thread *td, char *inputstr, struct sbuf **outputsbuf) { int error; id_t jid; @@ -867,7 +885,7 @@ sx_xunlock(&allprison_lock); return (ENOENT); } - error = copyout(&pr->pr_usage, bufp, sizeof(pr->pr_usage)); + *outputsbuf = hrl_usage_to_sbuf(&pr->pr_usage); prison_free(pr); sx_xunlock(&allprison_lock); @@ -879,6 +897,7 @@ { int error; char *inputstr = NULL; + struct sbuf *outputsbuf = NULL; if (uap->inbufp != NULL && uap->inbuflen != 0) { if (uap->inbuflen <= 0) @@ -901,28 +920,39 @@ error = hrl_remove_rule(td, inputstr); break; case HRL_OP_GET_USAGE_PID: - error = hrl_get_usage_pid(td, inputstr, uap->outbufp, uap->outbuflen); + error = hrl_get_usage_pid(td, inputstr, &outputsbuf); break; case HRL_OP_GET_USAGE_UID: - error = hrl_get_usage_uid(td, inputstr, uap->outbufp, uap->outbuflen); + error = hrl_get_usage_uid(td, inputstr, &outputsbuf); break; case HRL_OP_GET_USAGE_GID: - error = hrl_get_usage_gid(td, inputstr, uap->outbufp, uap->outbuflen); + error = hrl_get_usage_gid(td, inputstr, &outputsbuf); break; case HRL_OP_GET_USAGE_JAILID: - error = hrl_get_usage_jid(td, inputstr, uap->outbufp, uap->outbuflen); + error = hrl_get_usage_jid(td, inputstr, &outputsbuf); break; default: error = EINVAL; } /* - * XXX: Move copyouts into this place. + * XXX: Finish moving copyouts into this place. */ + if (outputsbuf != NULL) { + sbuf_finish(outputsbuf); + if (uap->outbuflen < sbuf_len(outputsbuf) + 1) { + error = EFBIG; + goto out; + } + error = copyout(sbuf_data(outputsbuf), uap->outbufp, sbuf_len(outputsbuf) + 1); + } + out: if (inputstr != NULL) free(inputstr, M_HRL); + if (outputsbuf != NULL) + sbuf_delete(outputsbuf); return (error); } ==== //depot/projects/soc2009/trasz_limits/sys/sys/hrl.h#16 (text+ko) ==== @@ -103,6 +103,8 @@ #define HRL_OP_GET_USAGE_GID 4 #define HRL_OP_GET_USAGE_JAILID 5 +#ifdef _KERNEL + /* * 'hrl_usage' defines resource consumption for a particular * object, such as process or user. @@ -111,8 +113,6 @@ int64_t hu_resources[HRL_RESOURCE_MAX + 1]; }; -#ifdef _KERNEL - struct proc; int hrl_alloc(int object, uint64_t amount); @@ -131,7 +131,7 @@ #else /* !_KERNEL */ __BEGIN_DECLS -int hrl(int op, const void *inbufp, size_t inbuflen, void *outbufp, size_t outbuflen); +int hrl(int op, const char *inbufp, size_t inbuflen, char *outbufp, size_t outbuflen); __END_DECLS #endif /* !_KERNEL */ ==== //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#12 (text+ko) ==== @@ -295,25 +295,31 @@ static void print_usage(int op, id_t id) { - int error, i; - struct hrl_usage usage; - char *str; + int error; + char *inbuf, *outbuf = NULL, *tmp; + size_t outbuflen = BUFLEN_DEFAULT / 4; - asprintf(&str, "%d", (int)id); - if (str == NULL) + asprintf(&inbuf, "%d", (int)id); + if (inbuf == NULL) err(1, "asprintf"); - error = hrl(op, str, strlen(str) + 1, &usage, sizeof(usage)); - if (error) - err(1, "hrl"); + do { + outbuflen *= 4; + outbuf = realloc(outbuf, outbuflen); + if (outbuf == NULL) + err(1, "realloc"); + + error = hrl(op, inbuf, strlen(inbuf) + 1, outbuf, outbuflen); + if (error && errno != EFBIG) + err(1, "hrl"); + } while (error && errno == EFBIG); - printf("Resource utilisation:\n"); + for (tmp = outbuf; *tmp != '\0'; tmp++) + if (*tmp == ',') + *tmp = '\n'; - for (i = 1; i < HRL_RESOURCE_MAX + 1; i++) { - print_resource(i); - printf("="); - printf("%jd\n", usage.hu_resources[i]); - } + printf("Resource utilisation:\n%s\n", outbuf); + free(outbuf); } /*