-
Notifications
You must be signed in to change notification settings - Fork 18
/
DaRPCEndpoint.java
232 lines (200 loc) · 7.8 KB
/
DaRPCEndpoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* DaRPC: Data Center Remote Procedure Call
*
* Author: Patrick Stuedi <stu@zurich.ibm.com>
*
* Copyright (C) 2016-2018, IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ibm.darpc;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ibm.disni.verbs.*;
import com.ibm.disni.util.MemoryUtils;
import com.ibm.disni.*;
public abstract class DaRPCEndpoint<R extends DaRPCMessage, T extends DaRPCMessage> extends RdmaEndpoint {
private static final Logger logger = LoggerFactory.getLogger("com.ibm.darpc");
private static final int headerSize = 4;
public abstract void dispatchReceive(ByteBuffer buffer, int ticket, int recvIndex) throws IOException;
public abstract void dispatchSend(int ticket) throws IOException;
private DaRPCEndpointGroup<? extends DaRPCEndpoint<R,T>, R, T> rpcGroup;
private ByteBuffer dataBuffer;
private IbvMr dataMr;
private ByteBuffer receiveBuffer;
private ByteBuffer sendBuffer;
private ByteBuffer[] recvBufs;
private ByteBuffer[] sendBufs;
private SVCPostRecv[] recvCall;
private SVCPostSend[] sendCall;
private ConcurrentHashMap<Integer, SVCPostSend> pendingPostSend;
private ArrayBlockingQueue<SVCPostSend> freePostSend;
private AtomicLong ticketCount;
private int pipelineLength;
private int payloadSize;
private int rawBufferSize;
private int maxinline;
private AtomicLong messagesSent;
private AtomicLong messagesReceived;
public DaRPCEndpoint(DaRPCEndpointGroup<? extends DaRPCEndpoint<R,T>, R, T> endpointGroup, RdmaCmId idPriv, boolean serverSide) throws IOException {
super(endpointGroup, idPriv, serverSide);
this.rpcGroup = endpointGroup;
this.maxinline = rpcGroup.getMaxInline();
this.payloadSize = rpcGroup.getBufferSize();
this.rawBufferSize = headerSize + this.payloadSize;
this.pipelineLength = rpcGroup.recvQueueSize();
this.freePostSend = new ArrayBlockingQueue<SVCPostSend>(pipelineLength);
this.pendingPostSend = new ConcurrentHashMap<Integer, SVCPostSend>();
this.recvBufs = new ByteBuffer[pipelineLength];
this.sendBufs = new ByteBuffer[pipelineLength];
this.recvCall = new SVCPostRecv[pipelineLength];
this.sendCall = new SVCPostSend[pipelineLength];
this.ticketCount = new AtomicLong(0);
this.messagesSent = new AtomicLong(0);
this.messagesReceived = new AtomicLong(0);
logger.info("RPC client endpoint, with payload buffer size = " + payloadSize + ", pipeline " + pipelineLength);
}
public void init() throws IOException {
int sendBufferOffset = pipelineLength * rawBufferSize;
/* Main data buffer for sends and receives. Will be split into two regions,
* one for sends and one for receives.
*/
dataBuffer = ByteBuffer.allocateDirect(pipelineLength * rawBufferSize * 2);
/* Only do one memory registration with the IB card. */
dataMr = registerMemory(dataBuffer).execute().free().getMr();
/* Receive memory region is the first half of the main buffer. */
dataBuffer.limit(dataBuffer.position() + sendBufferOffset);
receiveBuffer = dataBuffer.slice();
/* Send memory region is the second half of the main buffer. */
dataBuffer.position(sendBufferOffset);
dataBuffer.limit(dataBuffer.position() + sendBufferOffset);
sendBuffer = dataBuffer.slice();
for(int i = 0; i < pipelineLength; i++) {
/* Create single receive buffers within the receive region in form of slices. */
receiveBuffer.position(i * rawBufferSize);
receiveBuffer.limit(receiveBuffer.position() + rawBufferSize);
recvBufs[i] = receiveBuffer.slice();
/* Create single send buffers within the send region in form of slices. */
sendBuffer.position(i * rawBufferSize);
sendBuffer.limit(sendBuffer.position() + rawBufferSize);
sendBufs[i] = sendBuffer.slice();
this.recvCall[i] = setupRecvTask(i);
this.sendCall[i] = setupSendTask(i);
freePostSend.add(sendCall[i]);
recvCall[i].execute();
}
}
@Override
public synchronized void close() throws IOException, InterruptedException {
super.close();
deregisterMemory(dataMr);
}
public long getMessagesSent() {
return messagesSent.get();
}
public long getMessagesReceived() {
return messagesReceived.get();
}
protected boolean sendMessage(DaRPCMessage message, int ticket) throws IOException {
SVCPostSend postSend = freePostSend.poll();
if (postSend != null){
int index = (int) postSend.getWrMod(0).getWr_id();
sendBufs[index].putInt(0, ticket);
sendBufs[index].position(4);
int written = 4 + message.write(sendBufs[index]);
postSend.getWrMod(0).getSgeMod(0).setLength(written);
postSend.getWrMod(0).setSend_flags(IbvSendWR.IBV_SEND_SIGNALED);
if (written <= maxinline) {
postSend.getWrMod(0).setSend_flags(postSend.getWrMod(0).getSend_flags() | IbvSendWR.IBV_SEND_INLINE);
}
pendingPostSend.put(ticket, postSend);
postSend.execute();
messagesSent.incrementAndGet();
return true;
} else {
return false;
}
}
protected void postRecv(int index) throws IOException {
recvCall[index].execute();
}
public void freeSend(int ticket) throws IOException {
SVCPostSend sendOperation = pendingPostSend.remove(ticket);
if (sendOperation == null) {
throw new IOException("no pending ticket " + ticket + ", current ticket count " + ticketCount.get());
}
this.freePostSend.add(sendOperation);
}
public void dispatchCqEvent(IbvWC wc) throws IOException {
if (wc.getStatus() == 5){
//flush
return;
} else if (wc.getStatus() != 0){
throw new IOException("Faulty operation! wc.status " + wc.getStatus());
}
if (wc.getOpcode() == 128){
//receiving a message
int index = (int) wc.getWr_id();
ByteBuffer recvBuffer = recvBufs[index];
int ticket = recvBuffer.getInt(0);
recvBuffer.position(4);
dispatchReceive(recvBuffer, ticket, index);
} else if (wc.getOpcode() == 0) {
//send completion
int index = (int) wc.getWr_id();
ByteBuffer sendBuffer = sendBufs[index];
int ticket = sendBuffer.getInt(0);
dispatchSend(ticket);
} else {
throw new IOException("Unkown opcode " + wc.getOpcode());
}
}
private SVCPostSend setupSendTask(int wrid) throws IOException {
ArrayList<IbvSendWR> sendWRs = new ArrayList<IbvSendWR>(1);
LinkedList<IbvSge> sgeList = new LinkedList<IbvSge>();
IbvSge sge = new IbvSge();
sge.setAddr(MemoryUtils.getAddress(sendBufs[wrid]));
sge.setLength(rawBufferSize);
sge.setLkey(dataMr.getLkey());
sgeList.add(sge);
IbvSendWR sendWR = new IbvSendWR();
sendWR.setSg_list(sgeList);
sendWR.setWr_id(wrid);
sendWRs.add(sendWR);
sendWR.setSend_flags(IbvSendWR.IBV_SEND_SIGNALED);
sendWR.setOpcode(IbvSendWR.IbvWrOcode.IBV_WR_SEND.ordinal());
return postSend(sendWRs);
}
private SVCPostRecv setupRecvTask(int wrid) throws IOException {
ArrayList<IbvRecvWR> recvWRs = new ArrayList<IbvRecvWR>(1);
LinkedList<IbvSge> sgeList = new LinkedList<IbvSge>();
IbvSge sge = new IbvSge();
sge.setAddr(MemoryUtils.getAddress(recvBufs[wrid]));
sge.setLength(rawBufferSize);
sge.setLkey(dataMr.getLkey());
sgeList.add(sge);
IbvRecvWR recvWR = new IbvRecvWR();
recvWR.setWr_id(wrid);
recvWR.setSg_list(sgeList);
recvWRs.add(recvWR);
return postRecv(recvWRs);
}
}