-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc.c
More file actions
135 lines (111 loc) · 3 KB
/
Copy pathipc.c
File metadata and controls
135 lines (111 loc) · 3 KB
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
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/wait.h>
#include "ipc.h"
#define IPC_ENTRY_ALIGNMENT 32
extern void *__ipc_array_start;
extern void *__ipc_array_end;
static int ipc_get_arr_offs()
{
int offs;
if (sizeof(ipc_t) % IPC_ENTRY_ALIGNMENT != 0) {
offs = (sizeof(ipc_t) + IPC_ENTRY_ALIGNMENT) / IPC_ENTRY_ALIGNMENT;
offs *= IPC_ENTRY_ALIGNMENT;
} else {
offs = sizeof(ipc_t);
}
return offs / sizeof(void*);
}
int ipc_execute(const ipc_t* ipc, const testargs_t* args, report_t *report)
{
void *ctx;
int ret;
report->stat = -1;
report->ipc = ipc;
ret = ipc->parent_ops->setup(&ctx, args);
if (ret) {
return ret;
}
pid_t pid = fork();
if (pid < 0) {
return -1;
}
if (pid == 0) {
// TODO: child process should signal parent when error occured
size_t n = 0;
if (ipc->child_ops->setup(&ctx, args) < 0) {
_exit(1);
}
if (ipc->child_ops->start && ipc->child_ops->start(&ctx, args) < 0) {
_exit(1);
}
while (n < args->numBlks) {
if (ipc->child_ops->send(ctx, args) < 0) {
_exit(1);
}
n++;
}
ipc->child_ops->clean(ctx);
_exit(0);
} else {
struct timeval t1;
struct timeval t2;
int stat;
size_t n = 0;
report->revc_sz = 0;
if (ipc->parent_ops->start && ipc->parent_ops->start(ctx, args) < 0) {
return -1;
}
gettimeofday(&t1, NULL);
while (n < args->numBlks) {
if (ipc->parent_ops->revc(ctx, args, report) < 0) {
return -1;
}
n++;
}
gettimeofday(&t2, NULL);
wait(NULL); // TODO: check child status
ipc->parent_ops->clean(ctx);
if (report->revc_sz != (args->blkSz * args->numBlks)) {
report->stat = -1;
printf("Failure tranmission\n");
return -1;
} else {
report->stat = 0;
report->elapsed = (t2.tv_sec - t1.tv_sec) * 1000.0;
report->elapsed += (t2.tv_usec - t1.tv_usec) / 1000.0;
}
}
return 0;
}
int ipc_count()
{
uintptr_t start = (uintptr_t)&__ipc_array_start;
uintptr_t end = (uintptr_t)&__ipc_array_end;
if (end % IPC_ENTRY_ALIGNMENT == 0) {
return (end - start) / IPC_ENTRY_ALIGNMENT;
} else {
return (end - start) / IPC_ENTRY_ALIGNMENT + 1;
}
}
int ipc_get(int id, ipc_t **ipc)
{
int sz = ipc_count();
int offs = ipc_get_arr_offs();
if (id >= 0 && id < sz) {
*ipc = (ipc_t*)(&__ipc_array_start + id * offs);
return 0;
}
return -1;
}
void ipc_for_each(int(*cb)(ipc_t*, int))
{
int n = ipc_get_arr_offs();
int i = 0;
void *p = &__ipc_array_start;
do {
p = &__ipc_array_start + (i++)*n;
} while ((uintptr_t)p < (uintptr_t)&__ipc_array_end && !cb(p, i - 1));
}