1 : // Copyright 2012 Google Inc. All Rights Reserved.
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 : // This file implements the trace::service::Buffer and BufferPool
16 : // structures, which are used to represent the shared memory buffers
17 : // used by the call_trace service.
18 :
19 : #include "syzygy/trace/service/buffer_pool.h"
20 :
21 : #include "base/logging.h"
22 : #include "sawbuck/common/com_utils.h"
23 :
24 : namespace trace {
25 : namespace service {
26 :
27 E : BufferPool::BufferPool() : base_ptr_(NULL) {
28 E : }
29 :
30 E : BufferPool::~BufferPool() {
31 E : if (base_ptr_ && !::UnmapViewOfFile(base_ptr_)) {
32 i : DWORD error = ::GetLastError();
33 i : DCHECK(handle_.IsValid());
34 i : LOG(WARNING) << "Failed to release buffer: " << com::LogWe(error) << ".";
35 : }
36 E : }
37 :
38 : bool BufferPool::Init(Session* session,
39 : size_t num_buffers,
40 E : size_t buffer_size) {
41 E : DCHECK(num_buffers != 0);
42 E : DCHECK(buffer_size != 0);
43 E : DCHECK(base_ptr_ == NULL);
44 E : DCHECK(!handle_.IsValid());
45 :
46 E : size_t mapping_size = num_buffers * buffer_size;
47 :
48 E : VLOG(1) << "Creating " << (mapping_size >> 20) << "MB memory pool.";
49 :
50 : // Create a pagefile backed memory mapped file. This will be cut up into a
51 : // pool of buffers.
52 : base::win::ScopedHandle new_handle(
53 E : ::CreateFileMapping(NULL, NULL, PAGE_READWRITE, 0, mapping_size, NULL));
54 E : if (!new_handle.IsValid()) {
55 i : DWORD error = ::GetLastError();
56 i : LOG(ERROR) << "Failed to allocate buffer: " << com::LogWe(error) << ".";
57 i : return false;
58 : }
59 :
60 : // Map a view of the shared memory file into this process.
61 : uint8* new_base_ptr = reinterpret_cast<uint8*>(
62 E : ::MapViewOfFile(new_handle, FILE_MAP_ALL_ACCESS, 0, 0, mapping_size));
63 E : if (new_base_ptr == NULL) {
64 i : DWORD error = ::GetLastError();
65 i : LOG(ERROR) << "Failed mapping buffer: " << com::LogWe(error) << ".";
66 i : return false;
67 : }
68 :
69 : // Take ownership of the newly created resources.
70 E : handle_.Set(new_handle.Take());
71 E : base_ptr_ = new_base_ptr;
72 :
73 : // Create records for each buffer in the pool.
74 E : buffers_.resize(num_buffers);
75 E : for (size_t i = 0; i < num_buffers; ++i) {
76 E : Buffer& cb = buffers_[i];
77 E : size_t offset = i * buffer_size;
78 E : cb.shared_memory_handle = NULL;
79 E : cb.mapping_size = mapping_size;
80 E : cb.buffer_offset = offset;
81 E : cb.buffer_size = buffer_size;
82 E : cb.session = session;
83 E : cb.data_ptr = base_ptr_ + offset;
84 E : cb.state = Buffer::kAvailable;
85 E : }
86 :
87 E : return true;
88 E : }
89 :
90 E : void BufferPool::SetClientHandle(HANDLE client_handle) {
91 E : DCHECK(client_handle != NULL);
92 :
93 E : for (size_t i = 0; i < buffers_.size(); ++i) {
94 E : Buffer& cb = buffers_[i];
95 E : DCHECK_EQ(Buffer::kAvailable, cb.state);
96 E : DCHECK(cb.shared_memory_handle == NULL);
97 E : cb.shared_memory_handle = reinterpret_cast<unsigned long>(client_handle);
98 E : }
99 E : }
100 :
101 : } // namespace service
102 : } // namespace trace
|