From 1cceeb7e74de07688ac2d16ffe6de4d5a759ec5b Mon Sep 17 00:00:00 2001
From: rtoy <rtoy>
Date: Fri, 15 Apr 2005 01:40:08 +0000
Subject: [PATCH] lisp/lisp.c: o Add function for ppc to compute the number of
 CPU cycles per   timebase ticks.  Save this in a global variable.

code/time.lisp:
o Adjust cycle-count/float for ppc to use the cycles-per-tick to
  compute the actual number of CPU cycles, so we can display cycles
  instead of ticks.
---
 code/time.lisp | 19 +++++++++++++-----
 lisp/lisp.c    | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/code/time.lisp b/code/time.lisp
index 446fa77ca..7b7074d9e 100644
--- a/code/time.lisp
+++ b/code/time.lisp
@@ -5,7 +5,7 @@
 ;;; Carnegie Mellon University, and has been placed in the public domain.
 ;;;
 (ext:file-comment
-  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/time.lisp,v 1.27 2004/10/09 02:47:44 rtoy Exp $")
+  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/time.lisp,v 1.28 2005/04/15 01:40:07 rtoy Exp $")
 ;;;
 ;;; **********************************************************************
 ;;;
@@ -281,11 +281,22 @@
 		       (system:get-system-info)
     (values user sys faults (get-bytes-consed))))
 
-#+(or pentium sparc-v9 ppc)
+#+(or pentium sparc-v9)
 (defun cycle-count/float ()
   (multiple-value-bind (lo hi)
       (vm::read-cycle-counter)
     (+ (* hi (expt 2.0d0 32)) lo)))
+#+ppc
+(progn
+(alien:def-alien-variable cycles-per-tick c-call:int)
+(defun cycle-count/float ()
+  (multiple-value-bind (lo hi)
+      (vm::read-cycle-counter)
+    ;; The cycle counter on PPC isn't really a cycle counter.  It
+    ;; counts ticks, so we need to convert ticks to cycles.
+    ;; CYCLES-PER-TICK is the scale factor, computed in C.
+    (* cycles-per-tick (+ (* hi (expt 2.0d0 32)) lo))))
+)
 
 #-(or pentium sparc-v9 ppc)
 (defun cycle-count/float () 0.0)
@@ -379,9 +390,7 @@
 		    (max (/ (- new-run-utime old-run-utime) 1000000.0) 0.0)
 		    (max (/ (- new-run-stime old-run-stime) 1000000.0) 0.0)
 		    (truncate cycle-count)
-		    ;; The counter for PPC isn't the number of CPU
-		    ;; cycles, unlike x86 and Ultrasparc.
-		    #+ppc "time-base" #-ppc "CPU"
+		    "CPU"
 		    (unless (zerop gc-run-time)
 		      (/ (float gc-run-time)
 			 (float internal-time-units-per-second)))
diff --git a/lisp/lisp.c b/lisp/lisp.c
index 076a2d4db..0764ed2b5 100644
--- a/lisp/lisp.c
+++ b/lisp/lisp.c
@@ -1,7 +1,7 @@
 /*
  * main() entry point for a stand alone lisp image.
  *
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/lisp.c,v 1.46 2005/02/04 15:03:52 rtoy Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/lisp.c,v 1.47 2005/04/15 01:40:08 rtoy Exp $
  *
  */
 
@@ -395,6 +395,53 @@ prepend_core_path(char* lib, char* corefile)
 int builtin_image_flag = 0;
 long initial_function_addr = 0;
 
+#ifdef DARWIN
+#include <sys/param.h>
+#include <sys/sysctl.h>
+
+int cycles_per_tick = 1;
+
+/*
+ * Compute the conversion factor from the numbef of time base ticks to
+ * clock frequency.  The timebase counter on PPC is not a cycle
+ * counter; we have to derive the relationship ourselves.
+ */
+
+void timebase_init()
+{
+  int mib[2];
+  int tbfrequency;
+  int cpufrequency;
+  unsigned int miblen;
+  size_t len;
+
+  mib[0] = CTL_HW;
+#ifndef HW_TB_FREQ
+  /*
+   * Mac OS X 10.2.8 doesn't have this, so we take it from 10.3.8,
+   * which does.
+   */
+#define HW_TB_FREQ	23
+#endif  
+  mib[1] = HW_TB_FREQ;
+  miblen = 2;
+  len = sizeof(tbfrequency);
+
+  if (sysctl(mib, miblen, &tbfrequency, &len, NULL, 0) == -1) {
+    perror("Error getting HW_TB_FREQ from sysctl: ");
+  }
+
+  mib[0] = CTL_HW;
+  mib[1] = HW_CPU_FREQ;
+  miblen = 2;
+  len = sizeof(cpufrequency);
+  if (sysctl(mib, miblen, &cpufrequency, &len, NULL, 0) == -1) {
+    perror("Error getting HW_CPU_FREQ from sysctl: ");
+  }
+
+  cycles_per_tick = cpufrequency / tbfrequency;
+}
+#endif
 /* And here be main. */
 
 int main(int argc, char *argv[], char *envp[])
@@ -700,6 +747,9 @@ int main(int argc, char *argv[], char *envp[])
      */
     sigint_init();
 
+#ifdef DARWIN
+    timebase_init();
+#endif
     if (monitor) {
         while (1) {
             ldb_monitor();
-- 
GitLab