/* simple program to malloc memory, inialize it, and * then repetitively use it to keep it active. */ #include #include #include #include #include #include #include /* goal is to sweep memory every T1 sec by accessing a * percentage at a time and sleeping T2 sec in between accesses. * Once all the memory has been accessed, sleep for T3 sec * before starting the cycle over. */ #define T1 180 #define T2 5 #define T3 300 const char *timestamp(void); void usage(const char *prog) { fprintf(stderr, "\nusage: %s memlen{M|K}) [t1 t2 t3]\n", prog); } int main(int argc, char *argv[]) { int len; char *endp; int factor, endp_len; int start, incr; int t1 = T1, t2 = T2, t3 = T3; char *mem; char c = 0; if (argc < 2) { usage(basename(argv[0])); return 1; } /* * determine memory to request */ len = (int) strtol(argv[1], &endp, 0); factor = 1; endp_len = strlen(endp); if ((endp_len == 1) && ((*endp == 'M') || (*endp == 'm'))) factor = 1024 * 1024; else if ((endp_len == 1) && ((*endp == 'K') || (*endp == 'k'))) factor = 1024; else if (endp_len) { fprintf(stderr, "invalid memory len.\n"); return 1; } len *= factor; if (len == 0) { fprintf(stdout, "memory len is 0.\n"); return 1; } /* * convert times if given */ if (argc > 2) { if (argc < 5) { usage(basename(argv[0])); return 1; } t1 = atoi(argv[2]); t2 = atoi(argv[3]); t3 = atoi(argv[4]); } /* * amount of memory to sweep at one time */ if (t1 && t2) incr = len / t1 * t2; else incr = len; mem = (char *) malloc(len); if (mem == NULL) { fprintf(stderr, "malloc failed\n"); return 1; } printf("memory allocated. initializing to 0\n"); memset(mem, 0, len); start = 0; printf("%s starting memory update.\n", timestamp()); while (1) { c++; if (c == 0x7f) c = 0; memset(mem + start, c, incr); start += incr; if ((start >= len) || ((start + incr) >= len)) { printf("%s scan complete. sleeping %d\n", timestamp(), t3); start = 0; sleep(t3); printf("%s starting memory update.\n", timestamp()); } else if (t2) sleep(t2); } return 0; } const char *timestamp(void) { static char date[64]; struct timeval now; struct tm ltime; memset(date, 0, sizeof(date)); if (gettimeofday(&now, NULL) == 0) { if (localtime_r(&now.tv_sec, <ime)) strftime(date, sizeof(date), "%m/%d %H:%M:%S", <ime); } if (strlen(date) == 0) strcpy(date, "unknown"); return date; }