Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
request_context.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
20 #define NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
21 
22 #include <set>
23 
24 #include "base/logging.h"
25 #include "pagespeed/kernel/base/basictypes.h"
26 #include "pagespeed/kernel/base/ref_counted_ptr.h"
27 #include "pagespeed/kernel/base/scoped_ptr.h"
28 #include "pagespeed/kernel/base/string.h"
29 #include "pagespeed/kernel/base/string_util.h"
30 #include "pagespeed/kernel/http/http_options.h"
31 
32 namespace net_instaweb {
33 
34 class AbstractLogRecord;
35 class AbstractMutex;
36 class RequestContext;
37 class RequestTrace;
38 class ThreadSystem;
39 class Timer;
40 
41 typedef RefCountedPtr<RequestContext> RequestContextPtr;
42 
50 class RequestContext : public RefCounted<RequestContext> {
51  public:
54  SPLIT_FULL,
55  SPLIT_ABOVE_THE_FOLD,
56  SPLIT_BELOW_THE_FOLD,
57  };
58 
64  RequestContext(const HttpOptions& options, AbstractMutex* logging_mutex,
65  Timer* timer);
67  RequestContext(AbstractMutex* logging_mutex, Timer* timer);
68 
72  static RequestContextPtr NewTestRequestContext(ThreadSystem* thread_system) {
73  return NewTestRequestContextWithTimer(thread_system, NULL);
74  }
75  static RequestContextPtr NewTestRequestContextWithTimer(
76  ThreadSystem* thread_system, Timer* timer);
77  static RequestContextPtr NewTestRequestContext(AbstractLogRecord* log_record);
78 
83  AbstractMutex* logging_mutex);
84 
89  RequestTrace* root_trace_context() { return root_trace_context_.get(); }
91  void set_root_trace_context(RequestTrace* x);
92 
103  virtual RequestTrace* CreateDependentTraceContext(const StringPiece& label) {
104  return NULL;
105  }
106 
118  virtual void ReleaseDependentTraceContext(RequestTrace* t);
119 
121  virtual AbstractLogRecord* log_record();
122 
124  bool using_spdy() const { return using_spdy_; }
125  void set_using_spdy(bool x) { using_spdy_ = x; }
126 
139  const GoogleString& minimal_private_suffix() const {
140  return minimal_private_suffix_;
141  }
142  void set_minimal_private_suffix(StringPiece minimal_private_suffix) {
143  minimal_private_suffix.CopyToString(&minimal_private_suffix_);
144  }
145 
148  void set_accepts_webp(bool x) { accepts_webp_ = x; }
149  bool accepts_webp() const { return accepts_webp_; }
150 
153  return split_request_type_;
154  }
155  void set_split_request_type(SplitRequestType type) {
156  split_request_type_ = type;
157  }
158 
159  int64 request_id() const {
160  return request_id_;
161  }
162  void set_request_id(int64 x) {
163  request_id_ = x;
164  }
165 
166  const GoogleString& sticky_query_parameters_token() const {
167  return sticky_query_parameters_token_;
168  }
169  void set_sticky_query_parameters_token(StringPiece x) {
170  x.CopyToString(&sticky_query_parameters_token_);
171  }
172 
180  void AddSessionAuthorizedFetchOrigin(const GoogleString& origin) {
181  session_authorized_fetch_origins_.insert(origin);
182  }
183 
186  bool IsSessionAuthorizedFetchOrigin(const GoogleString& origin) const {
187  return session_authorized_fetch_origins_.find(origin)
188  != session_authorized_fetch_origins_.end();
189  }
190 
195 
198 
202  ThreadSystem* thread_system,
203  bool log_urls,
204  bool log_url_indices,
205  int max_rewrite_info_log_size);
206 
221  class TimingInfo {
222  public:
226  TimingInfo(Timer* timer, AbstractMutex* mutex);
227 
231  void RequestStarted();
232 
235  void ProcessingStarted() { SetToNow(&processing_start_ts_ms_); }
236 
238  void ParsingStarted() { SetToNow(&parsing_start_ts_ms_); }
239 
241  void FirstByteReturned();
242 
245  SetToNow(&pcache_lookup_start_ts_ms_);
246  }
247 
249  void PropertyCacheLookupFinished() { SetToNow(&pcache_lookup_end_ts_ms_); }
250 
253  void RequestFinished() { SetToNow(&end_ts_ms_); }
254 
260  void FetchStarted();
261  void FetchHeaderReceived();
262  void FetchFinished();
263 
266  void SetHTTPCacheLatencyMs(int64 latency_ms);
267  void SetL2HTTPCacheLatencyMs(int64 latency_ms);
268 
270  int64 GetElapsedMs() const;
271 
273  bool GetTimeToStartProcessingMs(int64* elapsed_ms) const {
274  return GetTimeFromStart(processing_start_ts_ms_, elapsed_ms);
275  }
276 
281  bool GetProcessingElapsedMs(int64* elapsed_ms) const;
282 
284  bool GetTimeToPropertyCacheLookupStartMs(int64* elapsed_ms) const {
285  return GetTimeFromStart(pcache_lookup_start_ts_ms_, elapsed_ms);
286  }
287 
289  bool GetTimeToPropertyCacheLookupEndMs(int64* elapsed_ms) const {
290  return GetTimeFromStart(pcache_lookup_end_ts_ms_, elapsed_ms);
291  }
292 
294  bool GetHTTPCacheLatencyMs(int64* latency_ms) const;
295  bool GetL2HTTPCacheLatencyMs(int64* latency_ms) const;
296 
298  bool GetTimeToStartFetchMs(int64* elapsed_ms) const;
299 
301  bool GetFetchHeaderLatencyMs(int64* latency_ms) const;
302 
304  bool GetFetchLatencyMs(int64* latency_ms) const;
305 
308  bool GetTimeToFirstByte(int64* latency_ms) const;
309 
311  bool GetTimeToStartParseMs(int64* elapsed_ms) const {
312  return GetTimeFromStart(parsing_start_ts_ms_, elapsed_ms);
313  }
314 
315  int64 init_ts_ms() const { return init_ts_ms_; }
316 
317  int64 start_ts_ms() const { return start_ts_ms_; }
318 
319  private:
320  int64 NowMs() const;
321 
323  void SetToNow(int64* ts_ms) const;
324 
327  bool GetTimeFromStart(int64 ts_ms, int64* elapsed_ms) const;
328 
329 
330  Timer* timer_;
331 
337  int64 init_ts_ms_;
338  int64 start_ts_ms_;
339  int64 processing_start_ts_ms_;
340  int64 pcache_lookup_start_ts_ms_;
341  int64 pcache_lookup_end_ts_ms_;
342  int64 parsing_start_ts_ms_;
343  int64 end_ts_ms_;
344 
345  AbstractMutex* mu_;
346  int64 fetch_start_ts_ms_;
348  int64 fetch_header_ts_ms_;
349  int64 fetch_end_ts_ms_;
350  int64 first_byte_ts_ms_;
351 
353  int64 http_cache_latency_ms_;
354  int64 l2http_cache_latency_ms_;
355 
356  DISALLOW_COPY_AND_ASSIGN(TimingInfo);
357  };
358 
359  const TimingInfo& timing_info() const { return timing_info_; }
360  TimingInfo* mutable_timing_info() { return &timing_info_; }
361 
362  void set_options(const HttpOptions& options) {
363  DCHECK(!options_set_);
364  options_set_ = true;
365  options_ = options;
366  }
370  void ResetOptions(const HttpOptions& options) {
371  options_set_ = true;
372  options_ = options;
373  }
374  const HttpOptions& options() const {
375  DCHECK(options_set_);
376  return options_;
377  }
378 
379  protected:
383  RequestContext(const HttpOptions& options, AbstractMutex* mutex,
384  Timer* timer, AbstractLogRecord* log_record);
386  virtual ~RequestContext();
387  REFCOUNT_FRIEND_DECLARATION(RequestContext);
388 
389  private:
391  scoped_ptr<AbstractLogRecord> log_record_;
392 
393  TimingInfo timing_info_;
394 
396  scoped_ptr<RequestTrace> root_trace_context_;
397 
399  scoped_ptr<AbstractLogRecord> background_rewrite_log_record_;
400 
401  StringSet session_authorized_fetch_origins_;
402 
403  bool using_spdy_;
404  bool accepts_webp_;
405  GoogleString minimal_private_suffix_;
406 
407  SplitRequestType split_request_type_;
408  int64 request_id_;
409 
412  GoogleString sticky_query_parameters_token_;
413 
414  bool options_set_;
415  HttpOptions options_;
416 
417  DISALLOW_COPY_AND_ASSIGN(RequestContext);
418 };
419 
420 }
421 
422 #endif
Definition: request_context.h:221
SplitRequestType
Types of split html request.
Definition: request_context.h:53
void RequestFinished()
Definition: request_context.h:253
const GoogleString & minimal_private_suffix() const
Definition: request_context.h:139
Definition: request_context.h:50
void SetHTTPCacheLatencyMs(int64 latency_ms)
bool GetTimeToFirstByte(int64 *latency_ms) const
bool GetHTTPCacheLatencyMs(int64 *latency_ms) const
HTTP Cache latencies.
void set_accepts_webp(bool x)
Definition: request_context.h:148
virtual ~RequestContext()
Destructors in refcounted classes should be protected.
Definition: log_record.h:61
int64 GetElapsedMs() const
Milliseconds since Init.
bool using_spdy() const
Determines whether this request is using the SPDY protocol.
Definition: request_context.h:124
bool GetTimeToStartProcessingMs(int64 *elapsed_ms) const
Milliseconds from request start to processing start.
Definition: request_context.h:273
void ProcessingStarted()
Definition: request_context.h:235
TimingInfo(Timer *timer, AbstractMutex *mutex)
bool GetProcessingElapsedMs(int64 *elapsed_ms) const
bool GetTimeToStartParseMs(int64 *elapsed_ms) const
Milliseconds from request start to parse start.
Definition: request_context.h:311
virtual RequestTrace * CreateDependentTraceContext(const StringPiece &label)
Definition: request_context.h:103
void ParsingStarted()
This should be called if/when HTML parsing begins.
Definition: request_context.h:238
virtual AbstractLogRecord * log_record()
The log record for the this request, created when the request context is.
virtual void ReleaseDependentTraceContext(RequestTrace *t)
void PropertyCacheLookupStarted()
This should be called when a PropertyCache lookup is initiated.
Definition: request_context.h:244
static RequestContextPtr NewTestRequestContext(ThreadSystem *thread_system)
Definition: request_context.h:72
RequestContext(const HttpOptions &options, AbstractMutex *logging_mutex, Timer *timer)
void set_root_trace_context(RequestTrace *x)
Takes ownership of the given context.
bool GetTimeToStartFetchMs(int64 *elapsed_ms) const
Milliseconds from request start to fetch start.
RequestTrace * root_trace_context()
Definition: request_context.h:89
void WriteBackgroundRewriteLog()
Write the log for background rewriting into disk.
void PropertyCacheLookupFinished()
This should be called when a PropertyCache lookup completes.
Definition: request_context.h:249
bool GetFetchHeaderLatencyMs(int64 *latency_ms) const
Milliseconds from fetch start to header received.
bool GetFetchLatencyMs(int64 *latency_ms) const
Milliseconds from fetch start to fetch end.
bool GetTimeToPropertyCacheLookupEndMs(int64 *elapsed_ms) const
Milliseconds from request start to pcache lookup end.
Definition: request_context.h:289
virtual AbstractLogRecord * NewSubordinateLogRecord(AbstractMutex *logging_mutex)
bool GetTimeToPropertyCacheLookupStartMs(int64 *elapsed_ms) const
Milliseconds from request start to pcache lookup start.
Definition: request_context.h:284
bool IsSessionAuthorizedFetchOrigin(const GoogleString &origin) const
Definition: request_context.h:186
SplitRequestType split_request_type() const
Indicates the type of split html request.
Definition: request_context.h:152
void ResetOptions(const HttpOptions &options)
Definition: request_context.h:370
AbstractLogRecord * GetBackgroundRewriteLog(ThreadSystem *thread_system, bool log_urls, bool log_url_indices, int max_rewrite_info_log_size)
void FirstByteReturned()
Called when the first byte is sent back to the user.