Belle II Software  release-05-01-25
mmap_statistics.h
1 #ifndef MMAP_STATISTICS_H
2 #define MMAP_STATISTICS_H
3 
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <stdint.h>
9 #include <sys/mman.h>
10 
11 #ifndef O_NOATIME
12 #define O_NOATIME 0
13 #endif
14 
16  uint32_t addr;
17  uint32_t port;
18  uint64_t event;
19  uint64_t byte;
20  uint64_t total_byte;
21 };
22 
24 private:
25  int m_n_u;
26  int m_n_d;
27  int m_fd_u;
28  int m_fd_d;
29  int m_len_u;
30  int m_len_d;
31  stream_statistics* m_stat_u;
32  stream_statistics* m_stat_d;
33 
34 public:
35  eb_statistics(const char* file_up, int nup, const char* file_down, int ndown)
36  {
37  m_n_u = nup;
38  m_n_d = ndown;
39 
40  m_len_u = sizeof(stream_statistics) * m_n_u;
41  m_len_d = sizeof(stream_statistics) * m_n_d;
42 
43  m_fd_u = open(file_up, O_RDWR | O_CREAT | O_NOATIME, 0644);
44  assert(m_fd_u >= 0);
45  m_fd_d = open(file_down, O_RDWR | O_CREAT | O_NOATIME, 0644);
46  assert(m_fd_d >= 0);
47 
48  stream_statistics buf_u[m_n_u];
49  stream_statistics buf_d[m_n_d];
50 
51  memset(buf_u, 0, m_len_u);
52  memset(buf_d, 0, m_len_d);
53 
54  int ret;
55 
56  ret = write(m_fd_u, buf_u, m_len_u);
57  assert(ret == m_len_u);
58  ret = write(m_fd_d, buf_d, m_len_d);
59  assert(ret == m_len_d);
60 
61  m_stat_u = (stream_statistics*)mmap(0, m_len_u, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd_u, 0);
62  assert(m_stat_u != MAP_FAILED);
63  m_stat_d = (stream_statistics*)mmap(0, m_len_d, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd_d, 0);
64  assert(m_stat_d != MAP_FAILED);
65  };
66 
67  ~eb_statistics()
68  {
69  munmap(m_stat_u, m_len_u);
70  munmap(m_stat_d, m_len_d);
71  close(m_fd_u);
72  close(m_fd_d);
73  };
74 
75  stream_statistics* up()
76  {
77  return m_stat_u;
78  };
79 
80  stream_statistics& up(int i)
81  {
82  return m_stat_u[i];
83  };
84 
85  stream_statistics* down()
86  {
87  return m_stat_d;
88  };
89 
90  stream_statistics& down(int i)
91  {
92  return m_stat_d[i];
93  };
94 };
95 
96 #endif
eb_statistics
Definition: mmap_statistics.h:23
stream_statistics
Definition: mmap_statistics.h:15