L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stats.h
1#include <l4/re/env>
2#include <l4/re/dataspace>
3#include <l4/re/error_helper>
5#include <l4/virtio-net-switch/stats.h>
6
7class Switch_statistics
8{
9private:
11 Virtio_net_switch::Statistics *_stats;
12 bool _initialized = false;
13
14 Switch_statistics() {}
15
16 ~Switch_statistics()
17 {
18 if (_initialized)
19 L4Re::Env::env()->rm()->detach(reinterpret_cast<l4_addr_t>(_stats), 0);
20 }
21
22 l4_size_t _size;
23
24public:
25 Virtio_net_switch::Statistics *stats()
26 {
27 if (_initialized)
28 return _stats;
29 else
30 throw L4::Runtime_error(-L4_EAGAIN, "Statistics not set up.");
31 }
32
33 static Switch_statistics& get_instance()
34 {
35 static Switch_statistics instance;
36 return instance;
37 }
38
39 void initialize(l4_uint64_t num_max_ports)
40 {
41 _size = l4_round_page(sizeof(Virtio_net_switch::Statistics)
42 + sizeof(Virtio_net_switch::Port_statistics) * num_max_ports);
43 void *addr = malloc(_size);
44 if (!addr)
46 "Could not allocate statistics memory.");
47
48 memset(addr, 0, _size);
49 _stats = reinterpret_cast<Virtio_net_switch::Statistics *>(addr);
50 _initialized = true;
51 _stats->max_ports = num_max_ports;
52 }
53
54 Virtio_net_switch::Port_statistics *
55 allocate_port_statistics(char const* name)
56 {
57 for (unsigned i = 0; i < _stats->max_ports; ++i)
58 {
59 if (!_stats->port_stats[i].in_use)
60 {
61 memset(reinterpret_cast<void*>(&_stats->port_stats[i]), 0,
62 sizeof(Virtio_net_switch::Port_statistics));
63 _stats->port_stats[i].in_use = 1;
64 size_t len = std::min(strlen(name), sizeof(_stats->port_stats[i].name) - 1);
65 memcpy(_stats->port_stats[i].name, name, len);
66 _stats->port_stats[i].name[len] = '\0';
67 _stats->age++;
68 return &_stats->port_stats[i];
69 }
70 }
71 return nullptr;
72 }
73
74 inline l4_size_t size()
75 { return _size; }
76
77 Switch_statistics(Switch_statistics const&) = delete;
78 void operator=(Switch_statistics const &) = delete;
79};
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
Definition env:120
Exception for an abstract runtime error.
Definition exceptions:129
Smart capability class.
Dataspace interface.
Environment interface.
Error helper.
unsigned int l4_size_t
Unsigned size type.
Definition l4int.h:24
unsigned long l4_addr_t
Address type.
Definition l4int.h:34
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:31
@ L4_EAGAIN
Try again.
Definition err.h:38
@ L4_ENOMEM
No memory.
Definition err.h:39
l4_addr_t l4_round_page(l4_addr_t address) L4_NOTHROW
Round address up to the next page.
Definition consts.h:473
Capability allocator.