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 "syzygy/common/com_utils.h"
23 :
24 : namespace trace {
25 : namespace service {
26 :
27 E : BufferPool::BufferPool() {
28 E : }
29 :
30 E : BufferPool::~BufferPool() {
31 E : }
32 :
33 : bool BufferPool::Init(Session* session,
34 : size_t num_buffers,
35 E : size_t buffer_size) {
36 E : DCHECK(num_buffers != 0);
37 E : DCHECK(buffer_size != 0);
38 E : DCHECK(!handle_.IsValid());
39 :
40 E : size_t mapping_size = num_buffers * buffer_size;
41 :
42 E : VLOG(1) << "Creating " << (mapping_size >> 20) << "MB memory pool.";
43 :
44 : // Create a pagefile backed memory mapped file. This will be cut up into a
45 : // pool of buffers.
46 : base::win::ScopedHandle new_handle(
47 E : ::CreateFileMapping(NULL, NULL, PAGE_READWRITE, 0, mapping_size, NULL));
48 E : if (!new_handle.IsValid()) {
49 i : DWORD error = ::GetLastError();
50 i : LOG(ERROR) << "Failed to allocate buffer: " << ::common::LogWe(error)
51 : << ".";
52 i : return false;
53 : }
54 :
55 : // Take ownership of the newly created resources.
56 E : handle_.Set(new_handle.Take());
57 :
58 : // Create records for each buffer in the pool.
59 E : buffers_.resize(num_buffers);
60 E : for (size_t i = 0; i < num_buffers; ++i) {
61 E : Buffer& cb = buffers_[i];
62 E : size_t offset = i * buffer_size;
63 E : cb.shared_memory_handle = NULL;
64 E : cb.mapping_size = mapping_size;
65 E : cb.buffer_offset = offset;
66 E : cb.buffer_size = buffer_size;
67 E : cb.session = session;
68 E : cb.pool = this;
69 E : cb.state = Buffer::kAvailable;
70 E : }
71 :
72 E : return true;
73 E : }
74 :
75 E : void BufferPool::SetClientHandle(HANDLE client_handle) {
76 E : DCHECK(client_handle != NULL);
77 :
78 E : for (size_t i = 0; i < buffers_.size(); ++i) {
79 E : Buffer& cb = buffers_[i];
80 E : DCHECK_EQ(Buffer::kAvailable, cb.state);
81 E : DCHECK(cb.shared_memory_handle == NULL);
82 E : cb.shared_memory_handle = reinterpret_cast<unsigned long>(client_handle);
83 E : }
84 E : }
85 :
86 : } // namespace service
87 : } // namespace trace
|