00001 /* 00002 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009 00003 * The President and Fellows of Harvard College. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the University nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 */ 00029 00030 /* sort.c 00031 * Test program to sort a large number of integers. 00032 * 00033 * Intention is to stress virtual memory system. 00034 * 00035 * Once the virtual memory assignment is complete, your system 00036 * should survive this. 00037 */ 00038 00039 #include <stdlib.h> 00040 #include <string.h> 00041 #include <err.h> 00042 00043 /* Larger than physical memory */ 00044 #define SIZE (144*1024) 00045 00046 00047 /* 00048 * Quicksort. 00049 * 00050 * This used to be a bubble sort, which was ok but slow in nachos with 00051 * 4k of memory and SIZE of 1024. However, with SIZE of 147,456 bubble 00052 * sort is completely unacceptable. 00053 * 00054 * Also, quicksort has somewhat more interesting memory usage patterns. 00055 */ 00056 00057 static 00058 void 00059 sort(int *arr, int size) 00060 { 00061 static int tmp[SIZE]; 00062 int pivot, i, j, k; 00063 00064 if (size<2) { 00065 return; 00066 } 00067 00068 pivot = size/2; 00069 sort(arr, pivot); 00070 sort(&arr[pivot], size-pivot); 00071 00072 i = 0; 00073 j = pivot; 00074 k = 0; 00075 while (i<pivot && j<size) { 00076 if (arr[i] < arr[j]) { 00077 tmp[k++] = arr[i++]; 00078 } 00079 else { 00080 tmp[k++] = arr[j++]; 00081 } 00082 } 00083 while (i<pivot) { 00084 tmp[k++] = arr[i++]; 00085 } 00086 while (j<size) { 00087 tmp[k++] = arr[j++]; 00088 } 00089 00090 memcpy(arr, tmp, size*sizeof(int)); 00091 } 00092 00093 //////////////////////////////////////////////////////////// 00094 00095 static int A[SIZE]; 00096 00097 static 00098 void 00099 initarray(void) 00100 { 00101 int i; 00102 00103 /* 00104 * Initialize the array, with pseudo-random but deterministic contents. 00105 */ 00106 srandom(533); 00107 00108 for (i = 0; i < SIZE; i++) { 00109 A[i] = random(); 00110 } 00111 } 00112 00113 static 00114 void 00115 check(void) 00116 { 00117 int i; 00118 00119 for (i=0; i<SIZE-1; i++) { 00120 if (A[i] > A[i+1]) { 00121 errx(1, "Failed: A[%d] is %d, A[%d] is %d", 00122 i, A[i], i+1, A[i+1]); 00123 } 00124 } 00125 warnx("Passed."); 00126 } 00127 00128 int 00129 main(void) 00130 { 00131 initarray(); 00132 sort(A, SIZE); 00133 check(); 00134 return 0; 00135 }