basicmem.cc 6.63 KB
Newer Older
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
/*
 * Copyright 2022 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include <cassert>
#include <ctime>
#include <iostream>
#include <vector>

#include <simbricks/base/cxxatomicfix.h>
extern "C" {
#include <simbricks/mem/if.h>
};

44
#define BASICMEM_DEBUG 1
45
46
47

static int exiting = 0;
static uint64_t cur_ts = 0;
48
49
50
uint8_t *mem_array;
uint64_t size;
uint64_t base_addr;
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

static void sigint_handler(int dummy) {
  exiting = 1;
}

static void sigusr1_handler(int dummy) {
    fprintf(stderr, "main_time = %lu\n", cur_ts);
}

bool MemifInit(struct SimbricksMemIf *memif, const char *shm_path,
                struct SimbricksBaseIfParams *memParams) {
  
  struct SimbricksBaseIf *membase = &memif->base;
  struct SimbricksBaseIfSHMPool pool_;
  memset(&pool_, 0, sizeof(pool_));
  
  struct SimBricksBaseIfEstablishData ests[1];
  struct SimbricksProtoMemHostIntro intro;
  unsigned n_bifs = 0;

  memset(&intro, 0, sizeof(intro));
  ests[n_bifs].base_if = membase;
  ests[n_bifs].tx_intro = &intro;
  ests[n_bifs].tx_intro_len = sizeof(intro);
  ests[n_bifs].rx_intro = &intro;
  ests[n_bifs].rx_intro_len = sizeof(intro);
  n_bifs++;
  
  if (SimbricksBaseIfInit(membase, memParams)) {
    perror("Init: SimbricksBaseIfInit failed");
  }

  std::string shm_path_ = shm_path;

  if (SimbricksBaseIfSHMPoolCreate(
          &pool_, shm_path_.c_str(),
          SimbricksBaseIfSHMSize(&membase->params)) != 0) {
    perror("MemifInit: SimbricksBaseIfSHMPoolCreate failed");
    return false;
  }

  if (SimbricksBaseIfListen(membase, &pool_) != 0) {
    perror("MemifInit: SimbricksBaseIfListen failed");
    return false;
  }

  if (SimBricksBaseIfEstablish(ests, 1)) {
    fprintf(stderr, "SimBricksBaseIfEstablish failed\n");
    return false;
  }

  printf("done connecting\n");
  return true;
}

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
volatile union SimbricksProtoMemM2H* M2HAlloc(SimbricksMemIf *memif, uint64_t cur_ts) {

  volatile union SimbricksProtoMemM2H *msg_to;
  bool first = true;
  while ((msg_to = SimbricksMemIfM2HOutAlloc(memif, cur_ts)) == NULL){
    if (first) {
      fprintf(stderr, "M2HAlloc: warning waiting for entry (%zu)\n",
              memif->base.out_pos);
      first = false;
    }
  }

  if (!first){
    fprintf(stderr, "D2HAlloc: entry successfully allocated\n");
  }

  return msg_to;
}


126
127
128
129
130
131
void PollH2M(struct SimbricksMemIf *memif, uint64_t cur_ts) {
  volatile union SimbricksProtoMemH2M *msg = SimbricksMemIfH2MInPoll(memif, cur_ts);

  if (msg == NULL){
    return;
  }
132
133

  int i;
134
  uint8_t type;
135
136
137
  uint64_t addr, len;
  volatile uint8_t *data;
  volatile union SimbricksProtoMemM2H *msg_to; 
138
139
140
141

  type = SimbricksMemIfH2MInType(memif, msg);
  switch (type) {
    case SIMBRICKS_PROTO_MEM_H2M_MSG_READ:
142
143
144
145
146
147
148
149
150
151
152
153
154
      addr = msg->read.addr;
      len = msg->read.len;

      msg_to = M2HAlloc(memif, cur_ts);
      msg_to->readcomp.req_id = msg->read.req_id;
      memcpy((void *)msg_to->readcomp.data, &mem_array[addr], len);


      SimbricksMemIfM2HOutSend(memif, msg_to, SIMBRICKS_PROTO_MEM_M2H_MSG_READCOMP);

#if BASICMEM_DEBUG 
      printf("received H2M read. Sending read comp msg\n");
#endif
155
156
      break;
    case SIMBRICKS_PROTO_MEM_H2M_MSG_WRITE:
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
      addr = msg->write.addr;
      len = msg->write.len;
      data = msg->write.data;
      
      for (i = 0; i < (int)len; i++){
        mem_array[addr + i] = data[i];
      }

      msg_to = M2HAlloc(memif, cur_ts);
      msg_to->writecomp.req_id = msg->write.req_id;
      SimbricksMemIfM2HOutSend(memif, msg_to, SIMBRICKS_PROTO_MEM_M2H_MSG_WRITECOMP);

#if BASICMEM_DEBUG 
      printf("received H2M write addr: %lu size: %d\n", addr, msg->write.len);
      for (i = 0; i < (int)len; i++){
        printf("%X ", msg->write.data[i]);
      }
      printf("\n");

#endif
177
178
179
180
181
182
183
184
185
186
187
188
      break;
    case SIMBRICKS_PROTO_MSG_TYPE_SYNC:
      break;
    default:
      fprintf(stderr, "poll_h2m: unsupported type=%u\n", type);
  }

  SimbricksMemIfH2MInDone(memif, msg);
}

int main(int argc, char *argv[]) {
  
189
190
191
192
  

  int asid = 0;

193
194
195
196
197
198
199
200
201
202
203
  signal(SIGINT, sigint_handler);
  signal(SIGUSR1, sigusr1_handler);

  int sync_mem = 1;
  uint64_t next_ts = 0;
  const char *shmPath;
  struct SimbricksBaseIfParams memParams;
  struct SimbricksMemIf memif;
  
  SimbricksMemIfDefaultParams(&memParams);

204
  if (argc < 6 || argc > 10) {
205
    fprintf(stderr,
206
            "Usage: basicmem [SIZE] [BASE-ADDR] [ASID] [MEM-SOCKET] "
207
208
209
            "SHM [SYNC-MODE] [START-TICK] [SYNC-PERIOD] [MEM-LATENCY]\n");
    return -1;
  }
210
211
212
213
214
215
216
217
218
219
220
221
  if (argc >= 8)
     cur_ts = strtoull(argv[7], NULL, 0);
  if (argc >= 9)
    memParams.sync_interval =  strtoull(argv[8], NULL, 0) * 1000ULL;
  if (argc >= 10)
    memParams.link_latency = strtoull(argv[9], NULL, 0) * 1000ULL;

  size = strtoull(argv[1], NULL, 0);
  base_addr = strtoull(argv[2], NULL, 0);
  asid = atoi(argv[3]);
  memParams.sock_path = argv[4];
  shmPath = argv[5];
222
223
224
225
226

  memParams.sync_mode = kSimbricksBaseIfSyncOptional;
  memParams.blocking_conn = false;
  memif.base.sync = sync_mem;

227
228
229
230
231
  mem_array = (uint8_t *) malloc(size * sizeof(uint8_t));
  if (!mem_array){
    perror("no array allocated\n");
  }

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  if (!MemifInit(&memif, shmPath, &memParams)){
    return EXIT_FAILURE;
  }

  printf("start polling\n");
  while (!exiting){
    while (SimbricksMemIfM2HOutSync(&memif, cur_ts)) {
      fprintf(stderr, "warn: SimbricksMemIfSync failed (t=%lu)\n", cur_ts);
    }

    do {
      
      PollH2M(&memif, cur_ts);

      if (sync_mem){
        next_ts = SimbricksMemIfH2MInTimestamp(&memif);
      }

    } while (!exiting && next_ts <= cur_ts);

  }
  return 0;
}