variations_crash_keys.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/variations/variations_crash_keys.h"
  5. #include <string>
  6. #include "base/debug/leak_annotations.h"
  7. #include "base/sequence_checker.h"
  8. #include "base/strings/string_number_conversions.h"
  9. #include "base/strings/string_piece.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "base/threading/sequenced_task_runner_handle.h"
  13. #include "build/buildflag.h"
  14. #include "build/chromeos_buildflags.h"
  15. #include "components/crash/core/common/crash_key.h"
  16. #include "components/variations/active_field_trials.h"
  17. #include "components/variations/buildflags.h"
  18. #include "components/variations/synthetic_trials.h"
  19. #if BUILDFLAG(IS_CHROMEOS_ASH)
  20. #include "base/task/thread_pool.h"
  21. #include "components/variations/variations_crash_keys_chromeos.h"
  22. #endif
  23. namespace variations {
  24. namespace {
  25. // Size of the "num-experiments" crash key in bytes. 4096 bytes should be able
  26. // to hold about 227 entries, given each entry is 18 bytes long (due to being
  27. // of the form "8e7abfb0-c16397b7,").
  28. #if BUILDFLAG(LARGE_VARIATION_KEY_SIZE)
  29. constexpr size_t kVariationsKeySize = 8192;
  30. #else
  31. constexpr size_t kVariationsKeySize = 4096;
  32. #endif
  33. // Crash key reporting the number of experiments. 8 is the size of the crash key
  34. // in bytes, which is used to hold an int as a string.
  35. crash_reporter::CrashKeyString<8> g_num_variations_crash_key(
  36. kNumExperimentsKey);
  37. // Crash key reporting the variations state.
  38. crash_reporter::CrashKeyString<kVariationsKeySize> g_variations_crash_key(
  39. kExperimentListKey);
  40. std::string ActiveGroupToString(const ActiveGroupId& active_group) {
  41. return base::StringPrintf("%x-%x,", active_group.name, active_group.group);
  42. }
  43. class VariationsCrashKeys final : public base::FieldTrialList::Observer {
  44. public:
  45. VariationsCrashKeys();
  46. VariationsCrashKeys(const VariationsCrashKeys&) = delete;
  47. VariationsCrashKeys& operator=(const VariationsCrashKeys&) = delete;
  48. ~VariationsCrashKeys() override;
  49. // base::FieldTrialList::Observer:
  50. void OnFieldTrialGroupFinalized(const std::string& trial_name,
  51. const std::string& group_name) override;
  52. // Notifies the object that the list of synthetic field trial groups has
  53. // changed. Note: This matches the SyntheticTrialObserver interface, but this
  54. // object isn't a direct observer, so doesn't implement it.
  55. void OnSyntheticTrialsChanged(const std::vector<SyntheticTrialGroup>& groups);
  56. // Gets the list of experiments and number of experiments in the format we
  57. // want to place it in the crash keys.
  58. ExperimentListInfo GetExperimentListInfo();
  59. private:
  60. // Adds an entry for the specified field trial to internal state, without
  61. // updating crash keys.
  62. void AppendFieldTrial(const std::string& trial_name,
  63. const std::string& group_name);
  64. // Updates crash keys based on internal state.
  65. void UpdateCrashKeys();
  66. // Task runner corresponding to the UI thread, used to reschedule synchronous
  67. // observer calls that happen on a different thread.
  68. scoped_refptr<base::SequencedTaskRunner> ui_thread_task_runner_;
  69. #if BUILDFLAG(IS_CHROMEOS_ASH)
  70. // Task runner corresponding to a background thread, used for tasks that may
  71. // block.
  72. scoped_refptr<base::SequencedTaskRunner> background_thread_task_runner_;
  73. #endif // IS_CHROMEOS_ASH
  74. // A serialized string containing the variations state.
  75. std::string variations_string_;
  76. // Number of entries in |variations_string_|.
  77. size_t num_variations_ = 0;
  78. // A serialized string containing the synthetic trials state.
  79. std::string synthetic_trials_string_;
  80. // Number of entries in |synthetic_trials_string_|.
  81. size_t num_synthetic_trials_ = 0;
  82. SEQUENCE_CHECKER(sequence_checker_);
  83. };
  84. VariationsCrashKeys::VariationsCrashKeys() {
  85. base::FieldTrial::ActiveGroups active_groups;
  86. base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
  87. for (const auto& entry : active_groups) {
  88. AppendFieldTrial(entry.trial_name, entry.group_name);
  89. }
  90. #if BUILDFLAG(IS_CHROMEOS_ASH)
  91. background_thread_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
  92. {base::TaskPriority::BEST_EFFORT, base::MayBlock()});
  93. #endif // IS_CHROMEOS_ASH
  94. UpdateCrashKeys();
  95. ui_thread_task_runner_ = base::SequencedTaskRunnerHandle::Get();
  96. base::FieldTrialList::AddObserver(this);
  97. }
  98. VariationsCrashKeys::~VariationsCrashKeys() {
  99. base::FieldTrialList::RemoveObserver(this);
  100. g_num_variations_crash_key.Clear();
  101. g_variations_crash_key.Clear();
  102. }
  103. void VariationsCrashKeys::OnFieldTrialGroupFinalized(
  104. const std::string& trial_name,
  105. const std::string& group_name) {
  106. // If this is called on a different thread, post it back to the UI thread.
  107. // Note: This is safe to do because in production, this object is never
  108. // deleted and if this is called, it means the constructor has already run,
  109. // which is the only place that |ui_thread_task_runner_| is set.
  110. if (!ui_thread_task_runner_->RunsTasksInCurrentSequence()) {
  111. ui_thread_task_runner_->PostTask(
  112. FROM_HERE,
  113. BindOnce(&VariationsCrashKeys::OnFieldTrialGroupFinalized,
  114. // base::Unretained() is safe here because this object is
  115. // never deleted in production.
  116. base::Unretained(this), trial_name, group_name));
  117. return;
  118. }
  119. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  120. AppendFieldTrial(trial_name, group_name);
  121. UpdateCrashKeys();
  122. }
  123. void VariationsCrashKeys::AppendFieldTrial(const std::string& trial_name,
  124. const std::string& group_name) {
  125. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  126. auto active_group_id = MakeActiveGroupId(trial_name, group_name);
  127. auto variation = ActiveGroupToString(active_group_id);
  128. variations_string_ += variation;
  129. ++num_variations_;
  130. }
  131. ExperimentListInfo VariationsCrashKeys::GetExperimentListInfo() {
  132. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  133. ExperimentListInfo result;
  134. result.num_experiments = num_variations_ + num_synthetic_trials_;
  135. result.experiment_list.reserve(variations_string_.size() +
  136. synthetic_trials_string_.size());
  137. result.experiment_list.append(variations_string_);
  138. result.experiment_list.append(synthetic_trials_string_);
  139. return result;
  140. }
  141. void VariationsCrashKeys::UpdateCrashKeys() {
  142. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  143. ExperimentListInfo info = GetExperimentListInfo();
  144. g_num_variations_crash_key.Set(base::NumberToString(info.num_experiments));
  145. if (info.experiment_list.size() > kVariationsKeySize) {
  146. // If size exceeded, truncate to the last full entry.
  147. int comma_index =
  148. info.experiment_list.substr(0, kVariationsKeySize).rfind(',');
  149. info.experiment_list.resize(comma_index + 1);
  150. // NOTREACHED() will let us know of the problem and adjust the limit.
  151. NOTREACHED();
  152. }
  153. g_variations_crash_key.Set(info.experiment_list);
  154. #if BUILDFLAG(IS_CHROMEOS_ASH)
  155. ReportVariationsToChromeOs(background_thread_task_runner_, info);
  156. #endif // IS_CHROMEOS_ASH
  157. }
  158. void VariationsCrashKeys::OnSyntheticTrialsChanged(
  159. const std::vector<SyntheticTrialGroup>& synthetic_trials) {
  160. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  161. // Note: This part is inefficient as each time synthetic trials change, this
  162. // code recomputes all their name hashes. However, given that there should
  163. // not be too many synthetic trials, this is not too big of an issue.
  164. synthetic_trials_string_.clear();
  165. for (const auto& synthetic_trial : synthetic_trials) {
  166. synthetic_trials_string_ += ActiveGroupToString(synthetic_trial.id());
  167. }
  168. num_synthetic_trials_ = synthetic_trials.size();
  169. UpdateCrashKeys();
  170. }
  171. // Singletone crash key manager. Allocated once at process start up and
  172. // intentionally leaked since it needs to live for the duration of the process
  173. // there's no benefit in cleaning it up at exit.
  174. VariationsCrashKeys* g_variations_crash_keys = nullptr;
  175. } // namespace
  176. const char kNumExperimentsKey[] = "num-experiments";
  177. const char kExperimentListKey[] = "variations";
  178. void InitCrashKeys() {
  179. DCHECK(!g_variations_crash_keys);
  180. g_variations_crash_keys = new VariationsCrashKeys();
  181. ANNOTATE_LEAKING_OBJECT_PTR(g_variations_crash_keys);
  182. }
  183. void UpdateCrashKeysWithSyntheticTrials(
  184. const std::vector<SyntheticTrialGroup>& synthetic_trials) {
  185. DCHECK(g_variations_crash_keys);
  186. g_variations_crash_keys->OnSyntheticTrialsChanged(synthetic_trials);
  187. }
  188. void ClearCrashKeysInstanceForTesting() {
  189. DCHECK(g_variations_crash_keys);
  190. delete g_variations_crash_keys;
  191. g_variations_crash_keys = nullptr;
  192. }
  193. ExperimentListInfo GetExperimentListInfo() {
  194. DCHECK(g_variations_crash_keys);
  195. return g_variations_crash_keys->GetExperimentListInfo();
  196. }
  197. } // namespace variations