Coverage for /Syzygy/trace/client/rpc_session.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
85.3%871020.C++source

Line-by-line coverage:

   1    :  // Copyright 2012 Google Inc.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  //
  15    :  // A utility class to manage the RPC session and the associated memory mappings.
  16    :  #include "syzygy/trace/client/rpc_session.h"
  17    :  
  18    :  #include "sawbuck/common/com_utils.h"
  19    :  #include "syzygy/trace/client/client_utils.h"
  20    :  #include "syzygy/trace/protocol/call_trace_defs.h"
  21    :  #include "syzygy/trace/rpc/call_trace_rpc.h"
  22    :  #include "syzygy/trace/rpc/rpc_helpers.h"
  23    :  
  24    :  namespace trace {
  25    :  namespace client {
  26    :  
  27    :  RpcSession::RpcSession()
  28  E :      : rpc_binding_(NULL), session_handle_(NULL), is_disabled_(false) {
  29  E :  }
  30    :  
  31  E :  RpcSession::~RpcSession() {
  32  E :    FreeSharedMemory();
  33  E :  }
  34    :  
  35  E :  bool RpcSession::MapSegmentBuffer(TraceFileSegment* segment) {
  36  E :    DCHECK(segment != NULL);
  37    :  
  38    :    HANDLE mem_handle =
  39  E :        reinterpret_cast<HANDLE>(segment->buffer_info.shared_memory_handle);
  40    :  
  41    :    // Get (or set) the mapping between the handle we've received and the
  42    :    // corresponding mapped base pointer. Note that the shared_memory_handles_
  43    :    // map is shared across threads, so we need to hold the shared_memory_lock_
  44    :    // when we access/update it.  This should be the only synchronization point
  45    :    // in the call trace client library (other than the initial creation of the
  46    :    // RpcSession object, of course).
  47    :    {
  48  E :      base::AutoLock scoped_lock(shared_memory_lock_);
  49    :  
  50  E :      uint8*& base_ptr = shared_memory_handles_[mem_handle];
  51  E :      if (base_ptr == NULL) {
  52    :        base_ptr = reinterpret_cast<uint8*>(
  53    :            ::MapViewOfFile(mem_handle, FILE_MAP_WRITE, 0, 0,
  54  E :                            segment->buffer_info.mapping_size));
  55  E :        if (base_ptr == NULL) {
  56  i :          DWORD error = ::GetLastError();
  57  i :          LOG(ERROR) << "Failed to map view of shared memory: "
  58    :                     << com::LogWe(error) << ".";
  59  i :          ignore_result(::CloseHandle(mem_handle));
  60  i :          shared_memory_handles_.erase(mem_handle);
  61  i :          return false;
  62    :        }
  63    :      }
  64    :  
  65    :      segment->base_ptr =
  66  E :          base_ptr + segment->buffer_info.buffer_offset;
  67  E :    }
  68    :  
  69  E :    segment->header = NULL;
  70  E :    segment->write_ptr = segment->base_ptr;
  71    :    segment->end_ptr =
  72  E :        segment->base_ptr + segment->buffer_info.buffer_size;
  73  E :    segment->WriteSegmentHeader(session_handle_);
  74    :  
  75  E :    DCHECK(segment->header != NULL);
  76    :  
  77  E :    return true;
  78  E :  }
  79    :  
  80  E :  bool RpcSession::CreateSession(TraceFileSegment* segment) {
  81  E :    DCHECK(session_handle_ == NULL);
  82  E :    DCHECK(rpc_binding_ == NULL);
  83    :  
  84  E :    std::wstring protocol;
  85  E :    std::wstring endpoint;
  86  E :    ::GetSyzygyCallTraceRpcProtocol(&protocol);
  87  E :    ::GetSyzygyCallTraceRpcEndpoint(instance_id_, &endpoint);
  88    :  
  89  E :    if (!CreateRpcBinding(protocol, endpoint, &rpc_binding_)) {
  90  i :      is_disabled_ = true;
  91  i :      return false;
  92    :    }
  93    :  
  94  E :    DCHECK(rpc_binding_ != 0);
  95    :  
  96    :    bool succeeded = InvokeRpc(CallTraceClient_CreateSession,
  97    :                               rpc_binding_,
  98    :                               &session_handle_,
  99    :                               &segment->buffer_info,
 100  E :                               &flags_).succeeded();
 101    :  
 102  E :    if (!succeeded) {
 103  E :      LOG(ERROR) << "Failed to create call trace session!";
 104  E :      is_disabled_ = true;
 105  E :      return false;
 106    :    }
 107    :  
 108  E :    if ((flags_ & TRACE_FLAG_BATCH_ENTER) != 0) {
 109    :      // Batch mode is mutually exclusive of all other flags.
 110  E :      flags_ = TRACE_FLAG_BATCH_ENTER;
 111    :    }
 112    :  
 113  E :    if (!MapSegmentBuffer(segment)) {
 114  i :      is_disabled_ = true;
 115  i :      return false;
 116    :    }
 117    :  
 118  E :    return true;
 119  E :  }
 120    :  
 121  E :  bool RpcSession::AllocateBuffer(TraceFileSegment* segment) {
 122  E :    DCHECK(IsTracing());
 123  E :    DCHECK(segment != NULL);
 124    :  
 125    :    bool succeeded = InvokeRpc(CallTraceClient_AllocateBuffer,
 126    :                               session_handle_,
 127  E :                               &segment->buffer_info).succeeded();
 128    :  
 129  E :    return succeeded ? MapSegmentBuffer(segment) : false;
 130  E :  }
 131    :  
 132  E :  bool RpcSession::AllocateBuffer(size_t min_size, TraceFileSegment* segment) {
 133  E :    DCHECK(IsTracing());
 134  E :    DCHECK(segment != NULL);
 135    :  
 136    :    // We want the actual buffer to have the provided minimum size. The call is
 137    :    // going to prepend the buffer with a RecordPrefix and a
 138    :    // TraceFileSegmentHeader, so we make room for those.
 139    :    const size_t kHeaderSize = sizeof(RecordPrefix) +
 140  E :        sizeof(TraceFileSegmentHeader);
 141    :  
 142    :    bool succeeded = InvokeRpc(CallTraceClient_AllocateLargeBuffer,
 143    :                               session_handle_,
 144    :                               min_size + kHeaderSize,
 145  E :                               &segment->buffer_info).succeeded();
 146  E :    if (!succeeded)
 147  i :      return false;
 148    :  
 149  E :    if (!MapSegmentBuffer(segment))
 150  i :      return false;
 151    :  
 152    :    // We want to make sure the mapped buffer has sufficient size.
 153  E :    DCHECK(segment->CanAllocateRaw(min_size));
 154    :  
 155  E :    return true;
 156  E :  }
 157    :  
 158  E :  bool RpcSession::ExchangeBuffer(TraceFileSegment* segment) {
 159  E :    DCHECK(IsTracing());
 160  E :    DCHECK(segment != NULL);
 161    :  
 162    :    bool succeeded = InvokeRpc(CallTraceClient_ExchangeBuffer,
 163    :                               session_handle_,
 164  E :                               &segment->buffer_info).succeeded();
 165    :  
 166  E :    return succeeded ? MapSegmentBuffer(segment) : false;
 167  E :  }
 168    :  
 169  E :  bool RpcSession::ReturnBuffer(TraceFileSegment* segment) {
 170  E :    DCHECK(IsTracing());
 171  E :    DCHECK(segment != NULL);
 172    :  
 173    :    return InvokeRpc(CallTraceClient_ReturnBuffer,
 174    :                     session_handle_,
 175  E :                     &segment->buffer_info).succeeded();
 176  E :  }
 177    :  
 178  E :  bool RpcSession::CloseSession() {
 179  E :    DCHECK(IsTracing());
 180    :  
 181    :    bool succeeded = InvokeRpc(CallTraceClient_CloseSession,
 182  E :                               &session_handle_).succeeded();
 183    :  
 184  E :    ignore_result(::RpcBindingFree(&rpc_binding_));
 185  E :    rpc_binding_ = NULL;
 186    :  
 187  E :    return succeeded;
 188  E :  }
 189    :  
 190  E :  void RpcSession::FreeSharedMemory() {
 191  E :    base::AutoLock scoped_lock_(shared_memory_lock_);
 192    :  
 193  E :    if (shared_memory_handles_.empty())
 194  E :      return;
 195    :  
 196  E :    SharedMemoryHandleMap::iterator it = shared_memory_handles_.begin();
 197  E :    for (; it != shared_memory_handles_.end(); ++it) {
 198  E :      DCHECK(it->second != NULL);
 199  E :      if (::UnmapViewOfFile(it->second) == 0) {
 200  i :        DWORD error = ::GetLastError();
 201  i :        LOG(WARNING) << "Failed to unmap memory handle: " << com::LogWe(error);
 202    :      }
 203    :  
 204  E :      if (::CloseHandle(it->first) == 0) {
 205  i :        DWORD error = ::GetLastError();
 206  i :        LOG(WARNING) << "Failed to close memory handle: " << com::LogWe(error);
 207    :      }
 208  E :    }
 209    :  
 210  E :    shared_memory_handles_.clear();
 211  E :  }
 212    :  
 213    :  }  // namespace trace::client
 214    :  }  // namespace trace

Coverage information generated Thu Sep 06 11:30:46 2012.