THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.8KB

  1. /*
  2. * Copyright (C) 2008 Linus Torvalds
  3. */
  4. #include "cache.h"
  5. #include "pathspec.h"
  6. #include "dir.h"
  7. #include "fsmonitor.h"
  8. #include "config.h"
  9. #include "progress.h"
  10. #include "thread-utils.h"
  11. #include "repository.h"
  12. /*
  13. * Mostly randomly chosen maximum thread counts: we
  14. * cap the parallelism to 20 threads, and we want
  15. * to have at least 500 lstat's per thread for it to
  16. * be worth starting a thread.
  17. */
  18. #define MAX_PARALLEL (20)
  19. #define THREAD_COST (500)
  20. struct progress_data {
  21. unsigned long n;
  22. struct progress *progress;
  23. pthread_mutex_t mutex;
  24. };
  25. struct thread_data {
  26. pthread_t pthread;
  27. struct index_state *index;
  28. struct pathspec pathspec;
  29. struct progress_data *progress;
  30. int offset, nr;
  31. };
  32. static void *preload_thread(void *_data)
  33. {
  34. int nr, last_nr;
  35. struct thread_data *p = _data;
  36. struct index_state *index = p->index;
  37. struct cache_entry **cep = index->cache + p->offset;
  38. struct cache_def cache = CACHE_DEF_INIT;
  39. nr = p->nr;
  40. if (nr + p->offset > index->cache_nr)
  41. nr = index->cache_nr - p->offset;
  42. last_nr = nr;
  43. do {
  44. struct cache_entry *ce = *cep++;
  45. struct stat st;
  46. if (ce_stage(ce))
  47. continue;
  48. if (S_ISGITLINK(ce->ce_mode))
  49. continue;
  50. if (ce_uptodate(ce))
  51. continue;
  52. if (ce_skip_worktree(ce))
  53. continue;
  54. if (ce->ce_flags & CE_FSMONITOR_VALID)
  55. continue;
  56. if (p->progress && !(nr & 31)) {
  57. struct progress_data *pd = p->progress;
  58. pthread_mutex_lock(&pd->mutex);
  59. pd->n += last_nr - nr;
  60. display_progress(pd->progress, pd->n);
  61. pthread_mutex_unlock(&pd->mutex);
  62. last_nr = nr;
  63. }
  64. if (!ce_path_match(index, ce, &p->pathspec, NULL))
  65. continue;
  66. if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
  67. continue;
  68. if (lstat(ce->name, &st))
  69. continue;
  70. if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
  71. continue;
  72. ce_mark_uptodate(ce);
  73. mark_fsmonitor_valid(index, ce);
  74. } while (--nr > 0);
  75. if (p->progress) {
  76. struct progress_data *pd = p->progress;
  77. pthread_mutex_lock(&pd->mutex);
  78. display_progress(pd->progress, pd->n + last_nr);
  79. pthread_mutex_unlock(&pd->mutex);
  80. }
  81. cache_def_clear(&cache);
  82. return NULL;
  83. }
  84. void preload_index(struct index_state *index,
  85. const struct pathspec *pathspec,
  86. unsigned int refresh_flags)
  87. {
  88. int threads, i, work, offset;
  89. struct thread_data data[MAX_PARALLEL];
  90. struct progress_data pd;
  91. if (!HAVE_THREADS || !core_preload_index)
  92. return;
  93. threads = index->cache_nr / THREAD_COST;
  94. if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
  95. threads = 2;
  96. if (threads < 2)
  97. return;
  98. trace_performance_enter();
  99. if (threads > MAX_PARALLEL)
  100. threads = MAX_PARALLEL;
  101. offset = 0;
  102. work = DIV_ROUND_UP(index->cache_nr, threads);
  103. memset(&data, 0, sizeof(data));
  104. memset(&pd, 0, sizeof(pd));
  105. if (refresh_flags & REFRESH_PROGRESS && isatty(2)) {
  106. pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr);
  107. pthread_mutex_init(&pd.mutex, NULL);
  108. }
  109. for (i = 0; i < threads; i++) {
  110. struct thread_data *p = data+i;
  111. int err;
  112. p->index = index;
  113. if (pathspec)
  114. copy_pathspec(&p->pathspec, pathspec);
  115. p->offset = offset;
  116. p->nr = work;
  117. if (pd.progress)
  118. p->progress = &pd;
  119. offset += work;
  120. err = pthread_create(&p->pthread, NULL, preload_thread, p);
  121. if (err)
  122. die(_("unable to create threaded lstat: %s"), strerror(err));
  123. }
  124. for (i = 0; i < threads; i++) {
  125. struct thread_data *p = data+i;
  126. if (pthread_join(p->pthread, NULL))
  127. die("unable to join threaded lstat");
  128. }
  129. stop_progress(&pd.progress);
  130. trace_performance_leave("preload index");
  131. }
  132. int repo_read_index_preload(struct repository *repo,
  133. const struct pathspec *pathspec,
  134. unsigned int refresh_flags)
  135. {
  136. int retval = repo_read_index(repo);
  137. preload_index(repo->index, pathspec, refresh_flags);
  138. return retval;
  139. }