OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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_COMMON_HISTOGRAM_SYNCHRONIZER_H_ |
| 6 #define CHROME_COMMON_HISTOGRAM_SYNCHRONIZER_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <map> |
| 10 #include <set> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/basictypes.h" |
| 15 #include "base/condition_variable.h" |
| 16 #include "base/lock.h" |
| 17 #include "base/message_loop.h" |
| 18 #include "base/process.h" |
| 19 #include "base/ref_counted.h" |
| 20 #include "base/scoped_ptr.h" |
| 21 #include "base/task.h" |
| 22 #include "base/time.h" |
| 23 |
| 24 class MessageLoop; |
| 25 |
| 26 class HistogramSynchronizer : public |
| 27 base::RefCountedThreadSafe<HistogramSynchronizer> { |
| 28 public: |
| 29 |
| 30 enum RendererHistogramRequester { |
| 31 ASYNC_HISTOGRAMS, |
| 32 SYNCHRONOUS_HISTOGRAMS |
| 33 }; |
| 34 |
| 35 HistogramSynchronizer(); |
| 36 |
| 37 ~HistogramSynchronizer(); |
| 38 |
| 39 // Return pointer to the singleton instance, which is allocated and |
| 40 // deallocated on the main UI thread (during system startup and teardown). |
| 41 static HistogramSynchronizer* CurrentSynchronizer(); |
| 42 |
| 43 // Contact all renderers, and get them to upload to the browser any/all |
| 44 // changes to histograms. Return when all changes have been acquired, or when |
| 45 // the wait time expires (whichever is sooner). This method is called on the |
| 46 // main UI thread from about:histograms. |
| 47 void FetchRendererHistogramsSynchronously(base::TimeDelta wait_time); |
| 48 |
| 49 // Contact all renderers, and get them to upload to the browser any/all |
| 50 // changes to histograms. When all changes have been acquired, or when the |
| 51 // wait time expires (whichever is sooner), post the callback_task to the UI |
| 52 // thread. Note the callback_task is posted exactly once. This method is |
| 53 // called on the IO thread from UMA via PostMessage. |
| 54 static void FetchRendererHistogramsAsynchronously( |
| 55 MessageLoop* callback_thread, Task* callback_task, int wait_time); |
| 56 |
| 57 // This method is called on the IO thread. Desrializes the histograms and |
| 58 // records that we have received histograms from a renderer process. |
| 59 static void DeserializeHistogramList( |
| 60 int sequence_number, const std::vector<std::string>& histograms); |
| 61 |
| 62 private: |
| 63 // Records that we have received the histograms from a renderer for the given |
| 64 // sequence number. If we have received a response from all histograms, either |
| 65 // signal the waiting process or call the callback function. Returns true when |
| 66 // we receive histograms from the last of N renderers that were contacted for |
| 67 // an update. This is called on IO Thread. |
| 68 bool RecordRendererHistogram(int sequence_number); |
| 69 |
| 70 void SetCallbackTaskToCallAfterGettingHistograms( |
| 71 MessageLoop* callback_thread, Task* callback_task); |
| 72 |
| 73 void ForceHistogramSynchronizationDoneCallback(int sequence_number); |
| 74 |
| 75 // Calls the callback task, if there is a callback_task. |
| 76 void CallCallbackTaskAndResetData(); |
| 77 |
| 78 // Method to get a new sequence number to be sent to renderers from broswer |
| 79 // process. |
| 80 int GetNextAvaibleSequenceNumber(RendererHistogramRequester requster, |
| 81 size_t renderer_histograms_requested); |
| 82 |
| 83 // For use ONLY in a DCHECK. This method initializes io_message_loop_ in its |
| 84 // first call and then compares io_message_loop_ with MessageLoop::current() |
| 85 // in subsequent calls. This method guarantees we're consistently on the |
| 86 // singular IO thread and we don't need to worry about locks. |
| 87 bool IsOnIoThread(); |
| 88 |
| 89 // This lock_ protects access to sequence number and |
| 90 // synchronous_renderers_pending_. |
| 91 Lock lock_; |
| 92 |
| 93 // This condition variable is used to block caller of the synchronous request |
| 94 // to update histograms, and to signal that thread when updates are completed. |
| 95 ConditionVariable received_all_renderer_historgrams_; |
| 96 |
| 97 // When a request is made to asynchronously update the histograms, we store |
| 98 // the task and thread we use to post a completion notification in |
| 99 // callback_task_ and callback_thread_. |
| 100 Task* callback_task_; |
| 101 MessageLoop* callback_thread_; |
| 102 |
| 103 // For use ONLY in a DCHECK and is used in IsOnIoThread(). io_message_loop_ is |
| 104 // initialized during the first call to IsOnIoThread(), and then compares |
| 105 // MessageLoop::current() against io_message_loop_ in subsequent calls for |
| 106 // consistency. |
| 107 MessageLoop* io_message_loop_; |
| 108 |
| 109 // We don't track the actual renderers that are contacted for an update, only |
| 110 // the count of the number of renderers, and we can sometimes time-out and |
| 111 // give up on a "slow to respond" renderer. We use a sequence_number to be |
| 112 // sure a response from a renderer is associated with the current round of |
| 113 // requests (and not merely a VERY belated prior response). |
| 114 // next_available_sequence_number_ is the next available number (used to |
| 115 // avoid reuse for a long time). |
| 116 int next_available_sequence_number_; |
| 117 |
| 118 // The sequence number used by the most recent asynchronous update request to |
| 119 // contact all renderers. |
| 120 int async_sequence_number_; |
| 121 |
| 122 // The number of renderers that have not yet responded to requests (as part of |
| 123 // an asynchronous update). |
| 124 int async_renderers_pending_; |
| 125 |
| 126 // The time when we were told to start the fetch histograms asynchronously |
| 127 // from renderers. |
| 128 base::TimeTicks async_callback_start_time_; |
| 129 |
| 130 // The sequence number used by the most recent synchronous update request to |
| 131 // contact all renderers. |
| 132 int synchronous_sequence_number_; |
| 133 |
| 134 // The number of renderers that have not yet responded to requests (as part of |
| 135 // a synchronous update). |
| 136 int synchronous_renderers_pending_; |
| 137 |
| 138 // This singleton instance should be started during the single threaded |
| 139 // portion of main(). It initializes globals to provide support for all future |
| 140 // calls. This object is created on the UI thread, and it is destroyed after |
| 141 // all the other threads have gone away. As a result, it is ok to call it |
| 142 // from the UI thread (for UMA uploads), or for about:histograms. |
| 143 static HistogramSynchronizer* histogram_synchronizer_; |
| 144 |
| 145 DISALLOW_EVIL_CONSTRUCTORS(HistogramSynchronizer); |
| 146 }; |
| 147 |
| 148 #endif // CHROME_COMMON_HISTOGRAM_SYNCHRONIZER_H_ |
OLD | NEW |