about summary refs log tree commit diff
path: root/sysdeps/mach
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/mach')
-rw-r--r--sysdeps/mach/bits/libc-lock.h21
-rw-r--r--sysdeps/mach/hurd/Dist1
-rw-r--r--sysdeps/mach/hurd/Makefile15
-rw-r--r--sysdeps/mach/hurd/Subdirs1
-rw-r--r--sysdeps/mach/hurd/profil.c153
5 files changed, 156 insertions, 35 deletions
diff --git a/sysdeps/mach/bits/libc-lock.h b/sysdeps/mach/bits/libc-lock.h
index 79a1e00464..17eeefcdb5 100644
--- a/sysdeps/mach/bits/libc-lock.h
+++ b/sysdeps/mach/bits/libc-lock.h
@@ -75,6 +75,27 @@ typedef cthread_key_t __libc_key_t;
     (*__save_FCT)(__save_ARG);						    \
 }
 
+/* Use mutexes as once control variables. */
+
+struct __libc_once
+  {
+    __libc_lock_t lock;
+    int done;
+  };
+
+#define __libc_once_define(CLASS,NAME) \
+  CLASS struct __libc_once NAME = { MUTEX_INITIALZER, 0 }
+
+
+/* Call handler iff the first call.  */
+#define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
+  do {									      \
+    __libc_lock_lock (ONCE_CONTROL.lock);				      \
+    if (!ONCE_CONTROL.done)						      \
+      (INIT_FUNCTION) ();						      \
+    ONCE_CONTROL.done = 1;						      \
+    __libc_lock_lock (ONCE_CONTROL.lock);				      \
+  } while (0)
 
 #ifdef _LIBC
 /* We need portable names for some functions.  E.g., when they are
diff --git a/sysdeps/mach/hurd/Dist b/sysdeps/mach/hurd/Dist
index 62fe4d96c6..e6067b8a73 100644
--- a/sysdeps/mach/hurd/Dist
+++ b/sysdeps/mach/hurd/Dist
@@ -1,3 +1,4 @@
 errnos.awk err_hurd.sub
 libc-ldscript
 libc_p-ldscript
+cthreads.c
diff --git a/sysdeps/mach/hurd/Makefile b/sysdeps/mach/hurd/Makefile
index 43d5eb9a2d..bf1c3b02bc 100644
--- a/sysdeps/mach/hurd/Makefile
+++ b/sysdeps/mach/hurd/Makefile
@@ -18,6 +18,9 @@
 
 ifdef in-Makerules
 
+subdirs := $(filter-out sunrpc,$(subdirs)) # XXX skip broken dirs
+# See hurd/Makefile for commands that install some crucial sunrpc headers.
+
 # Look for header files in hurd/ under the top-level library source directory.
 # Look for generated header files where they get created.
 includes += -I$(..)hurd -I$(common-objpfx)hurd/
@@ -33,7 +36,6 @@ inhibit-unix-syscalls = yes
 # be compatible with some existing binaries for that system.
 inhibit-glue = yes
 
-
 ifeq (,$(filter mach hurd,$(subdir)))
 # Subdirectories other than hurd/ might use the generated Hurd headers.
 # So make sure we get a chance to run in hurd/ to make them before all else.
@@ -48,6 +50,12 @@ $(patsubst %,$(hurd-objpfx)hurd/%.%,auth io fs process):
 	$(MAKE) -C $(..)hurd generated no_deps=t
 endif
 
+# Hurd profil.c includes this file, so give a rule to make it.
+ifeq ($(subdir),gmon)
+$(common-objpfx)hurd/../mach/RPC_task_get_sampled_pcs.c:
+	$(MAKE) -C $(..)mach generated no_deps=t
+endif
+
 
 # Generate errnos.h from the section of the manual that lists all the errno
 # codes.
@@ -115,11 +123,6 @@ $(inst_libdir)/libc.so: $(rpcuserlibs)
 ifeq (elf,$(subdir))
 $(objpfx)librtld.so: $(rpcuserlibs:.so=_pic.a)
 endif
-
-# We need the CThreads interface.
-ifeq (misc,$(subdir))
-sysdep_routines += cthreads
-endif
 
 
 endif	# in-Makerules
diff --git a/sysdeps/mach/hurd/Subdirs b/sysdeps/mach/hurd/Subdirs
index 16b8348437..739919f4b1 100644
--- a/sysdeps/mach/hurd/Subdirs
+++ b/sysdeps/mach/hurd/Subdirs
@@ -1 +1,2 @@
 hurd
+login
diff --git a/sysdeps/mach/hurd/profil.c b/sysdeps/mach/hurd/profil.c
index d3c5131b10..d76cc25a3c 100644
--- a/sysdeps/mach/hurd/profil.c
+++ b/sysdeps/mach/hurd/profil.c
@@ -34,10 +34,20 @@ static size_t maxsamples;
 static size_t pc_offset;
 static size_t sample_scale;
 static sampled_pc_seqno_t seqno;
-static struct mutex lock = MUTEX_INITIALIZER;
+static spin_lock_t lock = SPIN_LOCK_INITIALIZER;
 static mach_msg_timeout_t collector_timeout; /* ms between collections.  */
 static int profile_tick;
 
+/* Reply port used by profiler thread */
+static mach_port_t profil_reply_port;
+
+/* Forwards */
+static kern_return_t profil_task_get_sampled_pcs (mach_port_t,
+						  sampled_pc_seqno_t *,
+						  sampled_pc_array_t,
+						  mach_msg_type_number_t *);
+static void fetch_samples (void);
+
 /* Enable statistical profiling, writing samples of the PC into at most
    SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
    is enabled, the system examines the user PC and increments
@@ -87,7 +97,7 @@ update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
 }
 
 int
-__profile_frequency ()
+__profile_frequency (void)
 {
   return profile_tick;
 }
@@ -97,13 +107,20 @@ profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
 {
   error_t err;
 
-  __mutex_lock (&lock);
+  __spin_lock (&lock);
 
   if (scale == 0)
     {
       /* Disable profiling.  */
       int count;
-      __thread_suspend (profile_thread);
+
+      if (profile_thread != MACH_PORT_NULL)
+	__thread_suspend (profile_thread);
+
+      /* Fetch the last set of samples */
+      if (sample_scale)
+	fetch_samples ();
+
       err = __task_disable_pc_sampling (__mach_task_self (), &count);
       sample_scale = 0;
       seqno = 0;
@@ -111,57 +128,102 @@ profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
   else
     err = update_waiter (sample_buffer, size, offset, scale);
 
-  __mutex_unlock (&lock);
+  __spin_unlock (&lock);
 
   return err ? __hurd_fail (err) : 0;
 }
 
+/* Fetch PC samples.  This function must be very careful not to depend
+   on Hurd threadvar variables.  We arrange that by using a special
+   stub arranged for at the end of this file. */
 static void
-profile_waiter (void)
+fetch_samples (void)
 {
   sampled_pc_t pc_samples[MAX_PC_SAMPLES];
   mach_msg_type_number_t nsamples, i;
-  mach_port_t rcv = __mach_reply_port ();
-  mach_msg_header_t msg;
   error_t err;
 
-  while (1)
+  nsamples = MAX_PC_SAMPLES;
+
+  err = profil_task_get_sampled_pcs (__mach_task_self (), &seqno,
+				     pc_samples, &nsamples);
+  if (err)
     {
-      __mutex_lock (&lock);
+      static error_t special_profil_failure;
+      static volatile int a, b, c;
 
-      nsamples = sizeof pc_samples / sizeof pc_samples[0];
-      err = __task_get_sampled_pcs (__mach_task_self (), &seqno,
-				    pc_samples, &nsamples);
-      assert_perror (err);
+      special_profil_failure = err;
+      a = 1;
+      b = 0;
+      while (1)
+	c = a / b;
+    }
 
-      for (i = 0; i < nsamples; ++i)
-	{
-	  size_t idx = (((pc_samples[i].pc - pc_offset) / 2) *
-			sample_scale / 65536);
-	  if (idx < maxsamples)
-	    ++samples[idx];
-	}
+  for (i = 0; i < nsamples; ++i)
+    {
+      /* Do arithmetic in long long to avoid overflow problems. */
+      long long pc_difference = pc_samples[i].pc - pc_offset;
+      size_t idx = ((pc_difference / 2) * sample_scale) / 65536;
+      if (idx < maxsamples)
+	++samples[idx];
+    }
+}
+
+
+/* This function must be very careful not to depend on Hurd threadvar
+   variables.  We arrange that by using special stubs arranged for at the
+   end of this file. */
+static void
+profile_waiter (void)
+{
+  mach_msg_header_t msg;
+  mach_port_t timeout_reply_port;
+
+  profil_reply_port = __mach_reply_port ();
+  timeout_reply_port = __mach_reply_port ();
 
-      __vm_deallocate (__mach_task_self (),
-		       (vm_address_t) pc_samples,
-		       nsamples * sizeof *pc_samples);
+  while (1)
+    {
+      __spin_lock (&lock);
+
+      fetch_samples ();
 
-      __mutex_unlock (&lock);
+      __spin_unlock (&lock);
 
       __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
-		  rcv, collector_timeout, MACH_PORT_NULL);
+		  timeout_reply_port, collector_timeout, MACH_PORT_NULL);
     }
 }
 
-data_set_element (_hurd_fork_locks, lock);
+/* Fork interaction */
+
+/* Before fork, lock the interlock so that we are in a clean state. */
+static void
+fork_profil_prepare (void)
+{
+  __spin_lock (&lock);
+}
+text_set_element (_hurd_fork_prepare_hook, fork_profil_prepare);
+
+/* In the parent, unlock the interlock once fork is complete. */
+static void
+fork_profil_parent (void)
+{
+  __spin_unlock (&lock);
+}
+text_set_element (_hurd_fork_parent_hook, fork_profil_parent);
 
+/* In the childs, unlock the interlock, and start a profiling thread up
+   if necessary. */
 static void
-fork_profil (void)
+fork_profil_child (void)
 {
   u_short *sb;
   size_t n, o, ss;
   error_t err;
 
+  __spin_unlock (&lock);
+
   if (profile_thread != MACH_PORT_NULL)
     {
       __mach_port_deallocate (__mach_task_self (), profile_thread);
@@ -183,4 +245,37 @@ fork_profil (void)
       assert_perror (err);
     }
 }
-text_set_element (_hurd_fork_child_hook, fork_profil);
+text_set_element (_hurd_fork_child_hook, fork_profil_child);
+
+
+
+
+/* Special RPC stubs for profile_waiter are made by including the normal
+   source code, with special CPP state to prevent it from doing the
+   usual thing. */
+
+/* Include these first; then our #define's will take full effect, not
+   being overridden. */
+#include <mach/mig_support.h>
+
+/* This need not do anything; it is always associated with errors, which
+   are fatal in profile_waiter anyhow. */
+#define __mig_put_reply_port(foo)
+
+/* Use our static variable instead of the usual threadvar mechanism for
+   this. */
+#define __mig_get_reply_port() profil_reply_port
+
+/* Make the functions show up as static */
+#define mig_external static
+
+/* Turn off the attempt to generate ld aliasing records. */
+#undef weak_alias
+#define weak_alias(a,b)
+
+/* And change their names to avoid confusing disasters. */
+#define __vm_deallocate_rpc profil_vm_deallocate
+#define __task_get_sampled_pcs profil_task_get_sampled_pcs
+
+/* And include the source code */
+#include <../mach/RPC_task_get_sampled_pcs.c>