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