Page Speed Optimization Libraries  1.7.30.1
net/instaweb/http/public/request_context.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
00020 #define NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
00021 
00022 #include <set>
00023 
00024 #include "pagespeed/kernel/base/basictypes.h"
00025 #include "pagespeed/kernel/base/ref_counted_ptr.h"
00026 #include "pagespeed/kernel/base/scoped_ptr.h"
00027 #include "pagespeed/kernel/base/string.h"
00028 #include "pagespeed/kernel/base/string_util.h"
00029 
00030 namespace net_instaweb {
00031 
00032 class AbstractLogRecord;
00033 class AbstractMutex;
00034 class RequestContext;
00035 class RequestTrace;
00036 class ThreadSystem;
00037 class Timer;
00038 
00039 typedef RefCountedPtr<RequestContext> RequestContextPtr;
00040 
00048 class RequestContext : public RefCounted<RequestContext> {
00049  public:
00051   enum SplitRequestType {
00052     SPLIT_FULL,
00053     SPLIT_ABOVE_THE_FOLD,
00054     SPLIT_BELOW_THE_FOLD,
00055   };
00056 
00062   explicit RequestContext(AbstractMutex* logging_mutex, Timer* timer);
00063 
00066   static RequestContextPtr NewTestRequestContext(ThreadSystem* thread_system) {
00067     return NewTestRequestContextWithTimer(thread_system, NULL);
00068   }
00069   static RequestContextPtr NewTestRequestContextWithTimer(
00070       ThreadSystem* thread_system, Timer* timer);
00071   static RequestContextPtr NewTestRequestContext(AbstractLogRecord* log_record);
00072 
00076   virtual AbstractLogRecord* NewSubordinateLogRecord(
00077       AbstractMutex* logging_mutex);
00078 
00083   RequestTrace* root_trace_context() { return root_trace_context_.get(); }
00085   void set_root_trace_context(RequestTrace* x);
00086 
00097   virtual RequestTrace* CreateDependentTraceContext(const StringPiece& label) {
00098     return NULL;
00099   }
00100 
00112   virtual void ReleaseDependentTraceContext(RequestTrace* t);
00113 
00115   virtual AbstractLogRecord* log_record();
00116 
00118   bool using_spdy() const { return using_spdy_; }
00119   void set_using_spdy(bool x) { using_spdy_ = x; }
00120 
00122   SplitRequestType split_request_type() const {
00123     return split_request_type_;
00124   }
00125   void set_split_request_type(SplitRequestType type) {
00126     split_request_type_ = type;
00127   }
00128 
00129   int64 request_id() const {
00130     return request_id_;
00131   }
00132   void set_request_id(int64 x) {
00133     request_id_ = x;
00134   }
00135 
00143   void AddSessionAuthorizedFetchOrigin(const GoogleString& origin) {
00144     session_authorized_fetch_origins_.insert(origin);
00145   }
00146 
00149   bool IsSessionAuthorizedFetchOrigin(const GoogleString& origin) const {
00150     return session_authorized_fetch_origins_.find(origin)
00151            != session_authorized_fetch_origins_.end();
00152   }
00153 
00157   void PrepareLogRecordForOutput();
00158 
00160   void WriteBackgroundRewriteLog();
00161 
00164   AbstractLogRecord* GetBackgroundRewriteLog(
00165       ThreadSystem* thread_system,
00166       bool log_urls,
00167       bool log_url_indices,
00168       int max_rewrite_info_log_size);
00169 
00184   class TimingInfo {
00185    public:
00189     TimingInfo(Timer* timer, AbstractMutex* mutex);
00190 
00194     void RequestStarted();
00195 
00198     void ProcessingStarted() { SetToNow(&processing_start_ts_ms_); }
00199 
00201     void ParsingStarted() { SetToNow(&parsing_start_ts_ms_); }
00202 
00204     void FirstByteReturned();
00205 
00207     void PropertyCacheLookupStarted() {
00208       SetToNow(&pcache_lookup_start_ts_ms_);
00209     }
00210 
00212     void PropertyCacheLookupFinished() { SetToNow(&pcache_lookup_end_ts_ms_); }
00213 
00216     void RequestFinished() { SetToNow(&end_ts_ms_); }
00217 
00223     void FetchStarted();
00224     void FetchHeaderReceived();
00225     void FetchFinished();
00226 
00229     void SetHTTPCacheLatencyMs(int64 latency_ms);
00230     void SetL2HTTPCacheLatencyMs(int64 latency_ms);
00231 
00233     int64 GetElapsedMs() const;
00234 
00236     bool GetTimeToStartProcessingMs(int64* elapsed_ms) const {
00237       return GetTimeFromStart(processing_start_ts_ms_, elapsed_ms);
00238     }
00239 
00244     bool GetProcessingElapsedMs(int64* elapsed_ms) const;
00245 
00247     bool GetTimeToPropertyCacheLookupStartMs(int64* elapsed_ms) const {
00248       return GetTimeFromStart(pcache_lookup_start_ts_ms_, elapsed_ms);
00249     }
00250 
00252     bool GetTimeToPropertyCacheLookupEndMs(int64* elapsed_ms) const {
00253       return GetTimeFromStart(pcache_lookup_end_ts_ms_, elapsed_ms);
00254     }
00255 
00257     bool GetHTTPCacheLatencyMs(int64* latency_ms) const;
00258     bool GetL2HTTPCacheLatencyMs(int64* latency_ms) const;
00259 
00261     bool GetTimeToStartFetchMs(int64* elapsed_ms) const;
00262 
00264     bool GetFetchHeaderLatencyMs(int64* latency_ms) const;
00265 
00267     bool GetFetchLatencyMs(int64* latency_ms) const;
00268 
00271     bool GetTimeToFirstByte(int64* latency_ms) const;
00272 
00274     bool GetTimeToStartParseMs(int64* elapsed_ms) const {
00275       return GetTimeFromStart(parsing_start_ts_ms_, elapsed_ms);
00276     }
00277 
00278     int64 init_ts_ms() const { return init_ts_ms_; }
00279 
00280     int64 start_ts_ms() const { return start_ts_ms_; }
00281 
00282    private:
00283     int64 NowMs() const;
00284 
00286     void SetToNow(int64* ts_ms) const;
00287 
00290     bool GetTimeFromStart(int64 ts_ms, int64* elapsed_ms) const;
00291 
00292 
00293     Timer* timer_;
00294 
00300     int64 init_ts_ms_;
00301     int64 start_ts_ms_;
00302     int64 processing_start_ts_ms_;
00303     int64 pcache_lookup_start_ts_ms_;
00304     int64 pcache_lookup_end_ts_ms_;
00305     int64 parsing_start_ts_ms_;
00306     int64 end_ts_ms_;
00307 
00308     AbstractMutex* mu_; 
00309 
00310     int64 fetch_start_ts_ms_;
00311     int64 fetch_header_ts_ms_;
00312     int64 fetch_end_ts_ms_;
00313     int64 first_byte_ts_ms_;
00314 
00316     int64 http_cache_latency_ms_;
00317     int64 l2http_cache_latency_ms_;
00318 
00319     DISALLOW_COPY_AND_ASSIGN(TimingInfo);
00320   };
00321 
00322   const TimingInfo& timing_info() const { return timing_info_; }
00323   TimingInfo* mutable_timing_info() { return &timing_info_; }
00324 
00325  protected:
00329   RequestContext(AbstractMutex* mutex, Timer* timer,
00330                  AbstractLogRecord* log_record);
00332   virtual ~RequestContext();
00333   REFCOUNT_FRIEND_DECLARATION(RequestContext);
00334 
00335  private:
00337   scoped_ptr<AbstractLogRecord> log_record_;
00338 
00339   TimingInfo timing_info_;
00340 
00342   scoped_ptr<RequestTrace> root_trace_context_;
00343 
00345   scoped_ptr<AbstractLogRecord> background_rewrite_log_record_;
00346 
00347   StringSet session_authorized_fetch_origins_;
00348 
00349   bool using_spdy_;
00350   SplitRequestType split_request_type_;;
00351   int64 request_id_;
00352 
00353   DISALLOW_COPY_AND_ASSIGN(RequestContext);
00354 };
00355 
00356 }  
00357 
00358 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines