| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 | |
| 5 #ifndef CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ | |
| 6 #define CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/synchronization/condition_variable.h" | |
| 15 #include "base/synchronization/lock.h" | |
| 16 #include "base/time.h" | |
| 17 | |
| 18 class MessageLoop; | |
| 19 | |
| 20 // This class maintains state that is used to upload histogram data from the | |
| 21 // various renderer processes, into the browser process. Such transactions are | |
| 22 // usually instigated by the browser. In general, a renderer process will | |
| 23 // respond by gathering snapshots of all internal histograms, calculating what | |
| 24 // has changed since its last upload, and transmitting a pickled collection of | |
| 25 // deltas. | |
| 26 // | |
| 27 // There are actually two modes of update request. One is synchronous (and | |
| 28 // blocks the UI thread, waiting to populate an about:histograms tab) and the | |
| 29 // other is asynchronous, and used by the metrics services in preparation for a | |
| 30 // log upload. | |
| 31 // | |
| 32 // To assure that all the renderers have responded, a counter is maintained (for | |
| 33 // each mode) to indicate the number of pending (not yet responsive) renderers. | |
| 34 // To avoid confusion about a response (i.e., is the renderer responding to a | |
| 35 // current request for an update, or to an old request for an update) we tag | |
| 36 // each group of requests with a sequence number. When an update arrives we can | |
| 37 // ignore it (relative to the counter) if it does not relate to a current | |
| 38 // outstanding sequence number. | |
| 39 // | |
| 40 // There is one final mode of use, where a renderer spontaneously decides to | |
| 41 // transmit a collection of histogram data. This is designed for use when the | |
| 42 // renderer is terminating. Unfortunately, renders may be terminated without | |
| 43 // warning, and the best we can do is periodically acquire data from a tab, such | |
| 44 // as when a page load has completed. In this mode, the renderer uses a | |
| 45 // reserved sequence number, different from any sequence number that might be | |
| 46 // specified by a browser request. Since this sequence number can't match an | |
| 47 // outstanding sequence number, the pickled data is accepted into the browser, | |
| 48 // but there is no impact on the counters. | |
| 49 | |
| 50 class HistogramSynchronizer : public | |
| 51 base::RefCountedThreadSafe<HistogramSynchronizer> { | |
| 52 public: | |
| 53 | |
| 54 enum RendererHistogramRequester { | |
| 55 ASYNC_HISTOGRAMS, | |
| 56 SYNCHRONOUS_HISTOGRAMS | |
| 57 }; | |
| 58 | |
| 59 // Construction also sets up the global singleton instance. This instance is | |
| 60 // used to communicate between the IO and UI thread, and is destroyed only | |
| 61 // as the main thread (browser_main) terminates, which means the IO thread has | |
| 62 // already completed, and will not need this instance any further. | |
| 63 HistogramSynchronizer(); | |
| 64 | |
| 65 // Return pointer to the singleton instance, which is allocated and | |
| 66 // deallocated on the main UI thread (during system startup and teardown). | |
| 67 static HistogramSynchronizer* CurrentSynchronizer(); | |
| 68 | |
| 69 // Contact all renderers, and get them to upload to the browser any/all | |
| 70 // changes to histograms. Return when all changes have been acquired, or when | |
| 71 // the wait time expires (whichever is sooner). This method is called on the | |
| 72 // main UI thread from about:histograms. | |
| 73 void FetchRendererHistogramsSynchronously(base::TimeDelta wait_time); | |
| 74 | |
| 75 // Contact all renderers, and get them to upload to the browser any/all | |
| 76 // changes to histograms. When all changes have been acquired, or when the | |
| 77 // wait time expires (whichever is sooner), post the callback to the | |
| 78 // specified message loop. Note the callback is posted exactly once. | |
| 79 static void FetchRendererHistogramsAsynchronously( | |
| 80 MessageLoop* callback_thread, | |
| 81 const base::Closure& callback, | |
| 82 base::TimeDelta wait_time); | |
| 83 | |
| 84 // This method is called on the IO thread. Deserializes the histograms and | |
| 85 // records that we have received histograms from a renderer process. | |
| 86 static void DeserializeHistogramList( | |
| 87 int sequence_number, const std::vector<std::string>& histograms); | |
| 88 | |
| 89 private: | |
| 90 friend class base::RefCountedThreadSafe<HistogramSynchronizer>; | |
| 91 | |
| 92 ~HistogramSynchronizer(); | |
| 93 | |
| 94 // Establish a new sequence_number_, and use it to notify all the renderers of | |
| 95 // the need to supply, to the browser, any changes in their histograms. | |
| 96 // The argument indicates whether this will set async_sequence_number_ or | |
| 97 // synchronous_sequence_number_. | |
| 98 // Return the sequence number that was used. | |
| 99 int NotifyAllRenderers(RendererHistogramRequester requester); | |
| 100 | |
| 101 // Records that we are waiting for one less histogram from a renderer for the | |
| 102 // given sequence number. If we have received a response from all renderers, | |
| 103 // either signal the waiting process or call the callback function. | |
| 104 void DecrementPendingRenderers(int sequence_number); | |
| 105 | |
| 106 // Set the callback_thread_ and callback_ members. If these members already | |
| 107 // had values, then as a side effect, post the old callback_ to the old | |
| 108 // callaback_thread_. This side effect should not generally happen, but is in | |
| 109 // place to assure correctness (that any tasks that were set, are eventually | |
| 110 // called, and never merely discarded). | |
| 111 void SetCallbackTaskAndThread(MessageLoop* callback_thread, | |
| 112 const base::Closure& callback); | |
| 113 | |
| 114 void ForceHistogramSynchronizationDoneCallback(int sequence_number); | |
| 115 | |
| 116 // Gets a new sequence number to be sent to renderers from browser process and | |
| 117 // set the number of pending responses for the given type to renderer_count. | |
| 118 int GetNextAvailableSequenceNumber(RendererHistogramRequester requster, | |
| 119 int renderer_count); | |
| 120 | |
| 121 // Internal helper function, to post task, and record callback stats. | |
| 122 void InternalPostTask(MessageLoop* thread, | |
| 123 const base::Closure& callback, | |
| 124 int unresponsive_renderers, | |
| 125 const base::TimeTicks& started); | |
| 126 | |
| 127 // This lock_ protects access to all members. | |
| 128 base::Lock lock_; | |
| 129 | |
| 130 // This condition variable is used to block caller of the synchronous request | |
| 131 // to update histograms, and to signal that thread when updates are completed. | |
| 132 base::ConditionVariable received_all_renderer_histograms_; | |
| 133 | |
| 134 // When a request is made to asynchronously update the histograms, we store | |
| 135 // the task and thread we use to post a completion notification in | |
| 136 // callback_ and callback_thread_. | |
| 137 base::Closure callback_; | |
| 138 MessageLoop* callback_thread_; | |
| 139 | |
| 140 // We don't track the actual renderers that are contacted for an update, only | |
| 141 // the count of the number of renderers, and we can sometimes time-out and | |
| 142 // give up on a "slow to respond" renderer. We use a sequence_number to be | |
| 143 // sure a response from a renderer is associated with the current round of | |
| 144 // requests (and not merely a VERY belated prior response). | |
| 145 // All sequence numbers used are non-negative. | |
| 146 // last_used_sequence_number_ is the most recently used number (used to avoid | |
| 147 // reuse for a long time). | |
| 148 int last_used_sequence_number_; | |
| 149 | |
| 150 // The sequence number used by the most recent asynchronous update request to | |
| 151 // contact all renderers. | |
| 152 int async_sequence_number_; | |
| 153 | |
| 154 // The number of renderers that have not yet responded to requests (as part of | |
| 155 // an asynchronous update). | |
| 156 int async_renderers_pending_; | |
| 157 | |
| 158 // The time when we were told to start the fetch histograms asynchronously | |
| 159 // from renderers. | |
| 160 base::TimeTicks async_callback_start_time_; | |
| 161 | |
| 162 // The sequence number used by the most recent synchronous update request to | |
| 163 // contact all renderers. | |
| 164 int synchronous_sequence_number_; | |
| 165 | |
| 166 // The number of renderers that have not yet responded to requests (as part of | |
| 167 // a synchronous update). | |
| 168 int synchronous_renderers_pending_; | |
| 169 | |
| 170 // This singleton instance should be started during the single threaded | |
| 171 // portion of main(). It initializes globals to provide support for all future | |
| 172 // calls. This object is created on the UI thread, and it is destroyed after | |
| 173 // all the other threads have gone away. As a result, it is ok to call it | |
| 174 // from the UI thread (for UMA uploads), or for about:histograms. | |
| 175 static HistogramSynchronizer* histogram_synchronizer_; | |
| 176 | |
| 177 DISALLOW_COPY_AND_ASSIGN(HistogramSynchronizer); | |
| 178 }; | |
| 179 | |
| 180 #endif // CHROME_BROWSER_METRICS_HISTOGRAM_SYNCHRONIZER_H_ | |
| OLD | NEW |