ctest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Performance test from former 161 prof. Brad Chen
  31. * Stresses VM.
  32. *
  33. * Intended for the VM assignment. This should run successfully on a
  34. * variety of strides when the VM system is complete. Strides that are
  35. * not a multiple of 2 work better; see below.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. /*
  40. * SIZE is the amount of memory used.
  41. * DEFAULT is the default stride.
  42. * Note that SIZE and DEFAULT should be relatively prime.
  43. */
  44. #define SIZE (1024*1024/sizeof(struct entry))
  45. #define DEFAULT 477
  46. struct entry {
  47. struct entry *e;
  48. };
  49. struct entry array[SIZE];
  50. int
  51. main(int argc, char **argv)
  52. {
  53. volatile struct entry *e;
  54. unsigned i, stride;
  55. stride = DEFAULT;
  56. if (argc == 2) {
  57. stride = atoi(argv[1]);
  58. }
  59. if (stride <= 0 || argc > 2) {
  60. printf("Usage: ctest [stridesize]\n");
  61. printf(" stridesize should not be a multiple of 2.\n");
  62. return 1;
  63. }
  64. printf("Starting ctest: stride %d\n", stride);
  65. /*
  66. * Generate a huge linked list, with each entry pointing to
  67. * the slot STRIDE entries above it. As long as STRIDE and SIZE
  68. * are relatively prime, this will put all the entries on one
  69. * list. Otherwise you will get multiple disjoint lists. (All
  70. * these lists will be circular.)
  71. */
  72. for (i=0; i<SIZE; i++) {
  73. array[i].e = &array[(i+stride) % SIZE];
  74. }
  75. /*
  76. * Traverse the list. We stop after hitting each element once.
  77. *
  78. * (If STRIDE was even, this will hit some elements more than
  79. * once and others not at all.)
  80. */
  81. e = &array[0];
  82. for (i=0; i<SIZE; i++) {
  83. if (i % stride == 0) {
  84. putchar('.');
  85. }
  86. e = e->e;
  87. }
  88. printf("\nDone!\n");
  89. return 0;
  90. }