OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/values.h" | |
18 #include "chrome/browser/ui/webui/profiler_ui.h" | |
19 #include "content/public/browser/profiler_subscriber.h" | 17 #include "content/public/browser/profiler_subscriber.h" |
20 | 18 |
21 // This class maintains state that is used to upload profiler data from the | 19 // This class maintains state that is used to upload profiler data from the |
22 // various processes, into the browser process. Such transactions are usually | 20 // various processes, into the browser process. Such transactions are usually |
23 // instigated by the browser. In general, a process will respond by gathering | 21 // instigated by the browser. In general, a process will respond by gathering |
24 // profiler data, and transmitting the pickled profiler data. We collect the | 22 // profiler data, and transmitting the pickled profiler data. We collect the |
25 // data in asynchronous mode that doesn't block the UI thread. | 23 // data in asynchronous mode that doesn't block the UI thread. |
26 // | 24 // |
27 // To assure that all the processes have responded, a counter is maintained | 25 // To assure that all the processes have responded, a counter is maintained |
28 // to indicate the number of pending (not yet responsive) processes. We tag | 26 // to indicate the number of pending (not yet responsive) processes. We tag |
29 // each group of requests with a sequence number. For each group of requests, we | 27 // each group of requests with a sequence number. For each group of requests, we |
30 // create RequestContext object which stores the sequence number, pending | 28 // create RequestContext object which stores the sequence number, pending |
31 // processes and the callback_object that needs to be notified when we receive | 29 // processes and the callback_object that needs to be notified when we receive |
32 // an update from processes. When an update arrives we find the RequestContext | 30 // an update from processes. When an update arrives we find the RequestContext |
33 // associated with sequence number and send the unpickled profiler data to the | 31 // associated with sequence number and send the unpickled profiler data to the |
34 // |callback_object_|. | 32 // |callback_object_|. |
35 | 33 |
| 34 namespace content { |
| 35 struct SerializedProfilerData; |
| 36 } |
| 37 |
36 namespace chrome_browser_metrics { | 38 namespace chrome_browser_metrics { |
37 | 39 |
38 class RequestContext; | 40 class TrackingSynchronizerObserver; |
39 | 41 |
40 class TrackingSynchronizer | 42 class TrackingSynchronizer |
41 : public content::ProfilerSubscriber, | 43 : public content::ProfilerSubscriber, |
42 public base::RefCountedThreadSafe<TrackingSynchronizer> { | 44 public base::RefCountedThreadSafe<TrackingSynchronizer> { |
43 public: | 45 public: |
44 // Construction also sets up the global singleton instance. This instance is | 46 // Construction also sets up the global singleton instance. This instance is |
45 // used to communicate between the IO and UI thread, and is destroyed only as | 47 // used to communicate between the IO and UI thread, and is destroyed only as |
46 // the main thread (browser_main) terminates, which means the IO thread has | 48 // the main thread (browser_main) terminates, which means the IO thread has |
47 // already completed, and will not need this instance any further. | 49 // already completed, and will not need this instance any further. |
48 TrackingSynchronizer(); | 50 TrackingSynchronizer(); |
49 | 51 |
50 // Contact all processes, and get them to upload to the browser any/all | 52 // Contact all processes, and get them to upload to the browser any/all |
51 // changes to profiler data. It calls |callback_object|'s SetData method with | 53 // changes to profiler data. It calls |callback_object|'s SetData method with |
52 // the data received from each sub-process. | 54 // the data received from each sub-process. |
53 // This method is accessible on UI thread. | 55 // This method is accessible on UI thread. |
54 static void FetchProfilerDataAsynchronously( | 56 static void FetchProfilerDataAsynchronously( |
55 const base::WeakPtr<ProfilerUI>& callback_object); | 57 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); |
56 | 58 |
57 // ------------------------------------------------------ | 59 // ------------------------------------------------------ |
58 // ProfilerSubscriber methods for browser child processes | 60 // ProfilerSubscriber methods for browser child processes |
59 // ------------------------------------------------------ | 61 // ------------------------------------------------------ |
60 | 62 |
61 // Update the number of pending processes for the given |sequence_number|. | 63 // Update the number of pending processes for the given |sequence_number|. |
62 // This is called on UI thread. | 64 // This is called on UI thread. |
63 virtual void OnPendingProcesses(int sequence_number, | 65 virtual void OnPendingProcesses(int sequence_number, |
64 int pending_processes, | 66 int pending_processes, |
65 bool end) OVERRIDE; | 67 bool end) OVERRIDE; |
66 | 68 |
| 69 private: |
| 70 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| 71 |
| 72 class RequestContext; |
| 73 |
| 74 virtual ~TrackingSynchronizer(); |
| 75 |
67 // Send profiler_data back to callback_object_ by calling | 76 // Send profiler_data back to callback_object_ by calling |
68 // DecrementPendingProcessesAndSendData which records that we are waiting | 77 // DecrementPendingProcessesAndSendData which records that we are waiting |
69 // for one less profiler data from renderer or browser child process for the | 78 // for one less profiler data from renderer or browser child process for the |
70 // given sequence number. This method is accessible on UI thread. | 79 // given sequence number. This method is accessible on UI thread. |
71 virtual void OnProfilerDataCollected( | 80 virtual void OnProfilerDataCollected( |
72 int sequence_number, | 81 int sequence_number, |
73 base::DictionaryValue* profiler_data) OVERRIDE; | 82 const content::SerializedProfilerData& profiler_data) OVERRIDE; |
74 | |
75 private: | |
76 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; | |
77 friend class RequestContext; | |
78 | |
79 virtual ~TrackingSynchronizer(); | |
80 | |
81 // Send profiler_data back to callback_object_. It records that we are waiting | |
82 // for one less profiler data from renderer or browser child process for the | |
83 // given sequence number. This method is accessible on UI thread. | |
84 void OnProfilerDataCollectedOnUI(int sequence_number, | |
85 base::DictionaryValue* profiler_data); | |
86 | 83 |
87 // Establish a new sequence_number_, and use it to notify all the processes of | 84 // Establish a new sequence_number_, and use it to notify all the processes of |
88 // the need to supply, to the browser, their tracking data. It also registers | 85 // the need to supply, to the browser, their tracking data. It also registers |
89 // |callback_object| in |outstanding_requests_| map. Return the | 86 // |callback_object| in |outstanding_requests_| map. Return the |
90 // sequence_number_ that was used. This method is accessible on UI thread. | 87 // sequence_number_ that was used. This method is accessible on UI thread. |
91 int RegisterAndNotifyAllProcesses( | 88 int RegisterAndNotifyAllProcesses( |
92 const base::WeakPtr<ProfilerUI>& callback_object); | 89 const base::WeakPtr<TrackingSynchronizerObserver>& callback_object); |
93 | 90 |
94 // It finds the RequestContext for the given |sequence_number| and notifies | 91 // It finds the RequestContext for the given |sequence_number| and notifies |
95 // the RequestContext's |callback_object_| about the |value|. This is called | 92 // the RequestContext's |callback_object_| about the |value|. This is called |
96 // whenever we receive profiler data from processes. It also records that we | 93 // whenever we receive profiler data from processes. It also records that we |
97 // are waiting for one less profiler data from a process for the given | 94 // are waiting for one less profiler data from a process for the given |
98 // sequence number. If we have received a response from all renderers and | 95 // sequence number. If we have received a response from all renderers and |
99 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete | 96 // browser processes, then it calls RequestContext's DeleteIfAllDone to delete |
100 // the entry for sequence_number. This method is accessible on UI thread. | 97 // the entry for sequence_number. This method is accessible on UI thread. |
101 void DecrementPendingProcessesAndSendData(int sequence_number, | 98 void DecrementPendingProcessesAndSendData( |
102 base::DictionaryValue* value); | 99 int sequence_number, |
| 100 const content::SerializedProfilerData& profiler_data); |
103 | 101 |
104 // Get a new sequence number to be sent to processes from browser process. | 102 // Get a new sequence number to be sent to processes from browser process. |
105 // This method is accessible on UI thread. | 103 // This method is accessible on UI thread. |
106 int GetNextAvailableSequenceNumber(); | 104 int GetNextAvailableSequenceNumber(); |
107 | 105 |
108 // Return pointer to the singleton instance, which is allocated and | |
109 // deallocated on the main UI thread (during system startup and teardown). | |
110 // This method is accessible on UI thread. | |
111 static TrackingSynchronizer* CurrentSynchronizer(); | |
112 | |
113 // We don't track the actual processes that are contacted for an update, only | 106 // We don't track the actual processes that are contacted for an update, only |
114 // the count of the number of processes, and we can sometimes time-out and | 107 // the count of the number of processes, and we can sometimes time-out and |
115 // give up on a "slow to respond" process. We use a sequence_number to be | 108 // give up on a "slow to respond" process. We use a sequence_number to be |
116 // sure a response from a process is associated with the current round of | 109 // sure a response from a process is associated with the current round of |
117 // requests. All sequence numbers used are non-negative. | 110 // requests. All sequence numbers used are non-negative. |
118 // last_used_sequence_number_ is the most recently used number (used to avoid | 111 // last_used_sequence_number_ is the most recently used number (used to avoid |
119 // reuse for a long time). | 112 // reuse for a long time). |
120 int last_used_sequence_number_; | 113 int last_used_sequence_number_; |
121 | 114 |
122 // This singleton instance should be started during the single threaded | |
123 // portion of main(). It initializes globals to provide support for all future | |
124 // calls. This object is created on the UI thread, and it is destroyed after | |
125 // all the other threads have gone away. As a result, it is ok to call it | |
126 // from the UI thread, or for about:profiler. | |
127 static TrackingSynchronizer* tracking_synchronizer_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); | 115 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
130 }; | 116 }; |
131 | 117 |
132 } // namespace chrome_browser_metrics | 118 } // namespace chrome_browser_metrics |
133 | 119 |
134 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 120 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
OLD | NEW |