// mibtest.c : A simple memory inband test for testing x Mebibyte of RAM, for 64 bit platforms. // See // http://www.linuxdevcenter.com/pub/a/linux/2006/11/30/linux-out-of-memory.html // for testing much RAM. // ---------------------------------------------------------------------------- // "THE BEERWARE LICENSE" (Revision 44): // Dr. Rolf Freitag (rolf dot freitag at email dot de) wrote this file 2010. // As long as you retain this notice you can do whatever // the GPL (GNU Public License version 3) allows with this stuff. // If you think this stuff is worth it, you can send me money via // paypal, and get a contribution receipt if you wish, or if we met some day // you can buy me a beer in return. // ---------------------------------------------------------------------------- #include #include #include #include //#include //#include int main (int argc, char *argv[]) { unsigned long int *p_j = 0, *p_start = 0, *p_end = 0; unsigned long int j = 0, k = 0, ui_pattern = 0; unsigned long int m = 0, uli_error_counter = 0; time_t now; // for actual time struct tm *ptr_tm = (time (&now), localtime (&now)); // set actual time, convert to localtime // see if the platform uses >=64 bit ints printf ("long int size: %lu (should be >=8)\n", sizeof (long int)); (void) printf ("%s started at %d-%s%d-%s%d %s%d:%s%d:%s%d\n", argv[0], ptr_tm->tm_year + 1900, ptr_tm->tm_mon + 1 > 9 ? "" : "0", ptr_tm->tm_mon + 1, ptr_tm->tm_mday > 9 ? "" : "0", ptr_tm->tm_mday, ptr_tm->tm_hour > 9 ? "" : "0", ptr_tm->tm_hour, ptr_tm->tm_min > 9 ? "" : "0", ptr_tm->tm_min, ptr_tm->tm_sec > 9 ? "" : "0", ptr_tm->tm_sec); fflush (stdout); if (argc < 2) { printf ("Usage:\n mibtest \n"); fflush (stdout); exit (1); } m = strtol (argv[1], (char **) NULL, 0); // limit m to 1 ... 12345678901 if (m > (unsigned long int) 12345678901) m = (unsigned long int) 12345678901; if (m < 1) m = 1; // set the Mebibytes (IEC 60027.2) m = m * 1024 * 1024; p_start = malloc (m); // Pointer to the start of the allocated RAM p_end = p_start + (m / sizeof (unsigned long int)); // Pointer to the end for (j = 0;; j++) { // set the pattern (64 bit block) switch (j % 25) { case 0: // 0xaa (10101010b) ui_pattern = (unsigned long int) 0xaaaaaaaaaaaaaaaa; break; case 1: // 0x55 (01010101b) ui_pattern = (unsigned long int) 0x5555555555555555; break; case 2: ui_pattern = (unsigned long int) 0x0000000000000000; break; case 3: ui_pattern = (unsigned long int) 0xffffffffffffffff; break; case 4: //walking one (binary): 0x80 ui_pattern = (unsigned long int) 0x8080808080808080; break; case 5: ui_pattern = (unsigned long int) 0x4040404040404040; break; case 6: ui_pattern = (unsigned long int) 0x2020202020202020; break; case 7: ui_pattern = (unsigned long int) 0x1010101010101010; break; case 8: ui_pattern = (unsigned long int) 0x0808080808080808; break; case 9: ui_pattern = (unsigned long int) 0x0404040404040404; break; case 10: ui_pattern = (unsigned long int) 0x0202020202020202; break; case 11: ui_pattern = (unsigned long int) 0x0101010101010101; break; case 12: // walking zero: 0x7f ui_pattern = (unsigned long int) 0x7f7f7f7f7f7f7f7f; break; case 13: ui_pattern = (unsigned long int) 0xbfbfbfbfbfbfbfbf; break; case 14: ui_pattern = (unsigned long int) 0xdfdfdfdfdfdfdfdf; break; case 15: ui_pattern = (unsigned long int) 0xefefefefefefefef; break; case 16: ui_pattern = (unsigned long int) 0xf7f7f7f7f7f7f7f7; break; case 17: ui_pattern = (unsigned long int) 0xfbfbfbfbfbfbfbfb; break; case 18: ui_pattern = (unsigned long int) 0xfdfdfdfdfdfdfdfd; break; case 19: ui_pattern = (unsigned long int) 0xfefefefefefefefe; break; default: // random srand (time (NULL)); k ^= rand (); ui_pattern ^= k; k = rand (); ui_pattern ^= (k << 16); k = rand (); ui_pattern ^= (k << 32); k = rand (); ui_pattern ^= (k << 48); } // print round number and pattern printf ("round %lu, \t pattern %16.16lXh, \t %lu errors\n", j, ui_pattern, uli_error_counter); //fflush (stdout); // write the pattern from begin to end of the allocated block for (p_j = p_start; p_j < p_end; p_j++) { *p_j = ui_pattern; } // check the pattern for (p_j = p_start; p_j < p_end; p_j++) { if (*p_j != ui_pattern) printf ("ERROR: Wrote %lu and read %lu at %lu\n", ui_pattern, *p_j, (unsigned long int) p_j); } } return (1); }