OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 //------------------------------------------------------------------------------ | 5 //------------------------------------------------------------------------------ |
6 // Description of the life cycle of a instance of MetricsService. | 6 // Description of the life cycle of a instance of MetricsService. |
7 // | 7 // |
8 // OVERVIEW | 8 // OVERVIEW |
9 // | 9 // |
10 // A MetricsService instance is typically created at application startup. It is | 10 // A MetricsService instance is typically created at application startup. It is |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 | 550 |
551 //------------------------------------------------------------------------------ | 551 //------------------------------------------------------------------------------ |
552 // Initialization methods | 552 // Initialization methods |
553 | 553 |
554 void MetricsService::InitializeMetricsState() { | 554 void MetricsService::InitializeMetricsState() { |
555 local_state_->SetString(metrics::prefs::kStabilityStatsVersion, | 555 local_state_->SetString(metrics::prefs::kStabilityStatsVersion, |
556 client_->GetVersionString()); | 556 client_->GetVersionString()); |
557 local_state_->SetInt64(metrics::prefs::kStabilityStatsBuildTime, | 557 local_state_->SetInt64(metrics::prefs::kStabilityStatsBuildTime, |
558 MetricsLog::GetBuildTime()); | 558 MetricsLog::GetBuildTime()); |
559 | 559 |
| 560 log_manager_.LoadPersistedUnsentLogs(); |
| 561 |
560 session_id_ = local_state_->GetInteger(metrics::prefs::kMetricsSessionID); | 562 session_id_ = local_state_->GetInteger(metrics::prefs::kMetricsSessionID); |
561 | 563 |
562 if (!local_state_->GetBoolean(metrics::prefs::kStabilityExitedCleanly)) { | 564 if (!local_state_->GetBoolean(metrics::prefs::kStabilityExitedCleanly)) { |
563 IncrementPrefValue(metrics::prefs::kStabilityCrashCount); | 565 IncrementPrefValue(metrics::prefs::kStabilityCrashCount); |
564 // Reset flag, and wait until we call LogNeedForCleanShutdown() before | 566 // Reset flag, and wait until we call LogNeedForCleanShutdown() before |
565 // monitoring. | 567 // monitoring. |
566 local_state_->SetBoolean(metrics::prefs::kStabilityExitedCleanly, true); | 568 local_state_->SetBoolean(metrics::prefs::kStabilityExitedCleanly, true); |
567 | 569 |
568 // TODO(rtenneti): On windows, consider saving/getting execution_phase from | 570 // TODO(rtenneti): On windows, consider saving/getting execution_phase from |
569 // the registry. | 571 // the registry. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", | 736 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
735 log_manager_.current_log()->num_events()); | 737 log_manager_.current_log()->num_events()); |
736 log_manager_.DiscardCurrentLog(); | 738 log_manager_.DiscardCurrentLog(); |
737 OpenNewLog(); // Start trivial log to hold our histograms. | 739 OpenNewLog(); // Start trivial log to hold our histograms. |
738 } | 740 } |
739 | 741 |
740 // Put incremental data (histogram deltas, and realtime stats deltas) at the | 742 // Put incremental data (histogram deltas, and realtime stats deltas) at the |
741 // end of all log transmissions (initial log handles this separately). | 743 // end of all log transmissions (initial log handles this separately). |
742 // RecordIncrementalStabilityElements only exists on the derived | 744 // RecordIncrementalStabilityElements only exists on the derived |
743 // MetricsLog class. | 745 // MetricsLog class. |
744 MetricsLog* current_log = | 746 MetricsLog* current_log = log_manager_.current_log(); |
745 static_cast<MetricsLog*>(log_manager_.current_log()); | |
746 DCHECK(current_log); | 747 DCHECK(current_log); |
747 std::vector<variations::ActiveGroupId> synthetic_trials; | 748 std::vector<variations::ActiveGroupId> synthetic_trials; |
748 GetCurrentSyntheticFieldTrials(&synthetic_trials); | 749 GetCurrentSyntheticFieldTrials(&synthetic_trials); |
749 current_log->RecordEnvironment( | 750 current_log->RecordEnvironment( |
750 metrics_providers_.get(), synthetic_trials, GetInstallDate()); | 751 metrics_providers_.get(), synthetic_trials, GetInstallDate()); |
751 base::TimeDelta incremental_uptime; | 752 base::TimeDelta incremental_uptime; |
752 base::TimeDelta uptime; | 753 base::TimeDelta uptime; |
753 GetUptimes(local_state_, &incremental_uptime, &uptime); | 754 GetUptimes(local_state_, &incremental_uptime, &uptime); |
754 current_log->RecordStabilityMetrics(metrics_providers_.get(), | 755 current_log->RecordStabilityMetrics(metrics_providers_.get(), |
755 incremental_uptime, uptime); | 756 incremental_uptime, uptime); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 case INITIALIZED: | 882 case INITIALIZED: |
882 case INIT_TASK_SCHEDULED: // We should be further along by now. | 883 case INIT_TASK_SCHEDULED: // We should be further along by now. |
883 NOTREACHED(); | 884 NOTREACHED(); |
884 return; | 885 return; |
885 | 886 |
886 case INIT_TASK_DONE: | 887 case INIT_TASK_DONE: |
887 if (has_initial_stability_log_) { | 888 if (has_initial_stability_log_) { |
888 // There's an initial stability log, ready to send. | 889 // There's an initial stability log, ready to send. |
889 log_manager_.StageNextLogForUpload(); | 890 log_manager_.StageNextLogForUpload(); |
890 has_initial_stability_log_ = false; | 891 has_initial_stability_log_ = false; |
891 // Note: No need to call LoadPersistedUnsentLogs() here because unsent | |
892 // logs have already been loaded by PrepareInitialStabilityLog(). | |
893 state_ = SENDING_INITIAL_STABILITY_LOG; | 892 state_ = SENDING_INITIAL_STABILITY_LOG; |
894 } else { | 893 } else { |
895 PrepareInitialMetricsLog(); | 894 PrepareInitialMetricsLog(); |
896 // Load unsent logs (if any) from local state. | |
897 log_manager_.LoadPersistedUnsentLogs(); | |
898 state_ = SENDING_INITIAL_METRICS_LOG; | 895 state_ = SENDING_INITIAL_METRICS_LOG; |
899 } | 896 } |
900 break; | 897 break; |
901 | 898 |
902 case SENDING_OLD_LOGS: | 899 case SENDING_OLD_LOGS: |
903 NOTREACHED(); // Shouldn't be staging a new log during old log sending. | 900 NOTREACHED(); // Shouldn't be staging a new log during old log sending. |
904 return; | 901 return; |
905 | 902 |
906 case SENDING_CURRENT_LOGS: | 903 case SENDING_CURRENT_LOGS: |
907 CloseCurrentLog(); | 904 CloseCurrentLog(); |
(...skipping 15 matching lines...) Expand all Loading... |
923 | 920 |
924 scoped_ptr<MetricsLog> initial_stability_log( | 921 scoped_ptr<MetricsLog> initial_stability_log( |
925 CreateLog(MetricsLog::INITIAL_STABILITY_LOG)); | 922 CreateLog(MetricsLog::INITIAL_STABILITY_LOG)); |
926 | 923 |
927 // Do not call NotifyOnDidCreateMetricsLog here because the stability | 924 // Do not call NotifyOnDidCreateMetricsLog here because the stability |
928 // log describes stats from the _previous_ session. | 925 // log describes stats from the _previous_ session. |
929 | 926 |
930 if (!initial_stability_log->LoadSavedEnvironmentFromPrefs()) | 927 if (!initial_stability_log->LoadSavedEnvironmentFromPrefs()) |
931 return; | 928 return; |
932 | 929 |
933 log_manager_.LoadPersistedUnsentLogs(); | |
934 | |
935 log_manager_.PauseCurrentLog(); | 930 log_manager_.PauseCurrentLog(); |
936 log_manager_.BeginLoggingWithLog(initial_stability_log.Pass()); | 931 log_manager_.BeginLoggingWithLog(initial_stability_log.Pass()); |
937 | 932 |
938 // Note: Some stability providers may record stability stats via histograms, | 933 // Note: Some stability providers may record stability stats via histograms, |
939 // so this call has to be after BeginLoggingWithLog(). | 934 // so this call has to be after BeginLoggingWithLog(). |
940 log_manager_.current_log()->RecordStabilityMetrics( | 935 log_manager_.current_log()->RecordStabilityMetrics( |
941 metrics_providers_.get(), base::TimeDelta(), base::TimeDelta()); | 936 metrics_providers_.get(), base::TimeDelta(), base::TimeDelta()); |
942 RecordCurrentStabilityHistograms(); | 937 RecordCurrentStabilityHistograms(); |
943 | 938 |
944 // Note: RecordGeneralMetrics() intentionally not called since this log is for | 939 // Note: RecordGeneralMetrics() intentionally not called since this log is for |
(...skipping 21 matching lines...) Expand all Loading... |
966 base::TimeDelta uptime; | 961 base::TimeDelta uptime; |
967 GetUptimes(local_state_, &incremental_uptime, &uptime); | 962 GetUptimes(local_state_, &incremental_uptime, &uptime); |
968 | 963 |
969 // Histograms only get written to the current log, so make the new log current | 964 // Histograms only get written to the current log, so make the new log current |
970 // before writing them. | 965 // before writing them. |
971 log_manager_.PauseCurrentLog(); | 966 log_manager_.PauseCurrentLog(); |
972 log_manager_.BeginLoggingWithLog(initial_metrics_log_.Pass()); | 967 log_manager_.BeginLoggingWithLog(initial_metrics_log_.Pass()); |
973 | 968 |
974 // Note: Some stability providers may record stability stats via histograms, | 969 // Note: Some stability providers may record stability stats via histograms, |
975 // so this call has to be after BeginLoggingWithLog(). | 970 // so this call has to be after BeginLoggingWithLog(). |
976 MetricsLog* current_log = | 971 MetricsLog* current_log = log_manager_.current_log(); |
977 static_cast<MetricsLog*>(log_manager_.current_log()); | |
978 current_log->RecordStabilityMetrics(metrics_providers_.get(), | 972 current_log->RecordStabilityMetrics(metrics_providers_.get(), |
979 base::TimeDelta(), base::TimeDelta()); | 973 base::TimeDelta(), base::TimeDelta()); |
980 RecordCurrentHistograms(); | 974 RecordCurrentHistograms(); |
981 | 975 |
982 current_log->RecordGeneralMetrics(metrics_providers_.get()); | 976 current_log->RecordGeneralMetrics(metrics_providers_.get()); |
983 | 977 |
984 log_manager_.FinishCurrentLog(); | 978 log_manager_.FinishCurrentLog(); |
985 log_manager_.ResumePausedLog(); | 979 log_manager_.ResumePausedLog(); |
986 | 980 |
| 981 // Store unsent logs, including the initial log that was just saved, so |
| 982 // that they're not lost in case of a crash before upload time. |
| 983 log_manager_.PersistUnsentLogs(); |
| 984 |
987 DCHECK(!log_manager_.has_staged_log()); | 985 DCHECK(!log_manager_.has_staged_log()); |
988 log_manager_.StageNextLogForUpload(); | 986 log_manager_.StageNextLogForUpload(); |
989 } | 987 } |
990 | 988 |
991 void MetricsService::SendStagedLog() { | 989 void MetricsService::SendStagedLog() { |
992 DCHECK(log_manager_.has_staged_log()); | 990 DCHECK(log_manager_.has_staged_log()); |
993 if (!log_manager_.has_staged_log()) | 991 if (!log_manager_.has_staged_log()) |
994 return; | 992 return; |
995 | 993 |
996 DCHECK(!log_upload_in_progress_); | 994 DCHECK(!log_upload_in_progress_); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 const size_t log_size = log_manager_.staged_log().length(); | 1038 const size_t log_size = log_manager_.staged_log().length(); |
1041 if (!upload_succeeded && log_size > kUploadLogAvoidRetransmitSize) { | 1039 if (!upload_succeeded && log_size > kUploadLogAvoidRetransmitSize) { |
1042 UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", | 1040 UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
1043 static_cast<int>(log_size)); | 1041 static_cast<int>(log_size)); |
1044 discard_log = true; | 1042 discard_log = true; |
1045 } else if (response_code == 400) { | 1043 } else if (response_code == 400) { |
1046 // Bad syntax. Retransmission won't work. | 1044 // Bad syntax. Retransmission won't work. |
1047 discard_log = true; | 1045 discard_log = true; |
1048 } | 1046 } |
1049 | 1047 |
1050 if (upload_succeeded || discard_log) | 1048 if (upload_succeeded || discard_log) { |
1051 log_manager_.DiscardStagedLog(); | 1049 log_manager_.DiscardStagedLog(); |
| 1050 // Store the updated list to disk now that the removed log is uploaded. |
| 1051 log_manager_.PersistUnsentLogs(); |
| 1052 } |
1052 | 1053 |
1053 if (!log_manager_.has_staged_log()) { | 1054 if (!log_manager_.has_staged_log()) { |
1054 switch (state_) { | 1055 switch (state_) { |
1055 case SENDING_INITIAL_STABILITY_LOG: | 1056 case SENDING_INITIAL_STABILITY_LOG: |
1056 // Store the updated list to disk now that the removed log is uploaded. | |
1057 log_manager_.PersistUnsentLogs(); | |
1058 PrepareInitialMetricsLog(); | 1057 PrepareInitialMetricsLog(); |
1059 SendStagedLog(); | 1058 SendStagedLog(); |
1060 state_ = SENDING_INITIAL_METRICS_LOG; | 1059 state_ = SENDING_INITIAL_METRICS_LOG; |
1061 break; | 1060 break; |
1062 | 1061 |
1063 case SENDING_INITIAL_METRICS_LOG: | 1062 case SENDING_INITIAL_METRICS_LOG: |
1064 // The initial metrics log never gets persisted to local state, so it's | |
1065 // not necessary to call log_manager_.PersistUnsentLogs() here. | |
1066 // TODO(asvitkine): It should be persisted like the initial stability | |
1067 // log and old unsent logs. http://crbug.com/328417 | |
1068 state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS | 1063 state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS |
1069 : SENDING_CURRENT_LOGS; | 1064 : SENDING_CURRENT_LOGS; |
1070 break; | 1065 break; |
1071 | 1066 |
1072 case SENDING_OLD_LOGS: | 1067 case SENDING_OLD_LOGS: |
1073 // Store the updated list to disk now that the removed log is uploaded. | |
1074 log_manager_.PersistUnsentLogs(); | |
1075 if (!log_manager_.has_unsent_logs()) | 1068 if (!log_manager_.has_unsent_logs()) |
1076 state_ = SENDING_CURRENT_LOGS; | 1069 state_ = SENDING_CURRENT_LOGS; |
1077 break; | 1070 break; |
1078 | 1071 |
1079 case SENDING_CURRENT_LOGS: | 1072 case SENDING_CURRENT_LOGS: |
1080 break; | 1073 break; |
1081 | 1074 |
1082 default: | 1075 default: |
1083 NOTREACHED(); | 1076 NOTREACHED(); |
1084 break; | 1077 break; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1144 | 1137 |
1145 void MetricsService::CheckForClonedInstall( | 1138 void MetricsService::CheckForClonedInstall( |
1146 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 1139 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
1147 state_manager_->CheckForClonedInstall(task_runner); | 1140 state_manager_->CheckForClonedInstall(task_runner); |
1148 } | 1141 } |
1149 | 1142 |
1150 void MetricsService::GetCurrentSyntheticFieldTrials( | 1143 void MetricsService::GetCurrentSyntheticFieldTrials( |
1151 std::vector<variations::ActiveGroupId>* synthetic_trials) { | 1144 std::vector<variations::ActiveGroupId>* synthetic_trials) { |
1152 DCHECK(synthetic_trials); | 1145 DCHECK(synthetic_trials); |
1153 synthetic_trials->clear(); | 1146 synthetic_trials->clear(); |
1154 const MetricsLog* current_log = | 1147 const MetricsLog* current_log = log_manager_.current_log(); |
1155 static_cast<const MetricsLog*>(log_manager_.current_log()); | |
1156 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) { | 1148 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) { |
1157 if (synthetic_trial_groups_[i].start_time <= current_log->creation_time()) | 1149 if (synthetic_trial_groups_[i].start_time <= current_log->creation_time()) |
1158 synthetic_trials->push_back(synthetic_trial_groups_[i].id); | 1150 synthetic_trials->push_back(synthetic_trial_groups_[i].id); |
1159 } | 1151 } |
1160 } | 1152 } |
1161 | 1153 |
1162 scoped_ptr<MetricsLog> MetricsService::CreateLog(MetricsLog::LogType log_type) { | 1154 scoped_ptr<MetricsLog> MetricsService::CreateLog(MetricsLog::LogType log_type) { |
1163 return make_scoped_ptr(new MetricsLog(state_manager_->client_id(), | 1155 return make_scoped_ptr(new MetricsLog(state_manager_->client_id(), |
1164 session_id_, | 1156 session_id_, |
1165 log_type, | 1157 log_type, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1205 RecordCurrentState(local_state_); | 1197 RecordCurrentState(local_state_); |
1206 } | 1198 } |
1207 | 1199 |
1208 void MetricsService::RecordCurrentState(PrefService* pref) { | 1200 void MetricsService::RecordCurrentState(PrefService* pref) { |
1209 pref->SetInt64(metrics::prefs::kStabilityLastTimestampSec, | 1201 pref->SetInt64(metrics::prefs::kStabilityLastTimestampSec, |
1210 Time::Now().ToTimeT()); | 1202 Time::Now().ToTimeT()); |
1211 | 1203 |
1212 for (size_t i = 0; i < metrics_providers_.size(); ++i) | 1204 for (size_t i = 0; i < metrics_providers_.size(); ++i) |
1213 metrics_providers_[i]->RecordCurrentState(); | 1205 metrics_providers_[i]->RecordCurrentState(); |
1214 } | 1206 } |
OLD | NEW |