Belle II Software  release-05-01-25
NSMDataStore.cc
1 #include "daq/slc/nsm/NSMDataStore.h"
2 
3 #include <daq/slc/system/Time.h>
4 
5 #include <daq/slc/base/StringUtil.h>
6 
7 #include <cstdio>
8 #include <cstring>
9 #include <sys/mman.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <daq/slc/system/LockGuard.h>
13 
14 using namespace Belle2;
15 
16 NSMDataStore NSMDataStore::g_store;
17 
18 bool NSMDataStore::open(unsigned short max)
19 {
20  size_t size = (max > 0) ? m_mutex.size() + m_cond.size() +
21  sizeof(Header) + sizeof(Entry) * max : 0;
22  if (!m_mem.open("NSMDataStore", size)) {
23  return false;
24  }
25  m_mem.truncate(0);
26  char* buf = (char*)m_mem.map();
27  m_buf = buf;
28  m_mutex = MMutex(buf);
29  buf += m_mutex.size();
30  m_cond = MCond(buf);
31  buf += m_cond.size();
32  m_header = (Header*)buf;
33  buf += sizeof(Header);
34  m_entries = (Entry*)buf;
35  if (max > 0) {
36  m_header->maxentries = max;
37  }
38  return true;
39 }
40 
41 void NSMDataStore::init()
42 {
43  if (m_header == NULL) return;
44  int max = m_header->maxentries;
45  memset(m_buf, 0, m_mem.size());
46  m_header->maxentries = max;
47  m_mutex.init();
48  m_cond.init();
49 }
50 
51 NSMDataStore::Entry* NSMDataStore::add(unsigned int addr,
52  unsigned int size,
53  unsigned int revision,
54  const std::string& name,
55  const std::string& format,
56  unsigned int rid)
57 {
58  MLockGuard lockGuard(m_mutex);
59  unsigned int n = m_header->nentries;
60  for (unsigned int i = 0; i < n; i++) {
61  if (m_entries[i].id > 0 && name == m_entries[i].name) {
62  return &m_entries[i];
63  }
64  }
65  memset(&m_entries[n], 0, sizeof(Entry));
66  m_entries[n].id = n + 1;
67  m_entries[n].rid = rid;
68  m_entries[n].addr = addr;
69  m_entries[n].size = size;
70  m_entries[n].revision = revision;
71  m_entries[n].utime = (unsigned int)Time().getSecond();
72  strcpy(m_entries[n].name, name.c_str());
73  strcpy(m_entries[n].format, format.c_str());
74  m_header->nentries++;
75  return &m_entries[n];
76 }
77 
78 NSMDataStore::Entry* NSMDataStore::get(const std::string& name)
79 {
80  MLockGuard lockGuard(m_mutex);
81  const unsigned int n = m_header->nentries;
82  for (unsigned int i = 0; i < n; i++) {
83  if (m_entries[i].id > 0 && name == m_entries[i].name) {
84  return &m_entries[i];
85  }
86  }
87  return NULL;
88 }
89 
90 NSMDataStore::Entry* NSMDataStore::get(unsigned int id)
91 {
92  if (id == 0) return NULL;
93  MLockGuard lockGuard(m_mutex);
94  const unsigned int n = m_header->nentries;
95  for (unsigned int i = 0; i < n; i++) {
96  if (m_entries[i].id > 0 &&
97  m_entries[i].id == (unsigned int)id) {
98  return &m_entries[i];
99  }
100  }
101  return NULL;
102 }
103 
104 NSMDataStore::Entry* NSMDataStore::get(unsigned int addr,
105  unsigned int id)
106 {
107  if (id == 0) return NULL;
108  MLockGuard lockGuard(m_mutex);
109  const unsigned int n = m_header->nentries;
110  for (unsigned int i = 0; i < n; i++) {
111  if (m_entries[i].rid > 0 &&
112  m_entries[i].addr == (unsigned int)addr &&
113  m_entries[i].rid == (unsigned int)id) {
114  return &m_entries[i];
115  }
116  }
117  return NULL;
118 }
119 
120 void NSMDataStore::unlink()
121 {
122  const unsigned int n = m_header->nentries;
123  for (unsigned int i = 0; i < n; i++) {
124  char* name = m_entries[i].name;
125  if (strlen(name) > 0) {
126  std::string path = "";
127  sockaddr_in sa;
128  sa.sin_addr.s_addr = m_entries[i].addr;
129  if (m_entries[i].addr > 0) {
130  path = StringUtil::form("%s:%s",
131  inet_ntoa(sa.sin_addr), name);
132  } else {
133  path = StringUtil::form("127.0.0.1:%s", name);
134  }
135  shm_unlink(path.c_str());
136  }
137  }
138  m_mem.unlink();
139 }
Belle2::NSMDataStore
Definition: NSMDataStore.h:14
Belle2::MMutex
Definition: MMutex.h:19
Belle2::GenericLockGuard
Lock Guard for a Mutex instance.
Definition: LockGuard.h:40
Belle2::MCond
Definition: MCond.h:14
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Time
Definition: Time.h:14
Belle2::NSMDataStore::Entry
Definition: NSMDataStore.h:32