Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Jun 2009 20:58:18 GMT
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 165445 for review
Message-ID:  <200906292058.n5TKwIuJ087566@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <sys/eventhandler.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
+#include <sys/sbuf.h>
 #include <sys/tree.h>
 #include <vm/uma.h>
 
@@ -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);
 }
 
 /*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906292058.n5TKwIuJ087566>