sparse.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * sparse.c
  3. *
  4. * This program declares a large array, but only uses a small
  5. * part of it. The intention is that the full array will not
  6. * fit into memory, but the used part of the array will fit.
  7. *
  8. * This is similar to testbin/huge.
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. /*
  14. * set these to match the page size of the
  15. * machine and the number of pages of physical memory
  16. * in the machine.
  17. */
  18. #define PageSize 4096
  19. #define NumPages 128
  20. /* a large array, 2 times the size of physical memory */
  21. #define ArraySize (2*NumPages*PageSize)
  22. char sparse[ArraySize];
  23. int
  24. main()
  25. {
  26. int i,j;
  27. printf("Starting the sparse program\n");
  28. /* write some values into the array, but only
  29. touch one array entry on every 10th page */
  30. for (i=0; i<ArraySize; i+=(10*PageSize)) {
  31. sparse[i]= 'a';
  32. }
  33. printf("stage [1] done\n");
  34. /* increment each location 5 times */
  35. for(j=0; j<5; j++) {
  36. for (i=0; i<ArraySize; i+=(10*PageSize)) {
  37. sparse[i] += 1;
  38. }
  39. }
  40. printf("stage [2] done\n");
  41. /* check the values that were written */
  42. /* increment each location 5 times */
  43. for (i=0; i<ArraySize; i+=(10*PageSize)) {
  44. if (sparse[i] != ('a'+5)) {
  45. printf("Test failed! Unexpected value at array position %d\n", i);
  46. return(1);
  47. }
  48. }
  49. printf("SUCCESS\n");
  50. return 0;
  51. }