[launchd-changes] [23186] trunk/launchd/src

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 28 17:43:31 PDT 2007


Revision: 23186
          http://trac.macosforge.org/projects/launchd/changeset/23186
Author:   zarzycki at apple.com
Date:     2007-03-28 17:43:31 -0700 (Wed, 28 Mar 2007)

Log Message:
-----------
Amortize kevent() fetching. I've seen more than 50 events pulled out of the
kernel all at once during shutdown with this change.

15-25 events is much more common during shutdown.

Modified Paths:
--------------
    trunk/launchd/src/launchd.c
    trunk/launchd/src/launchd_core_logic.c
    trunk/launchd/src/launchd_runtime.c
    trunk/launchd/src/launchd_runtime.h
    trunk/launchd/src/launchd_unix_ipc.c
    trunk/launchd/src/launchd_unix_ipc.h
    trunk/launchd/src/liblaunch.c
    trunk/launchd/src/liblaunch_private.h

Modified: trunk/launchd/src/launchd.c
===================================================================
--- trunk/launchd/src/launchd.c	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/launchd.c	2007-03-29 00:43:31 UTC (rev 23186)
@@ -304,13 +304,13 @@
 	int tmpfd;
 
 	if (-1 != (tmpfd = dup(fd))) {
-		launchd_assumes(close(tmpfd) == 0);
+		launchd_assumes(runtime_close(tmpfd) == 0);
 	} else {
 		if (-1 == (tmpfd = open(path, flags | O_NOCTTY, DEFFILEMODE))) {
 			runtime_syslog(LOG_ERR, "open(\"%s\", ...): %m", path);
 		} else if (tmpfd != fd) {
 			launchd_assumes(dup2(tmpfd, fd) != -1);
-			launchd_assumes(close(tmpfd) == 0);
+			launchd_assumes(runtime_close(tmpfd) == 0);
 		}
 	}
 }
@@ -386,7 +386,7 @@
 	kev_req.kev_class = KEV_NETWORK_CLASS;
 
 	if (!launchd_assumes(ioctl(pfs, SIOCSKEVFILT, &kev_req) != -1)) {
-		close(pfs);
+		runtime_close(pfs);
 		return;
 	}
 

Modified: trunk/launchd/src/launchd_core_logic.c
===================================================================
--- trunk/launchd/src/launchd_core_logic.c	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/launchd_core_logic.c	2007-03-29 00:43:31 UTC (rev 23186)
@@ -673,11 +673,11 @@
 	}
 
 	if (!job_assumes(j, j->forkfd == 0)) {
-		job_assumes(j, close(j->forkfd) != -1);
+		job_assumes(j, runtime_close(j->forkfd) != -1);
 	}
 
 	if (!job_assumes(j, j->log_redirect_fd == 0)) {
-		job_assumes(j, close(j->log_redirect_fd) != -1);
+		job_assumes(j, runtime_close(j->log_redirect_fd) != -1);
 	}
 
 	if (j->j_port) {
@@ -1704,12 +1704,12 @@
 	}
 
 	if (j->log_redirect_fd && (!j->wait4pipe_eof || j->mgr->shutting_down)) {
-		job_assumes(j, close(j->log_redirect_fd) != -1);
+		job_assumes(j, runtime_close(j->log_redirect_fd) != -1);
 		j->log_redirect_fd = 0;
 	}
 
 	if (j->forkfd) {
-		job_assumes(j, close(j->forkfd) != -1);
+		job_assumes(j, runtime_close(j->forkfd) != -1);
 		j->forkfd = 0;
 	}
 
@@ -1849,7 +1849,7 @@
 
 	if (rsz == 0) {
 		job_log(j, LOG_DEBUG, "Standard out/error pipe closed");
-		job_assumes(j, close(j->log_redirect_fd) != -1);
+		job_assumes(j, runtime_close(j->log_redirect_fd) != -1);
 		j->log_redirect_fd = 0;
 		job_dispatch(j, false);
 	} else if (job_assumes(j, rsz != -1)) {
@@ -2085,25 +2085,25 @@
 	switch (c = runtime_fork(j->weird_bootstrap ? j->j_port : j->mgr->jm_port)) {
 	case -1:
 		job_log_error(j, LOG_ERR, "fork() failed, will try again in one second");
-		job_assumes(j, close(execspair[0]) == 0);
-		job_assumes(j, close(execspair[1]) == 0);
+		job_assumes(j, runtime_close(execspair[0]) == 0);
+		job_assumes(j, runtime_close(execspair[1]) == 0);
 		if (sipc) {
-			job_assumes(j, close(spair[0]) == 0);
-			job_assumes(j, close(spair[1]) == 0);
+			job_assumes(j, runtime_close(spair[0]) == 0);
+			job_assumes(j, runtime_close(spair[1]) == 0);
 		}
 		break;
 	case 0:
 		if (!j->legacy_mach_job) {
 			job_assumes(j, dup2(oepair[1], STDOUT_FILENO) != -1);
 			job_assumes(j, dup2(oepair[1], STDERR_FILENO) != -1);
-			job_assumes(j, close(oepair[1]) != -1);
+			job_assumes(j, runtime_close(oepair[1]) != -1);
 		}
-		job_assumes(j, close(execspair[0]) == 0);
+		job_assumes(j, runtime_close(execspair[0]) == 0);
 		/* wait for our parent to say they've attached a kevent to us */
 		read(_fd(execspair[1]), &c, sizeof(c));
 
 		if (sipc) {
-			job_assumes(j, close(spair[0]) == 0);
+			job_assumes(j, runtime_close(spair[0]) == 0);
 			snprintf(nbuf, sizeof(nbuf), "%d", spair[1]);
 			setenv(LAUNCHD_TRUSTED_FD_ENV, nbuf, 1);
 		}
@@ -2114,13 +2114,13 @@
 		LIST_INSERT_HEAD(&j->mgr->active_jobs[ACTIVE_JOB_HASH(c)], j, pid_hash_sle);
 
 		if (!j->legacy_mach_job) {
-			job_assumes(j, close(oepair[1]) != -1);
+			job_assumes(j, runtime_close(oepair[1]) != -1);
 		}
 		j->p = c;
 		j->forkfd = _fd(execspair[0]);
-		job_assumes(j, close(execspair[1]) == 0);
+		job_assumes(j, runtime_close(execspair[1]) == 0);
 		if (sipc) {
-			job_assumes(j, close(spair[1]) == 0);
+			job_assumes(j, runtime_close(spair[1]) == 0);
 			ipc_open(_fd(spair[0]), j);
 		}
 		if (kevent_mod(c, EVFILT_PROC, EV_ADD, /* NOTE_EXEC|NOTE_FORK| */ NOTE_EXIT, 0, root_jobmgr ? root_jobmgr : j->mgr) == -1) {
@@ -2430,7 +2430,7 @@
 	}
 
 	job_assumes(j, dup2(fd, target_fd) != -1);
-	job_assumes(j, close(fd) == 0);
+	job_assumes(j, runtime_close(fd) == 0);
 }
 
 int
@@ -2745,7 +2745,7 @@
 
 	if (invalidation_reason[0]) {
 		job_log(j, LOG_DEBUG, "Path %s: %s", invalidation_reason, si->what);
-		job_assumes(j, close(si->fd) == 0);
+		job_assumes(j, runtime_close(si->fd) == 0);
 		si->fd = -1; /* this will get fixed in semaphoreitem_watch() */
 	}
 
@@ -2873,7 +2873,7 @@
 	unsigned int i;
 
 	for (i = 0; i < sg->fd_cnt; i++)
-		job_assumes(j, close(sg->fds[i]) != -1);
+		job_assumes(j, runtime_close(sg->fds[i]) != -1);
 
 	SLIST_REMOVE(&j->sockets, sg, socketgroup, sle);
 
@@ -3148,7 +3148,7 @@
 		case PATH_MISSING:
 			if ((bool)(stat(si->what, &sb) == 0) == wanted_state) {
 				if (si->fd != -1) {
-					job_assumes(j, close(si->fd) == 0);
+					job_assumes(j, runtime_close(si->fd) == 0);
 					si->fd = -1;
 				}
 				job_log(j, LOG_DEBUG, "KeepAlive: The following path %s: %s", wanted_state ? "exists" : "is missing", si->what);
@@ -3511,7 +3511,7 @@
 	/* this unblocks the child and avoids a race
 	 * between the above fork() and the kevent_mod() */
 	job_assumes(j, write(j->forkfd, &c, sizeof(c)) == sizeof(c));
-	job_assumes(j, close(j->forkfd) != -1);
+	job_assumes(j, runtime_close(j->forkfd) != -1);
 	j->forkfd = 0;
 }
 
@@ -3570,8 +3570,8 @@
 			int dfd, lfd = strtol(trusted_fd, NULL, 10);
 
 			if ((dfd = dup(lfd)) >= 0) {
-				jobmgr_assumes(jmr, close(dfd) != -1);
-				jobmgr_assumes(jmr, close(lfd) != -1);
+				jobmgr_assumes(jmr, runtime_close(dfd) != -1);
+				jobmgr_assumes(jmr, runtime_close(lfd) != -1);
 			}
 
 			unsetenv(LAUNCHD_TRUSTED_FD_ENV);
@@ -3970,11 +3970,11 @@
 
 	if (logfile_fd != -1) {
 		job_assumes(j, fcntl(logfile_fd, F_FULLFSYNC, 0) != -1);
-		job_assumes(j, close(logfile_fd) != -1);
+		job_assumes(j, runtime_close(logfile_fd) != -1);
 	}
 
 	if (console_fd != -1) {
-		job_assumes(j, close(console_fd) != -1);
+		job_assumes(j, runtime_close(console_fd) != -1);
 	}
 
 	job_log(j, LOG_DEBUG, "Finished sampling.");
@@ -4012,7 +4012,7 @@
 	SLIST_REMOVE(&j->semaphores, si, semaphoreitem, sle);
 
 	if (si->fd != -1) {
-		job_assumes(j, close(si->fd) != -1);
+		job_assumes(j, runtime_close(si->fd) != -1);
 	}
 
 	free(si);

Modified: trunk/launchd/src/launchd_runtime.c
===================================================================
--- trunk/launchd/src/launchd_runtime.c	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/launchd_runtime.c	2007-03-29 00:43:31 UTC (rev 23186)
@@ -71,12 +71,17 @@
 static int mainkq;
 static int asynckq;
 
+#define BULK_KEV_MAX 100
+static struct kevent *bulk_kev;
+static int bulk_kev_i;
+static int bulk_kev_cnt;
+
 static pthread_t kqueue_demand_thread;
 static pthread_t demand_thread;
 
 static void *mport_demand_loop(void *arg);
 static void *kqueue_demand_loop(void *arg);
-static void log_kevent_struct(int level, struct kevent *kev);
+static void log_kevent_struct(int level, struct kevent *kev, int indx);
 
 static void async_callback(void);
 static kq_callback kqasync_callback = (kq_callback)async_callback;
@@ -234,7 +239,7 @@
 }
 
 void
-log_kevent_struct(int level, struct kevent *kev)
+log_kevent_struct(int level, struct kevent *kev, int indx)
 {
 	const char *filter_str;
 	char ident_buf[100];
@@ -394,8 +399,8 @@
 		break;
 	}
 
-	runtime_syslog(level, "KEVENT: udata = %p data = 0x%lx ident = %s filter = %s flags = %s fflags = %s",
-			kev->udata, kev->data, ident_buf, filter_str, flags_buf, fflags_buf);
+	runtime_syslog(level, "KEVENT[%d]: udata = %p data = 0x%lx ident = %s filter = %s flags = %s fflags = %s",
+			indx, kev->udata, kev->data, ident_buf, filter_str, flags_buf, fflags_buf);
 }
 
 kern_return_t
@@ -423,7 +428,7 @@
 #if 0
 			if (launchd_assumes(kev.udata != NULL)) {
 #endif
-				log_kevent_struct(LOG_DEBUG, &kev);
+				log_kevent_struct(LOG_DEBUG, &kev, 0);
 				(*((kq_callback *)kev.udata))(kev.udata, &kev);
 #if 0
 			} else {
@@ -461,19 +466,28 @@
 x_handle_kqueue(mach_port_t junk __attribute__((unused)), integer_t fd)
 {
 	struct timespec ts = { 0, 0 };
-	struct kevent kev;
-	int kevr;
+	struct kevent kev[BULK_KEV_MAX];
+	int i;
 
-	launchd_assumes((kevr = kevent(fd, NULL, 0, &kev, 1, &ts)) != -1);
+	bulk_kev = kev;
 
-	if (kevr == 1) {
+	launchd_assumes((bulk_kev_cnt = kevent(fd, NULL, 0, kev, BULK_KEV_MAX, &ts)) != -1);
+
+	if (bulk_kev_cnt > 0) {
 #if 0
 		Dl_info dli;
 
 		if (launchd_assumes(malloc_size(kev.udata) || dladdr(kev.udata, &dli))) {
 #endif
-			log_kevent_struct(LOG_DEBUG, &kev);
-			(*((kq_callback *)kev.udata))(kev.udata, &kev);
+		for (i = 0; i < bulk_kev_cnt; i++) {
+			log_kevent_struct(LOG_DEBUG, &kev[i], i);
+		}
+		for (i = 0; i < bulk_kev_cnt; i++) {
+			bulk_kev_i = i;
+			if (kev[i].filter) {
+				(*((kq_callback *)kev[i].udata))(kev[i].udata, &kev[i]);
+			}
+		}
 #if 0
 		} else {
 			log_kevent_struct(LOG_ERR, &kev);
@@ -481,6 +495,8 @@
 #endif
 	}
 
+	bulk_kev = NULL;
+
 	return 0;
 }
 
@@ -683,7 +699,7 @@
 	struct kevent kev;
 
 	if (launchd_assumes(kevent(asynckq, NULL, 0, &kev, 1, &timeout) == 1)) {
-		log_kevent_struct(LOG_DEBUG, &kev);
+		log_kevent_struct(LOG_DEBUG, &kev, 0);
 		(*((kq_callback *)kev.udata))(kev.udata, &kev);
 	}
 }
@@ -925,6 +941,28 @@
 	}
 }
 
+int
+runtime_close(int fd)
+{
+	int i;
+
+	if (bulk_kev) for (i = bulk_kev_i + 1; i < bulk_kev_cnt; i++) {
+		switch (bulk_kev[i].filter) {
+		case EVFILT_VNODE:
+		case EVFILT_WRITE:
+		case EVFILT_READ:
+			if ((int)bulk_kev[i].ident == fd) {
+				runtime_syslog(LOG_DEBUG, "Skipping kevent index: %d", i);
+				bulk_kev[i].filter = 0;
+			}
+		default:
+			break;
+		}
+	}
+
+	return close(fd);
+}
+
 static FILE *ourlogfile;
 
 void

Modified: trunk/launchd/src/launchd_runtime.h
===================================================================
--- trunk/launchd/src/launchd_runtime.h	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/launchd_runtime.h	2007-03-29 00:43:31 UTC (rev 23186)
@@ -59,6 +59,8 @@
 void launchd_runtime_init(void);
 void launchd_runtime(void) __attribute__((noreturn));
 
+int runtime_close(int fd);
+
 void runtime_force_on_demand(bool);
 void runtime_set_timeout(timeout_callback to_cb, mach_msg_timeout_t to);
 kern_return_t runtime_add_mport(mach_port_t name, mig_callback demux, mach_msg_size_t msg_size);

Modified: trunk/launchd/src/launchd_unix_ipc.c
===================================================================
--- trunk/launchd/src/launchd_unix_ipc.c	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/launchd_unix_ipc.c	2007-03-29 00:43:31 UTC (rev 23186)
@@ -47,15 +47,15 @@
 #include <paths.h>
 #include <string.h>
 
-#include "launch.h"
-#include "launch_priv.h"
+#include "liblaunch_public.h"
+#include "liblaunch_private.h"
 #include "launchd.h"
 #include "launchd_runtime.h"
 #include "launchd_core_logic.h"
 
 extern char **environ;
 
-static SLIST_HEAD(, conncb) connections = { NULL };
+static LIST_HEAD(, conncb) connections;
 
 static launch_data_t adjust_rlimits(launch_data_t in);
 
@@ -171,7 +171,7 @@
 
 out_bad:
 	if (!ipc_inited && fd != -1) {
-		launchd_assumes(close(fd) == 0);
+		launchd_assumes(runtime_close(fd) == 0);
 	}
 }
 
@@ -185,7 +185,7 @@
 	c->kqconn_callback = ipc_callback;
 	c->conn = launchd_fdopen(fd);
 	c->j = j;
-	SLIST_INSERT_HEAD(&connections, c, sle);
+	LIST_INSERT_HEAD(&connections, c, sle);
 	kevent_mod(fd, EVFILT_READ, EV_ADD, 0, 0, &c->kqconn_callback);
 }
 
@@ -242,7 +242,7 @@
 {
 	struct conncb *ci, *cin;
 
-	SLIST_FOREACH_SAFE(ci, &connections, sle, cin) {
+	LIST_FOREACH_SAFE(ci, &connections, sle, cin) {
 		if (ci->j == j) {
 			ipc_close(ci);
 		}
@@ -264,7 +264,7 @@
 		break;
 	case LAUNCH_DATA_FD:
 		if (launch_data_get_fd(o) != -1) {
-			launchd_assumes(close(launch_data_get_fd(o)) == 0);
+			launchd_assumes(runtime_close(launch_data_get_fd(o)) == 0);
 		}
 		break;
 	default:
@@ -457,8 +457,8 @@
 {
 	batch_job_enable(true, c);
 
-	SLIST_REMOVE(&connections, c, conncb, sle);
-	launchd_close(c->conn);
+	LIST_REMOVE(c, sle);
+	launchd_close(c->conn, runtime_close);
 	free(c);
 }
 

Modified: trunk/launchd/src/launchd_unix_ipc.h
===================================================================
--- trunk/launchd/src/launchd_unix_ipc.h	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/launchd_unix_ipc.h	2007-03-29 00:43:31 UTC (rev 23186)
@@ -24,11 +24,11 @@
 
 #include "launchd_runtime.h"
 #include "launchd_core_logic.h"
-#include "launch_priv.h"
+#include "liblaunch_private.h"
 
 struct conncb {
 	kq_callback kqconn_callback;
-	SLIST_ENTRY(conncb) sle;
+	LIST_ENTRY(conncb) sle;
 	launch_t conn;
 	job_t j;
 	int disabled_batch:1, futureflags:31;

Modified: trunk/launchd/src/liblaunch.c
===================================================================
--- trunk/launchd/src/liblaunch.c	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/liblaunch.c	2007-03-29 00:43:31 UTC (rev 23186)
@@ -210,7 +210,7 @@
 	return;
 out_bad:
 	if (_lc->l)
-		launchd_close(_lc->l);
+		launchd_close(_lc->l, close);
 	else if (lfd != -1)
 		close(lfd);
 	if (_lc)
@@ -560,7 +560,7 @@
 }
 
 void
-launchd_close(launch_t lh)
+launchd_close(launch_t lh, typeof(close) closefunc)
 {
 	if (lh->sendbuf)
 		free(lh->sendbuf);
@@ -570,7 +570,7 @@
 		free(lh->recvbuf);
 	if (lh->recvfds)
 		free(lh->recvfds);
-	close(lh->fd);
+	closefunc(lh->fd);
 	free(lh);
 }
 

Modified: trunk/launchd/src/liblaunch_private.h
===================================================================
--- trunk/launchd/src/liblaunch_private.h	2007-03-29 00:39:58 UTC (rev 23185)
+++ trunk/launchd/src/liblaunch_private.h	2007-03-29 00:43:31 UTC (rev 23186)
@@ -23,6 +23,7 @@
 #include <mach/mach.h>
 #include <sys/types.h>
 #include <launch.h>
+#include <unistd.h>
 
 #pragma GCC visibility push(default)
 
@@ -62,7 +63,7 @@
 
 launch_t launchd_fdopen(int);
 int launchd_getfd(launch_t);
-void launchd_close(launch_t);
+void launchd_close(launch_t, typeof(close) closefunc);
 
 launch_data_t   launch_data_new_errno(int);
 bool		launch_data_set_errno(launch_data_t, int);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20070328/994c62cf/attachment.html


More information about the launchd-changes mailing list