]> git.saurik.com Git - apple/xnu.git/blobdiff - tools/tests/MPMMTest/KQMPMMtest.c
xnu-3248.30.4.tar.gz
[apple/xnu.git] / tools / tests / MPMMTest / KQMPMMtest.c
index b16c5f8471533d69ae54a25064b4e1e5d3ceb11c..0686350f3a80649cc3cc029d0bd60ec7078a7ece 100644 (file)
@@ -1,13 +1,12 @@
 #include <AvailabilityMacros.h>
-#ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
-#include </System/Library/Frameworks/System.framework/PrivateHeaders/mach/thread_policy.h>
-#endif
+#include <mach/thread_policy.h>
 
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <err.h>
+#include <unistd.h>
 
 #include <pthread.h>
 #include <mach/mach.h>
 #include <mach/notify.h>
 #include <servers/bootstrap.h>
 #include <sys/event.h>
+#include <sys/select.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/signal.h>
+#include <errno.h>
+#include "../unit_tests/tests_common.h"
 
 #define MAX(A, B) ((A) < (B) ? (B) : (A))
 
@@ -67,6 +69,9 @@ static boolean_t      affinity = FALSE;
 static boolean_t       timeshare = FALSE;
 static boolean_t       threaded = FALSE;
 static boolean_t       oneway = FALSE;
+static boolean_t       do_select = FALSE;
+static boolean_t    save_perfdata = FALSE;
+
 int                    msg_type;
 int                    num_ints;
 int                    num_msgs;
@@ -96,6 +101,8 @@ void usage(const char *progname) {
        fprintf(stderr, "    -delay num\t\tmicroseconds to sleep clients between messages\n");
        fprintf(stderr, "    -work num\t\tmicroseconds of client work\n");
        fprintf(stderr, "    -pages num\t\tpages of memory touched by client work\n");
+       fprintf(stderr, "    -select   \t\tselect prior to calling kevent().\n");
+       fprintf(stderr, "    -perf   \t\tCreate perfdata files for metrics.\n");
        fprintf(stderr, "default values are:\n");
        fprintf(stderr, "    . no affinity\n");
        fprintf(stderr, "    . not timeshare\n");
@@ -195,6 +202,12 @@ void parse_args(int argc, char *argv[]) {
                                usage(progname);
                        client_pages = strtoul(argv[1], NULL, 0);
                        argc -= 2; argv += 2;
+               } else if (0 == strcmp("-select", argv[0])) {
+                       do_select = TRUE;
+                       argc--; argv++;
+               } else if (0 == strcmp("-perf", argv[0])) {
+                       save_perfdata = TRUE;
+                       argc--; argv++;
                } else 
                        usage(progname);
        }
@@ -306,7 +319,6 @@ void setup_client_ports(struct port_args *ports)
 
 static void
 thread_setup(int tag) {
-#ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
        kern_return_t                   ret;
         thread_extended_policy_data_t   epolicy;
         thread_affinity_policy_data_t   policy;
@@ -330,7 +342,6 @@ thread_setup(int tag) {
                 if (ret != KERN_SUCCESS)
                         printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret);
         }
-#endif
 }
 
 void *
@@ -339,10 +350,12 @@ server(void *serverarg)
        int kq;
        struct kevent64_s kev[1];
        int err;
+       int count;
        struct port_args args;
        int idx;
        kern_return_t ret;
        int totalmsg = num_msgs * num_clients;
+       fd_set readfds;
 
        args.server_num = (int) (long) serverarg;
        setup_server_ports(&args);
@@ -365,11 +378,26 @@ server(void *serverarg)
                perror("kevent");
                exit(1);
        }
+       
        for (idx = 0; idx < totalmsg; idx++) {
 
                if (verbose) 
                        printf("server awaiting message %d\n", idx);
        retry:
+               if (do_select) {
+                       FD_ZERO(&readfds);
+                       FD_SET(kq, &readfds);
+
+                       if (verbose)
+                               printf("Calling select() prior to kevent64().\n");
+
+                       count = select(kq + 1, &readfds, NULL, NULL, NULL);
+                       if (count == -1) {
+                               perror("select");
+                               exit(1);
+                       }
+               }
+
                EV_SET64(&kev[0], args.pset, EVFILT_MACHPORT, EV_ENABLE, 
 #if DIRECT_MSG_RCV
                         MACH_RCV_MSG|MACH_RCV_LARGE, 0, 0, (mach_vm_address_t)args.req_msg, args.req_size);
@@ -396,7 +424,7 @@ server(void *serverarg)
                } 
 #else
                if (kev[0].data != args.port)
-                       printf("kevent64(MACH_PORT_NULL) port name (0x%x) != expected (0x%x)\n", kev[0].data, args.port);
+                       printf("kevent64(MACH_PORT_NULL) port name (%lld) != expected (0x%x)\n", kev[0].data, args.port);
 
                args.req_msg->msgh_bits = 0;
                args.req_msg->msgh_size = args.req_size;
@@ -447,6 +475,7 @@ server(void *serverarg)
                        }
                }
        }
+       return NULL;
 }
 
 static inline void
@@ -512,6 +541,7 @@ calibrate_client_work(void)
                        printf("calibration_count=%d calibration_usec=%d\n",
                                calibration_count, calibration_usec);
        }
+        return NULL;
 }
 
 static void *
@@ -526,6 +556,7 @@ client_work(void)
        if (client_delay) {
                usleep(client_delay);
        }
+       return NULL;
 }
 
 void *client(void *threadarg) 
@@ -535,7 +566,7 @@ void *client(void *threadarg)
        mach_msg_header_t *req, *reply; 
        mach_port_t bsport, servport;
        kern_return_t ret;
-       long server_num = (long) threadarg;
+       int server_num = (int) threadarg;
        void *ints = malloc(sizeof(u_int32_t) * num_ints);
 
        if (verbose) 
@@ -632,7 +663,7 @@ void *client(void *threadarg)
        }
 
        free(ints);
-       return;
+       return NULL;
 }
 
 static void
@@ -647,12 +678,12 @@ thread_spawn(thread_id_t *thread, void *(fn)(void *), void *arg) {
                if (ret != 0)
                        err(1, "pthread_create()");
                if (verbose)
-                       printf("created pthread 0x%x\n", thread->tid);
+                       printf("created pthread %p\n", thread->tid);
        } else {
                thread->pid = fork();
                if (thread->pid == 0) {
                        if (verbose)
-                               printf("calling 0x%x(0x%x)\n", fn, arg);
+                               printf("calling %p(%p)\n", fn, arg);
                        fn(arg);
                        exit(0);
                }
@@ -666,10 +697,10 @@ thread_join(thread_id_t *thread) {
        if (threaded) {
                kern_return_t   ret;
                if (verbose)
-                       printf("joining thread 0x%x\n", thread->tid);
+                       printf("joining thread %p\n", thread->tid);
                ret = pthread_join(thread->tid, NULL);
                if (ret != KERN_SUCCESS)
-                       err(1, "pthread_join(0x%x)", thread->tid);
+                       err(1, "pthread_join(%p)", thread->tid);
        } else {
                int     stat;
                if (verbose)
@@ -710,6 +741,7 @@ wait_for_servers(void)
        exit(1);
 }
 
+
 int main(int argc, char *argv[]) 
 {
        int             i;
@@ -797,13 +829,20 @@ int main(int argc, char *argv[])
        double dsecs = (double) deltatv.tv_sec + 
                1.0E-6 * (double) deltatv.tv_usec;
 
-       printf(" in %u.%03u seconds\n",  
-                       deltatv.tv_sec, deltatv.tv_usec/1000);
+       double time_in_sec = (double)deltatv.tv_sec + (double)deltatv.tv_usec/1000.0;
+       double throughput_msg_p_sec = (double) totalmsg/dsecs;
+       double avg_msg_latency = dsecs*1.0E6 / (double)totalmsg;
+
+       printf(" in %ld.%03u seconds\n",  
+              (long)deltatv.tv_sec, deltatv.tv_usec/1000);
        printf("  throughput in messages/sec:     %g\n",
                        (double)totalmsg / dsecs);
        printf("  average message latency (usec): %2.3g\n", 
                        dsecs * 1.0E6 / (double) totalmsg);
 
+       if (save_perfdata == TRUE) {
+               record_perf_data("kqmpmm_avg_msg_latency", "usec", avg_msg_latency, "Message latency measured in microseconds. Lower is better", stderr);
+       }
        return (0);
 
 }