t / helper / test-lazy-init-name-hash.con commit Merge branch 'sg/fsck-config-in-doc' (14fe4af)
   1#include "test-tool.h"
   2#include "cache.h"
   3#include "parse-options.h"
   4
   5static int single;
   6static int multi;
   7static int count = 1;
   8static int dump;
   9static int perf;
  10static int analyze;
  11static int analyze_step;
  12
  13/*
  14 * Dump the contents of the "dir" and "name" hash tables to stdout.
  15 * If you sort the result, you can compare it with the other type
  16 * mode and verify that both single and multi produce the same set.
  17 */
  18static void dump_run(void)
  19{
  20        struct hashmap_iter iter_dir;
  21        struct hashmap_iter iter_cache;
  22
  23        /* Stolen from name-hash.c */
  24        struct dir_entry {
  25                struct hashmap_entry ent;
  26                struct dir_entry *parent;
  27                int nr;
  28                unsigned int namelen;
  29                char name[FLEX_ARRAY];
  30        };
  31
  32        struct dir_entry *dir;
  33        struct cache_entry *ce;
  34
  35        read_cache();
  36        if (single) {
  37                test_lazy_init_name_hash(&the_index, 0);
  38        } else {
  39                int nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
  40                if (!nr_threads_used)
  41                        die("non-threaded code path used");
  42        }
  43
  44        dir = hashmap_iter_first(&the_index.dir_hash, &iter_dir);
  45        while (dir) {
  46                printf("dir %08x %7d %s\n", dir->ent.hash, dir->nr, dir->name);
  47                dir = hashmap_iter_next(&iter_dir);
  48        }
  49
  50        ce = hashmap_iter_first(&the_index.name_hash, &iter_cache);
  51        while (ce) {
  52                printf("name %08x %s\n", ce->ent.hash, ce->name);
  53                ce = hashmap_iter_next(&iter_cache);
  54        }
  55
  56        discard_cache();
  57}
  58
  59/*
  60 * Run the single or multi threaded version "count" times and
  61 * report on the time taken.
  62 */
  63static uint64_t time_runs(int try_threaded)
  64{
  65        uint64_t t0, t1, t2;
  66        uint64_t sum = 0;
  67        uint64_t avg;
  68        int nr_threads_used;
  69        int i;
  70
  71        for (i = 0; i < count; i++) {
  72                t0 = getnanotime();
  73                read_cache();
  74                t1 = getnanotime();
  75                nr_threads_used = test_lazy_init_name_hash(&the_index, try_threaded);
  76                t2 = getnanotime();
  77
  78                sum += (t2 - t1);
  79
  80                if (try_threaded && !nr_threads_used)
  81                        die("non-threaded code path used");
  82
  83                if (nr_threads_used)
  84                        printf("%f %f %d multi %d\n",
  85                                   ((double)(t1 - t0))/1000000000,
  86                                   ((double)(t2 - t1))/1000000000,
  87                                   the_index.cache_nr,
  88                                   nr_threads_used);
  89                else
  90                        printf("%f %f %d single\n",
  91                                   ((double)(t1 - t0))/1000000000,
  92                                   ((double)(t2 - t1))/1000000000,
  93                                   the_index.cache_nr);
  94                fflush(stdout);
  95
  96                discard_cache();
  97        }
  98
  99        avg = sum / count;
 100        if (count > 1)
 101                printf("avg %f %s\n",
 102                           (double)avg/1000000000,
 103                           (try_threaded) ? "multi" : "single");
 104
 105        return avg;
 106}
 107
 108/*
 109 * Try a series of runs varying the "istate->cache_nr" and
 110 * try to find a good value for the multi-threaded criteria.
 111 */
 112static void analyze_run(void)
 113{
 114        uint64_t t1s, t1m, t2s, t2m;
 115        int cache_nr_limit;
 116        int nr_threads_used = 0;
 117        int i;
 118        int nr;
 119
 120        read_cache();
 121        cache_nr_limit = the_index.cache_nr;
 122        discard_cache();
 123
 124        nr = analyze;
 125        while (1) {
 126                uint64_t sum_single = 0;
 127                uint64_t sum_multi = 0;
 128                uint64_t avg_single;
 129                uint64_t avg_multi;
 130
 131                if (nr > cache_nr_limit)
 132                        nr = cache_nr_limit;
 133
 134                for (i = 0; i < count; i++) {
 135                        read_cache();
 136                        the_index.cache_nr = nr; /* cheap truncate of index */
 137                        t1s = getnanotime();
 138                        test_lazy_init_name_hash(&the_index, 0);
 139                        t2s = getnanotime();
 140                        sum_single += (t2s - t1s);
 141                        the_index.cache_nr = cache_nr_limit;
 142                        discard_cache();
 143
 144                        read_cache();
 145                        the_index.cache_nr = nr; /* cheap truncate of index */
 146                        t1m = getnanotime();
 147                        nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
 148                        t2m = getnanotime();
 149                        sum_multi += (t2m - t1m);
 150                        the_index.cache_nr = cache_nr_limit;
 151                        discard_cache();
 152
 153                        if (!nr_threads_used)
 154                                printf("    [size %8d] [single %f]   non-threaded code path used\n",
 155                                           nr, ((double)(t2s - t1s))/1000000000);
 156                        else
 157                                printf("    [size %8d] [single %f] %c [multi %f %d]\n",
 158                                           nr,
 159                                           ((double)(t2s - t1s))/1000000000,
 160                                           (((t2s - t1s) < (t2m - t1m)) ? '<' : '>'),
 161                                           ((double)(t2m - t1m))/1000000000,
 162                                           nr_threads_used);
 163                        fflush(stdout);
 164                }
 165                if (count > 1) {
 166                        avg_single = sum_single / count;
 167                        avg_multi = sum_multi / count;
 168                        if (!nr_threads_used)
 169                                printf("avg [size %8d] [single %f]\n",
 170                                           nr,
 171                                           (double)avg_single/1000000000);
 172                        else
 173                                printf("avg [size %8d] [single %f] %c [multi %f %d]\n",
 174                                           nr,
 175                                           (double)avg_single/1000000000,
 176                                           (avg_single < avg_multi ? '<' : '>'),
 177                                           (double)avg_multi/1000000000,
 178                                           nr_threads_used);
 179                        fflush(stdout);
 180                }
 181
 182                if (nr >= cache_nr_limit)
 183                        return;
 184                nr += analyze_step;
 185        }
 186}
 187
 188int cmd__lazy_init_name_hash(int argc, const char **argv)
 189{
 190        const char *usage[] = {
 191                "test-tool lazy-init-name-hash -d (-s | -m)",
 192                "test-tool lazy-init-name-hash -p [-c c]",
 193                "test-tool lazy-init-name-hash -a a [--step s] [-c c]",
 194                "test-tool lazy-init-name-hash (-s | -m) [-c c]",
 195                "test-tool lazy-init-name-hash -s -m [-c c]",
 196                NULL
 197        };
 198        struct option options[] = {
 199                OPT_BOOL('s', "single", &single, "run single-threaded code"),
 200                OPT_BOOL('m', "multi", &multi, "run multi-threaded code"),
 201                OPT_INTEGER('c', "count", &count, "number of passes"),
 202                OPT_BOOL('d', "dump", &dump, "dump hash tables"),
 203                OPT_BOOL('p', "perf", &perf, "compare single vs multi"),
 204                OPT_INTEGER('a', "analyze", &analyze, "analyze different multi sizes"),
 205                OPT_INTEGER(0, "step", &analyze_step, "analyze step factor"),
 206                OPT_END(),
 207        };
 208        const char *prefix;
 209        uint64_t avg_single, avg_multi;
 210
 211        prefix = setup_git_directory();
 212
 213        argc = parse_options(argc, argv, prefix, options, usage, 0);
 214
 215        /*
 216         * istate->dir_hash is only created when ignore_case is set.
 217         */
 218        ignore_case = 1;
 219
 220        if (dump) {
 221                if (perf || analyze > 0)
 222                        die("cannot combine dump, perf, or analyze");
 223                if (count > 1)
 224                        die("count not valid with dump");
 225                if (single && multi)
 226                        die("cannot use both single and multi with dump");
 227                if (!single && !multi)
 228                        die("dump requires either single or multi");
 229                dump_run();
 230                return 0;
 231        }
 232
 233        if (perf) {
 234                if (analyze > 0)
 235                        die("cannot combine dump, perf, or analyze");
 236                if (single || multi)
 237                        die("cannot use single or multi with perf");
 238                avg_single = time_runs(0);
 239                avg_multi = time_runs(1);
 240                if (avg_multi > avg_single)
 241                        die("multi is slower");
 242                return 0;
 243        }
 244
 245        if (analyze) {
 246                if (analyze < 500)
 247                        die("analyze must be at least 500");
 248                if (!analyze_step)
 249                        analyze_step = analyze;
 250                if (single || multi)
 251                        die("cannot use single or multi with analyze");
 252                analyze_run();
 253                return 0;
 254        }
 255
 256        if (!single && !multi)
 257                die("require either -s or -m or both");
 258
 259        if (single)
 260                time_runs(0);
 261        if (multi)
 262                time_runs(1);
 263
 264        return 0;
 265}