| 1 | /* |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
| 8 | * You may select, at your option, one of the above-listed licenses. |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | /*-************************************ |
| 13 | * Dependencies |
| 14 | **************************************/ |
| 15 | #include "util.h" /* Compiler options */ |
| 16 | #include <stdio.h> /* fprintf, stderr */ |
| 17 | #include "datagen.h" /* RDG_generate */ |
| 18 | |
| 19 | |
| 20 | /*-************************************ |
| 21 | * Constants |
| 22 | **************************************/ |
| 23 | #define KB *(1 <<10) |
| 24 | #define MB *(1 <<20) |
| 25 | #define GB *(1U<<30) |
| 26 | |
| 27 | #define SIZE_DEFAULT ((64 KB) + 1) |
| 28 | #define SEED_DEFAULT 0 |
| 29 | #define COMPRESSIBILITY_DEFAULT 50 |
| 30 | |
| 31 | |
| 32 | /*-************************************ |
| 33 | * Macros |
| 34 | **************************************/ |
| 35 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 36 | #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 37 | static unsigned displayLevel = 2; |
| 38 | |
| 39 | |
| 40 | /*-******************************************************* |
| 41 | * Command line |
| 42 | *********************************************************/ |
| 43 | static int usage(const char* programName) |
| 44 | { |
| 45 | DISPLAY( "Compressible data generator\n"); |
| 46 | DISPLAY( "Usage :\n"); |
| 47 | DISPLAY( " %s [args]\n", programName); |
| 48 | DISPLAY( "\n"); |
| 49 | DISPLAY( "Arguments :\n"); |
| 50 | DISPLAY( " -g# : generate # data (default:%i)\n", SIZE_DEFAULT); |
| 51 | DISPLAY( " -s# : Select seed (default:%i)\n", SEED_DEFAULT); |
| 52 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", |
| 53 | COMPRESSIBILITY_DEFAULT); |
| 54 | DISPLAY( " -h : display help and exit\n"); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | int main(int argc, const char** argv) |
| 60 | { |
| 61 | unsigned probaU32 = COMPRESSIBILITY_DEFAULT; |
| 62 | double litProba = 0.0; |
| 63 | U64 size = SIZE_DEFAULT; |
| 64 | U32 seed = SEED_DEFAULT; |
| 65 | const char* const programName = argv[0]; |
| 66 | |
| 67 | int argNb; |
| 68 | for(argNb=1; argNb<argc; argNb++) { |
| 69 | const char* argument = argv[argNb]; |
| 70 | |
| 71 | if(!argument) continue; /* Protection if argument empty */ |
| 72 | |
| 73 | /* Handle commands. Aggregated commands are allowed */ |
| 74 | if (*argument=='-') { |
| 75 | argument++; |
| 76 | while (*argument!=0) { |
| 77 | switch(*argument) |
| 78 | { |
| 79 | case 'h': |
| 80 | return usage(programName); |
| 81 | case 'g': |
| 82 | argument++; |
| 83 | size=0; |
| 84 | while ((*argument>='0') && (*argument<='9')) |
| 85 | size *= 10, size += *argument++ - '0'; |
| 86 | if (*argument=='K') { size <<= 10; argument++; } |
| 87 | if (*argument=='M') { size <<= 20; argument++; } |
| 88 | if (*argument=='G') { size <<= 30; argument++; } |
| 89 | if (*argument=='B') { argument++; } |
| 90 | break; |
| 91 | case 's': |
| 92 | argument++; |
| 93 | seed=0; |
| 94 | while ((*argument>='0') && (*argument<='9')) |
| 95 | seed *= 10, seed += *argument++ - '0'; |
| 96 | break; |
| 97 | case 'P': |
| 98 | argument++; |
| 99 | probaU32 = 0; |
| 100 | while ((*argument>='0') && (*argument<='9')) |
| 101 | probaU32 *= 10, probaU32 += *argument++ - '0'; |
| 102 | if (probaU32>100) probaU32 = 100; |
| 103 | break; |
| 104 | case 'L': /* hidden argument : Literal distribution probability */ |
| 105 | argument++; |
| 106 | litProba=0.; |
| 107 | while ((*argument>='0') && (*argument<='9')) |
| 108 | litProba *= 10, litProba += *argument++ - '0'; |
| 109 | if (litProba>100.) litProba=100.; |
| 110 | litProba /= 100.; |
| 111 | break; |
| 112 | case 'v': |
| 113 | displayLevel = 4; |
| 114 | argument++; |
| 115 | break; |
| 116 | default: |
| 117 | return usage(programName); |
| 118 | } |
| 119 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 120 | |
| 121 | DISPLAYLEVEL(4, "Compressible data Generator \n"); |
| 122 | if (probaU32!=COMPRESSIBILITY_DEFAULT) |
| 123 | DISPLAYLEVEL(3, "Compressibility : %i%%\n", probaU32); |
| 124 | DISPLAYLEVEL(3, "Seed = %u \n", (unsigned)seed); |
| 125 | |
| 126 | RDG_genStdout(size, (double)probaU32/100, litProba, seed); |
| 127 | DISPLAYLEVEL(3, "\n"); |
| 128 | |
| 129 | return 0; |
| 130 | } |