// $Id: filler.cc 3733 2010-08-10 23:31:29Z flaterco $ // Data source for overwriting a disk. // g++ -Wall -Wextra -O2 -static -s -o filler filler.cc #define __STDC_FORMAT_MACROS #include #include #include #include #include #include #include void usage () { fprintf (stderr, "Usage: filler [0|1|F0|0F|R]\n"); fprintf (stderr, " 0 = generate zeros\n"); fprintf (stderr, " 1 = generate ones\n"); fprintf (stderr, " F0 = generate character F0\n"); fprintf (stderr, " 0F = generate character 0F\n"); fprintf (stderr, " R = generate cheap pseudorandom bits\n"); exit (-1); } int main (int argc, char **argv) { uintmax_t bcount = 0; if (argc != 2) usage (); if (!strcmp (argv[1], "0")) { while (putchar (0) != EOF) ++bcount; } else if (!strcmp (argv[1], "1")) { while (putchar (0xFF) != EOF) ++bcount; } else if (!strcmp (argv[1], "F0")) { while (putchar (0xF0) != EOF) ++bcount; } else if (!strcmp (argv[1], "0F")) { while (putchar (0x0F) != EOF) ++bcount; } else if (!strcmp (argv[1], "R")) { // The goal is to generate terabytes of nonrepeating pseudorandom data at // or above the speed at which it can be written to a hard drive. // /dev/urandom is too slow (5.9 MB/s on a 3.6 GHz P4). random() has a // period of approximately 16 * ((2^31) - 1) and generates 31 bits per // invocation, so you get at most 133 GB before it repeats. This one has // a period of approximately 2^2300000, generates 48 bits per invocation, // and produces about 21 MB/s on a 3.6 GHz P4 (still too slow). It uses // at least 356 kB of memory for state. static const uint32_t seedSize = 44497 * 2; std::vector seed (seedSize); FILE *fp = fopen ("/dev/urandom", "r"); assert (fp); assert (fread (&(seed[0]), sizeof(uint32_t), seedSize, fp) == seedSize); fclose (fp); std::vector::iterator itref (seed.begin()); boost::lagged_fibonacci44497 boostFibRng (itref, seed.end()); boost::uniform_int boostFibDist (0ULL, 0xFFFFFFFFFFFFULL); boost::variate_generator > pseudorand (boostFibRng, boostFibDist); uint64_t word = pseudorand(); while (fwrite (&word, 6, 1, stdout) == 1) { bcount += 6; word = pseudorand(); } } else { usage (); } fprintf (stderr, "%" PRIuMAX " bytes written\n", bcount); return 0; }