| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/translate/core/browser/ranker_model_loader.h" | 5 #include "components/ranker/ranker_model_loader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/important_file_writer.h" | 13 #include "base/files/important_file_writer.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/profiler/scoped_tracker.h" | 17 #include "base/profiler/scoped_tracker.h" |
| 18 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
| 21 #include "base/task_scheduler/post_task.h" | 21 #include "base/task_scheduler/post_task.h" |
| 22 #include "base/threading/sequenced_task_runner_handle.h" | 22 #include "base/threading/sequenced_task_runner_handle.h" |
| 23 #include "components/translate/core/browser/proto/ranker_model.pb.h" | 23 #include "components/ranker/proto/ranker_model.pb.h" |
| 24 #include "components/translate/core/browser/ranker_model.h" | 24 #include "components/ranker/ranker_model.h" |
| 25 #include "components/translate/core/browser/translate_url_fetcher.h" | 25 #include "components/ranker/ranker_url_fetcher.h" |
| 26 | 26 |
| 27 namespace translate { | 27 namespace ranker { |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 using chrome_intelligence::RankerModel; | |
| 31 using chrome_intelligence::RankerModelProto; | |
| 32 | |
| 33 constexpr int kUrlFetcherId = 2; | |
| 34 | |
| 35 // The minimum duration, in minutes, between download attempts. | 30 // The minimum duration, in minutes, between download attempts. |
| 36 constexpr int kMinRetryDelayMins = 3; | 31 constexpr int kMinRetryDelayMins = 3; |
| 37 | 32 |
| 38 // Suffixes for the various histograms produced by the backend. | 33 // Suffixes for the various histograms produced by the backend. |
| 39 const char kWriteTimerHistogram[] = ".Timer.WriteModel"; | 34 const char kWriteTimerHistogram[] = ".Timer.WriteModel"; |
| 40 const char kReadTimerHistogram[] = ".Timer.ReadModel"; | 35 const char kReadTimerHistogram[] = ".Timer.ReadModel"; |
| 41 const char kDownloadTimerHistogram[] = ".Timer.DownloadModel"; | 36 const char kDownloadTimerHistogram[] = ".Timer.DownloadModel"; |
| 42 const char kParsetimerHistogram[] = ".Timer.ParseModel"; | 37 const char kParsetimerHistogram[] = ".Timer.ParseModel"; |
| 43 const char kModelStatusHistogram[] = ".Model.Status"; | 38 const char kModelStatusHistogram[] = ".Model.Status"; |
| 44 | 39 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 GURL model_url, | 95 GURL model_url, |
| 101 std::string uma_prefix) | 96 std::string uma_prefix) |
| 102 : background_task_runner_(base::CreateSequencedTaskRunnerWithTraits( | 97 : background_task_runner_(base::CreateSequencedTaskRunnerWithTraits( |
| 103 {base::MayBlock(), base::TaskPriority::BACKGROUND, | 98 {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 104 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})), | 99 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})), |
| 105 validate_model_cb_(std::move(validate_model_cb)), | 100 validate_model_cb_(std::move(validate_model_cb)), |
| 106 on_model_available_cb_(std::move(on_model_available_cb)), | 101 on_model_available_cb_(std::move(on_model_available_cb)), |
| 107 model_path_(std::move(model_path)), | 102 model_path_(std::move(model_path)), |
| 108 model_url_(std::move(model_url)), | 103 model_url_(std::move(model_url)), |
| 109 uma_prefix_(std::move(uma_prefix)), | 104 uma_prefix_(std::move(uma_prefix)), |
| 110 url_fetcher_(base::MakeUnique<TranslateURLFetcher>(kUrlFetcherId)), | 105 url_fetcher_(base::MakeUnique<RankerURLFetcher>()), |
| 111 weak_ptr_factory_(this) {} | 106 weak_ptr_factory_(this) {} |
| 112 | 107 |
| 113 RankerModelLoader::~RankerModelLoader() { | 108 RankerModelLoader::~RankerModelLoader() { |
| 114 DCHECK(sequence_checker_.CalledOnValidSequence()); | 109 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 115 } | 110 } |
| 116 | 111 |
| 117 void RankerModelLoader::NotifyOfRankerActivity() { | 112 void RankerModelLoader::NotifyOfRankerActivity() { |
| 118 DCHECK(sequence_checker_.CalledOnValidSequence()); | 113 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 119 switch (state_) { | 114 switch (state_) { |
| 120 case LoaderState::NOT_STARTED: | 115 case LoaderState::NOT_STARTED: |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 // |url_fetcher_| maintains a request retry counter. If all allowed attempts | 221 // |url_fetcher_| maintains a request retry counter. If all allowed attempts |
| 227 // have already been exhausted, then the loader is finished and has abandoned | 222 // have already been exhausted, then the loader is finished and has abandoned |
| 228 // loading the model. | 223 // loading the model. |
| 229 if (!request_started) { | 224 if (!request_started) { |
| 230 DVLOG(2) << "Model download abandoned."; | 225 DVLOG(2) << "Model download abandoned."; |
| 231 ReportModelStatus(RankerModelStatus::MODEL_LOADING_ABANDONED); | 226 ReportModelStatus(RankerModelStatus::MODEL_LOADING_ABANDONED); |
| 232 state_ = LoaderState::FINISHED; | 227 state_ = LoaderState::FINISHED; |
| 233 } | 228 } |
| 234 } | 229 } |
| 235 | 230 |
| 236 void RankerModelLoader::OnURLFetched(int /* id */, | 231 void RankerModelLoader::OnURLFetched(bool success, const std::string& data) { |
| 237 bool success, | |
| 238 const std::string& data) { | |
| 239 DCHECK(sequence_checker_.CalledOnValidSequence()); | 232 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 240 DCHECK_EQ(state_, LoaderState::LOADING_FROM_URL); | 233 DCHECK_EQ(state_, LoaderState::LOADING_FROM_URL); |
| 241 | 234 |
| 242 // Record the duration of the download. | 235 // Record the duration of the download. |
| 243 RecordTimerHistogram(uma_prefix_ + kDownloadTimerHistogram, | 236 RecordTimerHistogram(uma_prefix_ + kDownloadTimerHistogram, |
| 244 base::TimeTicks::Now() - load_start_time_); | 237 base::TimeTicks::Now() - load_start_time_); |
| 245 | 238 |
| 246 // On request failure, transition back to IDLE. The loader will retry, or | 239 // On request failure, transition back to IDLE. The loader will retry, or |
| 247 // enforce the max download attempts, later. | 240 // enforce the max download attempts, later. |
| 248 if (!success || data.empty()) { | 241 if (!success || data.empty()) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 273 background_task_runner_->PostTask( | 266 background_task_runner_->PostTask( |
| 274 FROM_HERE, base::BindOnce(&SaveToFile, model_url_, model_path_, | 267 FROM_HERE, base::BindOnce(&SaveToFile, model_url_, model_path_, |
| 275 model->SerializeAsString(), uma_prefix_)); | 268 model->SerializeAsString(), uma_prefix_)); |
| 276 } | 269 } |
| 277 | 270 |
| 278 // The loader is finished. Transfer the model to the client. | 271 // The loader is finished. Transfer the model to the client. |
| 279 state_ = LoaderState::FINISHED; | 272 state_ = LoaderState::FINISHED; |
| 280 on_model_available_cb_.Run(std::move(model)); | 273 on_model_available_cb_.Run(std::move(model)); |
| 281 } | 274 } |
| 282 | 275 |
| 283 std::unique_ptr<chrome_intelligence::RankerModel> | 276 std::unique_ptr<RankerModel> RankerModelLoader::CreateAndValidateModel( |
| 284 RankerModelLoader::CreateAndValidateModel(const std::string& data) { | 277 const std::string& data) { |
| 285 DCHECK(sequence_checker_.CalledOnValidSequence()); | 278 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 286 MyScopedHistogramTimer timer(uma_prefix_ + kParsetimerHistogram); | 279 MyScopedHistogramTimer timer(uma_prefix_ + kParsetimerHistogram); |
| 287 auto model = RankerModel::FromString(data); | 280 auto model = RankerModel::FromString(data); |
| 288 if (ReportModelStatus(model ? validate_model_cb_.Run(*model) | 281 if (ReportModelStatus(model ? validate_model_cb_.Run(*model) |
| 289 : RankerModelStatus::PARSE_FAILED) != | 282 : RankerModelStatus::PARSE_FAILED) != |
| 290 RankerModelStatus::OK) { | 283 RankerModelStatus::OK) { |
| 291 return nullptr; | 284 return nullptr; |
| 292 } | 285 } |
| 293 return model; | 286 return model; |
| 294 } | 287 } |
| 295 | 288 |
| 296 RankerModelStatus RankerModelLoader::ReportModelStatus( | 289 RankerModelStatus RankerModelLoader::ReportModelStatus( |
| 297 RankerModelStatus model_status) { | 290 RankerModelStatus model_status) { |
| 298 DCHECK(sequence_checker_.CalledOnValidSequence()); | 291 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 299 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( | 292 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 300 uma_prefix_ + kModelStatusHistogram, 1, | 293 uma_prefix_ + kModelStatusHistogram, 1, |
| 301 static_cast<int>(RankerModelStatus::MAX), | 294 static_cast<int>(RankerModelStatus::MAX), |
| 302 static_cast<int>(RankerModelStatus::MAX) + 1, | 295 static_cast<int>(RankerModelStatus::MAX) + 1, |
| 303 base::HistogramBase::kUmaTargetedHistogramFlag); | 296 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 304 if (histogram) | 297 if (histogram) |
| 305 histogram->Add(static_cast<int>(model_status)); | 298 histogram->Add(static_cast<int>(model_status)); |
| 306 return model_status; | 299 return model_status; |
| 307 } | 300 } |
| 308 | 301 |
| 309 } // namespace translate | 302 } // namespace ranker |
| OLD | NEW |