Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(832)

Unified Diff: base/metrics/field_trial.cc

Issue 2463223002: Store field trial parameters in shared memory (Closed)
Patch Set: fix compilation error Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/metrics/field_trial.cc
diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc
index eb882da14f18c03b45682c29f6aa5501c100a0bf..662e635531ef72dcb454d78ea22c9c52a9b44954 100644
--- a/base/metrics/field_trial.cc
+++ b/base/metrics/field_trial.cc
@@ -13,6 +13,7 @@
#include "base/debug/alias.h"
#include "base/feature_list.h"
#include "base/logging.h"
+#include "base/metrics/field_trial_param_associator.h"
#include "base/pickle.h"
#include "base/process/memory.h"
#include "base/rand_util.h"
@@ -71,25 +72,81 @@ struct FieldTrialEntry {
// Size of the pickled structure, NOT the total size of this entry.
uint32_t size;
- // Calling this is only valid when the entry is initialized. That is, it
- // resides in shared memory and has a pickle containing the trial name and
- // group name following it.
- bool GetTrialAndGroupName(StringPiece* trial_name,
- StringPiece* group_name) const {
+ // Returns an iterator over the data containing names and params.
+ PickleIterator GetPickleIterator() const {
char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) +
sizeof(FieldTrialEntry);
Pickle pickle(src, size);
- PickleIterator pickle_iter(pickle);
+ return PickleIterator(pickle);
+ }
- if (!pickle_iter.ReadStringPiece(trial_name))
+ // Takes the iterator and writes out the first two items into |trial_name| and
+ // |group_name|.
+ bool ReadStringPair(PickleIterator* iter,
+ StringPiece* trial_name,
+ StringPiece* group_name) const {
+ if (!iter->ReadStringPiece(trial_name))
return false;
- if (!pickle_iter.ReadStringPiece(group_name))
+ if (!iter->ReadStringPiece(group_name))
return false;
return true;
}
+
+ // Calling this is only valid when the entry is initialized. That is, it
+ // resides in shared memory and has a pickle containing the trial name and
+ // group name following it.
+ bool GetTrialAndGroupName(StringPiece* trial_name,
+ StringPiece* group_name) const {
+ PickleIterator iter = GetPickleIterator();
+ return ReadStringPair(&iter, trial_name, group_name);
+ }
+
+ // Calling this is only valid when the entry is initialized as well. Reads the
+ // parameters following the trial and group name and stores them as key-value
+ // mappings in |params|.
+ bool GetParams(std::map<std::string, std::string>* params) const {
+ PickleIterator iter = GetPickleIterator();
+ StringPiece tmp;
+ if (!ReadStringPair(&iter, &tmp, &tmp))
+ return false;
+
+ while (true) {
+ StringPiece key;
+ StringPiece value;
+ if (!iter.ReadStringPiece(&key))
+ return true; // No more params to read.
+ if (!iter.ReadStringPiece(&value))
+ return false;
+ (*params)[key.as_string()] = value.as_string();
+ }
+ }
};
+// Writes out the field trial's contents (via trial_state) to the pickle. The
+// format of the pickle looks like:
+// TrialName, GroupName, ParamKey1, ParamValue1, ParamKey2, ParamValue2, ...
+// If there are no parameters, then it just ends at GroupName.
+bool PickleFieldTrial(const FieldTrial::State& trial_state, Pickle* pickle) {
+ if (!pickle->WriteString(trial_state.trial_name))
+ return false;
+ if (!pickle->WriteString(trial_state.group_name))
+ return false;
+
+ // Get field trial params.
+ std::map<std::string, std::string> params;
+ FieldTrialParamAssociator::GetInstance()->GetFieldTrialParamsWithoutFallback(
+ trial_state.trial_name.as_string(), trial_state.group_name.as_string(),
+ &params);
+
+ // Write params to pickle.
+ for (const auto& param : params) {
+ pickle->WriteString(StringPiece(param.first));
+ pickle->WriteString(StringPiece(param.second));
+ }
+ return true;
+}
+
// Created a time value based on |year|, |month| and |day_of_month| parameters.
Time CreateTimeFromParams(int year, int month, int day_of_month) {
DCHECK_GT(year, 1970);
@@ -584,6 +641,79 @@ std::string FieldTrialList::FindFullName(const std::string& trial_name) {
}
// static
+bool FieldTrialList::GetParamsFromSharedMemory(
+ FieldTrial* field_trial,
+ std::map<std::string, std::string>* params) {
+ DCHECK(global_);
+ // If the field trial allocator is not set up yet, then there are several
+ // cases:
+ // - We are in the browser process and the allocator has not been set up
+ // yet. If we got here, then we couldn't find the params in
+ // FieldTrialParamAssociator, so it's definitely not here. Return false.
+ // - Using shared memory for field trials is not enabled. If we got here,
+ // then there's nothing in shared memory. Return false.
+ // - We are in the child process and the allocator has not been set up yet.
+ // If this is the case, then you are calling this too early. The field trial
+ // allocator should get set up very early in the lifecycle. Try to see if
+ // you can call it after it's been set up.
+ if (!global_->field_trial_allocator_)
+ return false;
+
+ // If ref_ isn't set, then the field trial data can't be in shared memory.
+ if (!field_trial->ref_)
+ return false;
+
+ const FieldTrialEntry* entry =
+ global_->field_trial_allocator_->GetAsObject<const FieldTrialEntry>(
+ field_trial->ref_, kFieldTrialType);
+ return entry->GetParams(params);
+}
+
+// static
+void FieldTrialList::ClearParamsFromSharedMemoryForTesting() {
Alexei Svitkine (slow) 2016/11/23 17:23:39 Use the lock for this.
lawrencewu 2016/11/23 19:07:33 Done.
+ if (!global_ || !global_->field_trial_allocator_)
+ return;
+ // To clear the params, we iterate through every item in the allocator, copy
+ // just the trial and group name into a newly-allocated segment and then clear
+ // the existing item.
+ SharedPersistentMemoryAllocator* allocator =
Alexei Svitkine (slow) 2016/11/23 17:23:39 FieldTrialAllocator Change the other types below
lawrencewu 2016/11/23 19:07:33 Done.
+ global_->field_trial_allocator_.get();
+ PersistentMemoryAllocator::Iterator mem_iter(allocator);
+
+ SharedPersistentMemoryAllocator::Reference prev_ref;
+ while ((prev_ref = mem_iter.GetNextOfType(kFieldTrialType)) !=
+ SharedPersistentMemoryAllocator::kReferenceNull) {
+ // Get the existing field trial entry in shared memory.
+ const FieldTrialEntry* prev_entry =
+ allocator->GetAsObject<const FieldTrialEntry>(prev_ref,
+ kFieldTrialType);
+ StringPiece trial_name;
+ StringPiece group_name;
+ prev_entry->GetTrialAndGroupName(&trial_name, &group_name);
Alexei Svitkine (slow) 2016/11/23 17:23:39 Check return value?
lawrencewu 2016/11/23 19:07:33 Done by continuing if we failed to get the names.
+
+ // Write a new entry, minus the params.
+ Pickle pickle;
+ pickle.WriteString(trial_name);
+ pickle.WriteString(group_name);
+ size_t total_size = sizeof(FieldTrialEntry) + pickle.size();
+ SharedPersistentMemoryAllocator::Reference new_ref =
+ allocator->Allocate(total_size, kFieldTrialType);
+ FieldTrialEntry* new_entry =
+ allocator->GetAsObject<FieldTrialEntry>(new_ref, kFieldTrialType);
+ new_entry->activated = prev_entry->activated;
+ new_entry->size = pickle.size();
+
+ // Update the ref on the field trial.
+ FieldTrial* trial = Find(trial_name.as_string());
+ trial->ref_ = new_ref;
+
+ // Mark the existing entry as unused.
+ uint32_t oldType = allocator->GetType(prev_ref);
Alexei Svitkine (slow) 2016/11/23 17:23:39 old_type
lawrencewu 2016/11/23 19:07:33 Fixed.
+ allocator->ChangeType(prev_ref, 0, oldType);
+ }
+}
+
+// static
bool FieldTrialList::TrialExists(const std::string& trial_name) {
return Find(trial_name) != NULL;
}
@@ -1003,8 +1133,8 @@ void FieldTrialList::AddToAllocatorWhileLocked(FieldTrial* field_trial) {
return;
Pickle pickle;
- pickle.WriteString(trial_state.trial_name);
- pickle.WriteString(trial_state.group_name);
+ if (!PickleFieldTrial(trial_state, &pickle))
+ return;
size_t total_size = sizeof(FieldTrialEntry) + pickle.size();
FieldTrial::FieldTrialRef ref =

Powered by Google App Engine
This is Rietveld 408576698