-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcmdShm.c
More file actions
161 lines (149 loc) · 3.8 KB
/
Copy pathcmdShm.c
File metadata and controls
161 lines (149 loc) · 3.8 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
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
/*
SPDX-License-Identifier: Apache-2.0
Copyright (c) 2021-2022 Nordix Foundation
*/
#include "nfqueue.h"
#include <shmem.h>
#include <cmd.h>
#include <die.h>
#include <prime.h>
#include <fragutils.h>
#include <maglevdyn.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
char const* const defaultTargetShm = "nfqlb";
static void initShm(
char const* name, int ownFw, unsigned m, unsigned n)
{
unsigned len = magDataDyn_len(m, n);
struct SharedData* s = malloc(sizeof(struct SharedData) + len);
s->ownFwmark = ownFw;
createSharedDataOrDie(name, s, sizeof(struct SharedData) + len);
free(s);
s = mapSharedDataOrDie(name, O_RDWR, NULL);
magDataDyn_init(m, n, s->mem, len);
}
static int cmdInit(int argc, char **argv)
{
char const* shm = defaultTargetShm;
char const* M = "997";
char const* N = "32";
char const* ownFw = "0";
struct Option options[] = {
{"help", NULL, 0,
"init [options]\n"
" Initiate shared mem structures"},
{"ownfw", &ownFw, 0, "Own FW mark. Only used in the LB-tier!"},
{"shm", &shm, 0, "Target shared memory"},
{"N", &N, 0, "Maglev max targets"},
{"M", &M, 0, "Maglev lookup table size"},
{0, 0, 0, 0}
};
(void)parseOptionsOrDie(argc, argv, options);
unsigned m, n, p;
m = atoi(M);
if (m < 3)
p = 3;
else
p = primeBelow(m);
if (p != m) {
printf("M adjusted; %u -> %u\n", m, p);
m = p;
}
n = atoi(N);
if (n > m)
die("N can't be larger than M\n");
initShm(shm, atoi(ownFw), m, n);
return 0;
}
static int cmdDelete(int argc, char **argv)
{
char const* shm = NULL;
struct Option options[] = {
{"help", NULL, 0,
"delete [options]\n"
" Delete shared mem structures"},
{"shm", &shm, 1, "Target shared memory"},
{0, 0, 0, 0}
};
(void)parseOptionsOrDie(argc, argv, options);
if (shm_unlink(shm) != 0 && errno != ENOENT)
die("%s\n", strerror(errno));
return 0;
}
static int cmdPrimeBelow(int argc, char **argv)
{
struct Option options[] = {
{"help", NULL, 0,
"primebelow <n>\n"
" Give a prime below the passed number"},
{0, 0, 0, 0}
};
int nopt = parseOptionsOrDie(argc, argv, options);
argc -= nopt;
argv += nopt;
if (argc > 0)
printf("%u\n", primeBelow(atoi(*argv)));
return 0;
}
static int cmdShow(int argc, char **argv)
{
char const* shm = defaultTargetShm;
struct Option options[] = {
{"help", NULL, 0,
"show [options]\n"
" Show shared mem structures"},
{"shm", &shm, 0, "Shared memory"},
{0, 0, 0, 0}
};
(void)parseOptionsOrDie(argc, argv, options);
struct SharedData* s;
size_t len;
s = mapSharedDataOrDie(shm, O_RDONLY, &len);
if (magDataDyn_validate(s->mem, len - sizeof(struct SharedData)) != 0)
die("Shm invalid; %s\n", shm);
if (s == NULL)
die("Failed to open shared mem; %s\n", shm);
printf("Shm: %s\n", shm);
printf(" Fw: own=%d\n", s->ownFwmark);
struct MagDataDyn magd;
magDataDyn_map(&magd, s->mem);
printf(" Maglev: M=%d, N=%d\n", magd.M, magd.N);
printf(" Lookup:");
for (int i = 0; i < 25 && i < magd.M; i++)
printf(" %d", magd.lookup[i]);
printf("...\n");
printf(" Active:");
for (int i = 0; i < magd.N; i++) {
if (magd.active[i] >= 0)
printf(" %d(%d)", magd.active[i], i);
}
printf("\n");
magDataDyn_free(&magd);
return 0;
}
static int cmdStats(int argc, char **argv)
{
char const* ftShm = "ftshm";
struct Option options[] = {
{"help", NULL, 0,
"stats [options]\n"
" Show frag table stats"},
{"ft_shm", &ftShm, 0, "Frag table; shared memory stats"},
{0, 0, 0, 0}
};
(void)parseOptionsOrDie(argc, argv, options);
struct fragStats* sft = mapSharedDataOrDie(ftShm, O_RDONLY, NULL);
fragPrintStats(sft);
return 0;
}
__attribute__ ((__constructor__)) static void addCommands(void) {
addCmd("init", cmdInit);
addCmd("delete", cmdDelete);
addCmd("show", cmdShow);
addCmd("stats", cmdStats);
addCmd("primebelow", cmdPrimeBelow);
}