photon  1.1
photon_msgbuffer.c
Go to the documentation of this file.
1 // =============================================================================
2 // Photon RDMA Library (libphoton)
3 //
4 // Copyright (c) 2016, Trustees of Indiana University,
5 // All rights reserved.
6 //
7 // This software may be modified and distributed under the terms of the BSD
8 // license. See the COPYING file for details.
9 //
10 // This software was created at the Indiana University Center for Research in
11 // Extreme Scale Technologies (CREST).
12 // =============================================================================
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 
18 #include "photon.h"
19 #include "logging.h"
20 #include "photon_msgbuffer.h"
21 
22 photonMsgBuf photon_msgbuffer_new(uint64_t size, uint64_t p_size, int p_offset, int p_hsize) {
23 
24  photonMsgBuf mbuf;
25  char *bptr;
26  int ret, i;
27 
28  mbuf = malloc(sizeof(struct photon_msgbuffer_t));
29  if (!mbuf)
30  goto error_exit;
31 
32  memset(mbuf, 0, sizeof(*mbuf));
33 
34  // TODO: probably want to align each entry (p_size) on a cacheline
35  ret = posix_memalign((void*)&bptr, getpagesize(), size);
36  if (ret) {
37  dbg_err("could not allocate buffer space");
38  goto error_exit_buf;
39  }
40 
41  mbuf->db = photon_buffer_create(bptr, size, BUFFER_FLAG_NIL);
42  if (!mbuf->db) {
43  dbg_err("could not create photon buffer");
44  goto error_exit_buf;
45  }
46 
47  mbuf->p_size = p_size;
48  mbuf->m_size = p_size - p_offset - p_hsize;
49  mbuf->p_offset = p_offset;
50  mbuf->p_hsize = p_hsize;
51  mbuf->p_count = (int)(size / mbuf->p_size);
52 
53  // create metadata to track buffer offsets
54  mbuf->entries = (photon_mbe*)malloc(mbuf->p_count * sizeof(photon_mbe));
55  if (!mbuf->entries) {
56  dbg_err("could not allocate buf entries");
57  goto error_exit_db;
58  }
59 
60  for (i = 0; i < mbuf->p_count; i++) {
61  mbuf->entries[i].base = (void*)((mbuf->db)->bint.buf.addr + (i * p_size));
62  mbuf->entries[i].hptr = (void*)((mbuf->db)->bint.buf.addr + (i * p_size) + p_offset);
63  mbuf->entries[i].mptr = (void*)((mbuf->db)->bint.buf.addr + (i * p_size) + p_offset + p_hsize);
64  mbuf->entries[i].empty = true;
65  }
66 
67  mbuf->s_index = 0;
68  mbuf->status = 0;
69 
70  pthread_mutex_init(&mbuf->buf_lock, NULL);
71 
72  return mbuf;
73 
74  error_exit_db:
75  free(bptr);
76  error_exit_buf:
77  free(mbuf);
78  error_exit:
79  return NULL;
80 }
81 
82 int photon_msgbuffer_free(photonMsgBuf mbuf) {
83  if (mbuf) {
84  if (mbuf->entries)
85  free(mbuf->entries);
86  if (mbuf->db)
87  photon_buffer_free(mbuf->db);
88  free(mbuf);
89  return PHOTON_OK;
90  }
91  return PHOTON_ERROR;
92 }
93 
94 photon_mbe *photon_msgbuffer_get_entry(photonMsgBuf mbuf, int *ind) {
95  pthread_mutex_lock(&mbuf->buf_lock);
96  {
97  while (mbuf->entries[mbuf->s_index].empty == false) {
98  mbuf->s_index++;
99  if (mbuf->s_index == mbuf->p_count)
100  mbuf->s_index = 0;
101  }
102  mbuf->entries[mbuf->s_index].empty = false;
103  }
104  pthread_mutex_unlock(&mbuf->buf_lock);
105 
106  *ind = mbuf->s_index;
107  return &mbuf->entries[mbuf->s_index];
108 }
109 
110 int photon_msgbuffer_free_entry(photonMsgBuf mbuf, int ind) {
111  if ((ind < 0) || (ind >= mbuf->p_count)) {
112  dbg_err("index %d is out of bounds", ind);
113  return PHOTON_ERROR;
114  }
115 
116  mbuf->entries[ind].empty = true;
117 
118  return PHOTON_OK;
119 }
photonBufferHandle photon_buffer_create(void *addr, uint64_t size, int flags)
int photon_msgbuffer_free(photonMsgBuf mbuf)
int photon_msgbuffer_free_entry(photonMsgBuf mbuf, int ind)
void photon_buffer_free(photonBufferHandle buf)
photon_mbe * photon_msgbuffer_get_entry(photonMsgBuf mbuf, int *ind)
#define PHOTON_ERROR
Error code, general error.
Definition: photon.h:32
#define PHOTON_OK
Photon success code.
Definition: photon.h:30
photonMsgBuf photon_msgbuffer_new(uint64_t size, uint64_t p_size, int p_offset, int p_hsize)
Constants and macros for interacting with the library. Only this header is needed to use the Photon A...
#define BUFFER_FLAG_NIL