forked from BluEye-Robotics/tyndall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc_read.cpp
172 lines (150 loc) · 3.62 KB
/
ipc_read.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
//#include <tyndall/ext/embed.hpp>
//#include <experimental/reflect>
#include <typeinfo>
#include <string.h>
#include <ctype.h>
// ipc
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
#include <tyndall/ipc/seq_lock.h>
#include <tyndall/reflect/print_format.h>
void print(char* fmt, const char* buf, size_t buf_size)
{
const char* b = buf;
for (char* f = fmt; (*f!='\0'); ++f)
{
if (isdigit(*f))
{
long num = strtol(f, &f, 10);
printf("<%ld>, ", num);
b += num;
}
size_t printed = print_format(*f, b);
if (printed == 0)
{
switch(*f)
{
case 's':
printf("%ss, ", b);
printed = strlen(b);
b += printed;
break;
default:
printf("error, wrong parameter: %c\n", *f);
}
if (printed == 0)
break;
}
else
{
printf(",\t");
b += printed;
}
if ((size_t)(b - buf) > buf_size)
{
printf("fmt is too large for buffer at %zu", buf_size);
break;
}
}
printf("\n");
fflush(stdout);
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: %s <ipc_name> <format>\n", argv[0]);
exit(1);
}
const char* ipc_id = argv[1];
const char *ipc_prefix = "/dev/shm/";
if (strncmp(ipc_id, ipc_prefix, strlen(ipc_prefix)) == 0)
ipc_id += strlen(ipc_prefix);
//printf("id: %s\n", ipc_id);
// ipc
int fd = shm_open(ipc_id, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
assert(fd != -1);
size_t ipc_size;
{
struct stat st;
int rc = fstat(fd, &st);
assert(rc == 0);
ipc_size = st.st_size;
}
void* const mapped = mmap(NULL, ipc_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
assert(mapped != MAP_FAILED);
assert(reinterpret_cast<uintptr_t>(mapped) % CACHELINE_BYTES == 0);
unsigned* ipc_seq = (unsigned*)mapped;
const char* const ipc_buf = (const char*)mapped + CACHELINE_BYTES;
const size_t buf_size = ipc_size - 2*sizeof(int) - CACHELINE_BYTES;
char* buf = (char*)malloc(buf_size + CACHELINE_BYTES);
char* const buf_to_free = buf;
// align buf
{
constexpr size_t alignment = CACHELINE_BYTES;
size_t alignment_error = reinterpret_cast<uintptr_t>(buf) % alignment;
if (alignment_error > 0)
buf += alignment - alignment_error;
}
unsigned seq = 0;
while(1)
{
bool new_buf = false;
{
unsigned seq1;
do
{
seq1 = seq_lock_read_begin(ipc_seq);
memcpy(buf, ipc_buf, buf_size);
} while (seq_lock_read_retry(ipc_seq, seq1));
if (seq1 != seq)
{
new_buf = true;
seq = seq1;
}
}
if (new_buf)
{
if (argc > 2)
{
char* fmt = argv[2];
print(fmt, buf, buf_size);
}
else
{
// extract debug format
constexpr int type_info_hash_size = 4;
const size_t debug_tail_size = ipc_size % CACHELINE_BYTES;
if (debug_tail_size > type_info_hash_size)
{
const size_t debug_format_size = debug_tail_size - type_info_hash_size;
char* const debug_format_loc = static_cast<char*>(mapped) + ipc_size - debug_format_size;
char* fmt = debug_format_loc;
print(fmt, buf, buf_size);
}
else
{
for (size_t i=0; i<buf_size; ++i)
printf("%02x", buf[i]);
printf("\n");
fflush(stdout);
}
}
}
else
usleep(1000);
}
free(buf_to_free);
{
int rc = munmap(mapped, ipc_size);
assert(rc == 0);
}
return 0;
}